Merge branch 'master' of github.com:NodeBB/NodeBB

v1.18.x
Julian Lam 11 years ago
commit c7e731f4c4

@ -42,7 +42,7 @@
"nodebb-plugin-spam-be-gone": "^0.3.0", "nodebb-plugin-spam-be-gone": "^0.3.0",
"nodebb-theme-lavender": "~0.1.0", "nodebb-theme-lavender": "~0.1.0",
"nodebb-theme-vanilla": "~0.1.0", "nodebb-theme-vanilla": "~0.1.0",
"nodebb-widget-essentials": "~0.1.1", "nodebb-widget-essentials": "~0.2.0",
"npm": "^2.1.4", "npm": "^2.1.4",
"passport": "^0.2.1", "passport": "^0.2.1",
"passport-local": "1.0.0", "passport-local": "1.0.0",

@ -146,7 +146,7 @@ define('forum/register', function() {
}); });
username.on('keyup', function() { username.on('keyup', function() {
$('#yourUsername').html(this.value.length > 0 ? this.value : 'username'); $('#yourUsername').text(this.value.length > 0 ? this.value : 'username');
}); });
username.on('blur', function() { username.on('blur', function() {

@ -1,68 +1,26 @@
'use strict'; 'use strict';
var db = require('./database'), var async = require('async'),
posts = require('./posts'), nconf = require('nconf'),
utils = require('./../public/src/utils'),
db = require('./database'),
user = require('./user'), user = require('./user'),
Groups = require('./groups'), Groups = require('./groups'),
topics = require('./topics'),
plugins = require('./plugins'), plugins = require('./plugins'),
meta = require('./meta'),
validator = require('validator'), validator = require('validator'),
privileges = require('./privileges'), privileges = require('./privileges');
async = require('async'),
winston = require('winston'),
nconf = require('nconf');
(function(Categories) { (function(Categories) {
require('./categories/create')(Categories);
require('./categories/delete')(Categories); require('./categories/delete')(Categories);
require('./categories/topics')(Categories);
require('./categories/unread')(Categories);
require('./categories/activeusers')(Categories); require('./categories/activeusers')(Categories);
require('./categories/recentreplies')(Categories); require('./categories/recentreplies')(Categories);
require('./categories/update')(Categories); require('./categories/update')(Categories);
Categories.create = function(data, callback) {
db.incrObjectField('global', 'nextCid', function(err, cid) {
if (err) {
return callback(err);
}
var slug = cid + '/' + utils.slugify(data.name);
var category = {
cid: cid,
name: data.name,
description: data.description,
icon: data.icon,
bgColor: data.bgColor,
color: data.color,
slug: slug,
parentCid: 0,
topic_count: 0,
post_count: 0,
disabled: 0,
order: data.order,
link: '',
numRecentReplies: 1,
class: 'col-md-3 col-xs-6',
imageClass: 'auto'
};
async.series([
async.apply(db.setObject, 'category:' + cid, category),
async.apply(db.sortedSetAdd, 'categories:cid', data.order, cid)
], function(err) {
if (err) {
return callback(err);
}
callback(null, category);
});
});
};
Categories.exists = function(cid, callback) { Categories.exists = function(cid, callback) {
db.isSortedSetMember('categories:cid', cid, callback); db.isSortedSetMember('categories:cid', cid, callback);
}; };
@ -112,42 +70,6 @@ var db = require('./database'),
}); });
}; };
Categories.getCategoryTopics = function(data, callback) {
var tids;
async.waterfall([
function(next) {
Categories.getTopicIds(data.targetUid ? 'cid:' + data.cid + ':uid:' + data.targetUid + ':tids' : 'cid:' + data.cid + ':tids', data.start, data.stop, next);
},
function(topicIds, next) {
tids = topicIds;
topics.getTopicsByTids(tids, data.uid, next);
},
function(topics, next) {
if (!Array.isArray(topics) || !topics.length) {
return next(null, {
topics: [],
nextStart: 1
});
}
var indices = {},
i = 0;
for(i=0; i<tids.length; ++i) {
indices[tids[i]] = data.start + i;
}
for(i=0; i<topics.length; ++i) {
topics[i].index = indices[topics[i].tid];
}
next(null, {
topics: topics,
nextStart: data.stop + 1
});
}
], callback);
};
Categories.isIgnored = function(cids, uid, callback) { Categories.isIgnored = function(cids, uid, callback) {
user.getIgnoredCategories(uid, function(err, ignoredCids) { user.getIgnoredCategories(uid, function(err, ignoredCids) {
if (err) { if (err) {
@ -161,48 +83,27 @@ var db = require('./database'),
}); });
}; };
Categories.getTopicIds = function(set, start, stop, callback) {
db.getSortedSetRevRange(set, start, stop, callback);
};
Categories.getTopicIndex = function(tid, callback) {
topics.getTopicField(tid, 'cid', function(err, cid) {
if(err) {
return callback(err);
}
db.sortedSetRevRank('cid:' + cid + ':tids', tid, callback);
});
};
Categories.getPageCount = function(cid, uid, callback) { Categories.getPageCount = function(cid, uid, callback) {
Categories.getCategoryField(cid, 'topic_count', function(err, topicCount) { async.parallel({
topicCount: async.apply(Categories.getCategoryField, cid, 'topic_count'),
settings: async.apply(user.getSettings, uid)
}, function(err, results) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
if (parseInt(topicCount, 10) === 0) { if (!parseInt(results.topicCount, 10)) {
return callback(null, 1); return callback(null, 1);
} }
user.getSettings(uid, function(err, settings) { callback(null, Math.ceil(parseInt(results.topicCount, 10) / results.settings.topicsPerPage));
if (err) {
return callback(err);
}
callback(null, Math.ceil(parseInt(topicCount, 10) / settings.topicsPerPage));
});
}); });
}; };
Categories.getAllCategories = function(uid, callback) { Categories.getAllCategories = function(uid, callback) {
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) { db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) {
if (err) { if (err || !Array.isArray(cids) || !cids.length) {
return callback(err); return callback(err, []);
}
if (!Array.isArray(cids) || !cids.length) {
return callback(null, []);
} }
Categories.getCategories(cids, uid, callback); Categories.getCategories(cids, uid, callback);
@ -210,99 +111,35 @@ var db = require('./database'),
}; };
Categories.getCategoriesByPrivilege = function(uid, privilege, callback) { Categories.getCategoriesByPrivilege = function(uid, privilege, callback) {
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) { async.waterfall([
if (err) { function(next) {
return callback(err); db.getSortedSetRange('categories:cid', 0, -1, next);
} },
function(cids, next) {
if (!Array.isArray(cids) || !cids.length) { privileges.categories.filterCids(privilege, cids, uid, next);
return callback(null, []); },
} function(cids, next) {
Categories.getCategories(cids, uid, next);
privileges.categories.filterCids(privilege, cids, uid, function(err, cids) { },
if (err) { function(categories, next) {
return callback(err); categories = categories.filter(function(category) {
} return !category.disabled;
Categories.getCategories(cids, uid, function(err, categories) {
if (err) {
return callback(err);
}
categories = categories.filter(function(category) {
return !category.disabled;
});
callback(null, categories);
}); });
}); next(null, categories);
});
};
Categories.getModerators = function(cid, callback) {
Groups.get('cid:' + cid + ':privileges:mods', {}, function(err, groupObj) {
if (err && err === 'group-not-found') {
return callback(null, []);
} }
if (err) { ], callback);
return callback(err);
}
if (!Array.isArray(groupObj) || !groupObj.members.length) {
return callback(null, []);
}
user.getMultipleUserFields(groupObj.members, ['uid', 'username', 'userslug', 'picture'], callback);
});
}; };
Categories.markAsRead = function(cids, uid, callback) { Categories.getModerators = function(cid, callback) {
callback = callback || function() {}; Groups.getMembers('cid:' + cid + ':privileges:mods', function(err, uids) {
if (!Array.isArray(cids) || !cids.length) { if (err || !Array.isArray(uids) || !uids.length) {
return callback(); return callback(err, []);
}
var keys = cids.map(function(cid) {
return 'cid:' + cid + ':read_by_uid';
});
db.isMemberOfSets(keys, uid, function(err, hasRead) {
if (err) {
return callback(err);
}
keys = keys.filter(function(key, index) {
return !hasRead[index];
});
if (!keys.length) {
return callback();
} }
db.setsAdd(keys, uid, callback); user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], callback);
});
};
Categories.markAsUnreadForAll = function(cid, callback) {
callback = callback || function() {};
db.delete('cid:' + cid + ':read_by_uid', function(err) {
callback(err);
}); });
}; };
Categories.hasReadCategories = function(cids, uid, callback) {
var sets = [];
for (var i = 0, ii = cids.length; i < ii; i++) {
sets.push('cid:' + cids[i] + ':read_by_uid');
}
db.isMemberOfSets(sets, uid, callback);
};
Categories.hasReadCategory = function(cid, uid, callback) {
db.isSetMember('cid:' + cid + ':read_by_uid', uid, callback);
};
Categories.getCategoryData = function(cid, callback) { Categories.getCategoryData = function(cid, callback) {
Categories.getCategoriesData([cid], function(err, categories) { Categories.getCategoriesData([cid], function(err, categories) {
callback(err, categories ? categories[0] : null); callback(err, categories ? categories[0] : null);
@ -315,12 +152,8 @@ var db = require('./database'),
}); });
db.getObjects(keys, function(err, categories) { db.getObjects(keys, function(err, categories) {
if (err) { if (err || !Array.isArray(categories) || !categories.length) {
return callback(err); return callback(err, []);
}
if (!Array.isArray(categories) || !categories.length) {
return callback(null, []);
} }
async.map(categories, modifyCategory, callback); async.map(categories, modifyCategory, callback);
@ -381,7 +214,7 @@ var db = require('./database'),
} }
if (!cids.length) { if (!cids.length) {
return callback(null, []); return callback(null, []);
} }
async.parallel({ async.parallel({
@ -455,35 +288,4 @@ var db = require('./database'),
], callback); ], callback);
}; };
Categories.onNewPostMade = function(postData, callback) {
topics.getTopicFields(postData.tid, ['cid', 'pinned'], function(err, topicData) {
if (err) {
return callback(err);
}
if (!topicData || !topicData.cid) {
return callback();
}
var cid = topicData.cid;
async.parallel([
function(next) {
db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, postData.pid, next);
},
function(next) {
db.incrObjectField('category:' + cid, 'post_count', next);
},
function(next) {
if (parseInt(topicData.pinned, 10) === 1) {
next();
} else {
db.sortedSetAdd('cid:' + cid + ':tids', postData.timestamp, postData.tid, next);
}
}
], callback);
});
};
}(exports)); }(exports));

