Files
NodeBB/src/posts.js

324 lines
8.2 KiB
JavaScript
Raw Normal View History

2014-02-28 00:14:11 -05:00
'use strict';
2016-04-29 20:35:49 +03:00
var async = require('async');
2017-05-26 01:39:40 -06:00
var _ = require('lodash');
2014-06-12 16:45:00 -04:00
2016-04-29 20:35:49 +03:00
var db = require('./database');
var utils = require('./utils');
2016-04-29 20:35:49 +03:00
var user = require('./user');
var topics = require('./topics');
var privileges = require('./privileges');
var plugins = require('./plugins');
2017-04-19 12:33:53 -04:00
var Posts = module.exports;
2014-06-09 12:51:49 -04:00
2017-04-19 12:33:53 -04:00
require('./posts/create')(Posts);
require('./posts/delete')(Posts);
require('./posts/edit')(Posts);
require('./posts/parse')(Posts);
require('./posts/user')(Posts);
require('./posts/topics')(Posts);
require('./posts/category')(Posts);
require('./posts/summary')(Posts);
require('./posts/recent')(Posts);
require('./posts/tools')(Posts);
require('./posts/votes')(Posts);
require('./posts/bookmarks')(Posts);
require('./posts/queue')(Posts);
2018-02-16 16:41:06 -05:00
require('./posts/diffs')(Posts);
2018-04-13 16:12:11 -04:00
require('./posts/uploads')(Posts);
2014-10-31 22:04:09 -04:00
2017-04-19 12:33:53 -04:00
Posts.exists = function (pid, callback) {
db.isSortedSetMember('posts:pid', pid, callback);
};
2015-05-11 15:38:14 -04:00
2017-04-19 12:33:53 -04:00
Posts.getPidsFromSet = function (set, start, stop, reverse, callback) {
if (isNaN(start) || isNaN(stop)) {
return callback(null, []);
}
db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop, callback);
};
2014-01-20 21:00:10 -05:00
2017-04-19 12:33:53 -04:00
Posts.getPostsByPids = function (pids, uid, callback) {
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
2014-01-20 21:00:10 -05:00
2017-04-19 12:33:53 -04:00
async.waterfall([
function (next) {
var keys = pids.map(function (pid) {
return 'post:' + pid;
});
db.getObjects(keys, next);
},
function (posts, next) {
async.map(posts, function (post, next) {
if (!post) {
return next();
2015-04-07 14:15:35 -04:00
}
2017-04-19 12:33:53 -04:00
post.upvotes = parseInt(post.upvotes, 10) || 0;
post.downvotes = parseInt(post.downvotes, 10) || 0;
post.votes = post.upvotes - post.downvotes;
post.timestampISO = utils.toISOString(post.timestamp);
post.editedISO = parseInt(post.edited, 10) !== 0 ? utils.toISOString(post.edited) : '';
Posts.parsePost(post, next);
}, next);
},
async.apply(user.blocks.filter, uid),
2017-04-19 12:33:53 -04:00
function (posts, next) {
plugins.fireHook('filter:post.getPosts', { posts: posts, uid: uid }, next);
},
function (data, next) {
if (!data || !Array.isArray(data.posts)) {
return next(null, []);
}
2017-04-19 12:33:53 -04:00
data.posts = data.posts.filter(Boolean);
next(null, data.posts);
},
], callback);
};
2013-05-24 12:46:19 -04:00
2017-04-19 12:33:53 -04:00
Posts.getPostSummariesFromSet = function (set, uid, start, stop, callback) {
async.waterfall([
function (next) {
db.getSortedSetRevRange(set, start, stop, next);
},
function (pids, next) {
privileges.posts.filter('read', pids, uid, next);
},
function (pids, next) {
Posts.getPostSummaryByPids(pids, uid, { stripTags: false }, next);
},
function (posts, next) {
next(null, { posts: posts, nextStart: stop + 1 });
},
], callback);
};
2014-11-11 18:08:03 -05:00
2017-04-19 12:33:53 -04:00
Posts.getPostData = function (pid, callback) {
async.waterfall([
function (next) {
db.getObject('post:' + pid, next);
},
function (data, next) {
plugins.fireHook('filter:post.getPostData', { post: data }, next);
},
function (data, next) {
next(null, data.post);
},
], callback);
};
2014-11-11 18:08:03 -05:00
2018-05-26 12:49:29 -04:00
Posts.getPostsData = function (pids, callback) {
async.waterfall([
function (next) {
db.getObjects(pids.map(pid => 'post:' + pid), next);
},
function (data, next) {
plugins.fireHook('filter:post.getPostsData', { posts: data }, next);
},
function (data, next) {
next(null, data.posts);
},
], callback);
};
2017-04-19 12:33:53 -04:00
Posts.getPostField = function (pid, field, callback) {
async.waterfall([
function (next) {
Posts.getPostFields(pid, [field], next);
},
function (data, next) {
next(null, data[field]);
},
], callback);
};
2013-11-15 14:57:50 -05:00
2017-04-19 12:33:53 -04:00
Posts.getPostFields = function (pid, fields, callback) {
async.waterfall([
function (next) {
db.getObjectFields('post:' + pid, fields, next);
},
function (data, next) {
2013-11-15 14:57:50 -05:00
data.pid = pid;
2017-04-19 12:33:53 -04:00
plugins.fireHook('filter:post.getFields', { posts: [data], fields: fields }, next);
},
function (data, next) {
next(null, (data && Array.isArray(data.posts) && data.posts.length) ? data.posts[0] : null);
},
], callback);
};
2013-07-02 19:46:58 -04:00
2017-04-19 12:33:53 -04:00
Posts.getPostsFields = function (pids, fields, callback) {
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
2014-10-02 16:00:17 -04:00
2017-04-19 12:33:53 -04:00
var keys = pids.map(function (pid) {
return 'post:' + pid;
});
2017-04-19 12:33:53 -04:00
async.waterfall([
function (next) {
db.getObjectsFields(keys, fields, next);
},
function (posts, next) {
plugins.fireHook('filter:post.getFields', { posts: posts, fields: fields }, next);
},
function (data, next) {
next(null, (data && Array.isArray(data.posts)) ? data.posts : null);
},
], callback);
};
2017-04-19 12:33:53 -04:00
Posts.setPostField = function (pid, field, value, callback) {
async.waterfall([
function (next) {
db.setObjectField('post:' + pid, field, value, next);
},
function (next) {
2014-11-11 18:13:15 -05:00
var data = {
2017-02-17 19:31:21 -07:00
pid: pid,
2014-11-11 18:13:15 -05:00
};
data[field] = value;
2017-02-24 12:47:46 -05:00
plugins.fireHook('action:post.setFields', { data: data });
2017-04-19 12:33:53 -04:00
next();
},
], callback);
};
2013-12-21 19:42:07 -05:00
2017-04-19 12:33:53 -04:00
Posts.setPostFields = function (pid, data, callback) {
async.waterfall([
function (next) {
db.setObject('post:' + pid, data, next);
},
function (next) {
2014-11-11 18:13:15 -05:00
data.pid = pid;
2017-02-24 12:47:46 -05:00
plugins.fireHook('action:post.setFields', { data: data });
2017-04-19 12:33:53 -04:00
next();
},
], callback);
};
2017-04-19 12:33:53 -04:00
Posts.getPidIndex = function (pid, tid, topicPostSort, callback) {
async.waterfall([
function (next) {
var set = topicPostSort === 'most_votes' ? 'tid:' + tid + ':posts:votes' : 'tid:' + tid + ':posts';
db.sortedSetRank(set, pid, next);
},
function (index, next) {
2015-09-14 21:04:56 -04:00
if (!utils.isNumber(index)) {
2017-04-19 12:33:53 -04:00
return next(null, 0);
2014-02-17 20:57:12 -05:00
}
2017-04-19 12:33:53 -04:00
next(null, parseInt(index, 10) + 1);
},
], callback);
};
2017-04-19 12:33:53 -04:00
Posts.getPostIndices = function (posts, uid, callback) {
if (!Array.isArray(posts) || !posts.length) {
return callback(null, []);
}
2017-04-19 12:33:53 -04:00
async.waterfall([
function (next) {
user.getSettings(uid, next);
},
function (settings, next) {
var byVotes = settings.topicPostSort === 'most_votes';
var sets = posts.map(function (post) {
return byVotes ? 'tid:' + post.tid + ':posts:votes' : 'tid:' + post.tid + ':posts';
});
2016-12-02 16:10:07 +03:00
2017-04-19 12:33:53 -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
2017-04-19 12:33:53 -04:00
var pids = posts.map(function (post) {
return post.pid;
});
2016-12-02 16:10:07 +03:00
2017-04-19 12:33:53 -04:00
db[method](sets, pids, next);
},
function (indices, next) {
for (var i = 0; i < indices.length; i += 1) {
indices[i] = utils.isNumber(indices[i]) ? parseInt(indices[i], 10) + 1 : 0;
}
2014-08-16 21:33:42 -04:00
2017-04-19 12:33:53 -04:00
next(null, indices);
},
], callback);
};
2014-08-16 21:33:42 -04:00
2017-04-19 12:33:53 -04:00
Posts.updatePostVoteCount = function (postData, callback) {
if (!postData || !postData.pid || !postData.tid) {
return callback();
}
async.parallel([
function (next) {
if (postData.uid) {
if (postData.votes > 0) {
db.sortedSetAdd('uid:' + postData.uid + ':posts:votes', postData.votes, postData.pid, next);
} else {
2017-04-19 12:33:53 -04:00
db.sortedSetRemove('uid:' + postData.uid + ':posts:votes', postData.pid, next);
}
2017-04-19 12:33:53 -04:00
} else {
next();
}
2017-04-19 12:33:53 -04:00
},
function (next) {
async.waterfall([
function (next) {
2017-12-08 19:58:12 -05:00
topics.getTopicFields(postData.tid, ['mainPid', 'cid'], next);
2017-04-19 12:33:53 -04:00
},
2017-12-08 19:58:12 -05:00
function (topicData, next) {
if (parseInt(topicData.mainPid, 10) === parseInt(postData.pid, 10)) {
async.parallel([
function (next) {
topics.setTopicFields(postData.tid, {
upvotes: postData.upvotes,
downvotes: postData.downvotes,
}, next);
},
function (next) {
db.sortedSetAdd('topics:votes', postData.votes, postData.tid, next);
},
function (next) {
db.sortedSetAdd('cid:' + topicData.cid + ':tids:votes', postData.votes, postData.tid, next);
},
], function (err) {
next(err);
});
return;
2017-04-19 12:33:53 -04:00
}
db.sortedSetAdd('tid:' + postData.tid + ':posts:votes', postData.votes, postData.pid, next);
},
], next);
},
function (next) {
db.sortedSetAdd('posts:votes', postData.votes, postData.pid, next);
},
function (next) {
2017-12-08 19:58:12 -05:00
Posts.setPostFields(postData.pid, {
upvotes: postData.upvotes,
downvotes: postData.downvotes,
}, next);
2017-04-19 12:33:53 -04:00
},
], function (err) {
callback(err);
});
};
Posts.modifyPostByPrivilege = function (post, isAdminOrMod) {
if (post.deleted && !(isAdminOrMod || post.selfPost)) {
post.content = '[[topic:post_is_deleted]]';
if (post.user) {
post.user.signature = '';
}
2017-04-19 12:33:53 -04:00
}
};