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

+ 39 - 78
lib/models/global-notification-setting.js

@@ -1,111 +1,72 @@
-module.exports = function(crowi) {
-  const debug = require('debug')('growi:models:global-notification-setting');
-  const mongoose = require('mongoose');
-  const Notification = this;
+const debug = require('debug')('growi:models:global-notification-setting');
+const mongoose = require('mongoose');
+const Notification = this;
 
-  let notificationSchema;
+/*
+ * define schema
+ */
+const notificationSchema = new mongoose.Schema({
+  isEnabled: { type: Boolean, required: true, default: true },
+  triggerPath: { type: String, required: true },
+  triggerEvents: { type: [String] },
+  notifyTo: { type: String, default: '{}',
+    get: data => {
+      try {
+        return JSON.parse(data);
+      }
+      catch (e) {
+        return data;
+      }
+    },
+    set: data => JSON.stringify(data)
+  },
+});
 
-  notificationSchema = new mongoose.Schema({
-    isEnabled: { type: Boolean, required: true, default: false },
-    triggerPath: { type: String, required: true },
-    triggerEvents: { type: [String] },
-    toEmail: { type: String, required: true },
-  });
-
-  /**
-   * create new notification setting
-   * @param {string} triggerPath
-   * @param {[string]} triggerEvents
-   * @param {string} toEmail
-   */
-  notificationSchema.statics.create = (triggerPath, triggerEvents, toEmail) => {
-    // return new Promise((resolve, reject) => {
-      // save
-      // return resolve(Notification)
-    //}
-  };
+/**
+ * GlobalNotificationSetting Class
+ * @class GlobalNotificationSetting
+ */
+class GlobalNotificationSetting {
 
   /**
    * enable notification setting
    * @param {string} id
    */
-  notificationSchema.statics.enable = id => {
+  static enable(id) {
     // return new Promise((resolve, reject) => {
       // save
       // return resolve(Notification)
     //}
-  };
+  }
 
   /**
    * disable notification setting
    * @param {string} id
    */
-  notificationSchema.statics.disable = id => {
+  static disable(id) {
     // return new Promise((resolve, reject) => {
       // save
       // return resolve(Notification)
     //}
-  };
-
-  /**
-   * delete notification setting
-   * @param {string} id
-   */
-  notificationSchema.statics.delete = id => {
-    // return new Promise((resolve, reject) => {
-      // remove
-      // return resolve()
-    //}
-  };
-
-  /**
-   * update notification setting
-   * @param {string} id
-   */
-  notificationSchema.statics.update = id => {
-    // return new Promise((resolve, reject) => {
-      // save
-      // return resolve(Notification)
-    //}
-  };
-
-  /**
-   * find a notification setting by id
-   * @param {string} id
-   */
-  notificationSchema.statics.findSettingById = id => {
-    // return new Promise((resolve, reject) => {
-      // findOne
-      // 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
    */
-  notificationSchema.statics.findSettingByPathAndEvent = (path, event) => {
+  static findSettingByPathAndEvent(path, event, enabled) {
     // return new Promise((resolve, reject) => {
+      // sort by path in mongoDB
       // find
       // return resolve([Notification])
     //}
-  };
-
-  // classify a list of notification settings into enabled and disabled
-  notificationSchema.methods.classify = settings => {
-    // return resolve({enabled: [Notification], disabled: [Notification]})
-  };
-
-  // returns only enabled notification settings
-  notificationSchema.methods.getEnabeldSettings = settings => {
-    // return resolve([Notification])
-  };
-
-  // sort a list of notifications by path from short to long
-  notificationSchema.methods.sortByPath = settings => {
-    // return resolve([Notification])
-  };
+  }
+}
 
+module.exports = function(crowi) {
+  GlobalNotificationSetting.crowi = crowi;
+  notificationSchema.loadClass(GlobalNotificationSetting);
   return mongoose.model('GlobalNotificationSetting', notificationSchema);
 };

+ 1 - 5
lib/routes/admin.js

@@ -15,6 +15,7 @@ module.exports = function(crowi, app) {
     , pluginUtils = new PluginUtils()
     , ApiResponse = require('../util/apiResponse')
     , recommendedXssWhiteList = require('../util/recommendedXssWhiteList')
+    , notification = require('../util/global-notification')(crowi)
 
     , MAX_PAGE_LIST = 50
     , actions = {};
@@ -1070,11 +1071,6 @@ module.exports = function(crowi, app) {
     });
   };
 
-  // app.get('/_api/admin/notification/testGlobalNotification' , admin.api.testGlobalNotification);
-  // actions.api.testGlobalNotification = function(req, res) {
-  //   // NOTIFICATION: send test notification here
-  // };
-
   /**
    * save settings, update config cache, and response json
    *

+ 2 - 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('../util/global-notification')(crowi)
     , actions = {}
     , api = {};
 
@@ -101,6 +102,7 @@ module.exports = function(crowi, app) {
       }
     }
     // NOTIFICATION: send comment notification here
+    // notification.sendCommentNotification(comment, path);
   };
 
   /**

+ 0 - 1
lib/routes/index.js

@@ -105,7 +105,6 @@ module.exports = function(crowi, app) {
   app.post('/_api/admin/notification.add'    , loginRequired(crowi, app) , middleware.adminRequired() , csrf, admin.api.notificationAdd);
   app.post('/_api/admin/notification.remove' , loginRequired(crowi, app) , middleware.adminRequired() , csrf, admin.api.notificationRemove);
   app.get('/_api/admin/users.search'         , loginRequired(crowi, app) , middleware.adminRequired() , admin.api.usersSearch);
-  // app.post('/_api/admin/notification/testGlobalNotification', loginRequired(crowi, app) , middleware.adminRequired() , csrf, admin.api.testGlobalNotification);
 
   app.get('/admin/users'                , loginRequired(crowi, app) , middleware.adminRequired() , admin.user.index);
   app.post('/admin/user/invite'         , form.admin.userInvite ,  loginRequired(crowi, app) , middleware.adminRequired() , csrf, admin.user.invite);

+ 7 - 1
lib/routes/page.js

@@ -17,6 +17,7 @@ module.exports = function(crowi, app) {
     , pagePathUtil = require('../util/pagePathUtil')
     , swig = require('swig-templates')
     , getToday = require('../util/getToday')
+    , notification = require('../util/global-notification')(crowi)
 
     , actions = {};
 
@@ -678,14 +679,16 @@ module.exports = function(crowi, app) {
         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(() => {
+        // .then((page) => {
         //   // NOTIFICATION: send page create notification here
+        //   notification.sendPageCreateNotification(page);
         // })
       }
     }).then(function(data) {
@@ -977,6 +980,7 @@ module.exports = function(crowi, app) {
     });
     // .then(() => {
     //   // NOTIFICATION: send page like notification here
+    //   notification.sendPageLikeNotification(page);
     // })
   };
 
@@ -1085,6 +1089,7 @@ module.exports = function(crowi, app) {
     });
     // .then(() => {
     //   // NOTIFICATION: send page delete notification here
+    //   notification.sendPageDeleteNotification(page);
     // })
   };
 
@@ -1181,6 +1186,7 @@ module.exports = function(crowi, app) {
       });
       // .then(() => {
       //   // NOTIFICATION: send page move notification here
+      //   notification.sendPageMoveNotification(page);
       // })
     });
   };

+ 112 - 28
lib/service/global-notification.js

@@ -1,77 +1,161 @@
 const debug = require('debug')('growi:service:GlobalNotification');
+const Notification = require('../models/global-notification-setting');
+const mailer = require('../util/mailer');
+
+const pageCreateMailNotify = (notifications, page) => {
+  const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
+  mailNotifications.forEach(notification => {
+    mailer.send({
+      to: notification.notifyTo.toEmail,
+      subject: `#pageCreate - ${page.creator.username} created ${page.path}`,
+      template: 'notification/pageCreate.txt',
+      vars: {}
+    });
+  });
+};
+
+const pageEditMailNotify = (notifications, page) => {
+  const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
+  mailNotifications.forEach(notification => {
+    mailer.send({
+      to: notification.notifyTo.toEmail,
+      subject: `#pageEdit - ${page.creator.username} edited ${page.path}`,
+      template: 'notification/pageEdit.txt',
+      vars: {}
+    });
+  });
+};
+
+const pageDeleteMailNotify = (notifications, page) => {
+  const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
+  mailNotifications.forEach(notification => {
+    mailer.send({
+      to: notification.notifyTo.toEmail,
+      subject: `#pageDelete - ${page.creator.username} deleted ${page.path}`,  //FIXME
+      template: 'notification/pageDelete.txt',
+      vars: {}
+    });
+  });
+};
+
+const pageMoveMailNotify = (notifications, page) => {
+  const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
+  mailNotifications.forEach(notification => {
+    mailer.send({
+      to: notification.notifyTo.toEmail,
+      subject: `#pageMove - ${page.creator.username} moved ${page.path} to ${page.path}`, //FIXME
+      template: 'notification/pageMove.txt',
+      vars: {}
+    });
+  });
+};
+
+const pageLikeMailNotify = (notifications, page) => {
+  const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
+  mailNotifications.forEach(notification => {
+    mailer.send({
+      to: notification.notifyTo.toEmail,
+      subject: `#pageLike - ${page.creator.username} liked ${page.path}`,
+      template: 'notification/pageLike.txt',
+      vars: {}
+    });
+  });
+};
+
+const commentMailNotify = (notifications, comment, path) => {
+  const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
+  mailNotifications.forEach(notification => {
+    mailer.send({
+      to: notification.notifyTo.toEmail,
+      subject: `#comment - ${comment.creator.username} commented on ${path}`,
+      template: 'notification/comment.txt',
+      vars: {}
+    });
+  });
+};
 
 /**
  * the service class of GlobalNotificationSetting
+ * @static
  */
 class GlobalNotification {
 
-  constructor() {
+  constructor(crowi) {
+    this.config = crowi.getConfig();
   }
 
-  /**
-   * send test notification
-   * @memberof GlobalNotification
-   * @param {string} toEmail
-   */
-  sendTesteNotification(toEmail) {}
-
   /**
    * send notification at page creation
+   * @static
    * @memberof GlobalNotification
-   * @param {string} toEmail
    * @param {obejct} page
    */
-  sendCreateNotification(toEmail, page) {
-    // const option = {
-    //   to: toEmail,
-    //   subject: `#create - ${page.creator.username} created ${page.path}`,
-    //   template: 'notification/createPage.txt',
-    //   vars: {}
-    // };
-
-    // return mailer.send(option)
+  static sendPageCreateNotification(page) {
+    const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageCreate');
+    pageCreateMailNotify(notifications, page);
+    // slackNotify(notifications, page);
   }
 
   /**
    * send notification at page edit
+   * @static
    * @memberof GlobalNotification
-   * @param {string} toEmail
    * @param {obejct} page
    */
-  sendEditNotification(toEmail, page) {}
+  static sendPageEditNotification(page) {
+    const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageEdit');
+    pageEditMailNotify(notifications, page);
+    // slackNotify(notifications, page);
+  }
 
   /**
    * send notification at page deletion
+   * @static
    * @memberof GlobalNotification
-   * @param {string} toEmail
    * @param {obejct} page
    */
-  sendDeleteNotification(toEmail, page) {}
+  static sendPageDeleteNotification(page) {
+    const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageDelete');
+    pageDeleteMailNotify(notifications, page);
+    // slackNotify(notifications, page);
+  }
 
   /**
    * send notification at page move
+   * @static
    * @memberof GlobalNotification
-   * @param {string} toEmail
    * @param {obejct} page
    */
-  sendMoveNotification(toEmail, page) {}
+  static sendPageMoveNotification(page) {
+    const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageMove');
+    pageMoveMailNotify(notifications, page);
+    // slackNotify(notifications, page);
+  }
 
   /**
    * send notification at page like
+   * @static
    * @memberof GlobalNotification
-   * @param {string} toEmail
    * @param {obejct} page
    */
-  sendLikeNotification(toEmail, page) {}
+  static sendPageLikeNotification(page) {
+    const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageLike');
+    pageLikeMailNotify(notifications, page);
+    // slackNotify(notifications, page);
+  }
 
   /**
    * send notification at page comment
+   * @static
    * @memberof GlobalNotification
-   * @param {string} toEmail
    * @param {obejct} page
    * @param {obejct} comment
    */
-  sendCommentNotification(toEmail, page, comment) {}
+  static sendCommentNotification(comment, path) {
+    const notifications = Notification.findSettingByPathAndEvent(path, 'comment');
+    commentMailNotify(notifications, comment, path);
+    // slackNotify(notifications, comment, path);
+  }
 }
 
 module.exports = GlobalNotification;