index.ts 2.0 KB

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