فهرست منبع

Merge pull request #536 from weseek/feat/mail-notification-sou

Feat/mail notification sou
Sou Mizobuchi 7 سال پیش
والد
کامیت
7ed946104e

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


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


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


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


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


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


+ 0 - 0
lib/locales/ja/notifications/comment.txt


+ 0 - 0
lib/locales/ja/notifications/pageCreate.txt


+ 0 - 0
lib/locales/ja/notifications/pageDelete.txt


+ 0 - 0
lib/locales/ja/notifications/pageEdit.txt


+ 0 - 0
lib/locales/ja/notifications/pageLike.txt


+ 0 - 0
lib/locales/ja/notifications/pageMove.txt


+ 95 - 0
lib/models/GlobalNotificationSetting.js

@@ -0,0 +1,95 @@
+const debug = require('debug')('growi:models:GlobalNotificationSetting');
+const mongoose = require('mongoose');
+
+/**
+ * parent schema in this model
+ */
+const notificationSchema = new mongoose.Schema({
+  isEnabled: { type: Boolean, required: true, default: true },
+  triggerPath: { type: String, required: true },
+  triggerEvents: { type: [String] },
+});
+
+/**
+ * create child schemas inherited from parentSchema
+ * all child schemas are stored in globalnotificationsettings collection
+ * @link{http://url.com module_name}
+ * @param {object} parentSchema
+ * @param {string} modelName
+ * @param {string} discriminatorKey
+ */
+const createChildSchemas = (parentSchema, modelName, discriminatorKey) => {
+  const Notification = mongoose.model(modelName, parentSchema);
+  const mailNotification = Notification.discriminator('mail', new mongoose.Schema({
+    toEmail: String,
+  }, {discriminatorKey: discriminatorKey}));
+
+  const slackNotification = Notification.discriminator('slack', new mongoose.Schema({
+    slackChannels: String,
+  }, {discriminatorKey: discriminatorKey}));
+
+  return {
+    Mail: mailNotification,
+    Slack: slackNotification,
+  };
+};
+
+
+/**
+ * GlobalNotificationSetting Class
+ * @class GlobalNotificationSetting
+ */
+class GlobalNotificationSetting {
+
+  constructor(crowi) {
+    this.crowi = crowi;
+  }
+
+  /**
+   * enable notification setting
+   * @param {string} id
+   */
+  static enable(id) {
+    // return new Promise((resolve, reject) => {
+      // save
+      // return resolve(Notification)
+    //}
+  }
+
+  /**
+   * disable notification setting
+   * @param {string} id
+   */
+  static disable(id) {
+    // return new Promise((resolve, reject) => {
+      // save
+      // return resolve(Notification)
+    //}
+  }
+
+  /**
+   * find a list of notification settings by path and a list of events
+   * @param {string} path
+   * @param {string} event
+   * @param {boolean} enabled
+   */
+  static findSettingByPathAndEvent(path, event, enabled) {
+    // return new Promise((resolve, reject) => {
+      // if(enabled == null) {
+      //   find all
+      // }
+      // else {
+      //   find only enabled/disabled
+      // }
+      // sort by path in mongoDB
+
+      // return resolve([Notification])
+    //}
+  }
+}
+
+module.exports = function(crowi) {
+  GlobalNotificationSetting.crowi = crowi;
+  notificationSchema.loadClass(GlobalNotificationSetting);
+  return createChildSchemas(notificationSchema, 'GlobalNotificationSetting', 'type');
+};

+ 1 - 0
lib/models/index.js

@@ -12,4 +12,5 @@ module.exports = {
   Comment: require('./comment'),
   Attachment: require('./attachment'),
   UpdatePost: require('./updatePost'),
+  GlobalNotification: require('./GlobalNotificationSetting'),
 };

+ 3 - 0
lib/routes/comment.js

@@ -7,6 +7,7 @@ module.exports = function(crowi, app) {
     , User = crowi.model('User')
     , Page = crowi.model('Page')
     , ApiResponse = require('../util/apiResponse')
+    , notification = require('../service/global-notification')
     , actions = {}
     , api = {};
 
@@ -100,6 +101,8 @@ module.exports = function(crowi, app) {
           });
       }
     }
