sou 7 лет назад
Родитель
Сommit
b147749ba7

+ 3 - 117
lib/models/GlobalNotificationSetting.js

@@ -1,15 +1,7 @@
 const debug = require('debug')('growi:models:GlobalNotificationSetting');
 const mongoose = require('mongoose');
 const notificationSchema = require('./GlobalNotificationSetting/GlobalNotificationSettingParentSchema');
-
-/**
- * parent schema in this model
- */
-// const notificationSchema = new mongoose.Schema({
-//   isEnabled: { type: Boolean, required: true, default: true },
-//   triggerPath: { type: String, required: true },
-//   triggerEvents: { type: [String] },
-// });
+const GlobalNotificationSettingClass = require('./GlobalNotificationSetting/GlobalNotificationSettingClass');
 
 /**
  * create child schemas inherited from parentSchema
@@ -21,120 +13,14 @@ const notificationSchema = require('./GlobalNotificationSetting/GlobalNotificati
  */
 const createChildSchemas = (parentSchema, modelName, discriminatorKey) => {
   const Notification = mongoose.model(modelName, parentSchema);
-  // const mailNotification = Notification.discriminator('mail', new mongoose.Schema({
-  //   toEmail: String,
-  // }, {discriminatorKey: discriminatorKey}));
-
-  // const slackNotification = Notification.discriminator('slack', new mongoose.Schema({
-  //   slackChannels: String,
-  // }, {discriminatorKey: discriminatorKey}));
 
   return {
     Parent: Notification,
-    // Mail: mailNotification,
-    // Slack: slackNotification,
   };
 };
 
-
-/**
- * GlobalNotificationSetting Class
- * @class GlobalNotificationSetting
- */
-class GlobalNotificationSetting {
-
-  constructor(crowi) {
-    this.crowi = crowi;
-  }
-
-  /**
-   * enable notification setting
-   * @param {string} id
-   */
-  static async enable(id) {
-    const setting = await this.findOne({_id: id});
-
-    setting.isEnabled = true;
-    setting.save();
-
-    return setting;
-  }
-
-  /**
-   * disable notification setting
-   * @param {string} id
-   */
-  static async disable(id) {
-    const setting = await this.findOne({_id: id});
-
-    setting.isEnabled = false;
-    setting.save();
-
-    return setting;
-  }
-
-  /**
-   * find all notification settings
-   */
-  static async findAll() {
-    const settings = await this.find().sort({ triggerPath: 1 });
-
-    return settings;
-  }
-
-  /**
-   * find a list of notification settings by path and a list of events
-   * @param {string} path
-   * @param {string} event
-   */
-  static async findSettingByPathAndEvent(path, event) {
-    const pathsToMatch = generatePathsToMatch(path);
-
-    const settings = await this.find({
-      triggerPath: {$in: pathsToMatch},
-      triggerEvents: event,
-      isEnabled: true
-    })
-    .sort({ triggerPath: 1 });
-
-    return settings;
-  }
-}
-
-
-// move this to util
-// remove this from models/page
-const cutOffLastSlash = path => {
-  const lastSlash = path.lastIndexOf('/');
-  return path.substr(0, lastSlash);
-};
-
-const generatePathsOnTree = (path, pathList) => {
-  pathList.push(path);
-
-  if (path === '') {
-    return pathList;
-  }
-
-  const newPath = cutOffLastSlash(path);
-
-  return generatePathsOnTree(newPath, pathList);
-};
-
-const generatePathsToMatch = (originalPath) => {
-  const pathList = generatePathsOnTree(originalPath, []);
-  return pathList.map(path => {
-    if (path !== originalPath) {
-      return path + '/*';
-    }
-    else {
-      return path;
-    }
-  });
-};
-
 module.exports = function(crowi) {
-  GlobalNotificationSetting.crowi = crowi;
-  notificationSchema.loadClass(GlobalNotificationSetting);
+  GlobalNotificationSettingClass.crowi = crowi;
+  notificationSchema.loadClass(GlobalNotificationSettingClass);
   return createChildSchemas(notificationSchema, 'GlobalNotificationSetting', 'type');
 };

+ 3 - 99
lib/models/GlobalNotificationSetting/GlobalNotificationMailSetting.js

@@ -1,6 +1,6 @@
-const debug = require('debug')('growi:models:GlobalNotificationSetting');
 const mongoose = require('mongoose');
 const notificationSchema = require('./GlobalNotificationSettingParentSchema');
