Files
NodeBB/src/topics/posts.js

319 lines
8.2 KiB
JavaScript
Raw Normal View History

2014-03-21 17:04:15 -04:00
'use strict';
var async = require('async'),
2014-09-06 03:09:41 -04:00
winston = require('winston'),
_ = require('underscore'),
2014-03-21 17:04:15 -04:00
2014-06-12 16:45:00 -04:00
db = require('../database'),
user = require('../user'),
favourites = require('../favourites'),
posts = require('../posts'),
privileges = require('../privileges'),
meta = require('../meta');
2014-03-21 17:04:15 -04:00
module.exports = function(Topics) {
2014-09-19 15:54:13 -04:00
Topics.onNewPostMade = function(postData, callback) {
async.parallel([
function(next) {
Topics.increasePostCount(postData.tid, next);
},
function(next) {
Topics.updateTimestamp(postData.tid, postData.timestamp, next);
},
function(next) {
Topics.addPostToTopic(postData.tid, postData.pid, postData.timestamp, 0, next);
2014-12-12 23:25:16 -05:00
}
2014-09-19 15:54:13 -04:00
], callback);
2014-03-21 17:04:15 -04:00
};
Topics.getTopicPosts = function(tid, set, start, stop, uid, reverse, callback) {
2014-06-23 14:46:47 -04:00
callback = callback || function() {};
async.parallel({
posts: function(next) {
posts.getPostsByTid(tid, set, start, stop, uid, reverse, next);
},
postCount: function(next) {
Topics.getTopicField(tid, 'postcount', next);
}
}, function(err, results) {
2014-11-11 19:47:56 -05:00
if (err) {
2014-03-21 17:04:15 -04:00
return callback(err);
}
var indices = Topics.calculatePostIndices(start, stop, results.postCount, reverse);
results.posts.forEach(function(post, index) {
if (post) {
post.index = indices[index];
}
});
Topics.addPostData(results.posts, uid, callback);
});
};
2014-03-21 17:04:15 -04:00
Topics.addPostData = function(postData, uid, callback) {
2014-11-11 19:47:56 -05:00
if (!Array.isArray(postData) || !postData.length) {
return callback(null, []);
}
var pids = postData.map(function(post) {
2014-08-30 11:56:29 -04:00
return post && post.pid;
2014-08-30 14:42:48 -04:00
});
2014-08-30 11:56:29 -04:00
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
2014-03-21 17:04:15 -04:00
async.parallel({
2014-06-12 16:45:00 -04:00
favourites: function(next) {
favourites.getFavouritesByPostIDs(pids, uid, next);
},
2014-06-12 16:45:00 -04:00
voteData: function(next) {
favourites.getVoteStatusByPostIDs(pids, uid, next);
},
2014-06-12 16:45:00 -04:00
userData: function(next) {
var uids = [];
for(var i=0; i<postData.length; ++i) {
2014-08-30 14:42:48 -04:00
if (postData[i] && uids.indexOf(postData[i].uid) === -1) {
uids.push(postData[i].uid);
}
}
posts.getUserInfoForPosts(uids, uid, function(err, users) {
if (err) {
return next(err);
}
var userData = {};
2014-08-26 13:47:48 -04:00
users.forEach(function(user, index) {
userData[uids[index]] = user;
2014-06-12 16:45:00 -04:00
});
next(null, userData);
});
},
editors: function(next) {
var editors = [];
for(var i=0; i<postData.length; ++i) {
2014-08-30 14:42:48 -04:00
if (postData[i] && postData[i].editor && editors.indexOf(postData[i].editor) === -1) {
editors.push(postData[i].editor);
}
}
user.getMultipleUserFields(editors, ['uid', 'username', 'userslug'], function(err, editors) {
if (err) {
return next(err);
}
var editorData = {};
editors.forEach(function(editor) {
editorData[editor.uid] = editor;
2014-08-30 14:42:48 -04:00
});
next(null, editorData);
});
},
2014-06-12 16:45:00 -04:00
privileges: function(next) {
privileges.posts.get(pids, uid, next);
}
}, function(err, results) {
2015-01-01 13:05:58 -05:00
if (err) {
return callback(err);
}
2015-01-01 13:05:58 -05:00
postData.forEach(function(postObj, i) {
if (postObj) {
postObj.deleted = parseInt(postObj.deleted, 10) === 1;
2015-01-01 13:05:58 -05:00
postObj.user = parseInt(postObj.uid, 10) ? results.userData[postObj.uid] : _.clone(results.userData[postObj.uid]);
postObj.editor = postObj.editor ? results.editors[postObj.editor] : null;
postObj.favourited = results.favourites[i];
postObj.upvoted = results.voteData.upvotes[i];
postObj.downvoted = results.voteData.downvotes[i];
postObj.votes = postObj.votes || 0;
postObj.display_moderator_tools = results.privileges[i].editable;
postObj.display_move_tools = results.privileges[i].move && postObj.index !== 0;
postObj.selfPost = parseInt(uid, 10) === parseInt(postObj.uid, 10);
if(postObj.deleted && !results.privileges[i].view_deleted) {
postObj.content = '[[topic:post_is_deleted]]';
}
// Username override for guests, if enabled
if (parseInt(meta.config.allowGuestHandles, 10) === 1 && parseInt(postObj.uid, 10) === 0 && postObj.handle) {
postObj.user.username = postObj.handle;
2014-08-30 14:42:48 -04:00
}
2014-03-21 17:04:15 -04:00
}
2015-01-01 13:05:58 -05:00
});
2014-06-28 01:14:24 -04:00
callback(null, postData);
2014-03-21 17:04:15 -04:00
});
};
Topics.calculatePostIndices = function(start, stop, postCount, reverse) {
var indices = [];
var count = stop - start + 1;
for(var i=0; i<count; ++i) {
if (reverse) {
indices.push(postCount - (start + i + 1));
} else {
indices.push(start + i + 1);
}
}
return indices;
};
Topics.getLatestUndeletedPid = function(tid, callback) {
Topics.getLatestUndeletedReply(tid, function(err, pid) {
if (err) {
2014-04-24 20:05:05 -04:00
return callback(err);
}
if (parseInt(pid, 10)) {
return callback(null, pid.toString());
}
Topics.getTopicField(tid, 'mainPid', function(err, mainPid) {
callback(err, parseInt(mainPid, 10) ? mainPid.toString() : null);
});
2014-04-24 20:05:05 -04:00
});
};
Topics.getLatestUndeletedReply = function(tid, callback) {
var isDeleted = false;
var done = false;
var latestPid = null;
var index = 0;
async.doWhilst(
function(next) {
db.getSortedSetRevRange('tid:' + tid + ':posts', index, index, function(err, pids) {
if (err) {
return next(err);
}
2014-04-24 20:05:05 -04:00
if (!Array.isArray(pids) || !pids.length) {
done = true;
return next();
}
2014-07-22 17:58:27 -04:00
posts.getPostField(pids[0], 'deleted', function(err, deleted) {
if (err) {
return next(err);
}
latestPid = pids[0];
2015-02-04 15:49:08 -05:00
isDeleted = parseInt(deleted, 10) === 1;
++index;
next();
});
2014-04-24 20:05:05 -04:00
});
},
function() {
return isDeleted && !done;
},
function(err) {
callback(err, latestPid);
}
);
2014-04-24 20:05:05 -04:00
};
Topics.addPostToTopic = function(tid, pid, timestamp, votes, callback) {
Topics.getTopicField(tid, 'mainPid', function(err, mainPid) {
2014-12-11 23:59:23 -05:00
if (err) {
return callback(err);
}
if (!parseInt(mainPid, 10)) {
Topics.setTopicField(tid, 'mainPid', pid, callback);
} else {
async.parallel([
function(next) {
db.sortedSetAdd('tid:' + tid + ':posts', timestamp, pid, next);
},
function(next) {
db.sortedSetAdd('tid:' + tid + ':posts:votes', votes, pid, next);
}
], function(err) {
if (err) {
return callback(err);
}
Topics.updateTeaser(tid, callback);
});
}
});
2014-03-21 17:04:15 -04:00
};
Topics.removePostFromTopic = function(tid, pid, callback) {
db.sortedSetsRemove(['tid:' + tid + ':posts', 'tid:' + tid + ':posts:votes'], pid, function(err) {
if (err) {
return callback(err);
}
Topics.updateTeaser(tid, callback);
2014-12-12 00:45:43 -05:00
});
2014-03-21 17:04:15 -04:00
};
Topics.getPids = function(tid, callback) {
async.parallel({
mainPid: function(next) {
Topics.getTopicField(tid, 'mainPid', next);
},
pids: function(next) {
db.getSortedSetRange('tid:' + tid + ':posts', 0, -1, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
if (results.mainPid) {
results.pids = [results.mainPid].concat(results.pids);
}
callback(null, results.pids);
});
2014-03-21 17:04:15 -04:00
};
Topics.increasePostCount = function(tid, callback) {
incrementFieldAndUpdateSortedSet(tid, 'postcount', 1, 'topics:posts', callback);
};
Topics.decreasePostCount = function(tid, callback) {
incrementFieldAndUpdateSortedSet(tid, 'postcount', -1, 'topics:posts', callback);
};
Topics.increaseViewCount = function(tid, callback) {
incrementFieldAndUpdateSortedSet(tid, 'viewcount', 1, 'topics:views', callback);
};
function incrementFieldAndUpdateSortedSet(tid, field, by, set, callback) {
2014-09-05 20:35:24 -04:00
callback = callback || function() {};
2014-03-21 17:04:15 -04:00
db.incrObjectFieldBy('topic:' + tid, field, by, function(err, value) {
2014-12-25 18:21:06 -05:00
if (err) {
2014-03-21 17:04:15 -04:00
return callback(err);
}
db.sortedSetAdd(set, value, tid, callback);
});
}
Topics.getTitleByPid = function(pid, callback) {
Topics.getTopicFieldByPid('title', pid, callback);
};
Topics.getTopicFieldByPid = function(field, pid, callback) {
posts.getPostField(pid, 'tid', function(err, tid) {
2014-12-25 18:21:06 -05:00
if (err) {
return callback(err);
}
2014-03-21 17:04:15 -04:00
Topics.getTopicField(tid, field, callback);
});
};
Topics.getTopicDataByPid = function(pid, callback) {
posts.getPostField(pid, 'tid', function(err, tid) {
if (err) {
return callback(err);
}
2014-03-21 17:04:15 -04:00
Topics.getTopicData(tid, callback);
});
};
2014-06-02 20:41:03 -04:00
Topics.getPostCount = function(tid, callback) {
2014-10-14 23:12:47 -04:00
db.getObjectField('topic:' + tid, 'postcount', callback);
2014-06-02 20:41:03 -04:00
};
2014-03-21 17:04:15 -04:00
};