global-notification.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /**
  6. * the service class of GlobalNotificationSetting
  7. */
  8. class GlobalNotification {
  9. constructor(crowi) {
  10. this.crowi = crowi;
  11. this.config = crowi.getConfig();
  12. }
  13. notifyByMail(notification, mailOption) {
  14. mailer.send(Object.assign(mailOption, {to: notification.toEmail}));
  15. }
  16. notifyBySlack(notification, slackOption) {
  17. // send slack notification here
  18. }
  19. sendNotification(notifications, option) {
  20. notifications.forEach(notification => {
  21. if (notification.__t === 'mail') {
  22. this.notifyByMail(notification, option.mail);
  23. }
  24. else if (notification.__t === 'slack') {
  25. this.notifyBySlack(notification, option.slack);
  26. }
  27. });
  28. }
  29. /**
  30. * send notification at page creation
  31. * @memberof GlobalNotification
  32. * @param {obejct} page
  33. */
  34. notifyPageCreate(page) {
  35. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageCreate');
  36. const lang = 'en-US'; //FIXME
  37. const option = {
  38. mail: {
  39. subject: `#pageCreate - ${page.creator.username} created ${page.path}`,
  40. template: path.join(this.crowi.localeDir, lang, 'notifications/pageCreate.txt'),
  41. vars: {}
  42. },
  43. slack: {},
  44. };
  45. this.sendNotification(notifications, option);
  46. }
  47. /**
  48. * send notification at page edit
  49. * @memberof GlobalNotification
  50. * @param {obejct} page
  51. */
  52. notifyPageEdit(page) {
  53. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageEdit');
  54. const lang = 'en-US'; //FIXME
  55. const option = {
  56. mail: {
  57. subject: `#pageEdit - ${page.creator.username} edited ${page.path}`,
  58. template: path.join(this.crowi.localeDir, lang, 'notifications/pageEdit.txt'),
  59. vars: {}
  60. },
  61. slack: {},
  62. };
  63. this.sendNotification(notifications, option);
  64. }
  65. /**
  66. * send notification at page deletion
  67. * @memberof GlobalNotification
  68. * @param {obejct} page
  69. */
  70. notifyPageDelete(page) {
  71. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageDelete');
  72. const lang = 'en-US'; //FIXME
  73. const option = {
  74. mail: {
  75. subject: `#pageDelete - ${page.creator.username} deleted ${page.path}`, //FIXME
  76. template: path.join(this.crowi.localeDir, lang, 'notifications/pageDelete.txt'),
  77. vars: {}
  78. },
  79. slack: {},
  80. };
  81. this.sendNotification(notifications, option);
  82. }
  83. /**
  84. * send notification at page move
  85. * @memberof GlobalNotification
  86. * @param {obejct} page
  87. */
  88. notifyPageMove(page) {
  89. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageMove');
  90. const lang = 'en-US'; //FIXME
  91. const option = {
  92. mail: {
  93. subject: `#pageMove - ${page.creator.username} moved ${page.path} to ${page.path}`, //FIXME
  94. template: path.join(this.crowi.localeDir, lang, 'notifications/pageMove.txt'),
  95. vars: {}
  96. },
  97. slack: {},
  98. };
  99. this.sendNotification(notifications, option);
  100. }
  101. /**
  102. * send notification at page like
  103. * @memberof GlobalNotification
  104. * @param {obejct} page
  105. */
  106. notifyPageLike(page) {
  107. const notifications = Notification.findSettingByPathAndEvent(page.path, 'pageLike');
  108. const lang = 'en-US'; //FIXME
  109. const option = {
  110. mail: {
  111. subject: `#pageLike - ${page.creator.username} liked ${page.path}`,
  112. template: path.join(this.crowi.localeDir, lang, 'notifications/pageLike.txt'),
  113. vars: {}
  114. },
  115. slack: {},
  116. };
  117. this.sendNotification(notifications, option);
  118. }
  119. /**
  120. * send notification at page comment
  121. * @memberof GlobalNotification
  122. * @param {obejct} page
  123. * @param {obejct} comment
  124. */
  125. notifyComment(comment, path) {
  126. const notifications = Notification.findSettingByPathAndEvent(path, 'comment');
  127. const lang = 'en-US'; //FIXME
  128. const option = {
  129. mail: {
  130. subject: `#comment - ${comment.creator.username} commented on ${path}`,
  131. template: path.join(this.crowi.localeDir, lang, 'notifications/comment.txt'),
  132. vars: {}
  133. },
  134. slack: {},
  135. };
  136. this.sendNotification(notifications, option);
  137. }
  138. }
  139. module.exports = GlobalNotification;