sou 7 лет назад
Родитель
Сommit
b146f99bed
1 измененных файлов с 58 добавлено и 84 удалено
  1. 58 84
      lib/service/global-notification.js

+ 58 - 84
lib/service/global-notification.js

@@ -2,78 +2,6 @@ const debug = require('debug')('growi:service:GlobalNotification');
 const Notification = require('../models/global-notification-setting');
 const Notification = require('../models/global-notification-setting');
 const mailer = require('../util/mailer');
 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
  * the service class of GlobalNotificationSetting
  */
  */
@@ -84,6 +12,10 @@ class GlobalNotification {
     this.config = crowi.getConfig();
     this.config = crowi.getConfig();
   }
   }
 
 
+  mailNotify(notification, option) {
+    mailer.send(Object.assign(option, {to: notification.notifyTo.toEmail}));
+  }
+
   /**
   /**
    * send notification at page creation
    * send notification at page creation
    * @memberof GlobalNotification
    * @memberof GlobalNotification
@@ -91,8 +23,15 @@ class GlobalNotification {
    */
    */
   sendPageCreateNotification(page) {
   sendPageCreateNotification(page) {
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageCreate');
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageCreate');
-    pageCreateMailNotify(notifications, page);
-    // slackNotify(notifications, page);
+    const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
+    const option = {
+      subject: `#pageCreate - ${page.creator.username} created ${page.path}`,
+      template: 'notification/pageCreate.txt',
+      vars: {}
+    };
+    mailNotifications.forEach(notification => {
+      this.mailNotify(notification, option);
+    });
   }
   }
 
 
   /**
   /**
@@ -102,8 +41,15 @@ class GlobalNotification {
    */
    */
   sendPageEditNotification(page) {
   sendPageEditNotification(page) {
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageEdit');
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageEdit');
-    pageEditMailNotify(notifications, page);
-    // slackNotify(notifications, page);
+    const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
+    const option = {
+      subject: `#pageEdit - ${page.creator.username} edited ${page.path}`,
+      template: 'notification/pageEdit.txt',
+      vars: {}
+    };
+    mailNotifications.forEach(notification => {
+      this.mailNotify(notification, option);
+    });
   }
   }
 
 
   /**
   /**
@@ -113,8 +59,15 @@ class GlobalNotification {
    */
    */
   sendPageDeleteNotification(page) {
   sendPageDeleteNotification(page) {
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageDelete');
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageDelete');
-    pageDeleteMailNotify(notifications, page);
-    // slackNotify(notifications, page);
+    const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
+    const option = {
+      subject: `#pageDelete - ${page.creator.username} deleted ${page.path}`,  //FIXME
+      template: 'notification/pageDelete.txt',
+      vars: {}
+    };
+    mailNotifications.forEach(notification => {
+      this.mailNotify(notification, option);
+    });
   }
   }
 
 
   /**
   /**
@@ -124,8 +77,15 @@ class GlobalNotification {
    */
    */
   sendPageMoveNotification(page) {
   sendPageMoveNotification(page) {
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageMove');
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageMove');
-    pageMoveMailNotify(notifications, page);
-    // slackNotify(notifications, page);
+    const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
+    const option = {
+      subject: `#pageMove - ${page.creator.username} moved ${page.path} to ${page.path}`, //FIXME
+      template: 'notification/pageMove.txt',
+      vars: {}
+    };
+    mailNotifications.forEach(notification => {
+      this.mailNotify(notification, option);
+    });
   }
   }
 
 
   /**
   /**
@@ -135,8 +95,15 @@ class GlobalNotification {
    */
    */
   sendPageLikeNotification(page) {
   sendPageLikeNotification(page) {
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageLike');
     const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageLike');
-    pageLikeMailNotify(notifications, page);
-    // slackNotify(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: {}
+      });
+    });
   }
   }
 
 
   /**
   /**
@@ -147,8 +114,15 @@ class GlobalNotification {
    */
    */
   sendCommentNotification(comment, path) {
   sendCommentNotification(comment, path) {
     const notifications = Notification.findSettingByPathAndEvent(path, 'comment');
     const notifications = Notification.findSettingByPathAndEvent(path, 'comment');
-    commentMailNotify(notifications, comment, path);
-    // slackNotify(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: {}
+      });
+    });
   }
   }
 }
 }