global-notification.js 5.2 KB

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