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

Merge pull request #1710 from weseek/feat/put-restrictions-for-page

Feat/put restrictions for page
Yuki Takei 6 лет назад
Родитель
Сommit
80da03f39c

+ 9 - 3
src/server/routes/comment.js

@@ -256,9 +256,15 @@ module.exports = function(crowi, app) {
     const path = page.path;
 
     // global notification
-    globalNotificationService.fire(GlobalNotificationSetting.EVENT.COMMENT, path, req.user, {
-      comment: createdComment,
-    });
+    try {
+      await globalNotificationService.fire(GlobalNotificationSetting.EVENT.COMMENT, page, req.user, {
+        comment: createdComment,
+      });
+    }
+    catch (err) {
+      logger.error('Comment notification failed', err);
+    }
+
 
     // slack notification
     if (slackNotificationForm.isSlackEnabled) {

+ 22 - 12
src/server/routes/page.js

@@ -833,10 +833,10 @@ module.exports = function(crowi, app) {
 
     // global notification
     try {
-      await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_CREATE, createdPage.path, req.user);
+      await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_CREATE, createdPage, req.user);
     }
     catch (err) {
-      logger.error(err);
+      logger.error('Create notification failed', err);
     }
 
     // user notification
@@ -961,10 +961,10 @@ module.exports = function(crowi, app) {
 
     // global notification
     try {
-      await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_EDIT, page.path, req.user);
+      await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_EDIT, page, req.user);
     }
     catch (err) {
-      logger.error(err);
+      logger.error('Edit notification failed', err);
     }
 
     // user notification
@@ -1299,10 +1299,10 @@ module.exports = function(crowi, app) {
 
     try {
       // global notification
-      await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_LIKE, page.path, req.user);
+      await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_LIKE, page, req.user);
     }
     catch (err) {
-      logger.error('Like failed', err);
+      logger.error('Like notification failed', err);
     }
   };
 
@@ -1499,8 +1499,13 @@ module.exports = function(crowi, app) {
 
     res.json(ApiResponse.success(result));
 
-    // global notification
-    await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_DELETE, page.path, req.user);
+    try {
+      // global notification
+      await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_DELETE, page, req.user);
+    }
+    catch (err) {
+      logger.error('Delete notification failed', err);
+    }
   };
 
   /**
@@ -1652,10 +1657,15 @@ module.exports = function(crowi, app) {
 
     res.json(ApiResponse.success(result));
 
-    // global notification
-    globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_MOVE, page.path, req.user, {
-      oldPath: req.body.path,
-    });
+    try {
+      // global notification
+      await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_MOVE, page, req.user, {
+        oldPath: req.body.path,
+      });
+    }
+    catch (err) {
+      logger.error('Move notification failed', err);
+    }
 
     return page;
   };

+ 39 - 5
src/server/service/global-notification/index.js

@@ -13,32 +13,66 @@ class GlobalNotificationService {
 
     this.gloabalNotificationMail = new GloabalNotificationMail(crowi);
     this.gloabalNotificationSlack = new GloabalNotificationSlack(crowi);
+
+    this.Page = this.crowi.model('Page');
+
   }
 
+
   /**
    * fire global notification
    *
    * @memberof GlobalNotificationService
    *
    * @param {string} event event name triggered
-   * @param {string} path path triggered the event
+   * @param {string} page page triggered the event
    * @param {User} triggeredBy user who triggered the event
    * @param {object} vars event specific vars
    */
-  async fire(event, path, triggeredBy, vars = {}) {
+  async fire(event, page, triggeredBy, vars = {}) {
     logger.debug(`global notficatoin event ${event} was triggered`);
 
     // validation
-    if (event == null || path == null || triggeredBy == null) {
+    if (event == null || page.path == null || triggeredBy == null) {
       throw new Error(`invalid vars supplied to GlobalNotificationSlackService.generateOption for event ${event}`);
     }
 
+    if (!this.isSendNotification(page.grant)) {
+      logger.info('this page does not send notifications');
+      return;
+    }
+
     await Promise.all([
-      this.gloabalNotificationMail.fire(event, path, triggeredBy, vars),
-      this.gloabalNotificationSlack.fire(event, path, triggeredBy, vars),
+      this.gloabalNotificationMail.fire(event, page.path, triggeredBy, vars),
+      this.gloabalNotificationSlack.fire(event, page.path, triggeredBy, vars),
     ]);
   }
 
+  /**
+   * fire global notification
+   *
+   * @memberof GlobalNotificationService
+   *
+   * @param {number} grant page grant
+   * @return {boolean} isSendNotification
+   */
+  isSendNotification(grant) {
+    switch (grant) {
+      case this.Page.GRANT_PUBLIC:
+        return true;
+      case this.Page.GRANT_RESTRICTED:
+        return false;
+      case this.Page.GRANT_SPECIFIED:
+        return false;
+      case this.Page.GRANT_OWNER:
+        // TODO GW 1271 switch isEnabled
+        return (this.crowi.configManager.getConfig('crowi', 'notification:owner-page:isEnabled') || false);
+      case this.Page.GRANT_USER_GROUP:
+        // TODO GW 1271 switch isEnabled
+        return (this.crowi.configManager.getConfig('crowi', 'notification:group-page:isEnabled') || false);
+    }
+  }
+
 }
 
 module.exports = GlobalNotificationService;