global-notification.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. const debug = require('debug')('growi:service:GlobalNotification');
  2. const path = require('path');
  3. const Notification = require('../models/GlobalNotificationSetting');
  4. const mailer = require('../util/mailer');
  5. const testNotifyData = [
  6. {
  7. "_id": "5b45ab384a702f4484010066",
  8. "isEnabled": true,
  9. "triggerEvents": ["comment, pageCreate"],
  10. "__t": "mail",
  11. "triggerPath": "/*",
  12. "toEmail": "email@email.com",
  13. "__v": 0
  14. },
  15. {
  16. "_id": "5b45ab384a702f4484010067",
  17. "isEnabled": true,
  18. "triggerEvents": ["comment, pageCreate"],
  19. "__t": "slack",
  20. "triggerPath": "/*",
  21. "slackChannels": "general, random",
  22. "__v": 0
  23. }
  24. ];
  25. /**
  26. * the service class of GlobalNotificationSetting
  27. */
  28. class GlobalNotification {
  29. constructor(crowi) {
  30. this.crowi = crowi;
  31. this.config = crowi.getConfig();
  32. }
  33. notifyByMail(notification, mailOption) {
  34. mailer.send(Object.assign(mailOption, {to: notification.toEmail}));
  35. }
  36. notifyBySlack(notification, slackOption) {
  37. // send slack notification here
  38. }
  39. sendNotification(notifications, option) {
  40. notifications.forEach(notification => {
  41. if (notification.__t === 'mail') {
  42. this.notifyByMail(notification, option.mail);
  43. }
  44. else if (notification.__t === 'slack') {
  45. this.notifyBySlack(notification, option.slack);
  46. }
  47. });
  48. }
  49. /**
  50. * send notification at page creation
  51. * @memberof GlobalNotification
  52. * @param {obejct} page
  53. */
  54. notifyPageCreate(page) {
  55. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageCreate');
  56. const lang = 'en-US'; //FIXME
  57. const option = {
  58. mail: {
  59. subject: `#pageCreate - ${page.creator.username} created ${page.path}`,
  60. template: path.join(this.crowi.localeDir, lang, 'notifications/pageCreate.txt'),
  61. vars: {}
  62. },
  63. slack: {},
  64. };
  65. this.sendNotification(notifications, option);
  66. }
  67. /**
  68. * send notification at page edit
  69. * @memberof GlobalNotification
  70. * @param {obejct} page
  71. */
  72. notifyPageEdit(page) {
  73. // const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageEdit');
  74. const notifications = testNotifyData;
  75. const lang = 'en-US'; //FIXME
  76. const option = {
  77. mail: {
  78. subject: `#pageEdit - ${page.creator.username} edited ${page.path}`,
  79. template: path.join(this.crowi.localeDir, lang, 'notifications/pageEdit.txt'),
  80. vars: {}
  81. },
  82. slack: {},
  83. };
  84. this.sendNotification(notifications, option);
  85. }
  86. /**
  87. * send notification at page deletion
  88. * @memberof GlobalNotification
  89. * @param {obejct} page
  90. */
  91. notifyPageDelete(page) {
  92. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageDelete');
  93. const lang = 'en-US'; //FIXME
  94. const option = {
  95. mail: {
  96. subject: `#pageDelete - ${page.creator.username} deleted ${page.path}`, //FIXME
  97. template: path.join(this.crowi.localeDir, lang, 'notifications/pageDelete.txt'),
  98. vars: {}
  99. },
  100. slack: {},
  101. };
  102. this.sendNotification(notifications, option);
  103. }
  104. /**
  105. * send notification at page move
  106. * @memberof GlobalNotification
  107. * @param {obejct} page
  108. */
  109. notifyPageMove(page) {
  110. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageMove');
  111. const lang = 'en-US'; //FIXME
  112. const option = {
  113. mail: {
  114. subject: `#pageMove - ${page.creator.username} moved ${page.path} to ${page.path}`, //FIXME
  115. template: path.join(this.crowi.localeDir, lang, 'notifications/pageMove.txt'),
  116. vars: {}
  117. },
  118. slack: {},
  119. };
  120. this.sendNotification(notifications, option);
  121. }
  122. /**
  123. * send notification at page like
  124. * @memberof GlobalNotification
  125. * @param {obejct} page
  126. */
  127. notifyPageLike(page) {
  128. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageLike');
  129. const lang = 'en-US'; //FIXME
  130. const option = {
  131. mail: {
  132. subject: `#pageLike - ${page.creator.username} liked ${page.path}`,
  133. template: path.join(this.crowi.localeDir, lang, 'notifications/pageLike.txt'),
  134. vars: {}
  135. },
  136. slack: {},
  137. };
  138. this.sendNotification(notifications, option);
  139. }
  140. /**
  141. * send notification at page comment
  142. * @memberof GlobalNotification
  143. * @param {obejct} page
  144. * @param {obejct} comment
  145. */
  146. notifyComment(comment, path) {
  147. const notifications = Notification.findSettingByPathAndEvent(path, 'comment');
  148. const lang = 'en-US'; //FIXME
  149. const option = {
  150. mail: {
  151. subject: `#comment - ${comment.creator.username} commented on ${path}`,
  152. template: path.join(this.crowi.localeDir, lang, 'notifications/comment.txt'),
  153. vars: {}
  154. },
  155. slack: {},
  156. };
  157. this.sendNotification(notifications, option);
  158. }
  159. }
  160. module.exports = GlobalNotification;