Merge branch 'master' of github.com:NodeBB/NodeBB
commit
c7e731f4c4
@ -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);
|
||||
});
|
||||
});
|
||||
};
|
||||
};
|
@ -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);
|
||||
};
|
||||
|
||||
};
|
@ -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);
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
};
|
Loading…
Reference in New Issue