sou 7 лет назад
Родитель
Сommit
3046f79725
4 измененных файлов с 89 добавлено и 62 удалено
  1. 16 10
      lib/models/GlobalNotificationSetting.js
  2. 1 1
      lib/models/index.js
  3. 2 2
      lib/routes/admin.js
  4. 70 49
      lib/service/global-notification.js

+ 16 - 10
lib/models/global-notification-setting.js → lib/models/GlobalNotificationSetting.js

@@ -1,9 +1,8 @@
-const debug = require('debug')('growi:models:global-notification-setting');
+const debug = require('debug')('growi:models:GlobalNotificationSetting');
 const mongoose = require('mongoose');
 const mongoose = require('mongoose');
-// const Notification = this;
 
 
-/*
- * parent schema
+/**
+ * parent schema in this model
  */
  */
 const notificationSchema = new mongoose.Schema({
 const notificationSchema = new mongoose.Schema({
   isEnabled: { type: Boolean, required: true, default: true },
   isEnabled: { type: Boolean, required: true, default: true },
@@ -11,12 +10,15 @@ const notificationSchema = new mongoose.Schema({
   triggerEvents: { type: [String] },
   triggerEvents: { type: [String] },
 });
 });
 
 
-/*
- * child schema inherited from notificationSchema
- * stored in globalnotificationsettings collection
+/**
+ * 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, className, modelName, discriminatorKey) => {
-  parentSchema.loadClass(className);
+const createChildSchemas = (parentSchema, modelName, discriminatorKey) => {
   const Notification = mongoose.model(modelName, parentSchema);
   const Notification = mongoose.model(modelName, parentSchema);
   const mailNotification = Notification.discriminator('mail', new mongoose.Schema({
   const mailNotification = Notification.discriminator('mail', new mongoose.Schema({
     toEmail: String,
     toEmail: String,
@@ -39,6 +41,10 @@ const createChildSchemas = (parentSchema, className, modelName, discriminatorKey
  */
  */
 class GlobalNotificationSetting {
 class GlobalNotificationSetting {
 
 
+  constructor(crowi) {
+    this.crowi = crowi;
+  }
+
   /**
   /**
    * enable notification setting
    * enable notification setting
    * @param {string} id
    * @param {string} id
@@ -89,9 +95,9 @@ class GlobalNotificationSetting {
 
 
 module.exports = function(crowi) {
 module.exports = function(crowi) {
   GlobalNotificationSetting.crowi = crowi;
   GlobalNotificationSetting.crowi = crowi;
+  notificationSchema.loadClass(GlobalNotificationSetting);
   return createChildSchemas(
   return createChildSchemas(
     notificationSchema,
     notificationSchema,
-    GlobalNotificationSetting,
     'GlobalNotificationSetting',
     'GlobalNotificationSetting',
     'type',
     'type',
   );
   );

+ 1 - 1
lib/models/index.js

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

+ 2 - 2
lib/routes/admin.js

@@ -1158,8 +1158,8 @@ module.exports = function(crowi, app) {
         return;
         return;
       }
       }
     });
     });
-    notif.test('wwwwwwwwwwwwwwwwwww');
-    snotif.test('sssssssssssss');
+    notif.test(notif.__t);
+    snotif.test(snotif.__t);
     return res.json(ApiResponse.success());
     return res.json(ApiResponse.success());
   };
   };
 
 

+ 70 - 49
lib/service/global-notification.js

@@ -1,5 +1,5 @@
 const debug = require('debug')('growi:service:GlobalNotification');
 const debug = require('debug')('growi:service:GlobalNotification');
-const Notification = require('../models/global-notification-setting');
+const Notification = require('../models/GlobalNotificationSetting');
 const mailer = require('../util/mailer');
 const mailer = require('../util/mailer');
 
 
 /**
 /**
@@ -12,8 +12,23 @@ class GlobalNotification {
     this.config = crowi.getConfig();
     this.config = crowi.getConfig();
   }
   }
 
 
-  mailNotify(notification, option) {
-    mailer.send(Object.assign(option, {to: notification.notifyTo.toEmail}));
+  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);
+      }
+    });
   }
   }
 
 
   /**
   /**
@@ -21,17 +36,18 @@ class GlobalNotification {
    * @memberof GlobalNotification
    * @memberof GlobalNotification
    * @param {obejct} page
    * @param {obejct} page
    */
    */
-  sendPageCreateNotification(page) {
+  notifyPageCreate(page) {
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageCreate');
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageCreate');
-    const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
     const option = {
     const option = {
-      subject: `#pageCreate - ${page.creator.username} created ${page.path}`,
-      template: 'notification/pageCreate.txt',
-      vars: {}
+      mail: {
+        subject: `#pageCreate - ${page.creator.username} created ${page.path}`,
+        template: 'notification/pageCreate.txt',
+        vars: {}
+      },
+      slack: {},
     };
     };
-    mailNotifications.forEach(notification => {
-      this.mailNotify(notification, option);
-    });
+
+    this.sendNotification(notifications, option);
   }
   }
 
 
   /**
   /**
@@ -39,17 +55,18 @@ class GlobalNotification {
    * @memberof GlobalNotification
    * @memberof GlobalNotification
    * @param {obejct} page
    * @param {obejct} page
    */
    */
-  sendPageEditNotification(page) {
+  notifyPageEdit(page) {
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageEdit');
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageEdit');
-    const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
     const option = {
     const option = {
-      subject: `#pageEdit - ${page.creator.username} edited ${page.path}`,
-      template: 'notification/pageEdit.txt',
-      vars: {}
+      mail: {
+        subject: `#pageEdit - ${page.creator.username} edited ${page.path}`,
+        template: 'notification/pageEdit.txt',
+        vars: {}
+      },
+      slack: {},
     };
     };
-    mailNotifications.forEach(notification => {
-      this.mailNotify(notification, option);
-    });
+
+    this.sendNotification(notifications, option);
   }
   }
 
 
   /**
   /**
@@ -57,17 +74,18 @@ class GlobalNotification {
    * @memberof GlobalNotification
    * @memberof GlobalNotification
    * @param {obejct} page
    * @param {obejct} page
    */
    */
-  sendPageDeleteNotification(page) {
+  notifyPageDelete(page) {
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageDelete');
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageDelete');
-    const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
     const option = {
     const option = {
-      subject: `#pageDelete - ${page.creator.username} deleted ${page.path}`,  //FIXME
-      template: 'notification/pageDelete.txt',
-      vars: {}
+      mail: {
+        subject: `#pageDelete - ${page.creator.username} deleted ${page.path}`,  //FIXME
+        template: 'notification/pageDelete.txt',
+        vars: {}
+      },
+      slack: {},
     };
     };
-    mailNotifications.forEach(notification => {
-      this.mailNotify(notification, option);
-    });
+
+    this.sendNotification(notifications, option);
   }
   }
 
 
   /**
   /**
@@ -75,17 +93,18 @@ class GlobalNotification {
    * @memberof GlobalNotification
    * @memberof GlobalNotification
    * @param {obejct} page
    * @param {obejct} page
    */
    */
-  sendPageMoveNotification(page) {
+  notifyPageMove(page) {
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageMove');
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageMove');
-    const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
     const option = {
     const option = {
-      subject: `#pageMove - ${page.creator.username} moved ${page.path} to ${page.path}`, //FIXME
-      template: 'notification/pageMove.txt',
-      vars: {}
+      mail: {
+        subject: `#pageMove - ${page.creator.username} moved ${page.path} to ${page.path}`, //FIXME
+        template: 'notification/pageMove.txt',
+        vars: {}
+      },
+      slack: {},
     };
     };
-    mailNotifications.forEach(notification => {
-      this.mailNotify(notification, option);
-    });
+
+    this.sendNotification(notifications, option);
   }
   }
 
 
   /**
   /**
@@ -93,17 +112,18 @@ class GlobalNotification {
    * @memberof GlobalNotification
    * @memberof GlobalNotification
    * @param {obejct} page
    * @param {obejct} page
    */
    */
-  sendPageLikeNotification(page) {
+  notifyPageLike(page) {
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageLike');
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageLike');
-    const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
-    mailNotifications.forEach(notification => {
-      mailer.send({
-        to: notification.notifyTo.toEmail,
+    const option = {
+      mail: {
         subject: `#pageLike - ${page.creator.username} liked ${page.path}`,
         subject: `#pageLike - ${page.creator.username} liked ${page.path}`,
         template: 'notification/pageLike.txt',
         template: 'notification/pageLike.txt',
         vars: {}
         vars: {}
-      });
-    });
+      },
+      slack: {},
+    };
+
+    this.sendNotification(notifications, option);
   }
   }
 
 
   /**
   /**
@@ -112,17 +132,18 @@ class GlobalNotification {
    * @param {obejct} page
    * @param {obejct} page
    * @param {obejct} comment
    * @param {obejct} comment
    */
    */
-  sendCommentNotification(comment, path) {
+  notifyComment(comment, path) {
     const notifications = Notification.findSettingByPathAndEvent(path, 'comment');
     const notifications = Notification.findSettingByPathAndEvent(path, 'comment');
-    const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
-    mailNotifications.forEach(notification => {
-      mailer.send({
-        to: notification.notifyTo.toEmail,
+    const option = {
+      mail: {
         subject: `#comment - ${comment.creator.username} commented on ${path}`,
         subject: `#comment - ${comment.creator.username} commented on ${path}`,
         template: 'notification/comment.txt',
         template: 'notification/comment.txt',
         vars: {}
         vars: {}
-      });
-    });
+      },
+      slack: {},
+    };
+
+    this.sendNotification(notifications, option);
   }
   }
 }
 }