GlobalNotificationSetting.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. const debug = require('debug')('growi:models:GlobalNotificationSetting');
  2. const mongoose = require('mongoose');
  3. /**
  4. * parent schema in this model
  5. */
  6. const notificationSchema = new mongoose.Schema({
  7. isEnabled: { type: Boolean, required: true, default: true },
  8. triggerPath: { type: String, required: true },
  9. triggerEvents: { type: [String] },
  10. });
  11. /**
  12. * create child schemas inherited from parentSchema
  13. * all child schemas are stored in globalnotificationsettings collection
  14. * @link{http://url.com module_name}
  15. * @param {object} parentSchema
  16. * @param {string} modelName
  17. * @param {string} discriminatorKey
  18. */
  19. const createChildSchemas = (parentSchema, modelName, discriminatorKey) => {
  20. const Notification = mongoose.model(modelName, parentSchema);
  21. const mailNotification = Notification.discriminator('mail', new mongoose.Schema({
  22. toEmail: String,
  23. }, {discriminatorKey: discriminatorKey}));
  24. const slackNotification = Notification.discriminator('slack', new mongoose.Schema({
  25. slackChannels: String,
  26. }, {discriminatorKey: discriminatorKey}));
  27. return {
  28. Parent: Notification,
  29. Mail: mailNotification,
  30. Slack: slackNotification,
  31. };
  32. };
  33. /**
  34. * GlobalNotificationSetting Class
  35. * @class GlobalNotificationSetting
  36. */
  37. class GlobalNotificationSetting {
  38. constructor(crowi) {
  39. this.crowi = crowi;
  40. }
  41. /**
  42. * enable notification setting
  43. * @param {string} id
  44. */
  45. static async enable(id) {
  46. const setting = await this.findOne({_id: id});
  47. setting.isEnabled = true;
  48. setting.save();
  49. return setting;
  50. }
  51. /**
  52. * disable notification setting
  53. * @param {string} id
  54. */
  55. static async disable(id) {
  56. const setting = await this.findOne({_id: id});
  57. setting.isEnabled = false;
  58. setting.save();
  59. return setting;
  60. }
  61. /**
  62. * find all notification settings
  63. */
  64. static async findAll() {
  65. const settings = await this.find().sort({ triggerPath: 1 });
  66. return settings;
  67. }
  68. /**
  69. * find a list of notification settings by path and a list of events
  70. * @param {string} path
  71. * @param {string} event
  72. */
  73. static async findSettingByPathAndEvent(path, event) {
  74. const pathsToMatch = generatePathsToMatch(path);
  75. const settings = await this.find({
  76. triggerPath: {$in: pathsToMatch},
  77. triggerEvents: event,
  78. isEnabled: true
  79. })
  80. .sort({ triggerPath: 1 });
  81. return settings;
  82. }
  83. }
  84. // move this to util
  85. // remove this from models/page
  86. const cutOffLastSlash = path => {
  87. const lastSlash = path.lastIndexOf('/');
  88. return path.substr(0, lastSlash);
  89. };
  90. const generatePathsOnTree = (path, pathList) => {
  91. pathList.push(path);
  92. if (path === '') {
  93. return pathList;
  94. }
  95. const newPath = cutOffLastSlash(path);
  96. return generatePathsOnTree(newPath, pathList);
  97. };
  98. const generatePathsToMatch = (originalPath) => {
  99. const pathList = generatePathsOnTree(originalPath, []);
  100. return pathList.map(path => {
  101. if (path !== originalPath) {
  102. return path + '/*';
  103. }
  104. else {
  105. return path;
  106. }
  107. });
  108. };
  109. module.exports = function(crowi) {
  110. GlobalNotificationSetting.crowi = crowi;
  111. notificationSchema.loadClass(GlobalNotificationSetting);
  112. return createChildSchemas(notificationSchema, 'GlobalNotificationSetting', 'type');
  113. };