GlobalNotificationSetting.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. * disable notification setting
  43. * @param {string} id
  44. */
  45. static async toggleIsEnabled(id) {
  46. const setting = await this.findOne({_id: id});
  47. setting.isEnabled = !setting.isEnabled;
  48. setting.save();
  49. return setting;
  50. }
  51. /**
  52. * find all notification settings
  53. */
  54. static async findAll() {
  55. const settings = await this.find().sort({ triggerPath: 1 });
  56. //sort
  57. return settings;
  58. }
  59. /**
  60. * find a list of notification settings by path and a list of events
  61. * @param {string} path
  62. * @param {string} event
  63. */
  64. static async findSettingByPathAndEvent(path, event) {
  65. const pathsToMatch = generatePathsToMatch(path);
  66. const settings = await this.find({
  67. triggerPath: {$in: pathsToMatch},
  68. triggerEvents: event,
  69. isEnabled: true
  70. })
  71. .sort({ triggerPath: 1 });
  72. return settings;
  73. }
  74. }
  75. // move this to util
  76. // remove this from models/page
  77. const cutOffLastSlash = path => {
  78. const lastSlash = path.lastIndexOf('/');
  79. return path.substr(0, lastSlash);
  80. };
  81. const generatePathsOnTree = (path, pathList) => {
  82. pathList.push(path);
  83. if (path === '') {
  84. return pathList;
  85. }
  86. const newPath = cutOffLastSlash(path);
  87. return generatePathsOnTree(newPath, pathList);
  88. };
  89. const generatePathsToMatch = (originalPath) => {
  90. const pathList = generatePathsOnTree(originalPath, []);
  91. return pathList.map(path => {
  92. if (path !== originalPath) {
  93. return path + '/*';
  94. }
  95. else {
  96. return path;
  97. }
  98. });
  99. };
  100. module.exports = function(crowi) {
  101. GlobalNotificationSetting.crowi = crowi;
  102. notificationSchema.loadClass(GlobalNotificationSetting);
  103. return createChildSchemas(notificationSchema, 'GlobalNotificationSetting', 'type');
  104. };