Browse Source

use EVENT and TYPE master

mizozobu 6 years ago
parent
commit
3b2ed98803

+ 20 - 0
src/server/models/GlobalNotificationSetting.js

@@ -7,6 +7,26 @@ const GlobalNotificationSetting = require('./GlobalNotificationSetting/index');
 const GlobalNotificationSettingClass = GlobalNotificationSetting.class;
 const GlobalNotificationSettingSchema = GlobalNotificationSetting.schema;
 
+/**
+ * global notifcation event master
+ */
+GlobalNotificationSettingSchema.statics.EVENT = {
+  PAGE_CREATE: 'pageCreate',
+  PAGE_EDIT: 'pageEdit',
+  PAGE_DELETE: 'pageDelete',
+  PAGE_MOVE: 'pageMove',
+  PAGE_LIKE: 'pageLike',
+  COMMENT: 'comment',
+};
+
+/**
+ * global notifcation type master
+ */
+GlobalNotificationSettingSchema.statics.TYPE = {
+  MAIL: 'mail',
+  SLACK: 'slack',
+};
+
 module.exports = function(crowi) {
   GlobalNotificationSettingClass.crowi = crowi;
   GlobalNotificationSettingSchema.loadClass(GlobalNotificationSettingClass);

+ 8 - 3
src/server/models/GlobalNotificationSetting/GlobalNotificationMailSetting.js

@@ -9,9 +9,14 @@ module.exports = function(crowi) {
   GlobalNotificationSettingSchema.loadClass(GlobalNotificationSettingClass);
 
   const GlobalNotificationSettingModel = mongoose.model('GlobalNotificationSetting', GlobalNotificationSettingSchema);
-  const GlobalNotificationMailSettingModel = GlobalNotificationSettingModel.discriminator('mail', new mongoose.Schema({
-    toEmail: String,
-  }, { discriminatorKey: 'type' }));
+  const GlobalNotificationMailSettingModel = GlobalNotificationSettingModel.discriminator(
+    GlobalNotificationSetting.schema.statics.TYPE.MAIL,
+    new mongoose.Schema({
+      toEmail: String,
+    }, {
+      discriminatorKey: 'type',
+    }),
+  );
 
   return GlobalNotificationMailSettingModel;
 };

+ 8 - 3
src/server/models/GlobalNotificationSetting/GlobalNotificationSlackSetting.js

@@ -9,9 +9,14 @@ module.exports = function(crowi) {
   GlobalNotificationSettingSchema.loadClass(GlobalNotificationSettingClass);
 
   const GlobalNotificationSettingModel = mongoose.model('GlobalNotificationSetting', GlobalNotificationSettingSchema);
-  const GlobalNotificationSlackSettingModel = GlobalNotificationSettingModel.discriminator('slack', new mongoose.Schema({
-    slackChannels: String,
-  }, { discriminatorKey: 'type' }));
+  const GlobalNotificationSlackSettingModel = GlobalNotificationSettingModel.discriminator(
+    GlobalNotificationSetting.schema.statics.TYPE.SLACK,
+    new mongoose.Schema({
+      slackChannels: String,
+    }, {
+      discriminatorKey: 'type',
+    }),
+  );
 
   return GlobalNotificationSlackSettingModel;
 };

+ 0 - 12
src/server/models/GlobalNotificationSetting/index.js

@@ -11,18 +11,6 @@ const globalNotificationSettingSchema = new mongoose.Schema({
   triggerEvents: { type: [String] },
 });
 
-/**
- * global notifcation event master
- */
-globalNotificationSettingSchema.EVENT = {
-  PAGE_CREATE: 'pageCreate',
-  PAGE_EDIT: 'pageEdit',
-  PAGE_DELETE: 'pageDelete',
-  PAGE_MOVE: 'pageMove',
-  PAGE_LIKE: 'pageLike',
-  COMMENT: 'comment',
-};
-
 /*
 * e.g. "/a/b/c" => ["/a/b/c", "/a/b", "/a", "/"]
 */

+ 4 - 4
src/server/routes/admin.js

@@ -331,11 +331,11 @@ module.exports = function(crowi, app) {
     let setting;
 
     switch (form.notifyToType) {
-      case 'mail':
+      case GlobalNotificationSetting.TYPE.MAIL:
         setting = new GlobalNotificationMailSetting(crowi);
         setting.toEmail = form.toEmail;
         break;
-      case 'slack':
+      case GlobalNotificationSetting.TYPE.SLACK:
         setting = new GlobalNotificationSlackSetting(crowi);
         setting.slackChannels = form.slackChannels;
         break;
@@ -357,10 +357,10 @@ module.exports = function(crowi, app) {
     const setting = await GlobalNotificationSetting.findOne({ _id: form.id });
 
     switch (form.notifyToType) {
-      case 'mail':
+      case GlobalNotificationSetting.TYPE.MAIL:
         setting.toEmail = form.toEmail;
         break;
-      case 'slack':
+      case GlobalNotificationSetting.TYPE.SLACK:
         setting.slackChannels = form.slackChannels;
         break;
       default:

+ 1 - 1
src/server/routes/comment.js

@@ -132,7 +132,7 @@ module.exports = function(crowi, app) {
     const path = page.path;
 
     // global notification
-    globalNotificationService.fire(GlobalNotificationSetting.schema.EVENT.COMMENT, path, req.user, {
+    globalNotificationService.fire(GlobalNotificationSetting.EVENT.COMMENT, path, req.user, {
       comment: createdComment,
     });
 

+ 5 - 5
src/server/routes/page.js

@@ -608,7 +608,7 @@ module.exports = function(crowi, app) {
 
     // global notification
     try {
-      await globalNotificationService.fire(GlobalNotificationSetting.schema.EVENT.PAGE_CREATE, createdPage.path, req.user);
+      await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_CREATE, createdPage.path, req.user);
     }
     catch (err) {
       logger.error(err);
@@ -695,7 +695,7 @@ module.exports = function(crowi, app) {
 
     // global notification
     try {
-      await globalNotificationService.fire(GlobalNotificationSetting.schema.EVENT.PAGE_EDIT, page.path, req.user);
+      await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_EDIT, page.path, req.user);
     }
     catch (err) {
       logger.error(err);
@@ -863,7 +863,7 @@ module.exports = function(crowi, app) {
 
     try {
       // global notification
-      await globalNotificationService.fire(GlobalNotificationSetting.schema.EVENT.PAGE_LIKE, page.path, req.user);
+      await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_LIKE, page.path, req.user);
     }
     catch (err) {
       logger.error('Like failed', err);
@@ -1000,7 +1000,7 @@ module.exports = function(crowi, app) {
     res.json(ApiResponse.success(result));
 
     // global notification
-    await globalNotificationService.fire(GlobalNotificationSetting.schema.EVENT.PAGE_DELETE, page.path, req.user);
+    await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_DELETE, page.path, req.user);
   };
 
   /**
@@ -1105,7 +1105,7 @@ module.exports = function(crowi, app) {
     res.json(ApiResponse.success(result));
 
     // global notification
-    globalNotificationService.fire(GlobalNotificationSetting.schema.EVENT.PAGE_MOVE, page.path, req.user, {
+    globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_MOVE, page.path, req.user, {
       oldPath: req.body.path,
     });
 

+ 2 - 2
src/server/service/global-notification/global-notification-mail.js

@@ -8,9 +8,9 @@ class GlobalNotificationMailService {
 
   constructor(crowi) {
     this.crowi = crowi;
-    this.type = 'mail';
     this.mailer = crowi.getMailer();
-    this.event = crowi.model('GlobalNotificationSetting').schema.EVENT;
+    this.type = crowi.model('GlobalNotificationSetting').TYPE.MAIL;
+    this.event = crowi.model('GlobalNotificationSetting').EVENT;
   }
 
   /**

+ 2 - 2
src/server/service/global-notification/global-notification-slack.js

@@ -8,9 +8,9 @@ class GlobalNotificationSlackService {
 
   constructor(crowi) {
     this.crowi = crowi;
-    this.type = 'slack';
     this.slack = crowi.getSlack();
-    this.event = crowi.model('GlobalNotificationSetting').schema.EVENT;
+    this.type = crowi.model('GlobalNotificationSetting').TYPE.SLACK;
+    this.event = crowi.model('GlobalNotificationSetting').EVENT;
   }
 
   /**