global-notification.js 5.4 KB

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