@ -0,0 +1,48 @@
'use strict';
var async = require('async'),
db = require('../database'),
utils = require('../../public/src/utils');
module.exports = function(Categories) {
Categories.create = function(data, callback) {
db.incrObjectField('global', 'nextCid', function(err, cid) {
if (err) {
return callback(err);
}
var slug = cid + '/' + utils.slugify(data.name);
var category = {
cid: cid,
name: data.name,
description: data.description,
icon: data.icon,
bgColor: data.bgColor,
color: data.color,
slug: slug,
parentCid: 0,
topic_count: 0,
post_count: 0,
disabled: 0,
order: data.order,
link: '',
numRecentReplies: 1,
class: 'col-md-3 col-xs-6',
imageClass: 'auto'
};
async.series([
async.apply(db.setObject, 'category:' + cid, category),
async.apply(db.sortedSetAdd, 'categories:cid', data.order, cid)
], function(err) {
if (err) {
return callback(err);
}
callback(null, category);
});
});
};
};

@ -29,6 +29,9 @@ module.exports = function(Categories) {
return callback(null, []); return callback(null, []);
} }
async.map(categoryData, getRecentTopicPids, function(err, results) { async.map(categoryData, getRecentTopicPids, function(err, results) {
if (err) {
return callback(err);
}
var pids = _.flatten(results); var pids = _.flatten(results);
@ -41,21 +44,21 @@ module.exports = function(Categories) {
return callback(err); return callback(err);
} }
async.each(categoryData, function(category, next) { categoryData.forEach(function(category) {
assignPostsToCategory(category, posts, next); assignPostsToCategory(category, posts);
}, callback); });
callback();
}); });
}); });
}; };
function assignPostsToCategory(category, posts, next) { function assignPostsToCategory(category, posts) {
category.posts = posts.filter(function(post) { category.posts = posts.filter(function(post) {
return parseInt(post.category.cid, 10) === parseInt(category.cid, 10); return parseInt(post.category.cid, 10) === parseInt(category.cid, 10);
}).sort(function(a, b) { }).sort(function(a, b) {
return b.timestamp - a.timestamp; return b.timestamp - a.timestamp;
}).slice(0, parseInt(category.numRecentReplies, 10)); }).slice(0, parseInt(category.numRecentReplies, 10));
next();
} }
function getRecentTopicPids(category, callback) { function getRecentTopicPids(category, callback) {

@ -0,0 +1,89 @@
'use strict';
var async = require('async'),
db = require('../database'),
topics = require('../topics');
module.exports = function(Categories) {
Categories.getCategoryTopics = function(data, callback) {
var tids;
async.waterfall([
function(next) {
Categories.getTopicIds(data.targetUid ? 'cid:' + data.cid + ':uid:' + data.targetUid + ':tids' : 'cid:' + data.cid + ':tids', data.start, data.stop, next);
},
function(topicIds, next) {
tids = topicIds;
topics.getTopicsByTids(tids, data.uid, next);
},
function(topics, next) {
if (!Array.isArray(topics) || !topics.length) {
return next(null, {
topics: [],
nextStart: 1
});
}
var indices = {},
i = 0;
for(i=0; i<tids.length; ++i) {
indices[tids[i]] = data.start + i;
}
for(i=0; i<topics.length; ++i) {
topics[i].index = indices[topics[i].tid];
}
next(null, {
topics: topics,
nextStart: data.stop + 1
});
}
], callback);
};
Categories.getTopicIds = function(set, start, stop, callback) {
db.getSortedSetRevRange(set, start, stop, callback);
};
Categories.getTopicIndex = function(tid, callback) {
topics.getTopicField(tid, 'cid', function(err, cid) {
if(err) {
return callback(err);
}
db.sortedSetRevRank('cid:' + cid + ':tids', tid, callback);
});
};
Categories.onNewPostMade = function(postData, callback) {
topics.getTopicFields(postData.tid, ['cid', 'pinned'], function(err, topicData) {
if (err) {
return callback(err);
}
if (!topicData || !topicData.cid) {
return callback();
}
var cid = topicData.cid;
async.parallel([
function(next) {
db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, postData.pid, next);
},
function(next) {
db.incrObjectField('category:' + cid, 'post_count', next);
},
function(next) {
if (parseInt(topicData.pinned, 10) === 1) {
next();
} else {
db.sortedSetAdd('cid:' + cid + ':tids', postData.timestamp, postData.tid, next);
}
}
], callback);
});
};
};

@ -0,0 +1,56 @@
"use strict";
var async = require('async'),
db = require('../database');
module.exports = function(Categories) {
Categories.markAsRead = function(cids, uid, callback) {
callback = callback || function() {};
if (!Array.isArray(cids) || !cids.length) {
return callback();
}
var keys = cids.map(function(cid) {
return 'cid:' + cid + ':read_by_uid';
});
db.isMemberOfSets(keys, uid, function(err, hasRead) {
if (err) {
return callback(err);
}
keys = keys.filter(function(key, index) {
return !hasRead[index];
});
if (!keys.length) {
return callback();
}
db.setsAdd(keys, uid, callback);
});
};
Categories.markAsUnreadForAll = function(cid, callback) {
callback = callback || function() {};
db.delete('cid:' + cid + ':read_by_uid', function(err) {
callback(err);
});
};
Categories.hasReadCategories = function(cids, uid, callback) {
var sets = [];
for (var i = 0, ii = cids.length; i < ii; i++) {
sets.push('cid:' + cids[i] + ':read_by_uid');
}
db.isMemberOfSets(sets, uid, callback);
};
Categories.hasReadCategory = function(cid, uid, callback) {
db.isSetMember('cid:' + cid + ':read_by_uid', uid, callback);
};
};

@ -2,8 +2,8 @@
'use strict'; 'use strict';
var async = require('async'), var async = require('async'),
db = require('./../database'), db = require('../database'),
utils = require('./../../public/src/utils'); utils = require('../../public/src/utils');
module.exports = function(Categories) { module.exports = function(Categories) {

@ -9,7 +9,8 @@ var categoriesController = {},
categories = require('../categories'), categories = require('../categories'),
topics = require('../topics'), topics = require('../topics'),
meta = require('../meta'), meta = require('../meta'),
plugins = require('../plugins'); plugins = require('../plugins'),
utils = require('../../public/src/utils');
// todo: This might be better placed somewhere else // todo: This might be better placed somewhere else
var apiToRegular = function(url) { var apiToRegular = function(url) {
@ -44,7 +45,6 @@ categoriesController.popular = function(req, res, next) {
if (uid === 0) { if (uid === 0) {
if (anonCache[term] && (Date.now() - lastUpdateTime) < 60 * 60 * 1000) { if (anonCache[term] && (Date.now() - lastUpdateTime) < 60 * 60 * 1000) {
console.log('returning from cache');
return res.render('popular', anonCache[term]); return res.render('popular', anonCache[term]);
} }
} }
@ -105,6 +105,10 @@ categoriesController.get = function(req, res, next) {
uid = req.user ? req.user.uid : 0, uid = req.user ? req.user.uid : 0,
userPrivileges; userPrivileges;
if (req.params.topic_index && !utils.isNumber(req.params.topic_index)) {
return categoriesController.notFound(req, res);
}
async.waterfall([ async.waterfall([
function(next) { function(next) {
async.parallel({ async.parallel({
@ -112,7 +116,7 @@ categoriesController.get = function(req, res, next) {
categories.exists(cid, next); categories.exists(cid, next);
}, },
categoryData: function(next) { categoryData: function(next) {
categories.getCategoryFields(cid, ['slug', 'disabled'], next); categories.getCategoryFields(cid, ['slug', 'disabled', 'topic_count'], next);
}, },
privileges: function(next) { privileges: function(next) {
privileges.categories.get(cid, uid, next); privileges.categories.get(cid, uid, next);
@ -135,14 +139,21 @@ categoriesController.get = function(req, res, next) {
return categoriesController.notAllowed(req, res); return categoriesController.notAllowed(req, res);
} }
var topicIndex = utils.isNumber(req.params.topic_index) ? parseInt(req.params.topic_index, 10) : 1;
var topicCount = parseInt(results.categoryData.topic_count, 10) + 1;
if (topicIndex < 1 || topicIndex > topicCount) {
var url = '/category/' + cid + '/' + req.params.slug + (topicIndex > topicCount ? '/' + topicCount : '');
return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url);
}
userPrivileges = results.privileges; userPrivileges = results.privileges;
var settings = results.userSettings; var settings = results.userSettings;
var topicIndex = 0;
if (!settings.usePagination) { if (!settings.usePagination) {
topicIndex = Math.max((req.params.topic_index || 1) - (settings.topicsPerPage - 1), 0); topicIndex = Math.max((topicIndex || 1) - (settings.topicsPerPage - 1), 0);
} else if (!req.query.page) { } else if (!req.query.page) {
var index = Math.max(parseInt((req.params.topic_index || 0), 10), 0); var index = Math.max(parseInt((topicIndex || 0), 10), 0);
page = Math.ceil((index + 1) / settings.topicsPerPage); page = Math.ceil((index + 1) / settings.topicsPerPage);
} }

@ -60,11 +60,8 @@ topicsController.get = function(req, res, next) {
if (utils.isNumber(req.params.post_index)) { if (utils.isNumber(req.params.post_index)) {
var url = ''; var url = '';
if (req.params.post_index > postCount) { if (req.params.post_index < 1 || req.params.post_index > postCount) {
url = '/topic/' + req.params.topic_id + '/' + req.params.slug + '/' + postCount; url = '/topic/' + req.params.topic_id + '/' + req.params.slug + (req.params.post_index > postCount ? '/' + postCount : '');
return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url);
} else if (req.params.post_index < 1) {
url = '/topic/' + req.params.topic_id + '/' + req.params.slug;
return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url); return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url);
} }
} }

@ -140,6 +140,10 @@ var async = require('async'),
}); });
}; };
Groups.getMembers = function(groupName, callback) {
db.getSetMembers('group:' + groupName + ':members', callback);
};
Groups.search = function(query, options, callback) { Groups.search = function(query, options, callback) {
if (!query) { if (!query) {
return callback(null, []); return callback(null, []);

@ -127,26 +127,6 @@ middleware.addSlug = function(req, res, next) {
next(); next();
}; };
middleware.checkTopicIndex = function(req, res, next) {
categories.getCategoryField(req.params.category_id, 'topic_count', function(err, topicCount) {
if (err) {
return next(err);
}
var topicIndex = parseInt(req.params.topic_index, 10);
topicCount = parseInt(topicCount, 10) + 1;
var url = '';
if (topicIndex > topicCount) {
url = '/category/' + req.params.category_id + '/' + req.params.slug + '/' + topicCount;
return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url);
} else if (topicIndex < 1) {
url = '/category/' + req.params.category_id + '/' + req.params.slug;
return res.locals.isAPI ? res.status(302).json(url) : res.redirect(url);
}
next();
});
};
middleware.prepareAPI = function(req, res, next) { middleware.prepareAPI = function(req, res, next) {
res.locals.isAPI = true; res.locals.isAPI = true;
next(); next();

@ -129,9 +129,6 @@ var fs = require('fs'),
app.render.apply(app, arguments); app.render.apply(app, arguments);
}; };
// Deprecated as of v0.5.0, remove this hook call for NodeBB v0.6.0-1
Plugins.fireHook('action:app.load', router, middleware, controllers);
Plugins.fireHook('static:app.load', {app: app, router: router, middleware: middleware, controllers: controllers}, function() { Plugins.fireHook('static:app.load', {app: app, router: router, middleware: middleware, controllers: controllers}, function() {
hotswap.replace('plugins', router); hotswap.replace('plugins', router);
winston.info('[plugins] All plugins reloaded and rerouted'); winston.info('[plugins] All plugins reloaded and rerouted');
@ -391,120 +388,77 @@ var fs = require('fs'),
return !!(Plugins.loadedHooks[hook] && Plugins.loadedHooks[hook].length > 0); return !!(Plugins.loadedHooks[hook] && Plugins.loadedHooks[hook].length > 0);
}; };
Plugins.fireHook = function(hook) { Plugins.fireHook = function(hook, params, callback) {
var callback = typeof arguments[arguments.length-1] === 'function' ? arguments[arguments.length-1] : null, callback = typeof callback === 'function' ? callback : function() {};
args = arguments.length ? Array.prototype.slice.call(arguments, 1) : [];
if (callback) {
args.pop();
}
var hookList = Plugins.loadedHooks[hook]; var hookList = Plugins.loadedHooks[hook];
if (Array.isArray(hookList)) { if (!Array.isArray(hookList) || !hookList.length) {
// if (global.env === 'development') winston.info('[plugins] Firing hook: \'' + hook + '\''); return callback(null, params);
var hookType = hook.split(':')[0]; }
switch (hookType) {
case 'filter':
async.reduce(hookList, args, function(value, hookObj, next) {
if (hookObj.method) {
if (!hookObj.hasOwnProperty('callbacked') || hookObj.callbacked === true) {
// omg, after 6 months I finally realised what this does...
// It adds the callback to the arguments passed-in, since the callback
// is defined in *this* file (the async cb), and not the hooks themselves.
value = hookObj.method.apply(Plugins, value.concat(function() {
next(arguments[0], Array.prototype.slice.call(arguments, 1));
}));
/*
Backwards compatibility block for v0.5.0
Remove this once NodeBB enters v0.5.0-1
*/
if (value !== undefined && value !== callback) {
winston.warn('[plugins/' + hookObj.id + '] "callbacked" deprecated as of 0.4x. Use asynchronous method instead for hook: ' + hook);
next(null, [value]);
}
} else {
winston.warn('[plugins/' + hookObj.id + '] "callbacked" deprecated as of 0.4x. Use asynchronous method instead for hook: ' + hook);
value = hookObj.method.apply(Plugins, value);
next(null, [value]);
}
/* End backwards compatibility block */
} else {
if (global.env === 'development') {
winston.info('[plugins] Expected method for hook \'' + hook + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
}
next(null, [value]);
}
}, function(err, values) {
if (err) {
if (global.env === 'development') {
winston.info('[plugins] Problem executing hook: ' + hook + ' err: ' + JSON.stringify(err));
}
}
if (callback) { // if (global.env === 'development') winston.info('[plugins] Firing hook: \'' + hook + '\'');
callback.apply(Plugins, [err].concat(values)); var hookType = hook.split(':')[0];
} switch (hookType) {
}); case 'filter':
break; fireFilterHook(hook, hookList, params, callback);
case 'action': break;
var deprecationWarn = []; case 'action':
async.each(hookList, function(hookObj, next) { fireActionHook(hook, hookList, params, callback);
/* break;
Backwards compatibility block for v0.5.0 case 'static':
Remove this once NodeBB enters v0.6.0-1 fireStaticHook(hook, hookList, params, callback);
*/ break;
if (hook === 'action:app.load') { default:
deprecationWarn.push(hookObj.id); winston.warn('[plugins] Unknown hookType: ' + hookType + ', hook : ' + hook);
} break;
/* End backwards compatibility block */ }
};
if (hookObj.method) { function fireFilterHook(hook, hookList, params, callback) {
hookObj.method.apply(Plugins, args); async.reduce(hookList, params, function(params, hookObj, next) {
} else { if (typeof hookObj.method !== 'function') {
if (global.env === 'development') { if (global.env === 'development') {
winston.info('[plugins] Expected method \'' + hookObj.method + '\' in plugin \'' + hookObj.id + '\' not found, skipping.'); winston.warn('[plugins] Expected method for hook \'' + hook + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
} }
} return next(null, params);
}
next(); hookObj.method(params, next);
}, function() {
/* }, function(err, values) {
Backwards compatibility block for v0.5.0 if (err) {
Remove this once NodeBB enters v0.6.0-1 winston.error('[plugins] Problem executing hook: ' + hook + ' err: ' + err.stack);
*/
if (deprecationWarn.length) {
winston.warn('[plugins] The `action:app.load` hook is deprecated in favour of `static:app.load`, please notify the developers of the following plugins:');
for(var x=0,numDeprec=deprecationWarn.length;x<numDeprec;x++) {
process.stdout.write(' * ' + deprecationWarn[x] + '\n');
}
}
/* End backwards compatibility block */
});
break;
case 'static':
async.each(hookList, function(hookObj, next) {
if (hookObj.method) {
hookObj.method.apply(Plugins, args.concat(next));
}
}, function(err) {
callback(err);
});
break;
default:
// Do nothing...
break;
} }
} else {
// Otherwise, this hook contains no methods callback(err, values);
if (callback) { });
callback.apply(this, [null].concat(args)); }
function fireActionHook(hook, hookList, params, callback) {
async.each(hookList, function(hookObj, next) {
if (typeof hookObj.method !== 'function') {
if (global.env === 'development') {
winston.warn('[plugins] Expected method for hook \'' + hook + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
}
return next();
} }
return args[0]; hookObj.method(params);
} next();
}; }, callback);
}
function fireStaticHook(hook, hookList, params,callback) {
async.each(hookList, function(hookObj, next) {
if (typeof hookObj.method === 'function') {
hookObj.method(params, next);
} else {
next();
}
}, callback);
}
Plugins.isActive = function(id, callback) { Plugins.isActive = function(id, callback) {
db.isSetMember('plugins:active', id, callback); db.isSetMember('plugins:active', id, callback);

@ -1,107 +1,25 @@
'use strict'; 'use strict';
var async = require('async'), var async = require('async'),
path = require('path'),
fs = require('fs'),
nconf = require('nconf'),
_ = require('underscore'), _ = require('underscore'),
validator = require('validator'),
winston = require('winston'),
gravatar = require('gravatar'),
S = require('string'),
db = require('./database'), db = require('./database'),
utils = require('../public/src/utils'), utils = require('../public/src/utils'),
user = require('./user'), user = require('./user'),
groups = require('./groups'),
topics = require('./topics'), topics = require('./topics'),
favourites = require('./favourites'),
postTools = require('./postTools'), postTools = require('./postTools'),
privileges = require('./privileges'), plugins = require('./plugins');
categories = require('./categories'),
plugins = require('./plugins'),
meta = require('./meta'),
emitter = require('./emitter'),
websockets = require('./socket.io');
(function(Posts) { (function(Posts) {
require('./posts/recent')(Posts);
require('./posts/create')(Posts);
require('./posts/delete')(Posts); require('./posts/delete')(Posts);
require('./posts/user')(Posts);
require('./posts/category')(Posts);
require('./posts/summary')(Posts);
require('./posts/recent')(Posts);
require('./posts/flags')(Posts); require('./posts/flags')(Posts);
Posts.create = function(data, callback) {
var uid = data.uid,
tid = data.tid,
content = data.content,
timestamp = data.timestamp || Date.now();
if (uid === null) {
return callback(new Error('[[error:invalid-uid]]'));
}
var postData;
async.waterfall([
function(next) {
db.incrObjectField('global', 'nextPid', next);
},
function(pid, next) {
postData = {
'pid': pid,
'uid': uid,
'tid': tid,
'content': content,
'timestamp': timestamp,
'reputation': 0,
'votes': 0,
'editor': '',
'edited': 0,
'deleted': 0
};
if (data.toPid) {
postData.toPid = data.toPid;
}
plugins.fireHook('filter:post.save', postData, next);
},
function(postData, next) {
db.setObject('post:' + postData.pid, postData, next);
},
function(next) {
async.parallel([
function(next) {
user.onNewPostMade(postData, next);
},
function(next) {
topics.onNewPostMade(postData, next);
},
function(next) {
categories.onNewPostMade(postData, next);
},
function(next) {
db.sortedSetAdd('posts:pid', timestamp, postData.pid, next);
},
function(next) {
db.incrObjectField('global', 'postCount', next);
}
], function(err) {
if (err) {
return next(err);
}
plugins.fireHook('filter:post.get', postData, next);
});
},
function(postData, next) {
plugins.fireHook('action:post.save', postData);
next(null, postData);
}
], callback);
};
Posts.exists = function(pid, callback) { Posts.exists = function(pid, callback) {
db.isSortedSetMember('posts:pid', pid, callback); db.isSortedSetMember('posts:pid', pid, callback);
}; };
@ -167,184 +85,6 @@ var async = require('async'),
}); });
}; };
Posts.getUserInfoForPosts = function(uids, uid, callback) {
async.parallel({
groups: function(next) {
groups.getUserGroups(uids, next);
},
userData: function(next) {
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned', 'status'], next);
},
online: function(next) {
websockets.isUsersOnline(uids, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
var userData = results.userData;
for(var i=0; i<userData.length; ++i) {
userData[i].groups = results.groups[i];
userData[i].status = results.online[i] ? (userData[i].status || 'online') : 'offline';
}
async.map(userData, function(userData, next) {
userData.uid = userData.uid || 0;
userData.username = userData.username || '[[global:guest]]';
userData.userslug = userData.userslug || '';
userData.reputation = userData.reputation || 0;
userData.postcount = userData.postcount || 0;
userData.banned = parseInt(userData.banned, 10) === 1;
userData.picture = userData.picture || user.createGravatarURLFromEmail('');
async.parallel({
signature: function(next) {
if (parseInt(meta.config.disableSignatures, 10) === 1) {
userData.signature = '';
return next();
}
postTools.parseSignature(userData, uid, next);
},
customProfileInfo: function(next) {
plugins.fireHook('filter:posts.custom_profile_info', {profile: [], uid: userData.uid}, next);
}
}, function(err, results) {
if (err) {
return next(err);
}
userData.custom_profile_info = results.customProfileInfo.profile;
plugins.fireHook('filter:posts.modifyUserInfo', userData, next);
});
}, callback);
});
};
Posts.getPostSummaryByPids = function(pids, uid, options, callback) {
options.stripTags = options.hasOwnProperty('stripTags') ? options.stripTags : false;
options.parse = options.hasOwnProperty('parse') ? options.parse : true;
options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : [];
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
var keys = pids.map(function(pid) {
return 'post:' + pid;
});
var fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'].concat(options.extraFields);
db.getObjectsFields(keys, fields, function(err, posts) {
if (err) {
return callback(err);
}
posts = posts.filter(function(p) {
return !!p && parseInt(p.deleted, 10) !== 1;
});
var uids = [], tids = [];
for(var i=0; i<posts.length; ++i) {
if (uids.indexOf(posts[i].uid) === -1) {
uids.push(posts[i].uid);
}
if (tids.indexOf('topic:' + posts[i].tid) === -1) {
tids.push('topic:' + posts[i].tid);
}
}
async.parallel({
users: function(next) {
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
},
topicsAndCategories: function(next) {
db.getObjectsFields(tids, ['uid', 'tid', 'title', 'cid', 'slug', 'deleted'], function(err, topics) {
if (err) {
return next(err);
}
var cids = topics.map(function(topic) {
if (topic) {
topic.title = validator.escape(topic.title);
}
return topic && topic.cid;
}).filter(function(value, index, array) {
return value && array.indexOf(value) === index;
});
categories.getMultipleCategoryFields(cids, ['cid', 'name', 'icon', 'slug'], function(err, categories) {
next(err, {topics: topics, categories: categories});
});
});
},
indices: function(next) {
Posts.getPostIndices(posts, uid, next);
}
}, function(err, results) {
function toObject(key, data) {
var obj = {};
for(var i=0; i<data.length; ++i) {
obj[data[i][key]] = data[i];
}
return obj;
}
function stripTags(content) {
if (options.stripTags && content) {
var s = S(content);
return s.stripTags.apply(s, utils.stripTags).s;
}
return content;
}
if (err) {
return callback(err);
}
results.users = toObject('uid', results.users);
results.topics = toObject('tid', results.topicsAndCategories.topics);
results.categories = toObject('cid', results.topicsAndCategories.categories);
for (var i=0; i<posts.length; ++i) {
posts[i].index = utils.isNumber(results.indices[i]) ? parseInt(results.indices[i], 10) + 1 : 1;
}
posts = posts.filter(function(post) {
return results.topics[post.tid] && parseInt(results.topics[post.tid].deleted, 10) !== 1;
});
async.map(posts, function(post, next) {
post.user = results.users[post.uid];
post.topic = results.topics[post.tid];
post.category = results.categories[post.topic.cid];
post.relativeTime = utils.toISOString(post.timestamp);
if (!post.content || !options.parse) {
post.content = stripTags(post.content);
return next(null, post);
}
postTools.parsePost(post, uid, function(err, post) {
if (err) {
return next(err);
}
post.content = stripTags(post.content);
next(null, post);
});
}, function(err, posts) {
plugins.fireHook('filter:post.getPostSummaryByPids', {posts: posts, uid: uid}, function(err, postData) {
callback(err, postData.posts);
});
});
});
});
};
Posts.getPostData = function(pid, callback) { Posts.getPostData = function(pid, callback) {
db.getObject('post:' + pid, function(err, data) { db.getObject('post:' + pid, function(err, data) {
if(err) { if(err) {
@ -405,107 +145,6 @@ var async = require('async'),
db.setObject('post:' + pid, data, callback); db.setObject('post:' + pid, data, callback);
}; };
Posts.getCidByPid = function(pid, callback) {
Posts.getPostField(pid, 'tid', function(err, tid) {
if(err) {
return callback(err);
}
topics.getTopicField(tid, 'cid', function(err, cid) {
if(err || !cid) {
return callback(err || new Error('[[error:invalid-cid]]'));
}
callback(null, cid);
});
});
};
Posts.getCidsByPids = function(pids, callback) {
Posts.getPostsFields(pids, ['tid'], function(err, posts) {
if (err) {
return callback(err);
}
var tids = posts.map(function(post) {
return post.tid;
}).filter(function(tid, index, array) {
return tid && array.indexOf(tid) === index;
});
topics.getTopicsFields(tids, ['cid'], function(err, topics) {
if (err) {
return callback(err);
}
var map = {};
topics.forEach(function(topic, index) {
if (topic) {
map[tids[index]] = topic.cid;
}
});
var cids = posts.map(function(post) {
return map[post.tid];
});
callback(null, cids);
});
});
};
Posts.getPostsByUid = function(callerUid, uid, start, end, callback) {
user.getPostIds(uid, start, end, function(err, pids) {
if (err) {
return callback(err);
}
privileges.posts.filter('read', pids, callerUid, function(err, pids) {
if (err) {
return callback(err);
}
getPosts(pids, callerUid, function(err, posts) {
if (err) {
return callback(err);
}
callback(null, {posts: posts, nextStart: end + 1});
});
});
});
};
Posts.getFavourites = function(uid, start, end, callback) {
db.getSortedSetRevRange('uid:' + uid + ':favourites', start, end, function(err, pids) {
if (err) {
return callback(err);
}
getPosts(pids, uid, function(err, posts) {
if (err) {
return callback(err);
}
callback(null, {posts: posts, nextStart: end + 1});
});
});
};
function getPosts(pids, uid, callback) {
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
Posts.getPostSummaryByPids(pids, uid, {stripTags: false}, function(err, posts) {
if (err) {
return callback(err);
}
if (!Array.isArray(posts) || !posts.length) {
return callback(null, []);
}
callback(null, posts);
});
}
Posts.getPidIndex = function(pid, uid, callback) { Posts.getPidIndex = function(pid, uid, callback) {
async.parallel({ async.parallel({
settings: function(next) { settings: function(next) {
@ -568,43 +207,6 @@ var async = require('async'),
}); });
}; };
Posts.isOwner = function(pid, uid, callback) {
uid = parseInt(uid, 10);
if (Array.isArray(pid)) {
if (!uid) {
return callback(null, pid.map(function() {return false;}));
}
Posts.getPostsFields(pid, ['uid'], function(err, posts) {
if (err) {
return callback(err);
}
posts = posts.map(function(post) {
return post && parseInt(post.uid, 10) === uid;
});
callback(null, posts);
});
} else {
if (!uid) {
return callback(null, false);
}
Posts.getPostField(pid, 'uid', function(err, author) {
callback(err, parseInt(author, 10) === uid);
});
}
};
Posts.isModerator = function(pids, uid, callback) {
if (!parseInt(uid, 10)) {
return callback(null, pids.map(function() {return false;}));
}
Posts.getCidsByPids(pids, function(err, cids) {
if (err) {
return callback(err);
}
user.isModerator(uid, cids, callback);
});
}
Posts.isMain = function(pid, callback) { Posts.isMain = function(pid, callback) {
Posts.getPostField(pid, 'tid', function(err, tid) { Posts.getPostField(pid, 'tid', function(err, tid) {
if (err) { if (err) {

@ -0,0 +1,55 @@
'use strict';
var topics = require('../topics');
module.exports = function(Posts) {
Posts.getCidByPid = function(pid, callback) {
Posts.getPostField(pid, 'tid', function(err, tid) {
if(err) {
return callback(err);
}
topics.getTopicField(tid, 'cid', function(err, cid) {
if(err || !cid) {
return callback(err || new Error('[[error:invalid-cid]]'));
}
callback(null, cid);
});
});
};
Posts.getCidsByPids = function(pids, callback) {
Posts.getPostsFields(pids, ['tid'], function(err, posts) {
if (err) {
return callback(err);
}
var tids = posts.map(function(post) {
return post.tid;
}).filter(function(tid, index, array) {
return tid && array.indexOf(tid) === index;
});
topics.getTopicsFields(tids, ['cid'], function(err, topics) {
if (err) {
return callback(err);
}
var map = {};
topics.forEach(function(topic, index) {
if (topic) {
map[tids[index]] = topic.cid;
}
});
var cids = posts.map(function(post) {
return map[post.tid];
});
callback(null, cids);
});
});
};
};

@ -0,0 +1,86 @@
'use strict';
var async = require('async'),
db = require('../database'),
plugins = require('../plugins'),
user = require('../user'),
topics = require('../topics'),
categories = require('../categories');
module.exports = function(Posts) {
Posts.create = function(data, callback) {
var uid = data.uid,
tid = data.tid,
content = data.content,
timestamp = data.timestamp || Date.now();
if (!uid && parseInt(uid, 10) !== 0) {
return callback(new Error('[[error:invalid-uid]]'));
}
var postData;
async.waterfall([
function(next) {
db.incrObjectField('global', 'nextPid', next);
},
function(pid, next) {
postData = {
'pid': pid,
'uid': uid,
'tid': tid,
'content': content,
'timestamp': timestamp,
'reputation': 0,
'votes': 0,
'editor': '',
'edited': 0,
'deleted': 0
};
if (data.toPid) {
postData.toPid = data.toPid;
}
plugins.fireHook('filter:post.save', postData, next);
},
function(postData, next) {
db.setObject('post:' + postData.pid, postData, next);
},
function(next) {
async.parallel([
function(next) {
user.onNewPostMade(postData, next);
},
function(next) {
topics.onNewPostMade(postData, next);
},
function(next) {
categories.onNewPostMade(postData, next);
},
function(next) {
db.sortedSetAdd('posts:pid', timestamp, postData.pid, next);
},
function(next) {
db.incrObjectField('global', 'postCount', next);
}
], function(err) {
if (err) {
return next(err);
}
plugins.fireHook('filter:post.get', postData, next);
});
},
function(postData, next) {
plugins.fireHook('action:post.save', postData);
next(null, postData);
}
], callback);
};
};

@ -1,6 +1,7 @@
'use strict'; 'use strict';
var db = require('../database'), var async = require('async'),
db = require('../database'),
privileges = require('../privileges'); privileges = require('../privileges');
@ -19,52 +20,35 @@ module.exports = function(Posts) {
var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1; var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1;
db.getSortedSetRevRangeByScore('posts:pid', start, count, '+inf', Date.now() - since, function(err, pids) { async.waterfall([
if (err) { function(next) {
return callback(err); db.getSortedSetRevRangeByScore('posts:pid', start, count, '+inf', Date.now() - since, next);
},
function(pids, next) {
privileges.posts.filter('read', pids, uid, next);
},
function(pids, next) {
Posts.getPostSummaryByPids(pids, uid, {stripTags: true}, next);
} }
], callback);
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
privileges.posts.filter('read', pids, uid, function(err, pids) {
if (err) {
return callback(err);
}
Posts.getPostSummaryByPids(pids, uid, {stripTags: true}, callback);
});
});
}; };
Posts.getRecentPosterUids = function(start, end, callback) { Posts.getRecentPosterUids = function(start, end, callback) {
db.getSortedSetRevRange('posts:pid', start, end, function(err, pids) { async.waterfall([
if (err) { function(next) {
return callback(err); db.getSortedSetRevRange('posts:pid', start, end, next);
} },
function(pids, next) {
if (!Array.isArray(pids) || !pids.length) { Posts.getPostsFields(pids, ['uid'], next);
return callback(null, []); },
} function(postData, next) {
pids = pids.map(function(pid) {
return 'post:' + pid;
});
db.getObjectsFields(pids, ['uid'], function(err, postData) {
if (err) {
return callback(err);
}
postData = postData.map(function(post) { postData = postData.map(function(post) {
return post && post.uid; return post && post.uid;
}).filter(function(value, index, array) { }).filter(function(value, index, array) {
return value && array.indexOf(value) === index; return value && array.indexOf(value) === index;
}); });
next(null, postData);
callback(null, postData); }
}); ], callback);
}); };
};
}; };

@ -0,0 +1,136 @@
'use strict';
var async = require('async'),
validator = require('validator'),
S = require('string'),
db = require('../database'),
user = require('../user'),
plugins = require('../plugins'),
categories = require('../categories'),
postTools = require('../postTools'),
utils = require('../../public/src/utils');
module.exports = function(Posts) {
Posts.getPostSummaryByPids = function(pids, uid, options, callback) {
options.stripTags = options.hasOwnProperty('stripTags') ? options.stripTags : false;
options.parse = options.hasOwnProperty('parse') ? options.parse : true;
options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : [];
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
var fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'].concat(options.extraFields);
Posts.getPostsFields(pids, fields, function(err, posts) {
if (err) {
return callback(err);
}
posts = posts.filter(function(p) {
return !!p && parseInt(p.deleted, 10) !== 1;
});
var uids = [], tids = [];
for(var i=0; i<posts.length; ++i) {
if (uids.indexOf(posts[i].uid) === -1) {
uids.push(posts[i].uid);
}
if (tids.indexOf('topic:' + posts[i].tid) === -1) {
tids.push('topic:' + posts[i].tid);
}
}
async.parallel({
users: function(next) {
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
},
topicsAndCategories: function(next) {
db.getObjectsFields(tids, ['uid', 'tid', 'title', 'cid', 'slug', 'deleted'], function(err, topics) {
if (err) {
return next(err);
}
var cids = topics.map(function(topic) {
if (topic) {
topic.title = validator.escape(topic.title);
}
return topic && topic.cid;
}).filter(function(value, index, array) {
return value && array.indexOf(value) === index;
});
categories.getMultipleCategoryFields(cids, ['cid', 'name', 'icon', 'slug'], function(err, categories) {
next(err, {topics: topics, categories: categories});
});
});
},
indices: function(next) {
Posts.getPostIndices(posts, uid, next);
}
}, function(err, results) {
function toObject(key, data) {
var obj = {};
for(var i=0; i<data.length; ++i) {
obj[data[i][key]] = data[i];
}
return obj;
}
function stripTags(content) {
if (options.stripTags && content) {
var s = S(content);
return s.stripTags.apply(s, utils.stripTags).s;
}
return content;
}
if (err) {
return callback(err);
}
results.users = toObject('uid', results.users);
results.topics = toObject('tid', results.topicsAndCategories.topics);
results.categories = toObject('cid', results.topicsAndCategories.categories);
for (var i=0; i<posts.length; ++i) {
posts[i].index = utils.isNumber(results.indices[i]) ? parseInt(results.indices[i], 10) + 1 : 1;
}
posts = posts.filter(function(post) {
return results.topics[post.tid] && parseInt(results.topics[post.tid].deleted, 10) !== 1;
});
async.map(posts, function(post, next) {
post.user = results.users[post.uid];
post.topic = results.topics[post.tid];
post.category = results.categories[post.topic.cid];
post.relativeTime = utils.toISOString(post.timestamp);
if (!post.content || !options.parse) {
post.content = stripTags(post.content);
return next(null, post);
}
postTools.parsePost(post, uid, function(err, post) {
if (err) {
return next(err);
}
post.content = stripTags(post.content);
next(null, post);
});
}, function(err, posts) {
plugins.fireHook('filter:post.getPostSummaryByPids', {posts: posts, uid: uid}, function(err, postData) {
callback(err, postData.posts);
});
});
});
});
};
};

@ -0,0 +1,163 @@
'use strict';
var async = require('async'),
db = require('../database'),
user = require('../user'),
groups = require('../groups'),
meta = require('../meta'),
websockets = require('../socket.io'),
postTools = require('../postTools'),
plugins = require('../plugins'),
privileges = require('../privileges');
module.exports = function(Posts) {
Posts.getUserInfoForPosts = function(uids, uid, callback) {
async.parallel({
groups: function(next) {
groups.getUserGroups(uids, next);
},
userData: function(next) {
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'reputation', 'postcount', 'picture', 'signature', 'banned', 'status'], next);
},
online: function(next) {
websockets.isUsersOnline(uids, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
var userData = results.userData;
for(var i=0; i<userData.length; ++i) {
userData[i].groups = results.groups[i];
userData[i].status = results.online[i] ? (userData[i].status || 'online') : 'offline';
}
async.map(userData, function(userData, next) {
userData.uid = userData.uid || 0;
userData.username = userData.username || '[[global:guest]]';
userData.userslug = userData.userslug || '';
userData.reputation = userData.reputation || 0;
userData.postcount = userData.postcount || 0;
userData.banned = parseInt(userData.banned, 10) === 1;
userData.picture = userData.picture || user.createGravatarURLFromEmail('');
async.parallel({
signature: function(next) {
if (parseInt(meta.config.disableSignatures, 10) === 1) {
userData.signature = '';
return next();
}
postTools.parseSignature(userData, uid, next);
},
customProfileInfo: function(next) {
plugins.fireHook('filter:posts.custom_profile_info', {profile: [], uid: userData.uid}, next);
}
}, function(err, results) {
if (err) {
return next(err);
}
userData.custom_profile_info = results.customProfileInfo.profile;
plugins.fireHook('filter:posts.modifyUserInfo', userData, next);
});
}, callback);
});
};
Posts.isOwner = function(pid, uid, callback) {
uid = parseInt(uid, 10);
if (Array.isArray(pid)) {
if (!uid) {
return callback(null, pid.map(function() {return false;}));
}
Posts.getPostsFields(pid, ['uid'], function(err, posts) {
if (err) {
return callback(err);
}
posts = posts.map(function(post) {
return post && parseInt(post.uid, 10) === uid;
});
callback(null, posts);
});
} else {
if (!uid) {
return callback(null, false);
}
Posts.getPostField(pid, 'uid', function(err, author) {
callback(err, parseInt(author, 10) === uid);
});
}
};
Posts.isModerator = function(pids, uid, callback) {
if (!parseInt(uid, 10)) {
return callback(null, pids.map(function() {return false;}));
}
Posts.getCidsByPids(pids, function(err, cids) {
if (err) {
return callback(err);
}
user.isModerator(uid, cids, callback);
});
};
Posts.getPostsByUid = function(callerUid, uid, start, end, callback) {
user.getPostIds(uid, start, end, function(err, pids) {
if (err) {
return callback(err);
}
privileges.posts.filter('read', pids, callerUid, function(err, pids) {
if (err) {
return callback(err);
}
getPosts(pids, callerUid, function(err, posts) {
if (err) {
return callback(err);
}
callback(null, {posts: posts, nextStart: end + 1});
});
});
});
};
Posts.getFavourites = function(uid, start, end, callback) {
db.getSortedSetRevRange('uid:' + uid + ':favourites', start, end, function(err, pids) {
if (err) {
return callback(err);
}
getPosts(pids, uid, function(err, posts) {
if (err) {
return callback(err);
}
callback(null, {posts: posts, nextStart: end + 1});
});
});
};
function getPosts(pids, uid, callback) {
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
Posts.getPostSummaryByPids(pids, uid, {stripTags: false}, function(err, posts) {
if (err) {
return callback(err);
}
if (!Array.isArray(posts) || !posts.length) {
return callback(null, []);
}
callback(null, posts);
});
}
};

@ -70,7 +70,7 @@ module.exports = function(privileges) {
}; };
privileges.categories.filterCids = function(privilege, cids, uid, callback) { privileges.categories.filterCids = function(privilege, cids, uid, callback) {
if (!cids.length) { if (!Array.isArray(cids) || !cids.length) {
return callback(null, []); return callback(null, []);
} }

@ -72,7 +72,7 @@ module.exports = function(privileges) {
}; };
privileges.posts.filter = function(privilege, pids, uid, callback) { privileges.posts.filter = function(privilege, pids, uid, callback) {
if (!pids.length) { if (!Array.isArray(pids) || !pids.length) {
return callback(null, []); return callback(null, []);
} }
posts.getCidsByPids(pids, function(err, cids) { posts.getCidsByPids(pids, function(err, cids) {

@ -54,7 +54,7 @@ function categoryRoutes(app, middleware, controllers) {
setupPageRoute(app, '/unread', middleware, [middleware.authenticate], controllers.categories.unread); setupPageRoute(app, '/unread', middleware, [middleware.authenticate], controllers.categories.unread);
app.get('/api/unread/total', middleware.authenticate, controllers.categories.unreadTotal); app.get('/api/unread/total', middleware.authenticate, controllers.categories.unreadTotal);
setupPageRoute(app, '/category/:category_id/:slug/:topic_index', middleware, [middleware.applyCSRF, middleware.checkTopicIndex], controllers.categories.get); setupPageRoute(app, '/category/:category_id/:slug/:topic_index', middleware, [middleware.applyCSRF], controllers.categories.get);
setupPageRoute(app, '/category/:category_id/:slug?', middleware, [middleware.applyCSRF, middleware.addSlug], controllers.categories.get); setupPageRoute(app, '/category/:category_id/:slug?', middleware, [middleware.applyCSRF, middleware.addSlug], controllers.categories.get);
} }

@ -6,13 +6,13 @@
"^users/latest": "users", "^users/latest": "users",
"^users/sort-reputation": "users", "^users/sort-reputation": "users",
"^users/search": "users", "^users/search": "users",
"^user.*edit": "account/edit", "^user/.*/edit": "account/edit",
"^user.*following": "account/following", "^user/.*/following": "account/following",
"^user.*followers": "account/followers", "^user/.*/followers": "account/followers",
"^user.*settings": "account/settings", "^user/.*/settings": "account/settings",
"^user.*favourites": "account/favourites", "^user/.*/favourites": "account/favourites",
"^user.*posts": "account/posts", "^user/.*/posts": "account/posts",
"^user.*topics": "account/topics", "^user/.*/topics": "account/topics",
"^user/.*": "account/profile", "^user/.*": "account/profile",
"^reset/.*": "reset_code", "^reset/.*": "reset_code",
"^tags/.*": "tag", "^tags/.*": "tag",

Loading…
Cancel
Save