Procházet zdrojové kódy

impl InterceptorManager

Yuki Takei před 9 roky
rodič
revize
87955b7cb5

+ 35 - 3
lib/crowi/index.js

@@ -42,6 +42,7 @@ function Crowi (rootdir, env)
   this.config = {};
   this.searcher = null;
   this.mailer = {};
+  this.interceptorManager = {};
 
   this.tokens = null;
 
@@ -89,6 +90,8 @@ Crowi.prototype.init = function() {
     return self.setupSearcher();
   }).then(function() {
     return self.setupMailer();
+  }).then(function() {
+    return self.setupInterceptorManager();
   }).then(function() {
     return self.setupSlack();
   }).then(function() {
@@ -241,6 +244,10 @@ Crowi.prototype.getMailer = function() {
   return this.mailer;
 };
 
+Crowi.prototype.getInterceptorManager = function() {
+  return this.interceptorManager;
+}
+
 Crowi.prototype.setupSearcher = function() {
   var self = this;
   var searcherUri = this.env.ELASTICSEARCH_URI
@@ -269,6 +276,31 @@ Crowi.prototype.setupMailer = function() {
   });
 };
 
+Crowi.prototype.setupInterceptorManager = function() {
+  var self = this;
+  return new Promise(function(resolve, reject) {
+    self.interceptorManager = require('../util/interceptorManager')(self);
+
+    self.interceptorManager.addInterceptor({
+      isInterceptWhen: (contextName) => {
+        // implement this
+        return true;
+      },
+      isProcessableParallel: () => {
+        // implement this
+        return true;
+      },
+      process: (contextName, ...args) => {
+        console.log(args[0][0].page);
+        args[0][0].page.revision.body += 'hogehogefugafuga';
+        Promise.resolve(args[0]);
+      }
+    });
+
+    resolve();
+  });
+};
+
 Crowi.prototype.setupSlack = function() {
   var self = this;
   var config = this.getConfig();
@@ -334,7 +366,7 @@ Crowi.prototype.buildServer = function() {
   }
 
   require('../routes')(this, app);
-  
+
   if (env == 'development') {
     //swig.setDefaults({ cache: false });
     app.use(errorHandler({ dumpExceptions: true, showStack: true }));
@@ -355,10 +387,10 @@ Crowi.prototype.buildServer = function() {
 
 /**
  * require API for plugins
- * 
+ *
  * @param {string} modulePath
  * @return {module}
- * 
+ *
  * @memberof Crowi
  */
 Crowi.prototype.require = function(modulePath) {

+ 3 - 1
lib/routes/page.js

@@ -7,6 +7,7 @@ module.exports = function(crowi, app) {
     , Revision = crowi.model('Revision')
     , Bookmark = crowi.model('Bookmark')
     , ApiResponse = require('../util/apiResponse')
+    , interceptorManager = crowi.getInterceptorManager()
 
     , sprintf = require('sprintf')
 
@@ -271,12 +272,13 @@ module.exports = function(crowi, app) {
       } else {
         return Promise.resolve();
       }
+    }).then(function() {
+      return interceptorManager.process('beforeRenderPage', renderVars);
     }).then(function() {
       var defaultPageTeamplate = 'page';
       if (userData) {
         defaultPageTeamplate = 'user_page';
       }
-
       res.render(req.query.presentation ? 'page_presentation' : defaultPageTeamplate, renderVars);
     }).catch(function(err) {
       debug('Error: renderPage()', err);

+ 23 - 0
lib/util/basicInterceptor.js

@@ -0,0 +1,23 @@
+/**
+ * Basic Interceptor class
+ */
+class BasicInterceptor {
+
+  isInterceptWhen(contextName) {
+    // implement this
+    return false;
+  }
+
+  isProcessableParallel() {
+    // implement this
+    return true;
+  }
+
+  process(contextName, ...args) {
+    // override this
+    return Promise.resolve();
+  }
+
+}
+
+module.exports = BasicInterceptor;

+ 51 - 0
lib/util/interceptorManager.js

@@ -0,0 +1,51 @@
+var debug = require('debug')('crowi:InterceptorManager')
+
+/**
+ * the manager class of Interceptor
+ */
+class InterceptorManager {
+
+  constructor(crowi) {
+    this.interceptors = [];
+  }
+
+  addInterceptor(interceptor) {
+    this.addInterceptors([interceptor]);
+  }
+
+  addInterceptors(interceptors) {
+    this.interceptors = this.interceptors.concat(interceptors);
+  }
+
+  process(contextName, ...args) {
+    // filter only context matches
+    const matchInterceptors = this.interceptors.filter((i) => i.isInterceptWhen(contextName));
+
+    const parallels = matchInterceptors.filter((i) => i.isProcessableParallel());
+    const sequentials = matchInterceptors.filter((i) => !i.isProcessableParallel());
+
+    debug(`processing parallels(${parallels.length})`);
+    debug(`processing sequentials(${sequentials.length})`);
+
+    return Promise.all(
+      parallels.map((interceptor) => {
+        return interceptor.process(contextName, args);
+      })
+      // .concat[
+      //   sequentials.map((interceptor) => (results) => {
+      //     interceptor.process(contextName, results)
+      //   }).reduce((promise, func) => {
+      //     return promise.then((results) => func(results));
+      //   }, Promise.resolve(args)).then(() => { return Promise.resolve() })
+      // ]
+    ).then(() => {
+      console.log("Promise.all().then()");
+      return;
+    });
+  }
+
+}
+
+module.exports = (crowi) => {
+  return new InterceptorManager(crowi);
+}