global-notification.js 5.4 KB

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