mizozobu 6 лет назад
Родитель
Сommit
de6dc0c509
3 измененных файлов с 117 добавлено и 52 удалено
  1. 5 1
      src/server/crowi/index.js
  2. 83 51
      src/server/service/global-notification.js
  3. 29 0
      src/server/util/slack.js

+ 5 - 1
src/server/crowi/index.js

@@ -98,13 +98,17 @@ Crowi.prototype.init = async function() {
     this.setupMailer(),
     this.setupMailer(),
     this.setupSlack(),
     this.setupSlack(),
     this.setupCsrf(),
     this.setupCsrf(),
-    this.setUpGlobalNotification(),
     this.setUpFileUpload(),
     this.setUpFileUpload(),
     this.setUpAcl(),
     this.setUpAcl(),
     this.setUpCustomize(),
     this.setUpCustomize(),
     this.setUpRestQiitaAPI(),
     this.setUpRestQiitaAPI(),
     this.setupUserGroup(),
     this.setupUserGroup(),
   ]);
   ]);
+
+  // globalNotification depends on slack and mailer
+  await Promise.all([
+    this.setUpGlobalNotification(),
+  ]);
 };
 };
 
 
 Crowi.prototype.initForTest = async function() {
 Crowi.prototype.initForTest = async function() {

+ 83 - 51
src/server/service/global-notification.js

@@ -8,6 +8,7 @@ class GlobalNotificationService {
   constructor(crowi) {
   constructor(crowi) {
     this.crowi = crowi;
     this.crowi = crowi;
     this.mailer = crowi.getMailer();
     this.mailer = crowi.getMailer();
+    this.slack = crowi.slack;
     this.GlobalNotification = crowi.model('GlobalNotificationSetting');
     this.GlobalNotification = crowi.model('GlobalNotificationSetting');
     this.User = crowi.model('User');
     this.User = crowi.model('User');
     this.appTitle = crowi.appService.getAppTitle();
     this.appTitle = crowi.appService.getAppTitle();
@@ -18,7 +19,7 @@ class GlobalNotificationService {
   }
   }
 
 
   notifyBySlack(notification, slackOption) {
   notifyBySlack(notification, slackOption) {
-    // send slack notification here
+    this.slack.sendGlobalNotification(notification, slackOption);
   }
   }
 
 
   fire(notifications, option) {
   fire(notifications, option) {
@@ -40,17 +41,22 @@ class GlobalNotificationService {
   async notifyPageCreate(page) {
   async notifyPageCreate(page) {
     const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageCreate');
     const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageCreate');
     const lang = 'en-US'; // FIXME
     const lang = 'en-US'; // FIXME
+    const baseOption = {
+      template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageCreate.txt`),
+      vars: {
+        appTitle: this.appTitle,
+        path: page.path,
+        username: page.creator.username,
+      },
+    };
     const option = {
     const option = {
       mail: {
       mail: {
         subject: `#pageCreate - ${page.creator.username} created ${page.path}`,
         subject: `#pageCreate - ${page.creator.username} created ${page.path}`,
-        template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageCreate.txt`),
-        vars: {
-          appTitle: this.appTitle,
-          path: page.path,
-          username: page.creator.username,
-        },
-      },
-      slack: {},
+        ...baseOption,
+      },
+      slack: {
+        ...baseOption,
+      },
     };
     };
 
 
     logger.debug('notifyPageCreate', option);
     logger.debug('notifyPageCreate', option);
@@ -66,17 +72,22 @@ class GlobalNotificationService {
   async notifyPageEdit(page) {
   async notifyPageEdit(page) {
     const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageEdit');
     const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageEdit');
     const lang = 'en-US'; // FIXME
     const lang = 'en-US'; // FIXME
+    const baseOption = {
+      template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageEdit.txt`),
+      vars: {
+        appTitle: this.appTitle,
+        path: page.path,
+        username: page.creator.username,
+      },
+    };
     const option = {
     const option = {
       mail: {
       mail: {
         subject: `#pageEdit - ${page.creator.username} edited ${page.path}`,
         subject: `#pageEdit - ${page.creator.username} edited ${page.path}`,
-        template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageEdit.txt`),
-        vars: {
-          appTitle: this.appTitle,
-          path: page.path,
-          username: page.creator.username,
-        },
-      },
-      slack: {},
+        ...baseOption,
+      },
+      slack: {
+        ...baseOption,
+      },
     };
     };
 
 
     logger.debug('notifyPageEdit', option);
     logger.debug('notifyPageEdit', option);
@@ -92,17 +103,23 @@ class GlobalNotificationService {
   async notifyPageDelete(page) {
   async notifyPageDelete(page) {
     const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageDelete');
     const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageDelete');
     const lang = 'en-US'; // FIXME
     const lang = 'en-US'; // FIXME
+    const baseOption = {
+      template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageDelete.txt`),
+      vars: {
+        appTitle: this.appTitle,
+        path: page.path,
+        username: page.creator.username,
+      },
+    };
     const option = {
     const option = {
       mail: {
       mail: {
         subject: `#pageDelete - ${page.creator.username} deleted ${page.path}`, // FIXME
         subject: `#pageDelete - ${page.creator.username} deleted ${page.path}`, // FIXME
-        template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageDelete.txt`),
-        vars: {
-          appTitle: this.appTitle,
-          path: page.path,
-          username: page.creator.username,
-        },
-      },
-      slack: {},
+        ...baseOption,
+      },
+      slack: {
+        ...baseOption,
+
+      },
     };
     };
 
 
     this.fire(notifications, option);
     this.fire(notifications, option);
@@ -116,18 +133,23 @@ class GlobalNotificationService {
   async notifyPageMove(page, oldPagePath, user) {
   async notifyPageMove(page, oldPagePath, user) {
     const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageMove');
     const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageMove');
     const lang = 'en-US'; // FIXME
     const lang = 'en-US'; // FIXME
+    const baseOption = {
+      template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageMove.txt`),
+      vars: {
+        appTitle: this.appTitle,
+        oldPath: oldPagePath,
+        newPath: page.path,
+        username: user.username,
+      },
+    };
     const option = {
     const option = {
       mail: {
       mail: {
         subject: `#pageMove - ${user.username} moved ${page.path} to ${page.path}`, // FIXME
         subject: `#pageMove - ${user.username} moved ${page.path} to ${page.path}`, // FIXME
-        template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageMove.txt`),
-        vars: {
-          appTitle: this.appTitle,
-          oldPath: oldPagePath,
-          newPath: page.path,
-          username: user.username,
-        },
-      },
-      slack: {},
+        ...baseOption,
+      },
+      slack: {
+        ...baseOption,
+      },
     };
     };
 
 
     this.fire(notifications, option);
     this.fire(notifications, option);
@@ -141,17 +163,22 @@ class GlobalNotificationService {
   async notifyPageLike(page, user) {
   async notifyPageLike(page, user) {
     const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageLike');
     const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageLike');
     const lang = 'en-US'; // FIXME
     const lang = 'en-US'; // FIXME
+    const baseOption = {
+      template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageLike.txt`),
+      vars: {
+        appTitle: this.appTitle,
+        path: page.path,
+        username: page.creator.username,
+      },
+    };
     const option = {
     const option = {
       mail: {
       mail: {
         subject: `#pageLike - ${user.username} liked ${page.path}`,
         subject: `#pageLike - ${user.username} liked ${page.path}`,
-        template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageLike.txt`),
-        vars: {
-          appTitle: this.appTitle,
-          path: page.path,
-          username: page.creator.username,
-        },
-      },
-      slack: {},
+        ...baseOption,
+      },
+      slack: {
+        ...baseOption,
+      },
     };
     };
 
 
     this.fire(notifications, option);
     this.fire(notifications, option);
@@ -167,18 +194,23 @@ class GlobalNotificationService {
     const notifications = await this.GlobalNotification.findSettingByPathAndEvent(path, 'comment');
     const notifications = await this.GlobalNotification.findSettingByPathAndEvent(path, 'comment');
     const lang = 'en-US'; // FIXME
     const lang = 'en-US'; // FIXME
     const user = await this.User.findOne({ _id: comment.creator });
     const user = await this.User.findOne({ _id: comment.creator });
+    const baseOption = {
+      template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/comment.txt`),
+      vars: {
+        appTitle: this.appTitle,
+        path,
+        username: user.username,
+        comment: comment.comment,
+      },
+    };
     const option = {
     const option = {
       mail: {
       mail: {
         subject: `#comment - ${user.username} commented on ${path}`,
         subject: `#comment - ${user.username} commented on ${path}`,
-        template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/comment.txt`),
-        vars: {
-          appTitle: this.appTitle,
-          path,
-          username: user.username,
-          comment: comment.comment,
-        },
-      },
-      slack: {},
+        ...baseOption,
+      },
+      slack: {
+        ...baseOption,
+      },
     };
     };
 
 
     this.fire(notifications, option);
     this.fire(notifications, option);

+ 29 - 0
src/server/util/slack.js

@@ -1,5 +1,7 @@
 const debug = require('debug')('growi:util:slack');
 const debug = require('debug')('growi:util:slack');
+const { promisify } = require('util');
 const urljoin = require('url-join');
 const urljoin = require('url-join');
+const swig = require('swig-templates');
 
 
 /**
 /**
  * slack
  * slack
@@ -168,6 +170,27 @@ module.exports = function(crowi) {
     return message;
     return message;
   };
   };
 
 
+  /**
+   * For GlobalNotification
+   * @param {GlobalNotification} notification
+  */
+  const prepareSlackMessageForGlobalNotification = async(notification, config) => {
+    const appTitle = crowi.appService.getAppTitle();
+    const templateVars = config.vars || {};
+    const output = await promisify(swig.renderFile)(
+      config.template,
+      templateVars,
+    );
+
+    const message = {
+      channel: `#${notification.slackChannels}`,
+      username: appTitle,
+      text: output,
+    };
+
+    return message;
+  };
+
   const getSlackMessageTextForPage = function(path, pageId, user, updateType) {
   const getSlackMessageTextForPage = function(path, pageId, user, updateType) {
     let text;
     let text;
     const url = crowi.appService.getSiteUrl();
     const url = crowi.appService.getSiteUrl();
@@ -204,6 +227,12 @@ module.exports = function(crowi) {
     return slackPost(messageObj);
     return slackPost(messageObj);
   };
   };
 
 
+  slack.sendGlobalNotification = async(notification, config) => {
+    const messageObj = await prepareSlackMessageForGlobalNotification(notification, config);
+
+    return slackPost(messageObj);
+  };
+
   const slackPost = (messageObj) => {
   const slackPost = (messageObj) => {
     // when incoming Webhooks is prioritized
     // when incoming Webhooks is prioritized
     if (configManager.getConfig('notification', 'slack:isIncomingWebhookPrioritized')) {
     if (configManager.getConfig('notification', 'slack:isIncomingWebhookPrioritized')) {