GlobalNotificationMailSetting.js 2.8 KB

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