+const GlobalNotificationSettingClass = require('./GlobalNotificationSettingClass');
 
 /**
  * create child schemas inherited from parentSchema
@@ -19,104 +19,8 @@ const createChildSchemas = (parentSchema, modelName, discriminatorKey) => {
   return mailNotification;
 };
 
-/**
- * GlobalNotificationSetting Class
- * @class GlobalNotificationSetting
- */
-class GlobalNotificationSetting {
-
-  constructor(crowi) {
-    this.crowi = crowi;
-  }
-
-  /**
-   * enable notification setting
-   * @param {string} id
-   */
-  static async enable(id) {
-    const setting = await this.findOne({_id: id});
-
-    setting.isEnabled = true;
-    setting.save();
-
-    return setting;
-  }
-
-  /**
-   * disable notification setting
-   * @param {string} id
-   */
-  static async disable(id) {
-    const setting = await this.findOne({_id: id});
-
-    setting.isEnabled = false;
-    setting.save();
-
-    return setting;
-  }
-
-  /**
-   * find all notification settings
-   */
-  static async findAll() {
-    const settings = await this.find().sort({ triggerPath: 1 });
-
-    return settings;
-  }
-
-  /**
-   * find a list of notification settings by path and a list of events
-   * @param {string} path
-   * @param {string} event
-   */
-  static async findSettingByPathAndEvent(path, event) {
-    const pathsToMatch = generatePathsToMatch(path);
-
-    const settings = await this.find({
-      triggerPath: {$in: pathsToMatch},
-      triggerEvents: event,
-      isEnabled: true
-    })
-    .sort({ triggerPath: 1 });
-
-    return settings;
-  }
-}
-
-
-// move this to util
-// remove this from models/page
-const cutOffLastSlash = path => {
-  const lastSlash = path.lastIndexOf('/');
-  return path.substr(0, lastSlash);
-};
-
-const generatePathsOnTree = (path, pathList) => {
-  pathList.push(path);
-
-  if (path === '') {
-    return pathList;
-  }
-
-  const newPath = cutOffLastSlash(path);
-
-  return generatePathsOnTree(newPath, pathList);
-};
-
-const generatePathsToMatch = (originalPath) => {
-  const pathList = generatePathsOnTree(originalPath, []);
-  return pathList.map(path => {
-    if (path !== originalPath) {
-      return path + '/*';
-    }
-    else {
-      return path;
-    }
-  });
-};
-
 module.exports = function(crowi) {
-  GlobalNotificationSetting.crowi = crowi;
-  notificationSchema.loadClass(GlobalNotificationSetting);
+  GlobalNotificationSettingClass.crowi = crowi;
+  notificationSchema.loadClass(GlobalNotificationSettingClass);
   return createChildSchemas(notificationSchema, 'GlobalNotificationSetting', 'type');
 };

+ 97 - 0
lib/models/GlobalNotificationSetting/GlobalNotificationSettingClass.js

@@ -0,0 +1,97 @@
+/**
+ * GlobalNotificationSetting Class
+ * @class GlobalNotificationSetting
+ */
+class GlobalNotificationSetting {
+
+  constructor(crowi) {
+    this.crowi = crowi;
+  }
+
+  /**
+   * enable notification setting
+   * @param {string} id
+   */
+  static async enable(id) {
+    const setting = await this.findOne({_id: id});
+
+    setting.isEnabled = true;
+    setting.save();
+
+    return setting;
+  }
+
+  /**
+   * disable notification setting
+   * @param {string} id
+   */
+  static async disable(id) {
+    const setting = await this.findOne({_id: id});
+
+    setting.isEnabled = false;
+    setting.save();
+
+    return setting;
+  }
+
+  /**
+   * find all notification settings
+   */
+  static async findAll() {
+    const settings = await this.find().sort({ triggerPath: 1 });
+
+    return settings;
+  }
+
+  /**
+   * find a list of notification settings by path and a list of events
+   * @param {string} path
+   * @param {string} event
+   */
+  static async findSettingByPathAndEvent(path, event) {
+    const pathsToMatch = generatePathsToMatch(path);
+
+    const settings = await this.find({
+      triggerPath: {$in: pathsToMatch},
+      triggerEvents: event,
+      isEnabled: true
+    })
+    .sort({ triggerPath: 1 });
+
+    return settings;
+  }
+}
+
+
+// move this to util
+// remove this from models/page
+const cutOffLastSlash = path => {
+  const lastSlash = path.lastIndexOf('/');
+  return path.substr(0, lastSlash);
+};
+
+const generatePathsOnTree = (path, pathList) => {
+  pathList.push(path);
+
+  if (path === '') {
+    return pathList;
+  }
+
+  const newPath = cutOffLastSlash(path);
+
+  return generatePathsOnTree(newPath, pathList);
+};
+
+const generatePathsToMatch = (originalPath) => {
+  const pathList = generatePathsOnTree(originalPath, []);
+  return pathList.map(path => {
+    if (path !== originalPath) {
+      return path + '/*';
+    }
+    else {
+      return path;
+    }
+  });
+};
+
+module.exports = GlobalNotificationSetting;

