Skip to content

Node.js based forum software built for the modern web

tested filter-type hooks, working! :)

Julian Lam25a1d2510898
diff --git a/src/plugins.js b/src/plugins.js
index 4b9d1aebff..ae7274c2fb 100644
--- a/src/plugins.js
+++ b/src/plugins.js
@@ -71,24 +71,24 @@ var fs = require('fs'),
fire_hook: function(hook, args, callback) {
// TODO: Implement priority hook firing
var _self = this
- hookList = loaded_hooks[hook];
+ hookList = this.loaded_hooks[hook];
- if(loaded_hooks[hook] && Array.isArray(hookList)) {
- if (global.env === 'development') console.log('Info: [plugins] Firing hook: ' + hook);
+ if(hookList && Array.isArray(hookList)) {
+ if (global.env === 'development') console.log('Info: [plugins] Firing hook: \'' + hook + '\'');
var hookType = hook.split(':')[0];
switch(hookType) {
case 'filter':
// Filters only take one argument, so only args[0] will be passed in
- var returnVal = args[0];
+ var returnVal = (Array.isArray(args) ? args[0] : args);
async.each(hookList, function(hookObj, next) {
if (hookObj.callbacked) {
- _self.libraries[hookObj.id][hookObj.method](returnVal, function(err, afterVal) {
+ _self.libraries[hookObj[0]][hookObj[1]](returnVal, function(err, afterVal) {
returnVal = afterVal;
next(err);
});
} else {
- returnVal = _self.libraries[hookObj.id][hookObj.method](returnVal);
+ returnVal = _self.libraries[hookObj[0]][hookObj[1]](returnVal);
next();
}
}, function(err) {
diff --git a/src/posts.js b/src/posts.js
index c0f22cba9d..80d745178e 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -7,7 +7,8 @@ var RDB = require('./redis.js'),
threadTools = require('./threadTools.js'),
postTools = require('./postTools'),
feed = require('./feed.js'),
- async = require('async');
+ async = require('async'),
+ plugins = require('./plugins');
(function(Posts) {
@@ -275,91 +276,91 @@ var RDB = require('./redis.js'),
}
topics.isLocked(tid, function(locked) {
-
if (!locked || locked === '0') {
RDB.incr('global:next_post_id', function(err, pid) {
RDB.handle(err);
-
- var timestamp = Date.now();
-
- var postData = {
- 'pid': pid,
- 'uid': uid,
- 'tid': tid,
- 'content': content,
- 'timestamp': timestamp,
- 'reputation': 0,
- 'editor': '',
- 'edited': 0,
- 'deleted': 0,
- 'uploadedImages': ''
- };
-
- RDB.hmset('post:' + pid, postData);
-
- topics.increasePostCount(tid);
- topics.updateTimestamp(tid, timestamp);
- RDB.incr('totalpostcount');
+ plugins.fire_hook('filter:save_post_content', content, function(content) {
+ var timestamp = Date.now(),
+ postData = {
+ 'pid': pid,
+ 'uid': uid,
+ 'tid': tid,
+ 'content': content,
+ 'timestamp': timestamp,
+ 'reputation': 0,
+ 'editor': '',
+ 'edited': 0,
+ 'deleted': 0,
+ 'uploadedImages': ''
+ };
- topics.getTopicField(tid, 'cid', function(cid) {
- RDB.handle(err);
+ RDB.hmset('post:' + pid, postData);
- feed.updateTopic(tid, cid);
+ topics.increasePostCount(tid);
+ topics.updateTimestamp(tid, timestamp);
- RDB.zadd('categories:recent_posts:cid:' + cid, Date.now(), pid);
+ RDB.incr('totalpostcount');
+
+ topics.getTopicField(tid, 'cid', function(cid) {
+ RDB.handle(err);
- // this is a bit of a naive implementation, defn something to look at post-MVP
- RDB.scard('cid:' + cid + ':active_users', function(amount) {
- if (amount > 10) {
- RDB.spop('cid:' + cid + ':active_users');
- }
+ feed.updateTopic(tid, cid);
- RDB.sadd('cid:' + cid + ':active_users', uid);
- });
- });
-
- user.onNewPostMade(uid, tid, pid, timestamp);
+ RDB.zadd('categories:recent_posts:cid:' + cid, Date.now(), pid);
- var imgur = require('./imgur');
- // move clientID to config
- imgur.setClientID('09f3955fee9a0a6');
-
- var uploadedImages = [];
-
- function uploadImage(image, callback) {
- imgur.upload(image.data, 'base64', function(err, data) {
- if(err) {
- callback(err);
- } else {
- if(data.success) {
- var img= {url:data.data.link, name:image.name};
- uploadedImages.push(img);
- callback(null);
+ // this is a bit of a naive implementation, defn something to look at post-MVP
+ RDB.scard('cid:' + cid + ':active_users', function(amount) {
+ if (amount > 10) {
+ RDB.spop('cid:' + cid + ':active_users');
+ }
+
+ RDB.sadd('cid:' + cid + ':active_users', uid);
+ });
+ });
+
+ user.onNewPostMade(uid, tid, pid, timestamp);
+
+ var imgur = require('./imgur');
+ // move clientID to config
+ imgur.setClientID('09f3955fee9a0a6');
+
+ var uploadedImages = [];
+
+ function uploadImage(image, callback) {
+ imgur.upload(image.data, 'base64', function(err, data) {
+ if(err) {
+ callback(err);
} else {
- callback(data);
+ if(data.success) {
+ var img= {url:data.data.link, name:image.name};
+ uploadedImages.push(img);
+ callback(null);
+ } else {
+ callback(data);
+ }
}
- }
- });
- }
-
- if(!images) {
- postData.uploadedImages = JSON.stringify(uploadedImages);
- Posts.setPostField(pid, 'uploadedImages', postData.uploadedImages);
- callback(postData);
- } else {
- async.each(images, uploadImage, function(err) {
- if(!err) {
- postData.uploadedImages = JSON.stringify(uploadedImages);
- Posts.setPostField(pid, 'uploadedImages', postData.uploadedImages);
-
- callback(postData);
- } else {
- console.log(err);
- callback(null);
- }
- });
- }
+ });
+ }
+
+ if(!images) {
+ postData.uploadedImages = JSON.stringify(uploadedImages);
+ Posts.setPostField(pid, 'uploadedImages', postData.uploadedImages);
+ callback(postData);
+ } else {
+ async.each(images, uploadImage, function(err) {
+ if(!err) {
+ postData.uploadedImages = JSON.stringify(uploadedImages);
+ Posts.setPostField(pid, 'uploadedImages', postData.uploadedImages);
+
+ callback(postData);
+ } else {
+ console.log(err);
+ callback(null);
+ }
+ });
+ }
+ });
});
} else {
callback(null);