+    // NOTIFICATION: send comment notification here
+    // notification.sendCommentNotification(comment, path);
   };
 
   /**

+ 21 - 0
lib/routes/page.js

@@ -16,6 +16,7 @@ module.exports = function(crowi, app) {
     , pagePathUtil = require('../util/pagePathUtil')
     , swig = require('swig-templates')
     , getToday = require('../util/getToday')
+    , notification = require('../service/global-notification')
 
     , actions = {};
 
@@ -704,11 +705,19 @@ 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);
+        // })
       }
       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);
+        // })
       }
     }).then(function(data) {
       // data is a saved page data with revision.
@@ -997,6 +1006,10 @@ module.exports = function(crowi, app) {
       debug('Like failed', err);
       return res.json(ApiResponse.error({}));
     });
+    // .then(() => {
+    //   // NOTIFICATION: send page like notification here
+    //   notification.sendPageLikeNotification(page);
+    // })
   };
 
   /**
@@ -1102,6 +1115,10 @@ module.exports = function(crowi, app) {
       debug('Error occured while get setting', err, err.stack);
       return res.json(ApiResponse.error('Failed to delete page.'));
     });
+    // .then(() => {
+    //   // NOTIFICATION: send page delete notification here
+    //   notification.sendPageDeleteNotification(page);
+    // })
   };
 
   /**
@@ -1195,6 +1212,10 @@ module.exports = function(crowi, app) {
       .catch(function(err) {
         return res.json(ApiResponse.error('Failed to update page.'));
       });
+      // .then(() => {
+      //   // NOTIFICATION: send page move notification here
+      //   notification.sendPageMoveNotification(page);
+      // })
     });
   };
 

+ 157 - 0
lib/service/global-notification.js

@@ -0,0 +1,157 @@
+const debug = require('debug')('growi:service:GlobalNotification');
+const path = require('path');
+const Notification = require('../models/GlobalNotificationSetting');
+const mailer = require('../util/mailer');
+
+/**
+ * the service class of GlobalNotificationSetting
+ */
+class GlobalNotification {
+
+  constructor(crowi) {
+    this.crowi = crowi;
+    this.config = crowi.getConfig();
+  }
+
+  notifyByMail(notification, mailOption) {
+    mailer.send(Object.assign(mailOption, {to: notification.toEmail}));
+  }
+
+  notifyBySlack(notification, slackOption) {
+    // send slack notification here
+  }
+
+  sendNotification(notifications, option) {
+    notifications.forEach(notification => {
+      if (notification.__t === 'mail') {
+        this.notifyByMail(notification, option.mail);
+      }
+      else if (notification.__t === 'slack') {
+        this.notifyBySlack(notification, option.slack);
+      }
+    });
+  }
+
+  /**
+   * send notification at page creation
+   * @memberof GlobalNotification
+   * @param {obejct} page
+   */
+  notifyPageCreate(page) {
+    const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageCreate');
+    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'),
+        vars: {}
+      },
+      slack: {},
+    };
+
+    this.sendNotification(notifications, option);
+  }
+
+  /**
+   * send notification at page edit
+   * @memberof GlobalNotification
+   * @param {obejct} page
+   */
+  notifyPageEdit(page) {
+    const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageEdit');
+    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'),
+        vars: {}
+      },
+      slack: {},
+    };
+
+    this.sendNotification(notifications, option);
+  }
+
+  /**
+   * send notification at page deletion
+   * @memberof GlobalNotification
+   * @param {obejct} page
+   */
+  notifyPageDelete(page) {
+    const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageDelete');
+    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'),
+        vars: {}
+      },
+      slack: {},
+    };
+
+    this.sendNotification(notifications, option);
+  }
+
+  /**
+   * send notification at page move
+   * @memberof GlobalNotification
+   * @param {obejct} page
+   */
+  notifyPageMove(page) {
+    const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageMove');
+    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'),
+        vars: {}
+      },
+      slack: {},
+    };
+
+    this.sendNotification(notifications, option);
+  }
+
+  /**
+   * send notification at page like
+   * @memberof GlobalNotification
+   * @param {obejct} page
+   */
+  notifyPageLike(page) {
+    const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageLike');
+    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'),
+        vars: {}
+      },
+      slack: {},
+    };
+
+    this.sendNotification(notifications, option);
+  }
+
+  /**
+   * send notification at page comment
+   * @memberof GlobalNotification
+   * @param {obejct} page
+   * @param {obejct} comment
+   */
+  notifyComment(comment, path) {
+    const notifications = Notification.findSettingByPathAndEvent(path, 'comment');
+    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'),
+        vars: {}
+      },
+      slack: {},
+    };
+
+    this.sendNotification(notifications, option);
+  }
+}
+
+module.exports = GlobalNotification;