GlobalNotificationSetting.js 3.0 KB

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