index.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { toArrayFromCsv } from '~/utils/to-array-from-csv';
  2. import {
  3. prepareSlackMessageForPage,
  4. prepareSlackMessageForComment,
  5. } from '../../util/slack';
  6. /**
  7. * service class of UserNotification
  8. */
  9. export class UserNotificationService {
  10. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  11. crowi!: any;
  12. constructor(crowi) {
  13. this.crowi = crowi;
  14. }
  15. /**
  16. * fire user notification
  17. *
  18. * @memberof UserNotificationService
  19. *
  20. * @param {Page} page
  21. * @param {User} user
  22. * @param {string} slackChannelsStr comma separated string. e.g. 'general,channel1,channel2'
  23. * @param {string} mode 'create' or 'update' or 'comment'
  24. * @param {string} previousRevision
  25. * @param {Comment} comment
  26. */
  27. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  28. async fire(page, user, slackChannelsStr, mode, option?: { previousRevision: string }, comment = {}): Promise<PromiseSettledResult<any>[]> {
  29. const {
  30. appService, slackIntegrationService,
  31. } = this.crowi;
  32. if (!slackIntegrationService.isSlackConfigured) {
  33. throw new Error('slackIntegrationService has not been set up');
  34. }
  35. // update slackChannels attribute asynchronously
  36. page.updateSlackChannels(slackChannelsStr);
  37. const { previousRevision } = option ?? {};
  38. // "dev,slacktest" => [dev,slacktest]
  39. const slackChannels: (string|null)[] = toArrayFromCsv(slackChannelsStr);
  40. const appTitle = appService.getAppTitle();
  41. const siteUrl = appService.getSiteUrl();
  42. const promises = slackChannels.map(async(chan) => {
  43. let messageObj;
  44. if (mode === 'comment') {
  45. messageObj = prepareSlackMessageForComment(comment, user, appTitle, siteUrl, chan, page.path);
  46. }
  47. else {
  48. messageObj = prepareSlackMessageForPage(page, user, appTitle, siteUrl, chan, mode, previousRevision);
  49. }
  50. return slackIntegrationService.postMessage(messageObj);
  51. });
  52. return Promise.allSettled(promises);
  53. }
  54. }