tested filter-type hooks, working! :)

v1.18.x
Julian Lam 12 years ago
parent ec4ab19a1e
commit 25a1d25108

@ -71,24 +71,24 @@ var fs = require('fs'),
fire_hook: function(hook, args, callback) { fire_hook: function(hook, args, callback) {
// TODO: Implement priority hook firing // TODO: Implement priority hook firing
var _self = this var _self = this
hookList = loaded_hooks[hook]; hookList = this.loaded_hooks[hook];
if(loaded_hooks[hook] && Array.isArray(hookList)) { if(hookList && Array.isArray(hookList)) {
if (global.env === 'development') console.log('Info: [plugins] Firing hook: ' + hook); if (global.env === 'development') console.log('Info: [plugins] Firing hook: \'' + hook + '\'');
var hookType = hook.split(':')[0]; var hookType = hook.split(':')[0];
switch(hookType) { switch(hookType) {
case 'filter': case 'filter':
// Filters only take one argument, so only args[0] will be passed in // 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) { async.each(hookList, function(hookObj, next) {
if (hookObj.callbacked) { 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; returnVal = afterVal;
next(err); next(err);
}); });
} else { } else {
returnVal = _self.libraries[hookObj.id][hookObj.method](returnVal); returnVal = _self.libraries[hookObj[0]][hookObj[1]](returnVal);
next(); next();
} }
}, function(err) { }, function(err) {

@ -7,7 +7,8 @@ var RDB = require('./redis.js'),
threadTools = require('./threadTools.js'), threadTools = require('./threadTools.js'),
postTools = require('./postTools'), postTools = require('./postTools'),
feed = require('./feed.js'), feed = require('./feed.js'),
async = require('async'); async = require('async'),
plugins = require('./plugins');
(function(Posts) { (function(Posts) {
@ -275,91 +276,91 @@ var RDB = require('./redis.js'),
} }
topics.isLocked(tid, function(locked) { topics.isLocked(tid, function(locked) {
if (!locked || locked === '0') { if (!locked || locked === '0') {
RDB.incr('global:next_post_id', function(err, pid) { RDB.incr('global:next_post_id', function(err, pid) {
RDB.handle(err); 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.hmset('post:' + pid, postData);
RDB.handle(err);
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 feed.updateTopic(tid, cid);
RDB.scard('cid:' + cid + ':active_users', function(amount) {
if (amount > 10) {
RDB.spop('cid:' + cid + ':active_users');
}
RDB.sadd('cid:' + cid + ':active_users', uid); RDB.zadd('categories:recent_posts:cid:' + cid, Date.now(), pid);
});
});
user.onNewPostMade(uid, tid, pid, timestamp);
var imgur = require('./imgur'); // this is a bit of a naive implementation, defn something to look at post-MVP
// move clientID to config RDB.scard('cid:' + cid + ':active_users', function(amount) {
imgur.setClientID('09f3955fee9a0a6'); if (amount > 10) {
RDB.spop('cid:' + cid + ':active_users');
var uploadedImages = []; }
function uploadImage(image, callback) { RDB.sadd('cid:' + cid + ':active_users', uid);
imgur.upload(image.data, 'base64', function(err, data) { });
if(err) { });
callback(err);
} else { user.onNewPostMade(uid, tid, pid, timestamp);
if(data.success) {
var img= {url:data.data.link, name:image.name}; var imgur = require('./imgur');
uploadedImages.push(img); // move clientID to config
callback(null); imgur.setClientID('09f3955fee9a0a6');
var uploadedImages = [];
function uploadImage(image, callback) {
imgur.upload(image.data, 'base64', function(err, data) {
if(err) {
callback(err);
} else { } 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) {
if(!images) { postData.uploadedImages = JSON.stringify(uploadedImages);
postData.uploadedImages = JSON.stringify(uploadedImages); Posts.setPostField(pid, 'uploadedImages', postData.uploadedImages);
Posts.setPostField(pid, 'uploadedImages', postData.uploadedImages); callback(postData);
callback(postData); } else {
} else { async.each(images, uploadImage, function(err) {
async.each(images, uploadImage, function(err) { if(!err) {
if(!err) { postData.uploadedImages = JSON.stringify(uploadedImages);
postData.uploadedImages = JSON.stringify(uploadedImages); Posts.setPostField(pid, 'uploadedImages', postData.uploadedImages);
Posts.setPostField(pid, 'uploadedImages', postData.uploadedImages);
callback(postData);
callback(postData); } else {
} else { console.log(err);
console.log(err); callback(null);
callback(null); }
} });
}); }
} });
}); });
} else { } else {
callback(null); callback(null);

Loading…
Cancel
Save