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

1135 lines
28 KiB
JavaScript

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'),
Plugins = require('./plugins');
(function(Topics) {
Topics.create = function(data, callback) {
var uid = data.uid,
title = data.title,
cid = data.cid,
thumb = data.thumb;
db.incrObjectField('global', 'nextTid', function(err, tid) {
if(err) {
return callback(err);
}
var slug = tid + '/' + utils.slugify(title),
timestamp = Date.now();
var topicData = {
'tid': tid,
'uid': uid,
'cid': cid,
'title': title,
'slug': slug,
'timestamp': timestamp,
'lastposttime': 0,
'postcount': 0,
'viewcount': 0,
'locked': 0,
'deleted': 0,
'pinned': 0
};
if(thumb) {
topicData['thumb'] = thumb;
}
db.setObject('topic:' + tid, topicData, function(err) {
if(err) {
return callback(err);
}
db.sortedSetAdd('topics:tid', timestamp, tid);
11 years ago
Plugins.fireHook('action:topic.save', tid);
user.addTopicIdToUser(uid, tid, timestamp);
db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid);
db.incrObjectField('category:' + cid, 'topic_count');
db.incrObjectField('global', 'topicCount');
callback(null, tid);
});
});
};
Topics.post = function(data, callback) {
var uid = data.uid,
title = data.title,
content = data.content,
cid = data.cid,
thumb = data.thumb;
if (title) {
title = title.trim();
}
if (!title || title.length < parseInt(meta.config.minimumTitleLength, 10)) {
return callback(new Error('title-too-short'));
} else if(title.length > parseInt(meta.config.maximumTitleLength, 10)) {
return callback(new Error('title-too-long'));
}
11 years ago
if (content) {
content = content.trim();
}
if (!content || content.length < meta.config.miminumPostLength) {
return callback(new Error('content-too-short'));
}
11 years ago
if (!cid) {
return callback(new Error('invalid-cid'));
}
11 years ago
async.waterfall([
function(next) {
categoryTools.exists(cid, next);
},
function(categoryExists, next) {
if(!categoryExists) {
return next(new Error('category doesn\'t exist'))
}
categoryTools.privileges(cid, uid, next);
},
function(privileges, next) {
if(!privileges.write) {
return next(new Error('no-privileges'));
}
next();
},
function(next) {
user.isReadyToPost(uid, next);
},
function(next) {
Topics.create({uid: uid, title: title, cid: cid, thumb: thumb}, next);
},
function(tid, next) {
Topics.reply({uid:uid, tid:tid, content:content}, next);
},
function(postData, next) {
threadTools.toggleFollow(postData.tid, uid);
next(null, postData);
},
function(postData, next) {
Topics.getTopicsByTids([postData.tid], data.cid, uid, function(err, topicData) {
if(err) {
return next(err);
}
if(!topicData || !topicData.length) {
return next(new Error('no-topic'));
}
topicData = topicData[0];
topicData.unreplied = 1;
next(null, {
topicData: topicData,
postData: postData
11 years ago
});
});
}
], callback);
11 years ago
};
Topics.reply = function(data, callback) {
var tid = data.tid,
uid = data.uid,
toPid = data.toPid,
content = data.content,
privileges,
postData;
11 years ago
async.waterfall([
function(next) {
threadTools.exists(tid, next);
},
function(topicExists, next) {
if (!topicExists) {
return next(new Error('topic doesn\'t exist'));
}
threadTools.privileges(tid, uid, next);
},
function(privilegesData, next) {
privileges = privilegesData;
if (!privileges.write) {
return next(new Error('no-privileges'));
11 years ago
}
next();
},
function(next) {
user.isReadyToPost(uid, next);
},
function(next) {
if (content) {
content = content.trim();
}
11 years ago
if (!content || content.length < meta.config.minimumPostLength) {
return next(new Error('content-too-short'));
}
11 years ago
posts.create({uid:uid, tid:tid, content:content, toPid:toPid}, next);
},
function(data, next) {
postData = data;
11 years ago
threadTools.notifyFollowers(tid, postData.pid, uid);
user.sendPostNotificationToFollowers(uid, tid, postData.pid);
11 years ago
next();
},
function(next) {
Topics.markAsUnreadForAll(tid, next);
},
function(next) {
Topics.markAsRead(tid, uid, next);
},
function(next) {
Topics.pushUnreadCount();
posts.addUserInfoToPost(postData, next);
},
function(postData,next) {
posts.getPidIndex(postData.pid, next);
},
function(index, next) {
postData.index = index;
postData.favourited = false;
postData.votes = 0;
postData.display_moderator_tools = true;
postData.display_move_tools = privileges.admin || privileges.moderator;
postData.relativeTime = utils.toISOString(postData.timestamp);
next(null, postData);
}
], callback);
};
11 years ago
Topics.createTopicFromPosts = function(uid, title, pids, callback) {
11 years ago
if(title) {
title = title.trim();
}
if(!title) {
return callback(new Error('invalid-title'));
}
if(!pids || !pids.length) {
return callback(new Error('invalid-pids'));
}
pids.sort();
var mainPid = pids[0];
async.parallel({
postData: function(callback) {
posts.getPostData(mainPid, callback);
},
cid: function(callback) {
posts.getCidByPid(mainPid, callback);
}
}, function(err, results) {
Topics.create({uid: results.postData.uid, title: title, cid: results.cid}, function(err, tid) {
if(err) {
return callback(err);
}
async.eachSeries(pids, move, function(err) {
if(err) {
return callback(err);
}
Topics.getTopicData(tid, callback);
});
function move(pid, next) {
postTools.privileges(pid, uid, function(err, privileges) {
11 years ago
if(err) {
return next(err);
11 years ago
}
if(privileges.editable) {
Topics.movePostToTopic(pid, tid, next);
} else {
next();
}
11 years ago
});
}
});
});
};
11 years ago
Topics.movePostToTopic = function(pid, tid, callback) {
threadTools.exists(tid, function(err, exists) {
if(err || !exists) {
return callback(err || new Error('Topic doesn\'t exist'));
11 years ago
}
11 years ago
11 years ago
posts.getPostFields(pid, ['deleted', 'tid', 'timestamp'], function(err, postData) {
11 years ago
if(err) {
return callback(err);
}
if(!postData) {
11 years ago
return callback(new Error('Post doesn\'t exist'));
}
11 years ago
Topics.removePostFromTopic(postData.tid, pid, function(err) {
11 years ago
if(err) {
return callback(err);
}
11 years ago
if(!parseInt(postData.deleted, 10)) {
Topics.decreasePostCount(postData.tid);
Topics.increasePostCount(tid);
}
11 years ago
posts.setPostField(pid, 'tid', tid);
Topics.addPostToTopic(tid, pid, postData.timestamp, callback);
11 years ago
});
});
});
};
11 years ago
Topics.getTopicData = function(tid, callback) {
11 years ago
db.getObject('topic:' + tid, function(err, data) {
if(err) {
return callback(err, null);
}
if(data) {
11 years ago
data.title = validator.escape(data.title);
data.relativeTime = utils.toISOString(data.timestamp);
}
callback(null, data);
});
};
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.username = userData.username || 'Anonymous';
topic.userslug = userData.userslug || '';
topic.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;
}
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;
11 years ago
postData[i].display_moderator_tools = (uid != 0) && results.privileges[i].editable;
postData[i].display_move_tools = results.privileges[i].move;
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));
});
});
};
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, 0, uid, function(err, topicData) {
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 calllback(err);
}
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.getTotalUnread = function(uid, callback) {
11 years ago
Topics.getUnreadTids(uid, 0, 21, function(err, tids) {
callback(err, {count: tids ? tids.length : 0});
});
};
Topics.getUnreadTids = function(uid, start, stop, callback) {
var unreadTids = [],
done = false;
11 years ago
uid = parseInt(uid, 10);
if(uid === 0) {
return callback(null, unreadTids);
}
11 years ago
async.whilst(function() {
return unreadTids.length < 20 && !done;
}, function(callback) {
Topics.getLatestTids(start, stop, 'month', function(err, tids) {
if (err) {
return callback(err);
}
if (tids && !tids.length) {
done = true;
11 years ago
return callback();
}
11 years ago
Topics.hasReadTopics(tids, uid, function(err, read) {
if(err) {
return callback(err);
}
var newtids = tids.filter(function(tid, index, self) {
return parseInt(read[index], 10) === 0;
});
11 years ago
11 years ago
async.filter(newtids, function(tid, next) {
threadTools.privileges(tid, uid, function(err, privileges) {
next(!err && privileges.read);
});
}, function(newtids) {
unreadTids.push.apply(unreadTids, newtids);
11 years ago
start = stop + 1;
stop = start + 19;
11 years ago
callback();
});
11 years ago
});
});
}, function(err) {
callback(err, unreadTids);
});
};
Topics.getUnreadTopics = function(uid, start, stop, callback) {
var unreadTopics = {
no_topics_message: '',
show_markallread_button: 'hidden',
nextStart : 0,
topics: []
};
function sendUnreadTopics(topicIds) {
11 years ago
Topics.getTopicsByTids(topicIds, 0, uid, function(err, topicData) {
if(err) {
return callback(err);
}
11 years ago
db.sortedSetRevRank('topics:recent', topicData[topicData.length - 1].tid, function(err, rank) {
if(err) {
return callback(err);
}
unreadTopics.topics = topicData;
unreadTopics.nextStart = parseInt(rank, 10) + 1;
unreadTopics.no_topics_message = (!topicData || topicData.length === 0) ? '' : 'hidden';
unreadTopics.show_markallread_button = topicData.length === 0 ? 'hidden' : '';
11 years ago
callback(null, unreadTopics);
});
});
}
Topics.getUnreadTids(uid, start, stop, function(err, unreadTids) {
if (err) {
return callback(err);
}
if (unreadTids.length) {
sendUnreadTopics(unreadTids);
} else {
callback(null, unreadTopics);
}
});
};
11 years ago
Topics.pushUnreadCount = function(uids, callback) {
var websockets = require('./socket.io');
if (!uids) {
uids = websockets.getConnectedClients();
} else if (!Array.isArray(uids)) {
uids = [uids];
}
11 years ago
uids = uids.filter(function(value) {
return parseInt(value, 10) !== 0;
});
async.each(uids, function(uid, next) {
Topics.getUnreadTids(uid, 0, 19, function(err, tids) {
websockets.in('uid_' + uid).emit('event:unread.updateCount', null, tids);
next();
});
}, function(err) {
if (err) {
winston.error(err.message);
}
if (callback) {
callback();
}
});
};
Topics.getTopicsByTids = function(tids, cid, current_user, callback) {
if (!Array.isArray(tids) || tids.length === 0) {
11 years ago
return callback(null, []);
}
function getTopicInfo(topicData, callback) {
async.parallel({
hasread : function (next) {
Topics.hasReadTopic(topicData.tid, current_user, next);
},
teaser : function (next) {
Topics.getTeaser(topicData.tid, next);
},
privileges : function (next) {
categoryTools.privileges(topicData.cid, current_user, next);
},
categoryData : function (next) {
categories.getCategoryFields(topicData.cid, ['name', 'slug', 'icon'], next);
}
}, callback);
}
function isTopicVisible(topicData, topicInfo) {
var deleted = parseInt(topicData.deleted, 10) !== 0;
11 years ago
11 years ago
return !deleted || (deleted && topicInfo.privileges.view_deleted) || parseInt(topicData.uid, 10) === parseInt(current_user, 10);
}
function loadTopic(tid, next) {
Topics.getTopicDataWithUser(tid, function(err, topicData) {
if(err) {
return next(err);
}
if (!topicData) {
return next();
}
getTopicInfo(topicData, function(err, topicInfo) {
if(err) {
return next(err);
}
11 years ago
if (!isTopicVisible(topicData, topicInfo)) {
return next();
}
topicData.pinned = parseInt(topicData.pinned, 10) === 1;
topicData.locked = parseInt(topicData.locked, 10) === 1;
topicData.deleted = parseInt(topicData.deleted, 10) === 1;
11 years ago
topicData.unread = !(topicInfo.hasread && parseInt(current_user, 10) !== 0);
11 years ago
topicData.unreplied = parseInt(topicData.postcount, 10) === 1;
topicData.category = topicInfo.categoryData;
topicData.teaser = topicInfo.teaser;
11 years ago
next(null, topicData);
});
});
}
11 years ago
async.map(tids, loadTopic, function(err, topics) {
if(err) {
return callback(err);
}
11 years ago
topics = topics.filter(function(topic) {
return !!topic;
});
callback(null, topics);
});
};
Topics.getTopicWithPosts = function(tid, current_user, start, end, quiet, callback) {
threadTools.exists(tid, function(err, exists) {
if (err || !exists) {
return callback(err || new Error('Topic tid \'' + tid + '\' not found'));
}
// "quiet" is used for things like RSS feed updating, HTML parsing for non-js users, etc
if (!quiet) {
Topics.markAsRead(tid, current_user, function(err) {
Topics.pushUnreadCount(current_user);
});
Topics.increaseViewCount(tid);
}
function getTopicData(next) {
Topics.getTopicData(tid, next);
}
function getTopicPosts(next) {
11 years ago
Topics.getTopicPosts(tid, start, end, current_user, false, next);
}
function getPrivileges(next) {
threadTools.privileges(tid, current_user, next);
}
function getCategoryData(next) {
Topics.getCategoryData(tid, next);
}
function getPageCount(next) {
Topics.getPageCount(tid, current_user, next);
}
function getThreadTools(next) {
Plugins.fireHook('filter:topic.thread_tools', [], function(err, threadTools) {
next(err, threadTools);
});
}
async.parallel([getTopicData, getTopicPosts, getPrivileges, getCategoryData, getPageCount, getThreadTools], function(err, results) {
if (err) {
winston.error('[Topics.getTopicWithPosts] Could not retrieve topic data: ', err.message);
return callback(err);
}
var topicData = results[0],
privileges = results[2],
categoryData = results[3],
pageCount = results[4],
threadTools = results[5];
callback(null, {
'topic_name': topicData.title,
'category_name': categoryData.name,
'category_slug': categoryData.slug,
'locked': topicData.locked,
'deleted': topicData.deleted,
'pinned': topicData.pinned,
'timestamp': topicData.timestamp,
'slug': topicData.slug,
'thumb': topicData.thumb,
'postcount': topicData.postcount,
'viewcount': topicData.viewcount,
'pageCount': pageCount,
'unreplied': parseInt(topicData.postcount, 10) === 1,
'topic_id': tid,
'expose_tools': privileges.editable ? 1 : 0,
'thread_tools': threadTools,
11 years ago
'disableSocialButtons': meta.config.disableSocialButtons !== undefined ? parseInt(meta.config.disableSocialButtons, 10) !== 0 : false,
'posts': results[1]
});
});
});
};
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.markAllRead = function(uid, callback) {
11 years ago
Topics.getLatestTids(0, -1, 'month', function(err, tids) {
if (err) {
11 years ago
return callback(err);
}
if(!tids || !tids.length) {
11 years ago
return callback();
}
function markRead(tid, next) {
Topics.markAsRead(tid, uid, next);
}
async.each(tids, markRead, 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);
});
};
Topics.uploadTopicThumb = function(image, callback) {
if(plugins.hasListeners('filter:uploadImage')) {
plugins.fireHook('filter:uploadImage', image, callback);
} else {
if (meta.config.allowTopicsThumbnail) {
var filename = 'upload-' + utils.generateUUID() + path.extname(image.name);
require('./file').saveFileToLocal(filename, image.path, function(err, upload) {
if(err) {
return callback(err);
}
callback(null, {
url: upload.url,
name: image.name
});
});
} else {
callback(new Error('Topic Thumbnails are disabled!'));
}
}
};
Topics.markAsUnreadForAll = function(tid, callback) {
db.delete('tid:' + tid + ':read_by_uid', function(err) {
if(err) {
return callback(err);
}
Topics.markCategoryUnreadForAll(tid, callback)
});
};
12 years ago
Topics.markAsRead = function(tid, uid, callback) {
db.setAdd('tid:' + tid + ':read_by_uid', uid, function(err) {
if(callback) {
callback(err);
}
});
Topics.getTopicField(tid, 'cid', function(err, cid) {
11 years ago
categories.markAsRead(cid, uid);
});
user.notifications.getUnreadByUniqueId(uid, 'topic:' + tid, function(err, nids) {
11 years ago
notifications.mark_read_multiple(nids, uid, function() {
user.pushNotifCount(uid);
11 years ago
});
});
};
Topics.markCategoryUnreadForAll = function(tid, callback) {
Topics.getTopicField(tid, 'cid', function(err, cid) {
if(err) {
return callback(err);
}
categories.markAsUnreadForAll(cid, callback);
});
};
Topics.hasReadTopics = function(tids, uid, callback) {
11 years ago
if(!parseInt(uid, 10)) {
return callback(null, tids.map(function() {
11 years ago
return false;
}));
}
11 years ago
var sets = [];
for (var i = 0, ii = tids.length; i < ii; i++) {
11 years ago
sets.push('tid:' + tids[i] + ':read_by_uid');
}
db.isMemberOfSets(sets, uid, callback);
};
Topics.hasReadTopic = function(tid, uid, callback) {
11 years ago
if(!parseInt(uid, 10)) {
return callback(null, false);
11 years ago
}
db.isSetMember('tid:' + tid + ':read_by_uid', uid, callback);
};
12 years ago
Topics.getTeasers = function(tids, callback) {
if(!Array.isArray(tids)) {
return callback(null, []);
}
11 years ago
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
db.incrObjectField('topic:' + tid, 'postcount', function(err, value) {
if(err) {
return callback(err);
}
db.sortedSetAdd('topics:posts', value, tid, callback);
});
}
Topics.decreasePostCount = function(tid, callback) {
11 years ago
db.decrObjectField('topic:' + tid, 'postcount', function(err, value) {
if(err) {
return callback(err);
}
db.sortedSetAdd('topics:posts', value, tid, callback);
});
}
11 years ago
Topics.increaseViewCount = function(tid, callback) {
11 years ago
db.incrObjectField('topic:' + tid, 'viewcount', function(err, value) {
if(err) {
return callback(err);
}
db.sortedSetAdd('topics:views', value, tid, callback);
});
}
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(tid, pid, timestamp, callback) {
11 years ago
Topics.increasePostCount(tid);
Topics.updateTimestamp(tid, timestamp);
Topics.addPostToTopic(tid, pid, timestamp, callback);
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));
});
});
}
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));