index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const toArrayFromCsv = require('~/utils/to-array-from-csv');
  2. /**
  3. * service class of UserNotification
  4. */
  5. class UserNotificationService {
  6. constructor(crowi) {
  7. this.crowi = crowi;
  8. this.Page = this.crowi.model('Page');
  9. }
  10. /**
  11. * fire user notification
  12. *
  13. * @memberof UserNotificationService
  14. *
  15. * @param {Page} page
  16. * @param {User} user
  17. * @param {string} slackChannelsStr comma separated string. e.g. 'general,channel1,channel2'
  18. * @param {string} mode 'create' or 'update' or 'comment'
  19. * @param {string} previousRevision
  20. * @param {Comment} comment
  21. */
  22. async fire(page, user, slackChannelsStr, mode, option, comment = {}) {
  23. const {
  24. slackIntegrationService, slackLegacy, slack,
  25. } = this.crowi;
  26. const opt = option || {};
  27. const previousRevision = opt.previousRevision || '';
  28. await page.updateSlackChannels(slackChannelsStr);
  29. if (!slackIntegrationService.hasSlackConfig()) {
  30. throw new Error('slackIntegrationService has not been set up');
  31. }
  32. // "dev,slacktest" => [dev,slacktest]
  33. const slackChannels = toArrayFromCsv(slackChannelsStr);
  34. const promises = slackChannels.map(async(chan) => {
  35. let res;
  36. if (mode === 'comment') {
  37. res = await slack.postComment(comment, user, chan, page.path);
  38. res = await slackLegacy.postComment(comment, user, chan, page.path);
  39. }
  40. else {
  41. res = await slack.postPage(page, user, chan, mode, previousRevision);
  42. res = await slackLegacy.postPage(page, user, chan, mode, previousRevision);
  43. }
  44. return res;
  45. });
  46. return Promise.allSettled(promises);
  47. }
  48. }
  49. module.exports = UserNotificationService;