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

create UserNotificationService

itizawa 5 лет назад
Родитель
Сommit
d8d339fb7c

+ 11 - 0
src/lib/util/toArrayFromCsv.js

@@ -0,0 +1,11 @@
+
+// converts csv item to array
+const toArrayFromCsv = (text) => {
+  if (text == null) {
+    return [];
+  }
+
+  return text.split(',');
+};
+
+module.exports = toArrayFromCsv;

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

@@ -41,6 +41,7 @@ function Crowi(rootdir) {
   this.mailService = null;
   this.mailService = null;
   this.passportService = null;
   this.passportService = null;
   this.globalNotificationService = null;
   this.globalNotificationService = null;
+  this.userNotificationService = null;
   this.slackNotificationService = null;
   this.slackNotificationService = null;
   this.xssService = null;
   this.xssService = null;
   this.aclService = null;
   this.aclService = null;
@@ -313,6 +314,10 @@ Crowi.prototype.getGlobalNotificationService = function() {
   return this.globalNotificationService;
   return this.globalNotificationService;
 };
 };
 
 
+Crowi.prototype.getUserNotificationService = function() {
+  return this.userNotificationService;
+};
+
 Crowi.prototype.getRestQiitaAPIService = function() {
 Crowi.prototype.getRestQiitaAPIService = function() {
   return this.restQiitaAPIService;
   return this.restQiitaAPIService;
 };
 };
@@ -478,6 +483,16 @@ Crowi.prototype.setUpGlobalNotification = async function() {
   }
   }
 };
 };
 
 
+/**
+ * setup UserNotificationService
+ */
+Crowi.prototype.setUpGlobalNotification = async function() {
+  const UserNotificationService = require('../service/user-notification');
+  if (this.userNotificationService == null) {
+    this.userNotificationService = new UserNotificationService(this);
+  }
+};
+
 /**
 /**
  * setup SlackNotificationService
  * setup SlackNotificationService
  */
  */

+ 3 - 33
src/server/routes/apiv3/pages.js

@@ -25,38 +25,8 @@ module.exports = (crowi) => {
   const GlobalNotificationSetting = crowi.model('GlobalNotificationSetting');
   const GlobalNotificationSetting = crowi.model('GlobalNotificationSetting');
 
 
   const globalNotificationService = crowi.getGlobalNotificationService();
   const globalNotificationService = crowi.getGlobalNotificationService();
-  const { pageService, slackNotificationService } = crowi;
-
-  // user notification
-  // TODO GW-3387 create '/service/user-notification' module
-  /**
-   *
-   * @param {Page} page
-   * @param {User} user
-   * @param {string} slackChannelsStr comma separated string. e.g. 'general,channel1,channel2'
-   * @param {boolean} updateOrCreate
-   * @param {string} previousRevision
-   */
-  async function notifyToSlackByUser(page, user, slackChannelsStr, updateOrCreate, previousRevision) {
-    await page.updateSlackChannel(slackChannelsStr)
-      .catch((err) => {
-        logger.error('Error occured in updating slack channels: ', err);
-      });
-
-
-    if (slackNotificationService.hasSlackConfig()) {
-      const slackChannels = slackChannelsStr != null ? slackChannelsStr.split(',') : [null];
-
-      const promises = slackChannels.map((chan) => {
-        return crowi.slack.postPage(page, user, chan, updateOrCreate, previousRevision);
-      });
-
-      Promise.all(promises)
-        .catch((err) => {
-          logger.error('Error occured in sending slack notification: ', err);
-        });
-    }
-  }
+  const userNotificationService = crowi.getUserNotificationService();
+  const { pageService } = crowi;
 
 
   // TODO write swagger(GW-3384) and validation(GW-3385)
   // TODO write swagger(GW-3384) and validation(GW-3385)
   router.post('/', accessTokenParser, loginRequiredStrictly, csrf, async(req, res) => {
   router.post('/', accessTokenParser, loginRequiredStrictly, csrf, async(req, res) => {
@@ -106,7 +76,7 @@ module.exports = (crowi) => {
 
 
     // user notification
     // user notification
     if (isSlackEnabled) {
     if (isSlackEnabled) {
-      await notifyToSlackByUser(createdPage, req.user, slackChannels, 'create', false);
+      await userNotificationService.fire(createdPage, req.user, slackChannels, 'create', false);
     }
     }
 
 
     return res.apiv3(result);
     return res.apiv3(result);

+ 57 - 0
src/server/service/user-notification/index.js

@@ -0,0 +1,57 @@
+const toArrayFromCsv = require('../../../lib/util/toArrayFromCsv');
+
+const logger = require('@alias/logger')('growi:service:UserNotificationService');
+
+/**
+ * service class of UserNotification
+ */
+class UserNotificationService {
+
+  constructor(crowi) {
+    this.crowi = crowi;
+
+    this.Page = this.crowi.model('Page');
+  }
+
+  /**
+   * fire user notification
+   *
+   * @memberof UserNotificationService
+   *
+   * @param {Page} page
+   * @param {User} user
+   * @param {string} slackChannelsStr comma separated string. e.g. 'general,channel1,channel2'
+   * @param {boolean} updateOrCreate
+   * @param {string} previousRevision
+   */
+  async fire(page, user, slackChannelsStr, updateOrCreate, previousRevision) {
+    const { slackNotificationService, slack } = this.crowi;
+
+    try {
+      await page.updateSlackChannel(slackChannelsStr);
+    }
+    catch (err) {
+      logger.error('Error occured in updating slack channels: ', err);
+    }
+
+
+    if (!slackNotificationService.hasSlackConfig()) {
+      return;
+    }
+
+    // "dev,slacktest" => [dev,slacktest]
+    const slackChannels = toArrayFromCsv(slackChannelsStr);
+
+    const promises = slackChannels.map((chan) => {
+      return slack.postPage(page, user, chan, updateOrCreate, previousRevision);
+    });
+
+    Promise.all(promises)
+      .catch((err) => {
+        logger.error('Error occured in sending slack notification: ', err);
+      });
+  }
+
+}
+
+module.exports = UserNotificationService;