global-notification.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const debug = require('debug')('growi:service:GlobalNotification');
  2. const Notification = require('../models/global-notification-setting');
  3. const mailer = require('../util/mailer');
  4. /**
  5. * the service class of GlobalNotificationSetting
  6. */
  7. class GlobalNotification {
  8. constructor(crowi) {
  9. this.crowi = crowi;
  10. this.config = crowi.getConfig();
  11. }
  12. mailNotify(notification, option) {
  13. mailer.send(Object.assign(option, {to: notification.notifyTo.toEmail}));
  14. }
  15. /**
  16. * send notification at page creation
  17. * @memberof GlobalNotification
  18. * @param {obejct} page
  19. */
  20. sendPageCreateNotification(page) {
  21. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageCreate');
  22. const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
  23. const option = {
  24. subject: `#pageCreate - ${page.creator.username} created ${page.path}`,
  25. template: 'notification/pageCreate.txt',
  26. vars: {}
  27. };
  28. mailNotifications.forEach(notification => {
  29. this.mailNotify(notification, option);
  30. });
  31. }
  32. /**
  33. * send notification at page edit
  34. * @memberof GlobalNotification
  35. * @param {obejct} page
  36. */
  37. sendPageEditNotification(page) {
  38. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageEdit');
  39. const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
  40. const option = {
  41. subject: `#pageEdit - ${page.creator.username} edited ${page.path}`,
  42. template: 'notification/pageEdit.txt',
  43. vars: {}
  44. };
  45. mailNotifications.forEach(notification => {
  46. this.mailNotify(notification, option);
  47. });
  48. }
  49. /**
  50. * send notification at page deletion
  51. * @memberof GlobalNotification
  52. * @param {obejct} page
  53. */
  54. sendPageDeleteNotification(page) {
  55. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageDelete');
  56. const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
  57. const option = {
  58. subject: `#pageDelete - ${page.creator.username} deleted ${page.path}`, //FIXME
  59. template: 'notification/pageDelete.txt',
  60. vars: {}
  61. };
  62. mailNotifications.forEach(notification => {
  63. this.mailNotify(notification, option);
  64. });
  65. }
  66. /**
  67. * send notification at page move
  68. * @memberof GlobalNotification
  69. * @param {obejct} page
  70. */
  71. sendPageMoveNotification(page) {
  72. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageMove');
  73. const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
  74. const option = {
  75. subject: `#pageMove - ${page.creator.username} moved ${page.path} to ${page.path}`, //FIXME
  76. template: 'notification/pageMove.txt',
  77. vars: {}
  78. };
  79. mailNotifications.forEach(notification => {
  80. this.mailNotify(notification, option);
  81. });
  82. }
  83. /**
  84. * send notification at page like
  85. * @memberof GlobalNotification
  86. * @param {obejct} page
  87. */
  88. sendPageLikeNotification(page) {
  89. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageLike');
  90. const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
  91. mailNotifications.forEach(notification => {
  92. mailer.send({
  93. to: notification.notifyTo.toEmail,
  94. subject: `#pageLike - ${page.creator.username} liked ${page.path}`,
  95. template: 'notification/pageLike.txt',
  96. vars: {}
  97. });
  98. });
  99. }
  100. /**
  101. * send notification at page comment
  102. * @memberof GlobalNotification
  103. * @param {obejct} page
  104. * @param {obejct} comment
  105. */
  106. sendCommentNotification(comment, path) {
  107. const notifications = Notification.findSettingByPathAndEvent(path, 'comment');
  108. const mailNotifications = notifications.filter(notification => notification.notifyTo.type === 'mail');
  109. mailNotifications.forEach(notification => {
  110. mailer.send({
  111. to: notification.notifyTo.toEmail,
  112. subject: `#comment - ${comment.creator.username} commented on ${path}`,
  113. template: 'notification/comment.txt',
  114. vars: {}
  115. });
  116. });
  117. }
  118. }
  119. module.exports = GlobalNotification;