Просмотр исходного кода

Merge remote-tracking branch 'origin/feat/mail-notification-takizawa' into feat/mail-notification-combine

sou 7 лет назад
Родитель
Сommit
3f8cf8f7c1

+ 1 - 0
lib/locales/en-US/notifications/comment.txt

@@ -0,0 +1 @@
+comment

+ 1 - 0
lib/locales/en-US/notifications/pageCreate.txt

@@ -0,0 +1 @@
+create

+ 1 - 0
lib/locales/en-US/notifications/pageDelete.txt

@@ -0,0 +1 @@
+delete

+ 1 - 0
lib/locales/en-US/notifications/pageEdit.txt

@@ -0,0 +1 @@
+edit

+ 1 - 0
lib/locales/en-US/notifications/pageLike.txt

@@ -0,0 +1 @@
+like

+ 1 - 0
lib/locales/en-US/notifications/pageMove.txt

@@ -0,0 +1 @@
+move

+ 1 - 0
lib/routes/comment.js

@@ -103,6 +103,7 @@ module.exports = function(crowi, app) {
     }
     // NOTIFICATION: send comment notification here
     // notification.sendCommentNotification(comment, path);
+    notification.sendCommentNotification(comment, path);
   };
 
   /**

+ 29 - 23
lib/routes/page.js

@@ -16,7 +16,8 @@ module.exports = function(crowi, app) {
     , pagePathUtil = require('../util/pagePathUtil')
     , swig = require('swig-templates')
     , getToday = require('../util/getToday')
-    , notification = require('../service/global-notification')
+    , globalNotification = require('../service/global-notification')
+    , notification = new globalNotification(crowi)
 
     , actions = {};
 
@@ -704,20 +705,22 @@ module.exports = function(crowi, app) {
 
       if (data) {
         previousRevision = data.revision;
-        return Page.updatePage(data, body, req.user, { grant, grantUserGroupId });
-        // .then(() => {
-        //   // NOTIFICATION: send page edit notification here
-        //   notification.sendPageEditNotification(page);
-        // })
+        return Page.updatePage(data, body, req.user, { grant, grantUserGroupId })
+        .then((page) => {
+          // NOTIFICATION: send page edit notification here
+          notification.notifyPageEdit(page);
+          return page;
+        });
       }
       else {
         // new page
         updateOrCreate = 'create';
-        return Page.create(path, body, req.user, { grant, grantUserGroupId });
-        // .then((page) => {
-        //   // NOTIFICATION: send page create notification here
-        //   notification.sendPageCreateNotification(page);
-        // })
+        return Page.create(path, body, req.user, { grant, grantUserGroupId })
+        .then((page) => {
+          // NOTIFICATION: send page create notification here
+          notification.notifyPageCreate(page);
+          return page;
+        });
       }
     }).then(function(data) {
       // data is a saved page data with revision.
@@ -1005,11 +1008,12 @@ module.exports = function(crowi, app) {
     }).catch(function(err) {
       debug('Like failed', err);
       return res.json(ApiResponse.error({}));
+    })
+    .then((page) => {
+      // NOTIFICATION: send page like notification here
+      notification.notifyPageLike(page, req.user);
+      return page;
     });
-    // .then(() => {
-    //   // NOTIFICATION: send page like notification here
-    //   notification.sendPageLikeNotification(page);
-    // })
   };
 
   /**
@@ -1114,11 +1118,12 @@ module.exports = function(crowi, app) {
     }).catch(function(err) {
       debug('Error occured while get setting', err, err.stack);
       return res.json(ApiResponse.error('Failed to delete page.'));
+    })
+    .then((page) => {
+      // NOTIFICATION: send page delete notification here
+      notification.notifyPageCreate(page);
+      return page;
     });
-    // .then(() => {
-    //   // NOTIFICATION: send page delete notification here
-    //   notification.sendPageDeleteNotification(page);
-    // })
   };
 
   /**
@@ -1211,11 +1216,12 @@ module.exports = function(crowi, app) {
       })
       .catch(function(err) {
         return res.json(ApiResponse.error('Failed to update page.'));
+      })
+      .then((page) => {
+        // NOTIFICATION: send page move notification here
+        notification.notifyPageMove(page, req.user);
+        return page;
       });
-      // .then(() => {
-      //   // NOTIFICATION: send page move notification here
-      //   notification.sendPageMoveNotification(page);
-      // })
     });
   };
 

+ 52 - 17
lib/service/global-notification.js

@@ -3,6 +3,27 @@ const path = require('path');
 const Notification = require('../models/GlobalNotificationSetting');
 const mailer = require('../util/mailer');
 
+const testNotifyData = [
+  {
+    "_id": "5b45ab384a702f4484010066",
+    "isEnabled": true,
+    "triggerEvents": ["comment, pageCreate"],
+    "__t": "mail",
+    "triggerPath": "/*",
+    "toEmail": "email@email.com",
+    "__v": 0
+  },
+  {
+    "_id": "5b45ab384a702f4484010067",
+    "isEnabled": true,
+    "triggerEvents": ["comment, pageCreate"],
+    "__t": "slack",
+    "triggerPath": "/*",
+    "slackChannels": "general, random",
+    "__v": 0
+  }
+];
+
 /**
  * the service class of GlobalNotificationSetting
  */
