You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/topics.js

648 lines
17 KiB
JavaScript

"use strict";
11 years ago
var async = require('async'),
gravatar = require('gravatar'),
path = require('path'),
11 years ago
nconf = require('nconf'),
validator = require('validator'),
S = require('string'),
winston = require('winston'),
11 years ago
11 years ago
db = require('./database'),
posts = require('./posts'),
utils = require('./../public/src/utils'),
plugins = require('./plugins'),
user = require('./user'),
categories = require('./categories'),
11 years ago
categoryTools = require('./categoryTools'),
posts = require('./posts'),
threadTools = require('./threadTools'),
postTools = require('./postTools'),
notifications = require('./notifications'),
favourites = require('./favourites'),
11 years ago
meta = require('./meta'),
11 years ago
Plugins = require('./plugins'),
emitter = require('./emitter');
(function(Topics) {
require('./topics/create')(Topics);
require('./topics/unread')(Topics);
require('./topics/fork')(Topics);
11 years ago
Topics.getTopicData = function(tid, callback) {
Topics.getTopicsData([tid], function(err, topics) {
if (err) {
return callback(err);
}
callback(null, topics ? topics[0] : null);
});
};
Topics.getTopicsData = function(tids, callback) {
var keys = [];
for (var i=0; i<tids.length; ++i) {
keys.push('topic:' + tids[i]);
}
db.getObjects(keys, function(err, topics) {
if (err) {
return callback(err);
}
for (var i=0; i<tids.length; ++i) {
if(topics[i]) {
topics[i].title = validator.escape(topics[i].title);
topics[i].relativeTime = utils.toISOString(topics[i].timestamp);
}
}
callback(null, topics);
});
};
Topics.getTopicDataWithUser = function(tid, callback) {
Topics.getTopicData(tid, function(err, topic) {
if (err || !topic) {
return callback(err || new Error('topic doesn\'t exist'));
}
user.getUserFields(topic.uid, ['username', 'userslug', 'picture'] , function(err, userData) {
if (err) {
return callback(err);
}
if (!userData) {
userData = {};
}
topic.user = {
11 years ago
username: userData.username || 'Anonymous',
userslug: userData.userslug || '',
picture: userData.picture || gravatar.url('', {}, true)
};
callback(null, topic);
12 years ago
});
});
};
11 years ago
Topics.getTopicPosts = function(tid, start, end, uid, reverse, callback) {
posts.getPostsByTid(tid, start, end, reverse, function(err, postData) {
if(err) {
return callback(err);
}
if (Array.isArray(postData) && !postData.length) {
return callback(null, []);
}
for(var i=0; i<postData.length; ++i) {
postData[i].index = start + i;
}
var pids = postData.map(function(post) {
return post.pid;
});
12 years ago
11 years ago
async.parallel({
favourites : function(next) {
favourites.getFavouritesByPostIDs(pids, uid, next);
},
voteData : function(next) {
favourites.getVoteStatusByPostIDs(pids, uid, next);
},
userData : function(next) {
async.each(postData, posts.addUserInfoToPost, next);
},
privileges : function(next) {
async.map(pids, function (pid, next) {
postTools.privileges(pid, uid, next);
}, next);
}
11 years ago
}, function(err, results) {
if(err) {
return callback(err);
}
for (var i = 0; i < postData.length; ++i) {
11 years ago
postData[i].favourited = results.favourites[i];
postData[i].upvoted = results.voteData[i].upvoted;
postData[i].downvoted = results.voteData[i].downvoted;
postData[i].votes = postData[i].votes || 0;
postData[i].display_moderator_tools = parseInt(uid, 10) !== 0 && results.privileges[i].editable;
11 years ago
postData[i].display_move_tools = results.privileges[i].move;
11 years ago
if(parseInt(postData[i].deleted, 10) === 1 && !results.privileges[i].view_deleted) {
postData[i].content = 'This post is deleted!';
}
12 years ago
}
callback(null, postData);
});
12 years ago
});
};
Topics.getPageCount = function(tid, uid, callback) {
db.sortedSetCard('tid:' + tid + ':posts', function(err, postCount) {
if(err) {
return callback(err);
}
if(!parseInt(postCount, 10)) {
return callback(null, 1);
}
user.getSettings(uid, function(err, settings) {
if(err) {
return callback(err);
}
callback(null, Math.ceil(parseInt(postCount, 10) / settings.postsPerPage));
});
});
};
11 years ago
Topics.getTidPage = function(tid, uid, callback) {
if(!tid) {
return callback(new Error('invalid-tid'));
}
async.parallel({
index: function(next) {
categories.getTopicIndex(tid, next);
},
settings: function(next) {
user.getSettings(uid, next);
}
}, function(err, results) {
if(err) {
return callback(err);
}
callback(null, Math.ceil((results.index + 1) / results.settings.topicsPerPage));
});
};
Topics.getCategoryData = function(tid, callback) {
Topics.getTopicField(tid, 'cid', function(err, cid) {
if(err) {
callback(err);
}
categories.getCategoryData(cid, callback);
});
};
11 years ago
function getTopics(set, uid, tids, callback) {
var returnTopics = {
topics: [],
nextStart: 0
11 years ago
};
if (!tids || !tids.length) {
return callback(null, returnTopics);
11 years ago
}
async.filter(tids, function(tid, next) {
threadTools.privileges(tid, uid, function(err, privileges) {
next(!err && privileges.read);
});
}, function(tids) {
Topics.getTopicsByTids(tids, uid, function(err, topicData) {
11 years ago
if(err) {
return callback(err);
}
if(!topicData || !topicData.length) {
return callback(null, returnTopics);
11 years ago
}
db.sortedSetRevRank(set, topicData[topicData.length - 1].tid, function(err, rank) {
if(err) {
return callback(err);
11 years ago
}
returnTopics.nextStart = parseInt(rank, 10) + 1;
returnTopics.topics = topicData;
callback(null, returnTopics);
11 years ago
});
});
});
}
11 years ago
Topics.getLatestTids = function(start, end, term, callback) {
var terms = {
day: 86400000,
week: 604800000,
month: 2592000000
};
var since = terms.day;
11 years ago
if(terms[term]) {
since = terms[term];
11 years ago
}
11 years ago
var count = parseInt(end, 10) === -1 ? end : end - start + 1;
db.getSortedSetRevRangeByScore(['topics:recent', '+inf', Date.now() - since, 'LIMIT', start, count], callback);
};
Topics.getLatestTopics = function(uid, start, end, term, callback) {
Topics.getLatestTids(start, end, term, function(err, tids) {
if(err) {
11 years ago
return callback(err);
}
11 years ago
getTopics('topics:recent', uid, tids, callback);
11 years ago
});
};
11 years ago
Topics.getTopicsFromSet = function(uid, set, start, end, callback) {
db.getSortedSetRevRange(set, start, end, function(err, tids) {
if(err) {
return callback(err);
}
11 years ago
getTopics(set, uid, tids, callback);
});
};
Topics.getTopicsByTids = function(tids, uid, callback) {
if (!Array.isArray(tids) || tids.length === 0) {
11 years ago
return callback(null, []);
}
var categoryCache = {},
privilegeCache = {},
userCache = {};
function loadTopicInfo(topicData, next) {
if (!topicData) {
return next(null, null);
}
function isTopicVisible(topicData, topicInfo) {
var deleted = parseInt(topicData.deleted, 10) !== 0;
return !deleted || (deleted && topicInfo.privileges.view_deleted) || parseInt(topicData.uid, 10) === parseInt(uid, 10);
}
async.parallel({
hasread: function(next) {
Topics.hasReadTopic(topicData.tid, uid, next);
},
teaser: function(next) {
Topics.getTeaser(topicData.tid, next);
},
privileges: function(next) {
if (privilegeCache[topicData.cid]) {
return next(null, privilegeCache[topicData.cid]);
}
categoryTools.privileges(topicData.cid, uid, next);
},
categoryData: function(next) {
if (categoryCache[topicData.cid]) {
return next(null, categoryCache[topicData.cid]);
}
categories.getCategoryFields(topicData.cid, ['name', 'slug', 'icon', 'bgColor', 'color'], next);
},
user: function(next) {
if (userCache[topicData.uid]) {
return next(null, userCache[topicData.uid]);
}
user.getUserFields(topicData.uid, ['username', 'userslug', 'picture'], next);
}
}, function(err, topicInfo) {
if(err) {
return next(err);
}
privilegeCache[topicData.cid] = topicInfo.privileges;
categoryCache[topicData.cid] = topicInfo.categoryData;
userCache[topicData.uid] = topicInfo.user;
if (!topicInfo.teaser) {
return next(null, null);
}
if (!isTopicVisible(topicData, topicInfo)) {
11 years ago
return next(null, null);
}
topicData.pinned = parseInt(topicData.pinned, 10) === 1;
topicData.locked = parseInt(topicData.locked, 10) === 1;
topicData.deleted = parseInt(topicData.deleted, 10) === 1;
topicData.unread = !(topicInfo.hasread && parseInt(uid, 10) !== 0);
topicData.unreplied = parseInt(topicData.postcount, 10) === 1;
topicData.category = topicInfo.categoryData;
topicData.teaser = topicInfo.teaser;
topicData.user = topicInfo.user;
11 years ago
next(null, topicData);
});
}
Topics.getTopicsData(tids, function(err, topics) {
if (err) {
11 years ago
return callback(err);
}
11 years ago
async.mapSeries(topics, loadTopicInfo, function(err, topics) {
if(err) {
return callback(err);
}
11 years ago
topics = topics.filter(function(topic) {
return !!topic;
});
callback(null, topics);
});
11 years ago
});
};
Topics.getTopicWithPosts = function(tid, uid, start, end, callback) {
threadTools.exists(tid, function(err, exists) {
if (err || !exists) {
return callback(err || new Error('Topic tid \'' + tid + '\' not found'));
}
async.parallel({
topicData : function(next) {
Topics.getTopicData(tid, next);
},
posts : function(next) {
Topics.getTopicPosts(tid, start, end, uid, false, next);
},
privileges : function(next) {
threadTools.privileges(tid, uid, next);
},
category : function(next) {
Topics.getCategoryData(tid, next);
},
pageCount : function(next) {
Topics.getPageCount(tid, uid, next);
},
threadTools : function(next) {
Plugins.fireHook('filter:topic.thread_tools', [], next);
}
}, function(err, results) {
if (err) {
winston.error('[Topics.getTopicWithPosts] Could not retrieve topic data: ', err.message);
return callback(err);
}
var topicData = results.topicData;
topicData.category = results.category;
topicData.posts = results.posts;
topicData.thread_tools = results.threadTools;
topicData.pageCount = results.pageCount;
topicData.unreplied = parseInt(topicData.postcount, 10) === 1;
topicData.expose_tools = results.privileges.editable ? 1 : 0;
callback(null, topicData);
});
});
};
Topics.getAllTopics = function(start, end, callback) {
db.getSortedSetRevRange('topics:tid', start, end, function(err, tids) {
if(err) {
return callback(err);
}
async.map(tids, function(tid, next) {
Topics.getTopicDataWithUser(tid, next);
}, callback);
});
};
Topics.getTitleByPid = function(pid, callback) {
Topics.getTopicFieldByPid('title', pid, callback);
};
Topics.getTopicFieldByPid = function(field, pid, callback) {
posts.getPostField(pid, 'tid', function(err, tid) {
Topics.getTopicField(tid, field, callback);
});
};
Topics.getTopicDataByPid = function(pid, callback) {
posts.getPostField(pid, 'tid', function(err, tid) {
Topics.getTopicData(tid, callback);
});
};
12 years ago
Topics.getTeasers = function(tids, callback) {
if(!Array.isArray(tids)) {
return callback(null, []);
}
async.map(tids, Topics.getTeaser, callback);
};
12 years ago
Topics.getTeaser = function(tid, callback) {
threadTools.getLatestUndeletedPid(tid, function(err, pid) {
if (err) {
return callback(err);
}
if (!pid) {
return callback(null, null);
}
posts.getPostFields(pid, ['pid', 'uid', 'timestamp'], function(err, postData) {
if (err) {
return callback(err);
} else if(!postData) {
return callback(new Error('no-teaser-found'));
}
user.getUserFields(postData.uid, ['username', 'userslug', 'picture'], function(err, userData) {
if (err) {
return callback(err);
}
callback(null, {
pid: postData.pid,
username: userData.username || 'anonymous',
userslug: userData.userslug || '',
11 years ago
picture: userData.picture || gravatar.url('', {}, true),
timestamp: utils.toISOString(postData.timestamp)
});
});
});
});
};
Topics.getTopicField = function(tid, field, callback) {
11 years ago
db.getObjectField('topic:' + tid, field, callback);
};
Topics.getTopicFields = function(tid, fields, callback) {
11 years ago
db.getObjectFields('topic:' + tid, fields, callback);
};
11 years ago
Topics.setTopicField = function(tid, field, value, callback) {
11 years ago
db.setObjectField('topic:' + tid, field, value, callback);
};
11 years ago
Topics.increasePostCount = function(tid, callback) {
11 years ago
incrementFieldAndUpdateSortedSet(tid, 'postcount', 1, 'topics:posts', callback);
};
Topics.decreasePostCount = function(tid, callback) {
11 years ago
incrementFieldAndUpdateSortedSet(tid, 'postcount', -1, 'topics:posts', callback);
};
11 years ago
Topics.increaseViewCount = function(tid, callback) {
11 years ago
incrementFieldAndUpdateSortedSet(tid, 'viewcount', 1, 'topics:views', callback);
};
function incrementFieldAndUpdateSortedSet(tid, field, by, set, callback) {
db.incrObjectFieldBy('topic:' + tid, field, by, function(err, value) {
11 years ago
if(err) {
return callback(err);
}
11 years ago
db.sortedSetAdd(set, value, tid, callback);
11 years ago
});
11 years ago
}
Topics.isLocked = function(tid, callback) {
Topics.getTopicField(tid, 'locked', function(err, locked) {
if(err) {
11 years ago
return callback(err);
}
11 years ago
callback(null, parseInt(locked, 10) === 1);
});
};
12 years ago
Topics.updateTimestamp = function(tid, timestamp) {
11 years ago
db.sortedSetAdd('topics:recent', timestamp, tid);
Topics.setTopicField(tid, 'lastposttime', timestamp);
};
11 years ago
Topics.onNewPostMade = function(postData) {
Topics.increasePostCount(postData.tid);
Topics.updateTimestamp(postData.tid, postData.timestamp);
Topics.addPostToTopic(postData.tid, postData.pid, postData.timestamp);
};
11 years ago
11 years ago
emitter.on('event:newpost', Topics.onNewPostMade);
11 years ago
Topics.addPostToTopic = function(tid, pid, timestamp, callback) {
db.sortedSetAdd('tid:' + tid + ':posts', timestamp, pid, callback);
};
12 years ago
11 years ago
Topics.removePostFromTopic = function(tid, pid, callback) {
db.sortedSetRemove('tid:' + tid + ':posts', pid, callback);
};
12 years ago
Topics.getPids = function(tid, callback) {
db.getSortedSetRange('tid:' + tid + ':posts', 0, -1, callback);
};
12 years ago
12 years ago
Topics.getUids = function(tid, callback) {
var uids = {};
Topics.getPids(tid, function(err, pids) {
function getUid(pid, next) {
posts.getPostField(pid, 'uid', function(err, uid) {
11 years ago
if (err) {
12 years ago
return next(err);
11 years ago
}
12 years ago
uids[uid] = 1;
11 years ago
next();
12 years ago
});
}
async.each(pids, getUid, function(err) {
11 years ago
if (err) {
return callback(err);
}
12 years ago
callback(null, Object.keys(uids));
});
});
};
12 years ago
11 years ago
Topics.updateTopicCount = function(callback) {
db.sortedSetCard('topics:recent', function(err, count) {
if(err) {
return callback(err);
}
db.setObjectField('global', 'topicCount', count, callback);
11 years ago
});
};
Topics.delete = function(tid, callback) {
async.parallel([
function(next) {
Topics.setTopicField(tid, 'deleted', 1, next);
},
function(next) {
db.sortedSetRemove('topics:recent', tid, next);
},
function(next) {
db.sortedSetRemove('topics:posts', tid, next);
},
function(next) {
db.sortedSetRemove('topics:views', tid, next);
},
function(next) {
Topics.getTopicField(tid, 'cid', function(err, cid) {
if(err) {
return next(err);
}
db.incrObjectFieldBy('category:' + cid, 'topic_count', -1, next);
});
}
11 years ago
], function(err) {
if (err) {
return callback(err);
}
Topics.updateTopicCount(callback);
});
};
Topics.restore = function(tid, callback) {
11 years ago
Topics.getTopicFields(tid, ['lastposttime', 'postcount', 'viewcount'], function(err, topicData) {
if(err) {
return callback(err);
}
async.parallel([
function(next) {
Topics.setTopicField(tid, 'deleted', 0, next);
},
function(next) {
db.sortedSetAdd('topics:recent', topicData.lastposttime, tid, next);
},
function(next) {
db.sortedSetAdd('topics:posts', topicData.postcount, tid, next);
},
function(next) {
db.sortedSetAdd('topics:views', topicData.viewcount, tid, next);
},
function(next) {
Topics.getTopicField(tid, 'cid', function(err, cid) {
if(err) {
return next(err);
}
db.incrObjectFieldBy('category:' + cid, 'topic_count', 1, next);
});
}
11 years ago
], function(err) {
if (err) {
return callback(err);
}
Topics.updateTopicCount(callback);
});
});
};
}(exports));