global-notification.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const debug = require('debug')('growi:service:GlobalNotification');
  2. const Notification = require('../models/GlobalNotificationSetting');
  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. notifyByMail(notification, mailOption) {
  13. mailer.send(Object.assign(mailOption, {to: notification.toEmail}));
  14. }
  15. notifyBySlack(notification, slackOption) {
  16. // send slack notification here
  17. }
  18. sendNotification(notifications, option) {
  19. notifications.forEach(notification => {
  20. if (notification.__t === 'mail') {
  21. this.notifyByMail(notification, option.mail);
  22. }
  23. else if (notification.__t === 'slack') {
  24. this.notifyBySlack(notification, option.slack);
  25. }
  26. });
  27. }
  28. /**
  29. * send notification at page creation
  30. * @memberof GlobalNotification
  31. * @param {obejct} page
  32. */
  33. notifyPageCreate(page) {
  34. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageCreate');
  35. const option = {
  36. mail: {
  37. subject: `#pageCreate - ${page.creator.username} created ${page.path}`,
  38. template: 'notification/pageCreate.txt',
  39. vars: {}
  40. },
  41. slack: {},
  42. };
  43. this.sendNotification(notifications, option);
  44. }
  45. /**
  46. * send notification at page edit
  47. * @memberof GlobalNotification
  48. * @param {obejct} page
  49. */
  50. notifyPageEdit(page) {
  51. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageEdit');
  52. const option = {
  53. mail: {
  54. subject: `#pageEdit - ${page.creator.username} edited ${page.path}`,
  55. template: 'notification/pageEdit.txt',
  56. vars: {}
  57. },
  58. slack: {},
  59. };
  60. this.sendNotification(notifications, option);
  61. }
  62. /**
  63. * send notification at page deletion
  64. * @memberof GlobalNotification
  65. * @param {obejct} page
  66. */
  67. notifyPageDelete(page) {
  68. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageDelete');
  69. const option = {
  70. mail: {
  71. subject: `#pageDelete - ${page.creator.username} deleted ${page.path}`, //FIXME
  72. template: 'notification/pageDelete.txt',
  73. vars: {}
  74. },
  75. slack: {},
  76. };
  77. this.sendNotification(notifications, option);
  78. }
  79. /**
  80. * send notification at page move
  81. * @memberof GlobalNotification
  82. * @param {obejct} page
  83. */
  84. notifyPageMove(page) {
  85. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageMove');
  86. const option = {
  87. mail: {
  88. subject: `#pageMove - ${page.creator.username} moved ${page.path} to ${page.path}`, //FIXME
  89. template: 'notification/pageMove.txt',
  90. vars: {}
  91. },
  92. slack: {},
  93. };
  94. this.sendNotification(notifications, option);
  95. }
  96. /**
  97. * send notification at page like
  98. * @memberof GlobalNotification
  99. * @param {obejct} page
  100. */
  101. notifyPageLike(page) {
  102. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageLike');
  103. const option = {
  104. mail: {
  105. subject: `#pageLike - ${page.creator.username} liked ${page.path}`,
  106. template: 'notification/pageLike.txt',
  107. vars: {}
  108. },
  109. slack: {},
  110. };
  111. this.sendNotification(notifications, option);
  112. }
  113. /**
  114. * send notification at page comment
  115. * @memberof GlobalNotification
  116. * @param {obejct} page
  117. * @param {obejct} comment
  118. */
  119. notifyComment(comment, path) {
  120. const notifications = Notification.findSettingByPathAndEvent(path, 'comment');
  121. const option = {
  122. mail: {
  123. subject: `#comment - ${comment.creator.username} commented on ${path}`,
  124. template: 'notification/comment.txt',
  125. vars: {}
  126. },
  127. slack: {},
  128. };
  129. this.sendNotification(notifications, option);
  130. }
  131. }
  132. module.exports = GlobalNotification;