global-notification-mail.js 3.7 KB

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