| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import UpdatePost from '~/server/models/update-post';
- import { toArrayFromCsv } from '~/utils/to-array-from-csv';
- import {
- prepareSlackMessageForPage,
- prepareSlackMessageForComment,
- } from '../../util/slack';
- /**
- * service class of UserNotification
- */
- export class UserNotificationService {
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- crowi!: any;
- constructor(crowi) {
- this.crowi = crowi;
- }
- /**
- * fire user notification
- *
- * @memberof UserNotificationService
- *
- * @param {Page} page
- * @param {User} user
- * @param {string} slackChannelsStr comma separated string. e.g. 'general,channel1,channel2'
- * @param {string} mode 'create' or 'update' or 'comment'
- * @param {string} previousRevision
- * @param {Comment} 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;
- 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: (string|null)[] = toArrayFromCsv(slackChannelsStr);
- const appTitle = appService.getAppTitle();
- const siteUrl = appService.getSiteUrl();
- const promises = slackChannels.map(async(chan) => {
- let messageObj;
- if (mode === 'comment') {
- messageObj = prepareSlackMessageForComment(comment, user, appTitle, siteUrl, chan, page.path);
- }
- else {
- messageObj = prepareSlackMessageForPage(page, user, appTitle, siteUrl, chan, mode, previousRevision);
- }
- return slackIntegrationService.postMessage(messageObj);
- });
- return Promise.allSettled(promises);
- }
- }
|