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

1129 lines
28 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'),
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], 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'));
}
Topics.isLocked(tid, next);
},
function(locked, next) {
if (locked) {
return next(new Error('topic-locked'));
}
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.notifications.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) {
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.getTotalUnread = function(uid, callback) {
11 years ago
Topics.getUnreadTids(uid, 0, 20, function(err, tids) {
callback(err, tids ? tids.length : 0);
11 years ago
});
};
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() {
11 years ago
return unreadTids.length < 21 && !done;
11 years ago
}, 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, 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) {
11 years ago
Topics.getTotalUnread(uid, function(err, count) {
websockets.in('uid_' + uid).emit('event:unread.updateCount', null, count);
next();
});
}, function(err) {
if (err) {
winston.error(err.message);
}
if (callback) {
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) {
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 (!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);
});
};
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
11 years ago
Topics.markAllRead = function(uid, tids, callback) {
if(!tids || !tids.length) {
return callback();
}
async.each(tids, function (tid, next) {
Topics.markAsRead(tid, uid, next);
}, callback);
};
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.notifications.pushCount(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, []);
}
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(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));
});
});
};
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));