GlobalNotificationSetting.js 3.3 KB

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