Files
NodeBB/src/posts.js

630 lines
16 KiB
JavaScript
Raw Normal View History

2014-02-28 00:14:11 -05:00
'use strict';
2014-06-12 16:45:00 -04:00
var async = require('async'),
path = require('path'),
fs = require('fs'),
nconf = require('nconf'),
_ = require('underscore'),
2014-06-12 16:45:00 -04:00
validator = require('validator'),
winston = require('winston'),
gravatar = require('gravatar'),
S = require('string'),
db = require('./database'),
2014-09-19 14:25:43 -04:00
utils = require('../public/src/utils'),
user = require('./user'),
2014-06-12 16:45:00 -04:00
groups = require('./groups'),
topics = require('./topics'),
favourites = require('./favourites'),
postTools = require('./postTools'),
privileges = require('./privileges'),
categories = require('./categories'),
plugins = require('./plugins'),
meta = require('./meta'),
2014-09-02 05:04:39 -04:00
emitter = require('./emitter'),
websockets = require('./socket.io');
(function(Posts) {
2014-09-03 15:16:41 -04:00
require('./posts/recent')(Posts);
2014-06-09 12:51:49 -04:00
require('./posts/delete')(Posts);
2014-10-07 16:21:12 -04:00
require('./posts/flags')(Posts);
2014-06-09 12:51:49 -04:00
2014-02-22 17:56:13 -05:00
Posts.create = function(data, callback) {
var uid = data.uid,
tid = data.tid,
content = data.content,
2014-07-17 17:48:24 -04:00
timestamp = data.timestamp || Date.now();
2014-02-22 17:56:13 -05:00
2013-11-15 14:57:50 -05:00
if (uid === null) {
2014-04-09 22:26:23 -04:00
return callback(new Error('[[error:invalid-uid]]'));
2013-11-15 14:57:50 -05:00
}
2014-07-17 17:49:28 -04:00
var postData;
async.waterfall([
function(next) {
db.incrObjectField('global', 'nextPid', next);
},
function(pid, next) {
2014-03-01 19:15:18 -05:00
2014-07-17 17:49:28 -04:00
postData = {
2014-03-01 19:15:18 -05:00
'pid': pid,
'uid': uid,
'tid': tid,
'content': content,
'timestamp': timestamp,
'reputation': 0,
'votes': 0,
'editor': '',
'edited': 0,
'deleted': 0
};
2014-07-17 17:48:24 -04:00
if (data.toPid) {
postData.toPid = data.toPid;
2014-02-22 17:56:13 -05:00
}
2014-03-01 19:15:18 -05:00
plugins.fireHook('filter:post.save', postData, next);
},
function(postData, next) {
db.setObject('post:' + postData.pid, postData, next);
},
2014-09-03 19:38:48 -04:00
function(next) {
2014-09-19 14:25:43 -04:00
async.parallel([
2014-09-19 15:54:13 -04:00
function(next) {
user.onNewPostMade(postData, next);
},
function(next) {
topics.onNewPostMade(postData, next);
},
function(next) {
categories.onNewPostMade(postData, next);
},
2014-09-19 14:25:43 -04:00
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);
2013-11-15 14:57:50 -05:00
};
Posts.getPostsByTid = function(tid, set, start, end, uid, reverse, callback) {
Posts.getPidsFromSet(set, start, end, reverse, function(err, pids) {
2013-12-02 21:20:55 -05:00
if(err) {
return callback(err);
}
2013-05-24 12:46:19 -04:00
2014-03-01 19:15:18 -05:00
if(!Array.isArray(pids) || !pids.length) {
2013-12-11 16:08:20 -05:00
return callback(null, []);
}
Posts.getPostsByPids(pids, uid, callback);
2013-05-24 12:46:19 -04:00
});
};
2013-08-20 12:11:17 -04:00
Posts.getPidsFromSet = function(set, start, end, reverse, callback) {
if (isNaN(start) || isNaN(end)) {
return callback(null, []);
}
db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, end, callback);
};
Posts.getPostsByPids = function(pids, uid, callback) {
2014-01-20 21:00:10 -05:00
var keys = [];
for(var x=0, numPids=pids.length; x<numPids; ++x) {
keys.push('post:' + pids[x]);
}
db.getObjects(keys, function(err, data) {
if(err) {
return callback(err);
}
async.map(data, function(postData, next) {
if(!postData) {
return next(null);
}
postData.relativeTime = utils.toISOString(postData.timestamp);
postData.relativeEditTime = parseInt(postData.edited, 10) !== 0 ? utils.toISOString(postData.edited) : '';
postTools.parse(postData.content, function(err, content) {
if(err) {
return next(err);
}
postData.content = content;
next(null, postData);
});
}, function(err, posts) {
if (err) {
return callback(err);
}
2014-09-17 18:10:19 -04:00
plugins.fireHook('filter:post.getPosts', {posts: posts, uid: uid}, function(err, data) {
if (err) {
return callback(err);
}
if (!data || !Array.isArray(data.posts)) {
return callback(null, []);
}
2014-08-30 14:42:48 -04:00
data.posts = data.posts.filter(Boolean);
callback(null, data.posts);
});
});
2014-01-20 21:00:10 -05:00
});
};
Posts.getUserInfoForPosts = function(uids, callback) {
async.parallel({
groups: function(next) {
groups.getUserGroups(uids, next);
},
userData: function(next) {
2014-09-02 05:04:39 -04:00
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) {
2013-12-06 21:08:21 -05:00
return callback(err);
}
var userData = results.userData;
for(var i=0; i<userData.length; ++i) {
userData[i].groups = results.groups[i];
2014-10-09 16:16:28 -04:00
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) {
return next();
}
postTools.parseSignature(userData.signature, 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.signature = results.signature;
userData.custom_profile_info = results.customProfileInfo.profile;
plugins.fireHook('filter:posts.modifyUserInfo', userData, next);
});
}, callback);
2013-07-02 19:46:58 -04:00
});
};
2014-08-16 21:33:42 -04:00
Posts.getPostSummaryByPids = function(pids, uid, options, callback) {
2014-07-07 17:36:10 -04:00
options.stripTags = options.hasOwnProperty('stripTags') ? options.stripTags : false;
options.parse = options.hasOwnProperty('parse') ? options.parse : true;
2014-10-07 16:21:12 -04:00
options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : [];
2013-08-20 12:11:17 -04:00
2014-09-16 16:10:02 -04:00
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
2014-07-29 17:33:28 -04:00
var keys = pids.map(function(pid) {
return 'post:' + pid;
});
2014-10-07 16:21:12 -04:00
var fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted'].concat(options.extraFields);
db.getObjectsFields(keys, fields, function(err, posts) {
2014-07-29 17:33:28 -04:00
if (err) {
return callback(err);
}
2014-07-29 17:33:28 -04:00
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);
}
}
2014-04-20 15:07:53 -04:00
async.parallel({
2014-07-29 17:33:28 -04:00
users: function(next) {
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
},
2014-07-29 17:33:28 -04:00
topicsAndCategories: function(next) {
2014-08-18 19:04:49 -04:00
db.getObjectsFields(tids, ['uid', 'tid', 'title', 'cid', 'slug', 'deleted'], function(err, topics) {
2013-12-02 13:28:46 -05:00
if (err) {
2014-03-20 13:18:12 -04:00
return next(err);
2013-12-02 13:28:46 -05:00
}
2014-03-20 13:18:12 -04:00
var cids = topics.map(function(topic) {
if (topic) {
topic.title = validator.escape(topic.title);
}
return topic && topic.cid;
2014-07-29 17:33:28 -04:00
}).filter(function(value, index, array) {
return value && array.indexOf(value) === index;
2014-07-29 17:33:28 -04:00
});
2014-04-20 15:07:53 -04:00
categories.getMultipleCategoryFields(cids, ['cid', 'name', 'icon', 'slug'], function(err, categories) {
2014-07-29 17:33:28 -04:00
next(err, {topics: topics, categories: categories});
2014-02-28 00:14:11 -05:00
});
2013-08-01 17:27:37 -04:00
});
},
2014-07-29 17:33:28 -04:00
indices: function(next) {
2014-08-16 21:33:42 -04:00
Posts.getPostIndices(posts, uid, next);
2014-04-20 15:07:53 -04:00
}
}, function(err, results) {
2014-07-29 17:33:28 -04:00
function toObject(key, data) {
var obj = {};
for(var i=0; i<data.length; ++i) {
obj[data[i][key]] = data[i];
}
return obj;
}
2014-08-23 22:34:39 -04:00
function stripTags(content) {
if (options.stripTags && content) {
var s = S(content);
return s.stripTags.apply(s, utils.stripTags).s;
}
return content;
}
2014-04-20 15:07:53 -04:00
if (err) {
return callback(err);
}
2014-07-29 17:33:28 -04:00
results.users = toObject('uid', results.users);
results.topics = toObject('tid', results.topicsAndCategories.topics);
results.categories = toObject('cid', results.topicsAndCategories.categories);
2013-12-06 21:08:21 -05:00
2014-07-30 18:40:00 -04:00
for (var i=0; i<posts.length; ++i) {
2014-08-16 21:33:42 -04:00
posts[i].index = utils.isNumber(results.indices[i]) ? parseInt(results.indices[i], 10) + 1 : 1;
}
2014-04-20 15:07:53 -04:00
2014-07-30 18:40:00 -04:00
posts = posts.filter(function(post) {
2014-09-10 18:48:43 -04:00
return results.topics[post.tid] && parseInt(results.topics[post.tid].deleted, 10) !== 1;
2014-07-30 18:40:00 -04:00
});
2013-08-20 12:11:17 -04:00
2014-07-30 18:40:00 -04:00
async.map(posts, function(post, next) {
2014-07-29 17:33:28 -04:00
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);
2014-07-29 17:33:28 -04:00
if (!post.content || !options.parse) {
2014-08-23 22:34:39 -04:00
post.content = stripTags(post.content);
2014-07-29 17:33:28 -04:00
return next(null, post);
}
2014-07-29 17:33:28 -04:00
postTools.parse(post.content, function(err, content) {
if (err) {
return next(err);
}
2014-03-20 13:18:12 -04:00
2014-08-23 22:34:39 -04:00
post.content = stripTags(content);
2014-03-20 13:18:12 -04:00
2014-07-29 17:33:28 -04:00
next(null, post);
});
2014-09-23 13:58:03 -04:00
}, function(err, posts) {
2014-09-23 14:00:28 -04:00
plugins.fireHook('filter:post.getPostSummaryByPids', {posts: posts, uid: uid}, function(err, postData) {
callback(err, postData.posts);
});
2014-09-23 13:58:03 -04:00
});
2014-03-20 13:18:12 -04:00
});
2013-07-15 14:34:15 -04:00
});
2013-05-24 12:46:19 -04:00
};
2013-07-02 16:24:13 -04:00
Posts.getPostData = function(pid, callback) {
2013-12-02 21:20:55 -05:00
db.getObject('post:' + pid, function(err, data) {
if(err) {
2014-02-08 13:44:15 -05:00
return callback(err);
}
2014-02-08 13:44:15 -05:00
plugins.fireHook('filter:post.get', data, callback);
2013-07-02 16:24:13 -04:00
});
2013-12-21 19:42:07 -05:00
};
2013-05-24 12:46:19 -04:00
2013-07-22 17:08:07 -04:00
Posts.getPostFields = function(pid, fields, callback) {
2013-12-02 21:20:55 -05:00
db.getObjectFields('post:' + pid, fields, function(err, data) {
2013-11-15 14:57:50 -05:00
if(err) {
2014-02-08 13:44:15 -05:00
return callback(err);
2013-07-22 17:08:07 -04:00
}
2013-11-15 14:57:50 -05:00
// TODO: I think the plugins system needs an optional 'parameters' paramter so I don't have to do this:
data = data || {};
data.pid = pid;
data.fields = fields;
2014-02-08 13:44:15 -05:00
plugins.fireHook('filter:post.getFields', data, callback);
2013-08-20 12:11:17 -04:00
});
2013-12-21 19:42:07 -05:00
};
2013-07-02 19:46:58 -04:00
Posts.getPostsFields = function(pids, fields, callback) {
2014-10-02 19:03:03 -04:00
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
2014-10-02 16:00:17 -04:00
var keys = pids.map(function(pid) {
return 'post:' + pid;
});
db.getObjectsFields(keys, fields, callback);
};
Posts.getPostField = function(pid, field, callback) {
2013-11-15 14:57:50 -05:00
Posts.getPostFields(pid, [field], function(err, data) {
if(err) {
2014-02-08 13:44:15 -05:00
return callback(err);
}
2013-11-15 14:57:50 -05:00
callback(null, data[field]);
});
2013-12-21 19:42:07 -05:00
};
2013-11-27 15:03:36 -05:00
Posts.setPostField = function(pid, field, value, callback) {
2013-12-02 21:20:55 -05:00
db.setObjectField('post:' + pid, field, value, callback);
plugins.fireHook('action:post.setField', {
'pid': pid,
'field': field,
'value': value
2013-11-27 15:03:36 -05:00
});
2013-12-21 19:42:07 -05:00
};
Posts.setPostFields = function(pid, data, callback) {
db.setObject('post:' + pid, data, callback);
};
2013-11-15 14:57:50 -05:00
Posts.getCidByPid = function(pid, callback) {
Posts.getPostField(pid, 'tid', function(err, tid) {
if(err) {
2014-02-08 13:44:15 -05:00
return callback(err);
2013-07-02 16:24:13 -04:00
}
2013-11-15 14:57:50 -05:00
topics.getTopicField(tid, 'cid', function(err, cid) {
2014-03-13 20:24:04 -04:00
if(err || !cid) {
2014-04-09 22:26:23 -04:00
return callback(err || new Error('[[error:invalid-cid]]'));
2013-11-15 14:57:50 -05:00
}
2014-03-13 20:24:04 -04:00
callback(null, cid);
2013-11-15 14:57:50 -05:00
});
2013-07-02 16:24:13 -04:00
});
2014-02-28 00:14:11 -05:00
};
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;
2014-09-06 00:58:03 -04:00
}).filter(function(tid, index, array) {
2014-09-07 12:36:50 -04:00
return tid && array.indexOf(tid) === index;
});
topics.getTopicsFields(tids, ['cid'], function(err, topics) {
if (err) {
return callback(err);
}
2014-09-07 12:36:50 -04:00
2014-09-06 00:58:03 -04:00
var map = {};
2014-09-07 12:36:50 -04:00
topics.forEach(function(topic, index) {
2014-09-06 00:58:03 -04:00
if (topic) {
2014-09-07 12:36:50 -04:00
map[tids[index]] = topic.cid;
2014-09-06 00:58:03 -04:00
}
});
2014-09-06 00:58:03 -04:00
var cids = posts.map(function(post) {
return map[post.tid];
2014-09-19 14:25:43 -04:00
});
2014-09-06 00:58:03 -04:00
callback(null, cids);
});
});
};
2014-05-20 17:46:13 -04:00
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});
});
2014-05-20 17:46:13 -04:00
});
});
};
Posts.getFavourites = function(uid, start, end, callback) {
db.getSortedSetRevRange('uid:' + uid + ':favourites', start, end, function(err, pids) {
2014-01-20 21:00:10 -05:00
if (err) {
return callback(err);
2014-01-20 21:00:10 -05:00
}
2013-08-20 12:11:17 -04:00
getPosts(pids, uid, function(err, posts) {
if (err) {
return callback(err);
}
callback(null, {posts: posts, nextStart: end + 1});
});
2014-05-20 17:46:13 -04:00
});
};
function getPosts(pids, uid, callback) {
2014-05-20 17:46:13 -04:00
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
2014-05-20 17:46:13 -04:00
}
2014-08-16 21:33:42 -04:00
Posts.getPostSummaryByPids(pids, uid, {stripTags: false}, function(err, posts) {
2014-05-20 17:46:13 -04:00
if (err) {
return callback(err);
}
if (!Array.isArray(posts) || !posts.length) {
return callback(null, []);
2014-05-20 17:46:13 -04:00
}
callback(null, posts);
2013-08-20 12:11:17 -04:00
});
2014-05-20 17:46:13 -04:00
}
2013-08-20 12:11:17 -04:00
2014-08-17 00:14:45 -04:00
Posts.getPidIndex = function(pid, uid, callback) {
async.parallel({
settings: function(next) {
2014-09-10 02:00:24 -04:00
user.getSettings(uid, next);
2014-08-17 00:14:45 -04:00
},
tid: function(next) {
Posts.getPostField(pid, 'tid', next);
}
}, function(err, results) {
2014-02-17 20:57:12 -05:00
if(err) {
return callback(err);
}
2014-08-17 00:14:45 -04:00
var set = results.settings.topicPostSort === 'most_votes' ? 'tid:' + results.tid + ':posts:votes' : 'tid:' + results.tid + ':posts';
db.sortedSetRank(set, pid, function(err, index) {
2014-06-13 15:33:22 -04:00
if (!utils.isNumber(index)) {
return callback(err, 1);
}
callback(err, parseInt(index, 10) + 2);
});
2014-02-17 20:57:12 -05:00
});
2014-02-28 00:14:11 -05:00
};
2014-08-16 21:33:42 -04:00
Posts.getPostIndices = function(posts, uid, callback) {
2014-09-21 13:30:20 -04:00
if (!Array.isArray(posts) || !posts.length) {
return callback(null, []);
}
2014-08-16 21:33:42 -04:00
user.getSettings(uid, function(err, settings) {
if (err) {
return callback(err);
}
2014-08-16 21:33:42 -04:00
var byVotes = settings.topicPostSort === 'most_votes';
var sets = posts.map(function(post) {
return byVotes ? 'tid:' + post.tid + ':posts:votes' : 'tid:' + post.tid + ':posts';
2014-08-16 21:33:42 -04:00
});
var uniqueSets = _.uniq(sets);
var method = 'sortedSetsRanks';
if (uniqueSets.length === 1) {
method = 'sortedSetRanks';
sets = uniqueSets[0];
}
2014-08-16 21:33:42 -04:00
var pids = posts.map(function(post) {
return post.pid;
});
db[method](sets, pids, function(err, indices) {
2014-08-16 21:33:42 -04:00
if (err) {
return callback(err);
}
for (var i=0; i<indices.length; ++i) {
indices[i] = utils.isNumber(indices[i]) ? parseInt(indices[i], 10) + 1 : 0;
}
callback(null, indices);
});
});
};
Posts.isOwner = function(pid, uid, callback) {
uid = parseInt(uid, 10);
if (Array.isArray(pid)) {
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 {
Posts.getPostField(pid, 'uid', function(err, author) {
callback(err, parseInt(author, 10) === uid);
});
}
};
Posts.isMain = function(pid, callback) {
Posts.getPostField(pid, 'tid', function(err, tid) {
if (err) {
return callback(err);
}
topics.getTopicField(tid, 'mainPid', function(err, mainPid) {
callback(err, parseInt(pid, 10) === parseInt(mainPid, 10));
});
});
};
Posts.updatePostVoteCount = function(pid, voteCount, callback) {
async.parallel([
function(next) {
Posts.getPostField(pid, 'tid', function(err, tid) {
if (err) {
return next(err);
}
topics.getTopicField(tid, 'mainPid', function(err, mainPid) {
if (err) {
return next(err);
}
if (parseInt(mainPid, 10) === parseInt(pid, 10)) {
return next();
}
db.sortedSetAdd('tid:' + tid + ':posts:votes', voteCount, pid, next);
});
});
},
function(next) {
Posts.setPostField(pid, 'votes', voteCount, next);
}
], callback);
};
}(exports));