global-notification.js 4.8 KB

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