+ 2 - 2
lib/models/GlobalNotificationSetting/GlobalNotificationSettingParentSchema.js

@@ -3,10 +3,10 @@ const mongoose = require('mongoose');
 /**
  * parent schema for GlobalNotificationSetting model
  */
-const notificationSchema = new mongoose.Schema({
+const globalNotificationSettingSchema = new mongoose.Schema({
   isEnabled: { type: Boolean, required: true, default: true },
   triggerPath: { type: String, required: true },
   triggerEvents: { type: [String] },
 });
 
-module.exports = notificationSchema;
+module.exports = globalNotificationSettingSchema;

+ 3 - 100
lib/models/GlobalNotificationSetting/GlobalNotificationSlackSetting.js

@@ -1,6 +1,6 @@
-const debug = require('debug')('growi:models:GlobalNotificationSetting');
 const mongoose = require('mongoose');
 const notificationSchema = require('./GlobalNotificationSettingParentSchema');
+const GlobalNotificationSettingClass = require('./GlobalNotificationSettingClass');
 
 /**
  * create child schemas inherited from parentSchema
@@ -19,105 +19,8 @@ const createChildSchemas = (parentSchema, modelName, discriminatorKey) => {
   return slackNotification;
 };
 
-
-/**
- * GlobalNotificationSetting Class
- * @class GlobalNotificationSetting
- */
-class GlobalNotificationSetting {
-
-  constructor(crowi) {
-    this.crowi = crowi;
-  }
-
-  /**
-   * enable notification setting
-   * @param {string} id
-   */
-  static async enable(id) {
-    const setting = await this.findOne({_id: id});
-
-    setting.isEnabled = true;
-    setting.save();
-
-    return setting;
-  }
-
-  /**
-   * disable notification setting
-   * @param {string} id
-   */
-  static async disable(id) {
-    const setting = await this.findOne({_id: id});
-
-    setting.isEnabled = false;
-    setting.save();
-
-    return setting;
-  }
-
-  /**
-   * find all notification settings
-   */
-  static async findAll() {
-    const settings = await this.find().sort({ triggerPath: 1 });
-
-    return settings;
-  }
-
-  /**
-   * find a list of notification settings by path and a list of events
-   * @param {string} path
-   * @param {string} event
-   */
-  static async findSettingByPathAndEvent(path, event) {
-    const pathsToMatch = generatePathsToMatch(path);
-
-    const settings = await this.find({
-      triggerPath: {$in: pathsToMatch},
-      triggerEvents: event,
-      isEnabled: true
-    })
-    .sort({ triggerPath: 1 });
-
-    return settings;
-  }
-}
-
-
-// move this to util
-// remove this from models/page
-const cutOffLastSlash = path => {
-  const lastSlash = path.lastIndexOf('/');
-  return path.substr(0, lastSlash);
-};
-
-const generatePathsOnTree = (path, pathList) => {
-  pathList.push(path);
-
-  if (path === '') {
-    return pathList;
-  }
-
-  const newPath = cutOffLastSlash(path);
-
-  return generatePathsOnTree(newPath, pathList);
-};
-
-const generatePathsToMatch = (originalPath) => {
-  const pathList = generatePathsOnTree(originalPath, []);
-  return pathList.map(path => {
-    if (path !== originalPath) {
-      return path + '/*';
-    }
-    else {
-      return path;
-    }
-  });
-};
-
 module.exports = function(crowi) {
-  GlobalNotificationSetting.crowi = crowi;
-  notificationSchema.loadClass(GlobalNotificationSetting);
+  GlobalNotificationSettingClass.crowi = crowi;
+  notificationSchema.loadClass(GlobalNotificationSettingClass);
   return createChildSchemas(notificationSchema, 'GlobalNotificationSetting', 'type');
 };