Browse Source

CrowiSlackNotificationService, notification ns

mizozobu 6 years ago
parent
commit
dc78d623ff

+ 11 - 0
src/server/crowi/index.js

@@ -38,6 +38,7 @@ function Crowi(rootdir) {
   this.mailer = {};
   this.passportService = null;
   this.globalNotificationService = null;
+  this.crowiSlackNotificationService = null;
   this.restQiitaAPIService = null;
   this.cdnResourcesService = new CdnResourcesService();
   this.interceptorManager = new InterceptorManager();
@@ -438,6 +439,16 @@ Crowi.prototype.setUpGlobalNotification = function() {
   }
 };
 
+/**
+ * setup CrowiSlackNotificationService
+ */
+Crowi.prototype.setUpCrowiSlacklNotification = function() {
+  const CrowiSlackNotificationService = require('../service/crowi-slack-notification');
+  if (this.crowiSlackNotificationService == null) {
+    this.crowiSlackNotificationService = new CrowiSlackNotificationService(this);
+  }
+};
+
 /**
  * setup RestQiitaAPIService
  */

+ 18 - 0
src/server/models/config.js

@@ -138,6 +138,14 @@ module.exports = function(crowi) {
     };
   }
 
+  function getDefaultNotificationConfigs() {
+    return {
+      'slack:isIncomingWebhookPrioritized': false,
+      'slack:incomingWebhookUrl': '',
+      'slack:token': '',
+    };
+  }
+
   function getValueForCrowiNS(config, key) {
     crowi.configManager.getConfig('crowi', key);
     // // return the default value if undefined
@@ -179,6 +187,13 @@ module.exports = function(crowi) {
     return getDefaultMarkdownConfigs();
   };
 
+  /**
+   * It is deprecated to use this for anything other than ConfigLoader#load.
+   */
+  configSchema.statics.getDefaultNotificationConfigsObject = function() {
+    return getDefaultNotificationConfigs();
+  };
+
   configSchema.statics.getRestrictGuestModeLabels = function() {
     const labels = {};
     labels[SECURITY_RESTRICT_GUEST_MODE_DENY] = 'security_setting.guest_mode.deny';
@@ -239,6 +254,9 @@ module.exports = function(crowi) {
     else if (ns === 'markdown') {
       defaultConfig = getDefaultMarkdownConfigs();
     }
+    else if (ns === 'notification') {
+      defaultConfig = getDefaultNotificationConfigs();
+    }
 
     if (!defaultConfig[ns]) {
       defaultConfig[ns] = {};

+ 1 - 1
src/server/service/config-loader.js

@@ -218,7 +218,7 @@ class ConfigLoader {
     // merge defaults
     let mergedConfigFromDB = Object.assign({ crowi: this.configModel.getDefaultCrowiConfigsObject() }, configFromDB);
     mergedConfigFromDB = Object.assign({ markdown: this.configModel.getDefaultMarkdownConfigsObject() }, mergedConfigFromDB);
-
+    mergedConfigFromDB = Object.assign({ notification: this.configModel.getDefaultNotificationConfigsObject() }, mergedConfigFromDB);
 
     // In getConfig API, only null is used as a value to indicate that a config is not set.
     // So, if a value loaded from the database is emtpy,

+ 0 - 11
src/server/service/config-manager.js

@@ -156,17 +156,6 @@ class ConfigManager {
     }
   }
 
-  hasSlackConfig() {
-    let hasSlackToken = false;
-    let hasSlackIwhUrl = false;
-
-    if (this.configObject.notification) {
-      hasSlackToken = !!this.configObject.notification['slack:token'];
-      hasSlackIwhUrl = !!this.configObject.notification['slack:incomingWebhookUrl'];
-    }
-
-    return hasSlackToken || hasSlackIwhUrl;
-  }
 
   getIsPublicWikiOnly() {
     // CONF.RF save PUBLIC_WIKI_ONLY in mongodb?

+ 25 - 0
src/server/service/crowi-slack-notification.js

@@ -0,0 +1,25 @@
+const logger = require('@alias/logger')('growi:service:CrowiSlackNotification'); // eslint-disable-line no-unused-vars
+/**
+ * the service class of GlobalNotificationSetting
+ */
+class CrowiSlackNotificationService {
+
+  constructor(crowi) {
+    this.crowi = crowi;
+  }
+
+  hasSlackConfig() {
+    let hasSlackToken = false;
+    let hasSlackIwhUrl = false;
+
+    if (this.configObject.notification) {
+      hasSlackToken = !!this.crowi.configManager.getConfig('notification', 'slack:token');
+      hasSlackIwhUrl = !!this.crowi.configManager.getConfig('notification', 'slack:incomingWebhookUrl');
+    }
+
+    return hasSlackToken || hasSlackIwhUrl;
+  }
+
+}
+
+module.exports = CrowiSlackNotificationService;