global-notification-mail.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import nodePath from 'path';
  2. import { GlobalNotificationSettingEvent, GlobalNotificationSettingType } from '~/server/models/GlobalNotificationSetting';
  3. import { configManager } from '~/server/service/config-manager';
  4. import loggerFactory from '~/utils/logger';
  5. const logger = loggerFactory('growi:service:GlobalNotificationMailService'); // eslint-disable-line no-unused-vars
  6. /**
  7. * sub service class of GlobalNotificationSetting
  8. */
  9. class GlobalNotificationMailService {
  10. constructor(crowi) {
  11. this.crowi = crowi;
  12. }
  13. /**
  14. * send mail global notification
  15. *
  16. * @memberof GlobalNotificationMailService
  17. *
  18. * @param {string} event event name triggered
  19. * @param {import('~/server/models/page').PageDocument} page page triggered the event
  20. * @param {User} triggeredBy user who triggered the event
  21. * @param {{ comment: Comment, oldPath: string }} _ event specific vars
  22. */
  23. async fire(event, page, triggeredBy, vars) {
  24. const { mailService } = this.crowi;
  25. const GlobalNotification = this.crowi.model('GlobalNotificationSetting');
  26. const notifications = await GlobalNotification.findSettingByPathAndEvent(event, page.path, GlobalNotificationSettingType.MAIL);
  27. const option = this.generateOption(event, page, triggeredBy, vars);
  28. await Promise.all(notifications.map((notification) => {
  29. return mailService.send({ ...option, to: notification.toEmail });
  30. }));
  31. }
  32. /**
  33. * fire global notification
  34. *
  35. * @memberof GlobalNotificationMailService
  36. *
  37. * @param {string} event event name triggered
  38. * @param {import('~/server/models/page').PageDocument} page path triggered the event
  39. * @param {User} triggeredBy user triggered the event
  40. * @param {{ comment: Comment, oldPath: string }} _ event specific vars
  41. *
  42. * @return {{ subject: string, template: string, vars: object }}
  43. */
  44. generateOption(event, page, triggeredBy, { comment, oldPath }) {
  45. const locale = configManager.getConfig('crowi', 'app:globalLang');
  46. // validate for all events
  47. if (event == null || page == null || triggeredBy == null) {
  48. throw new Error(`invalid vars supplied to GlobalNotificationMailService.generateOption for event ${event}`);
  49. }
  50. const template = nodePath.join(this.crowi.localeDir, `${locale}/notifications/${event}.ejs`);
  51. const path = page.path;
  52. const appTitle = this.crowi.appService.getAppTitle();
  53. const siteUrl = this.crowi.appService.getSiteUrl();
  54. const pageUrl = new URL(page._id, siteUrl);
  55. let subject;
  56. let vars = {
  57. appTitle,
  58. siteUrl,
  59. path,
  60. username: triggeredBy.username,
  61. };
  62. switch (event) {
  63. case GlobalNotificationSettingEvent.PAGE_CREATE:
  64. subject = `#${event} - ${triggeredBy.username} created ${path} at URL: ${pageUrl}`;
  65. break;
  66. case GlobalNotificationSettingEvent.PAGE_EDIT:
  67. subject = `#${event} - ${triggeredBy.username} edited ${path} at URL: ${pageUrl}`;
  68. break;
  69. case GlobalNotificationSettingEvent.PAGE_DELETE:
  70. subject = `#${event} - ${triggeredBy.username} deleted ${path} at URL: ${pageUrl}`;
  71. break;
  72. case GlobalNotificationSettingEvent.PAGE_MOVE:
  73. // validate for page move
  74. if (oldPath == null) {
  75. throw new Error(`invalid vars supplied to GlobalNotificationMailService.generateOption for event ${event}`);
  76. }
  77. subject = `#${event} - ${triggeredBy.username} moved ${oldPath} to ${path} at URL: ${pageUrl}`;
  78. vars = {
  79. ...vars,
  80. oldPath,
  81. newPath: path,
  82. };
  83. break;
  84. case GlobalNotificationSettingEvent.PAGE_LIKE:
  85. subject = `#${event} - ${triggeredBy.username} liked ${path} at URL: ${pageUrl}`;
  86. break;
  87. case GlobalNotificationSettingEvent.COMMENT:
  88. // validate for comment
  89. if (comment == null) {
  90. throw new Error(`invalid vars supplied to GlobalNotificationMailService.generateOption for event ${event}`);
  91. }
  92. subject = `#${event} - ${triggeredBy.username} commented on ${path} at URL: ${pageUrl}`;
  93. vars = {
  94. ...vars,
  95. comment: comment.comment,
  96. };
  97. break;
  98. default:
  99. throw new Error(`unknown global notificaiton event: ${event}`);
  100. }
  101. return {
  102. subject,
  103. template,
  104. vars,
  105. };
  106. }
  107. }
  108. module.exports = GlobalNotificationMailService;