global-notification.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.slack = crowi.slack;
  11. this.GlobalNotification = crowi.model('GlobalNotificationSetting');
  12. this.User = crowi.model('User');
  13. this.appTitle = crowi.appService.getAppTitle();
  14. }
  15. notifyByMail(notification, mailOption) {
  16. this.mailer.send(Object.assign(mailOption, { to: notification.toEmail }));
  17. }
  18. notifyBySlack(notification, slackOption) {
  19. this.slack.sendGlobalNotification(notification, slackOption);
  20. }
  21. fire(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 baseOption = {
  40. template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageCreate.txt`),
  41. vars: {
  42. appTitle: this.appTitle,
  43. path: page.path,
  44. username: page.creator.username,
  45. },
  46. };
  47. const option = {
  48. mail: {
  49. subject: `#pageCreate - ${page.creator.username} created ${page.path}`,
  50. ...baseOption,
  51. },
  52. slack: {
  53. ...baseOption,
  54. },
  55. };
  56. logger.debug('notifyPageCreate', option);
  57. this.fire(notifications, option);
  58. }
  59. /**
  60. * send notification at page edit
  61. * @memberof GlobalNotification
  62. * @param {obejct} page
  63. */
  64. async notifyPageEdit(page) {
  65. const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageEdit');
  66. const lang = 'en-US'; // FIXME
  67. const baseOption = {
  68. template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageEdit.txt`),
  69. vars: {
  70. appTitle: this.appTitle,
  71. path: page.path,
  72. username: page.creator.username,
  73. },
  74. };
  75. const option = {
  76. mail: {
  77. subject: `#pageEdit - ${page.creator.username} edited ${page.path}`,
  78. ...baseOption,
  79. },
  80. slack: {
  81. ...baseOption,
  82. },
  83. };
  84. logger.debug('notifyPageEdit', option);
  85. this.fire(notifications, option);
  86. }
  87. /**
  88. * send notification at page deletion
  89. * @memberof GlobalNotification
  90. * @param {obejct} page
  91. */
  92. async notifyPageDelete(page) {
  93. const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageDelete');
  94. const lang = 'en-US'; // FIXME
  95. const baseOption = {
  96. template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageDelete.txt`),
  97. vars: {
  98. appTitle: this.appTitle,
  99. path: page.path,
  100. username: page.creator.username,
  101. },
  102. };
  103. const option = {
  104. mail: {
  105. subject: `#pageDelete - ${page.creator.username} deleted ${page.path}`, // FIXME
  106. ...baseOption,
  107. },
  108. slack: {
  109. ...baseOption,
  110. },
  111. };
  112. this.fire(notifications, option);
  113. }
  114. /**
  115. * send notification at page move
  116. * @memberof GlobalNotification
  117. * @param {obejct} page
  118. */
  119. async notifyPageMove(page, oldPagePath, user) {
  120. const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageMove');
  121. const lang = 'en-US'; // FIXME
  122. const baseOption = {
  123. template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageMove.txt`),
  124. vars: {
  125. appTitle: this.appTitle,
  126. oldPath: oldPagePath,
  127. newPath: page.path,
  128. username: user.username,
  129. },
  130. };
  131. const option = {
  132. mail: {
  133. subject: `#pageMove - ${user.username} moved ${page.path} to ${page.path}`, // FIXME
  134. ...baseOption,
  135. },
  136. slack: {
  137. ...baseOption,
  138. },
  139. };
  140. this.fire(notifications, option);
  141. }
  142. /**
  143. * send notification at page like
  144. * @memberof GlobalNotification
  145. * @param {obejct} page
  146. */
  147. async notifyPageLike(page, user) {
  148. const notifications = await this.GlobalNotification.findSettingByPathAndEvent(page.path, 'pageLike');
  149. const lang = 'en-US'; // FIXME
  150. const baseOption = {
  151. template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/pageLike.txt`),
  152. vars: {
  153. appTitle: this.appTitle,
  154. path: page.path,
  155. username: page.creator.username,
  156. },
  157. };
  158. const option = {
  159. mail: {
  160. subject: `#pageLike - ${user.username} liked ${page.path}`,
  161. ...baseOption,
  162. },
  163. slack: {
  164. ...baseOption,
  165. },
  166. };
  167. this.fire(notifications, option);
  168. }
  169. /**
  170. * send notification at page comment
  171. * @memberof GlobalNotification
  172. * @param {obejct} page
  173. * @param {obejct} comment
  174. */
  175. async notifyComment(comment, path) {
  176. const notifications = await this.GlobalNotification.findSettingByPathAndEvent(path, 'comment');
  177. const lang = 'en-US'; // FIXME
  178. const user = await this.User.findOne({ _id: comment.creator });
  179. const baseOption = {
  180. template: nodePath.join(this.crowi.localeDir, `${lang}/notifications/comment.txt`),
  181. vars: {
  182. appTitle: this.appTitle,
  183. path,
  184. username: user.username,
  185. comment: comment.comment,
  186. },
  187. };
  188. const option = {
  189. mail: {
  190. subject: `#comment - ${user.username} commented on ${path}`,
  191. ...baseOption,
  192. },
  193. slack: {
  194. ...baseOption,
  195. },
  196. };
  197. this.fire(notifications, option);
  198. }
  199. }
  200. module.exports = GlobalNotificationService;