Files
NodeBB/src/posts.js

243 lines
5.8 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'),
_ = require('underscore'),
2014-06-12 16:45:00 -04:00
db = require('./database'),
2014-09-19 14:25:43 -04:00
utils = require('../public/src/utils'),
user = require('./user'),
topics = require('./topics'),
postTools = require('./postTools'),
2014-11-09 01:12:24 -05:00
plugins = require('./plugins');
(function(Posts) {
2014-11-09 01:12:24 -05:00
require('./posts/create')(Posts);
2014-06-09 12:51:49 -04:00
require('./posts/delete')(Posts);
2014-11-09 01:12:24 -05:00
require('./posts/user')(Posts);
require('./posts/category')(Posts);
require('./posts/summary')(Posts);
require('./posts/recent')(Posts);
2014-10-07 16:21:12 -04:00
require('./posts/flags')(Posts);
2014-06-09 12:51:49 -04:00
2014-10-31 22:04:09 -04:00
Posts.exists = function(pid, callback) {
db.isSortedSetMember('posts:pid', pid, callback);
};
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.parsePost(postData, uid, next);
}, 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
});
};
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) {
2014-11-11 18:05:42 -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
data.pid = pid;
2014-11-11 18:05:42 -05:00
plugins.fireHook('filter:post.getFields', {post: data, fields: fields}, 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);
};
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.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));