index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const mongoose = require('mongoose');
  2. const nodePath = require('path');
  3. const { pathUtils } = require('@growi/core');
  4. /**
  5. * parent schema for GlobalNotificationSetting model
  6. */
  7. const globalNotificationSettingSchema = new mongoose.Schema({
  8. isEnabled: { type: Boolean, required: true, default: true },
  9. triggerPath: { type: String, required: true },
  10. triggerEvents: { type: [String] },
  11. });
  12. /*
  13. * e.g. "/a/b/c" => ["/a/b/c", "/a/b", "/a", "/"]
  14. */
  15. const generatePathsOnTree = (path, pathList) => {
  16. pathList.push(path);
  17. if (path === '/') {
  18. return pathList;
  19. }
  20. const newPath = nodePath.posix.dirname(path);
  21. return generatePathsOnTree(newPath, pathList);
  22. };
  23. /*
  24. * e.g. "/a/b/c" => ["/a/b/c", "/a/b", "/a", "/"]
  25. */
  26. const generatePathsToMatch = (originalPath) => {
  27. const pathList = generatePathsOnTree(originalPath, []);
  28. return pathList.map((path) => {
  29. // except for the original trigger path ("/a/b/c"), append "*" to find all matches
  30. // e.g. ["/a/b/c", "/a/b", "/a", "/"] => ["/a/b/c", "/a/b/*", "/a/*", "/*"]
  31. if (path !== originalPath) {
  32. return `${pathUtils.addTrailingSlash(path)}*`;
  33. }
  34. return path;
  35. });
  36. };
  37. /**
  38. * GlobalNotificationSetting Class
  39. * @class GlobalNotificationSetting
  40. */
  41. class GlobalNotificationSetting {
  42. constructor(crowi) {
  43. this.crowi = crowi;
  44. }
  45. /**
  46. * enable notification setting
  47. * @param {string} id
  48. */
  49. static async enable(id) {
  50. const setting = await this.findOne({ _id: id });
  51. setting.isEnabled = true;
  52. setting.save();
  53. return setting;
  54. }
  55. /**
  56. * disable notification setting
  57. * @param {string} id
  58. */
  59. static async disable(id) {
  60. const setting = await this.findOne({ _id: id });
  61. setting.isEnabled = false;
  62. setting.save();
  63. return setting;
  64. }
  65. /**
  66. * find all notification settings
  67. */
  68. static async findAll() {
  69. const settings = await this.find().sort({ triggerPath: 1 });
  70. return settings;
  71. }
  72. /**
  73. * find a list of notification settings by path and a list of events
  74. * @param {string} path
  75. * @param {string} event
  76. */
  77. static async findSettingByPathAndEvent(event, path, type) {
  78. const pathsToMatch = generatePathsToMatch(path);
  79. const settings = await this.find({
  80. triggerPath: { $in: pathsToMatch },
  81. triggerEvents: event,
  82. __t: type,
  83. isEnabled: true,
  84. })
  85. .sort({ triggerPath: 1 });
  86. return settings;
  87. }
  88. }
  89. module.exports = {
  90. class: GlobalNotificationSetting,
  91. schema: globalNotificationSettingSchema,
  92. };