global-notification.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. const crowi = this.crowi;
  35. const mailer = crowi.getMailer();
  36. mailer.send(Object.assign(mailOption, {to: notification.toEmail}),
  37. function(err, s) {
  38. debug('completed to send email: ', err, s);
  39. next();
  40. }
  41. );
  42. }
  43. notifyBySlack(notification, slackOption) {
  44. // send slack notification here
  45. }
  46. sendNotification(notifications, option) {
  47. notifications.forEach(notification => {
  48. if (notification.__t === 'mail') {
  49. this.notifyByMail(notification, option.mail);
  50. }
  51. else if (notification.__t === 'slack') {
  52. this.notifyBySlack(notification, option.slack);
  53. }
  54. });
  55. }
  56. /**
  57. * send notification at page creation
  58. * @memberof GlobalNotification
  59. * @param {obejct} page
  60. */
  61. notifyPageCreate(page) {
  62. // const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageCreate');
  63. const notifications = testNotifyData;
  64. const lang = 'en-US'; //FIXME
  65. const option = {
  66. mail: {
  67. subject: `#pageCreate - ${page.creator.username} created ${page.path}`,
  68. template: `../../locales/${lang}/notifications/pageCreate.txt`,
  69. vars: {}
  70. },
  71. slack: {},
  72. };
  73. this.sendNotification(notifications, option);
  74. }
  75. /**
  76. * send notification at page edit
  77. * @memberof GlobalNotification
  78. * @param {obejct} page
  79. */
  80. notifyPageEdit(page) {
  81. // const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageEdit');
  82. const notifications = testNotifyData;
  83. const lang = 'en-US'; //FIXME
  84. const option = {
  85. mail: {
  86. subject: `#pageEdit - ${page.creator.username} edited ${page.path}`,
  87. template: `../../locales/${lang}/notifications/pageEdit.txt`,
  88. vars: {}
  89. },
  90. slack: {},
  91. };
  92. this.sendNotification(notifications, option);
  93. }
  94. /**
  95. * send notification at page deletion
  96. * @memberof GlobalNotification
  97. * @param {obejct} page
  98. */
  99. notifyPageDelete(page) {
  100. // const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageDelete');
  101. const notifications = testNotifyData;
  102. const lang = 'en-US'; //FIXME
  103. const option = {
  104. mail: {
  105. subject: `#pageDelete - ${page.creator.username} deleted ${page.path}`, //FIXME
  106. template: `../../locales/${lang}/notifications/pageDelete.txt`,
  107. vars: {}
  108. },
  109. slack: {},
  110. };
  111. this.sendNotification(notifications, option);
  112. }
  113. /**
  114. * send notification at page move
  115. * @memberof GlobalNotification
  116. * @param {obejct} page
  117. */
  118. notifyPageMove(page, user) {
  119. // const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageMove');
  120. const notifications = testNotifyData;
  121. const lang = 'en-US'; //FIXME
  122. const option = {
  123. mail: {
  124. subject: `#pageMove - ${user.username} moved ${page.path} to ${page.path}`, //FIXME
  125. template: `../../locales/${lang}/notifications/pageMove.txt`,
  126. vars: {}
  127. },
  128. slack: {},
  129. };
  130. this.sendNotification(notifications, option);
  131. }
  132. /**
  133. * send notification at page like
  134. * @memberof GlobalNotification
  135. * @param {obejct} page
  136. */
  137. notifyPageLike(page, user) {
  138. // const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageLike');
  139. const notifications = testNotifyData;
  140. const lang = 'en-US'; //FIXME
  141. const option = {
  142. mail: {
  143. subject: `#pageLike - ${user.username} liked ${page.path}`,
  144. template: `../../locales/${lang}/notifications/pageLike.txt`,
  145. vars: {}
  146. },
  147. slack: {},
  148. };
  149. this.sendNotification(notifications, option);
  150. }
  151. /**
  152. * send notification at page comment
  153. * @memberof GlobalNotification
  154. * @param {obejct} page
  155. * @param {obejct} comment
  156. */
  157. notifyComment(comment, path) {
  158. // const notifications = Notification.findSettingByPathAndEvent(path, 'comment');
  159. const notifications = testNotifyData;
  160. const lang = 'en-US'; //FIXME
  161. const option = {
  162. mail: {
  163. subject: `#comment - ${comment.creator.username} commented on ${path}`,
  164. template: `../../locales/${lang}/notifications/comment.txt`,
  165. vars: {}
  166. },
  167. slack: {},
  168. };
  169. this.sendNotification(notifications, option);
  170. }
  171. }
  172. module.exports = GlobalNotification;