Yuki Takei 4 лет назад
Родитель
Сommit
ddd9f5d129

+ 1 - 1
packages/app/src/server/crowi/index.js

@@ -17,6 +17,7 @@ import AppService from '../service/app';
 import AclService from '../service/acl';
 import AttachmentService from '../service/attachment';
 import { SlackIntegrationService } from '../service/slack-integration';
+import { UserNotificationService } from '../service/user-notification';
 
 const logger = loggerFactory('growi:crowi');
 const httpErrorHandler = require('../middlewares/http-error-handler');
@@ -502,7 +503,6 @@ Crowi.prototype.setUpGlobalNotification = async function() {
  * setup UserNotificationService
  */
 Crowi.prototype.setUpUserNotification = async function() {
-  const UserNotificationService = require('../service/user-notification');
   if (this.userNotificationService == null) {
     this.userNotificationService = new UserNotificationService(this);
   }

+ 29 - 16
packages/app/src/server/service/user-notification/index.ts

@@ -1,5 +1,8 @@
+import mongoose from 'mongoose';
+
 import { toArrayFromCsv } from '~/utils/to-array-from-csv';
 
+
 import {
   prepareSlackMessageForPage,
   prepareSlackMessageForComment,
@@ -8,12 +11,13 @@ import {
 /**
  * service class of UserNotification
  */
-class UserNotificationService {
+export class UserNotificationService {
+
+  // eslint-disable-next-line @typescript-eslint/no-explicit-any
+  crowi!: any;
 
   constructor(crowi) {
     this.crowi = crowi;
-
-    this.Page = this.crowi.model('Page');
   }
 
   /**
@@ -28,26 +32,25 @@ class UserNotificationService {
    * @param {string} previousRevision
    * @param {Comment} comment
    */
-  async fire(page, user, slackChannelsStr, mode, option, comment = {}) {
+  // eslint-disable-next-line @typescript-eslint/no-explicit-any
+  async fire(page, user, slackChannelsStr, mode, option, comment = {}): Promise<PromiseSettledResult<any>[]> {
     const {
       appService, slackIntegrationService,
     } = this.crowi;
 
-    const opt = option || {};
-    const previousRevision = opt.previousRevision || '';
-
-    await page.updateSlackChannels(slackChannelsStr);
-
     if (!slackIntegrationService.isSlackConfigured) {
       throw new Error('slackIntegrationService has not been set up');
     }
 
+    // update slackChannels attribute asynchronously
+    page.updateSlackChannels(slackChannelsStr);
+
+    const opt = option || {};
+    const previousRevision = opt.previousRevision || '';
+
     // "dev,slacktest" => [dev,slacktest]
-    const slackChannels = toArrayFromCsv(slackChannelsStr);
-    // insert null if empty to notify once
-    if (slackChannels.length === 0) {
-      slackChannels.push(null);
-    }
+    const slackChannels: (string|null)[] = toArrayFromCsv(slackChannelsStr);
+    await this.putDefaultChannelIfEmpty(page.path, slackChannels);
 
     const appTitle = appService.getAppTitle();
     const siteUrl = appService.getSiteUrl();
@@ -67,6 +70,16 @@ class UserNotificationService {
     return Promise.allSettled(promises);
   }
 
-}
+  private async putDefaultChannelIfEmpty(pagePath:string, slackChannels: (string|null)[]): Promise<void> {
+    const UpdatePost = mongoose.model('UpdatePost');
+
+    const updatePosts = await UpdatePost.findSettingsByPath(pagePath);
+    slackChannels.push(...(updatePosts).map(up => up.channel));
+
+    // insert null if empty to notify once
+    if (slackChannels.length === 0) {
+      slackChannels.push(null);
+    }
+  }
 
-module.exports = UserNotificationService;
+}