sou 7 лет назад
Родитель
Сommit
363cc10112
3 измененных файлов с 48 добавлено и 27 удалено
  1. 4 2
      lib/models/comment.js
  2. 3 3
      lib/routes/comment.js
  3. 41 22
      lib/util/slack.js

+ 4 - 2
lib/models/comment.js

@@ -36,8 +36,10 @@ module.exports = function(crowi) {
           debug('Error on saving comment.', err);
           return reject(err);
         }
-        debug('Comment saved.', data);
-        return resolve(data);
+        data.populate('page', function(err, datas) {
+          debug('Comment saved.', datas);
+          return resolve(datas);
+        });
       });
     });
   };

+ 3 - 3
lib/routes/comment.js

@@ -71,15 +71,15 @@ module.exports = function(crowi, app) {
       });
 
     // update page
-    const page = await Page.findOneAndUpdate({ _id: pageId }, {
+    await Page.findOneAndUpdate({ _id: pageId }, {
       lastUpdateUser: req.user,
       updatedAt: new Date()
     });
 
-    //slack notification
+    // slack notification
     const slackNotify = new slack(crowi);
     const user = await User.findUserByUsername(req.user.username);
-    await slackNotify.post(page, user, 'general', 'comment', null, createdComment);
+    await slackNotify.post(createdComment, user, 'general, random', 'comment', null);
 
     return res.json(ApiResponse.success({comment: createdComment}));
   };

+ 41 - 22
lib/util/slack.js

@@ -88,21 +88,18 @@ module.exports = function(crowi) {
     };
   };
 
-  const prepareSlackMessage = function(page, user, channel, updateType, previousRevision, comment) {
-    var url = config.crowi['app:url'] || '';
-    var body = page.revision.body;
+  const prepareSlackMessageForPage = function(page, user, channel, updateType, previousRevision) {
+    const url = config.crowi['app:url'] || '';
+    let body = page.revision.body;
 
     if (updateType == 'create') {
       body = prepareAttachmentTextForCreate(page, user);
     }
-    else if (updateType == 'comment') {
-      body = prepareAttachmentTextForComment(page, user);
-    }
     else {
       body = prepareAttachmentTextForUpdate(page, user, previousRevision);
     }
 
-    var attachment = {
+    const attachment = {
       color: '#263a3c',
       author_name: '@' + user.username,
       author_link: url + '/user/' + user.username,
@@ -113,27 +110,43 @@ module.exports = function(crowi) {
       mrkdwn_in: ['text'],
     };
 
-    if (updateType == 'comment') {
-      attachment = {
-        color: '#263a3c',
-        author_name: '@' + user.username,
-        author_link: url + '/user/' + user.username,
-        author_icon: user.image,
-        title: comment.comment,
-        title_link: url + '/' + page._id,
-        text: body,
-        mrkdwn_in: ['text'],
-      };
+    if (user.image) {
+      attachment.author_icon = user.image;
     }
 
+    const message = {
+      channel: '#' + channel,
+      username: Config.appTitle(config),
+      text: getSlackMessageText(page.path, user, updateType),
+      attachments: [attachment],
+    };
+
+    return message;
+  };
+
+  const prepareSlackMessageForComment = function(comment, user, channel, updateType) {
+    const url = config.crowi['app:url'] || '';
+    const body = prepareAttachmentTextForComment(comment, user);
+
+    const attachment = {
+      color: '#263a3c',
+      author_name: '@' + user.username,
+      author_link: url + '/user/' + user.username,
+      author_icon: user.image,
+      title: comment.comment,
+      title_link: url + '/' + comment._id,
+      text: body,
+      mrkdwn_in: ['text'],
+    };
+
     if (user.image) {
       attachment.author_icon = user.image;
     }
 
-    var message = {
+    const message = {
       channel: '#' + channel,
       username: Config.appTitle(config),
-      text: getSlackMessageText(page.path, user, updateType),
+      text: getSlackMessageText(comment.path, user, updateType),
       attachments: [attachment],
     };
 
@@ -159,8 +172,14 @@ module.exports = function(crowi) {
   };
 
   // slack.post = function (channel, message, opts) {
-  slack.post = (page, user, channel, updateType, previousRevision, comment) => {
-    const messageObj = prepareSlackMessage(page, user, channel, updateType, previousRevision, comment);
+  slack.post = (pageOrComment, user, channel, updateType, previousRevision) => {
+    let messageObj;
+    if (updateType === 'comment') {
+      messageObj = prepareSlackMessageForComment(pageOrComment, user, channel, updateType);
+    }
+    else {
+      messageObj = prepareSlackMessageForPage(pageOrComment, user, channel, updateType, previousRevision);
+    }
 
     return new Promise((resolve, reject) => {
       // define callback function for Promise