@@ -14,7 +35,15 @@ class GlobalNotification {
   }
 
   notifyByMail(notification, mailOption) {
-    mailer.send(Object.assign(mailOption, {to: notification.toEmail}));
+    const crowi = this.crowi;
+    const mailer = crowi.getMailer();
+
+    mailer.send(Object.assign(mailOption, {to: notification.toEmail}),
+      function(err, s) {
+        debug('completed to send email: ', err, s);
+        next();
+      }
+    );
   }
 
   notifyBySlack(notification, slackOption) {
@@ -38,12 +67,13 @@ class GlobalNotification {
    * @param {obejct} page
    */
   notifyPageCreate(page) {
-    const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageCreate');
+    // const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageCreate');
+    const notifications = testNotifyData;
     const lang = 'en-US'; //FIXME
     const option = {
       mail: {
         subject: `#pageCreate - ${page.creator.username} created ${page.path}`,
-        template: path.join(this.crowi.localeDir, lang, 'notifications/pageCreate.txt'),
+        template: `../../locales/${lang}/notifications/pageCreate.txt`,
         vars: {}
       },
       slack: {},
@@ -58,12 +88,13 @@ class GlobalNotification {
    * @param {obejct} page
    */
   notifyPageEdit(page) {
-    const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageEdit');
+    // const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageEdit');
+    const notifications = testNotifyData;
     const lang = 'en-US'; //FIXME
     const option = {
       mail: {
         subject: `#pageEdit - ${page.creator.username} edited ${page.path}`,
-        template: path.join(this.crowi.localeDir, lang, 'notifications/pageEdit.txt'),
+        template: `../../locales/${lang}/notifications/pageEdit.txt`,
         vars: {}
       },
       slack: {},
@@ -78,12 +109,13 @@ class GlobalNotification {
    * @param {obejct} page
    */
   notifyPageDelete(page) {
-    const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageDelete');
+    // const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageDelete');
+    const notifications = testNotifyData;
     const lang = 'en-US'; //FIXME
     const option = {
       mail: {
         subject: `#pageDelete - ${page.creator.username} deleted ${page.path}`,  //FIXME
-        template: path.join(this.crowi.localeDir, lang, 'notifications/pageDelete.txt'),
+        template: `../../locales/${lang}/notifications/pageDelete.txt`,
         vars: {}
       },
       slack: {},
@@ -97,13 +129,14 @@ class GlobalNotification {
    * @memberof GlobalNotification
    * @param {obejct} page
    */
-  notifyPageMove(page) {
-    const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageMove');
+  notifyPageMove(page, user) {
+    // const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageMove');
+    const notifications = testNotifyData;
     const lang = 'en-US'; //FIXME
     const option = {
       mail: {
-        subject: `#pageMove - ${page.creator.username} moved ${page.path} to ${page.path}`, //FIXME
-        template: path.join(this.crowi.localeDir, lang, 'notifications/pageMove.txt'),
+        subject: `#pageMove - ${user.username} moved ${page.path} to ${page.path}`, //FIXME
+        template: `../../locales/${lang}/notifications/pageMove.txt`,
         vars: {}
       },
       slack: {},
@@ -117,13 +150,14 @@ class GlobalNotification {
    * @memberof GlobalNotification
    * @param {obejct} page
    */
-  notifyPageLike(page) {
-    const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageLike');
+  notifyPageLike(page, user) {
+    // const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageLike');
+    const notifications = testNotifyData;
     const lang = 'en-US'; //FIXME
     const option = {
       mail: {
-        subject: `#pageLike - ${page.creator.username} liked ${page.path}`,
-        template: path.join(this.crowi.localeDir, lang, 'notifications/pageLike.txt'),
+        subject: `#pageLike - ${user.username} liked ${page.path}`,
+        template: `../../locales/${lang}/notifications/pageLike.txt`,
         vars: {}
       },
       slack: {},
@@ -139,12 +173,13 @@ class GlobalNotification {
    * @param {obejct} comment
    */
   notifyComment(comment, path) {
-    const notifications = Notification.findSettingByPathAndEvent(path, 'comment');
+    // const notifications = Notification.findSettingByPathAndEvent(path, 'comment');
+    const notifications = testNotifyData;
     const lang = 'en-US'; //FIXME
     const option = {
       mail: {
         subject: `#comment - ${comment.creator.username} commented on ${path}`,
-        template: path.join(this.crowi.localeDir, lang, 'notifications/comment.txt'),
+        template: `../../locales/${lang}/notifications/comment.txt`,
         vars: {}
       },
       slack: {},