Files
NodeBB/src/topics.js

1105 lines
27 KiB
JavaScript
Raw Normal View History

2014-02-26 20:38:49 -05:00
"use strict";
2013-12-01 21:12:05 -05:00
var async = require('async'),
gravatar = require('gravatar'),
2014-02-20 02:05:49 -05:00
path = require('path'),
2013-12-01 21:12:05 -05:00
nconf = require('nconf'),
validator = require('validator'),
S = require('string'),
2014-01-07 17:30:29 -05:00
winston = require('winston'),
2013-12-01 21:12:05 -05:00
2013-12-02 21:20:55 -05:00
db = require('./database'),
posts = require('./posts'),
utils = require('./../public/src/utils'),
2014-02-20 02:05:49 -05:00
plugins = require('./plugins'),
user = require('./user'),
categories = require('./categories'),
2013-12-01 21:12:05 -05:00
categoryTools = require('./categoryTools'),
posts = require('./posts'),
threadTools = require('./threadTools'),
postTools = require('./postTools'),
notifications = require('./notifications'),
favourites = require('./favourites'),
2014-02-19 21:06:30 -05:00
meta = require('./meta'),
Plugins = require('./plugins');
(function(Topics) {
2013-07-02 16:24:13 -04:00
2014-02-20 02:05:49 -05:00
Topics.create = function(data, callback) {
var uid = data.uid,
title = data.title,
cid = data.cid,
thumb = data.thumb;
2014-01-03 21:33:41 -05:00
db.incrObjectField('global', 'nextTid', function(err, tid) {
if(err) {
return callback(err);
}
var slug = tid + '/' + utils.slugify(title),
timestamp = Date.now();
2014-02-21 23:28:19 -05:00
var topicData = {
2014-01-03 21:33:41 -05:00
'tid': tid,
'uid': uid,
'cid': cid,
'title': title,
'slug': slug,
'timestamp': timestamp,
'lastposttime': 0,
'postcount': 0,
'viewcount': 0,
'locked': 0,
'deleted': 0,
2014-02-21 23:28:19 -05:00
'pinned': 0
};
if(thumb) {
2014-02-26 20:38:49 -05:00
topicData.thumb = thumb;
2014-02-21 23:28:19 -05:00
}
db.setObject('topic:' + tid, topicData, function(err) {
2014-01-03 21:33:41 -05:00
if(err) {
return callback(err);
}
2014-02-13 11:35:27 -05:00
db.sortedSetAdd('topics:tid', timestamp, tid);
2014-02-19 21:06:30 -05:00
Plugins.fireHook('action:topic.save', tid);
2014-01-03 21:33:41 -05:00
2014-01-07 17:30:29 -05:00
user.addTopicIdToUser(uid, tid, timestamp);
2014-01-03 21:33:41 -05:00
db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid);
db.incrObjectField('category:' + cid, 'topic_count');
db.incrObjectField('global', 'topicCount');
callback(null, tid);
});
});
};
2014-02-20 02:05:49 -05:00
Topics.post = function(data, callback) {
var uid = data.uid,
title = data.title,
content = data.content,
cid = data.cid,
thumb = data.thumb;
2014-01-23 19:01:30 -05:00
if (title) {
title = title.trim();
}
2014-01-23 19:01:30 -05:00
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'));
}
2014-01-16 19:58:57 -05:00
2014-01-23 19:01:30 -05:00
if (content) {
content = content.trim();
}
2014-01-23 19:01:30 -05:00
if (!content || content.length < meta.config.miminumPostLength) {
return callback(new Error('content-too-short'));
}
2013-11-15 16:16:50 -05:00
2014-01-23 19:01:30 -05:00
if (!cid) {
return callback(new Error('invalid-cid'));
}
2013-11-15 16:16:50 -05:00
2014-01-23 19:01:30 -05:00
async.waterfall([
function(next) {
categoryTools.exists(cid, next);
},
function(categoryExists, next) {
2014-02-26 20:38:49 -05:00
if (!categoryExists) {
return next(new Error('category doesn\'t exist'));
}
2014-01-23 19:01:30 -05:00
categoryTools.privileges(cid, uid, next);
},
function(privileges, next) {
if(!privileges.write) {
return next(new Error('no-privileges'));
}
2014-01-23 19:01:30 -05:00
next();
},
function(next) {
user.isReadyToPost(uid, next);
},
function(next) {
2014-02-21 23:15:25 -05:00
Topics.create({uid: uid, title: title, cid: cid, thumb: thumb}, next);
2014-01-23 19:01:30 -05:00
},
function(tid, next) {
2014-02-22 17:56:13 -05:00
Topics.reply({uid:uid, tid:tid, content:content}, next);
2014-01-23 19:01:30 -05:00
},
function(postData, next) {
threadTools.toggleFollow(postData.tid, uid);
next(null, postData);
},
function(postData, next) {
2014-02-26 19:55:28 -05:00
Topics.getTopicsByTids([postData.tid], uid, function(err, topicData) {
if(err) {
2014-01-23 19:01:30 -05:00
return next(err);
}
2014-02-25 14:03:47 -05:00
if(!topicData || !topicData.length) {
return next(new Error('no-topic'));
}
topicData = topicData[0];
2014-01-23 19:01:30 -05:00
topicData.unreplied = 1;
2014-02-25 14:05:14 -05:00
2014-01-23 19:01:30 -05:00
next(null, {
topicData: topicData,
postData: postData
2013-11-15 16:16:50 -05:00
});
});
2014-01-23 19:01:30 -05:00
}
], callback);
2013-11-15 16:16:50 -05:00
};
2014-02-22 17:56:13 -05:00
Topics.reply = function(data, callback) {
var tid = data.tid,
2014-02-22 17:56:13 -05:00
uid = data.uid,
toPid = data.toPid,
content = data.content,
privileges,
postData;
2014-01-11 15:33:58 -05:00
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'));
2014-01-11 15:33:58 -05:00
}
next();
},
function(next) {
2014-01-23 19:01:30 -05:00
user.isReadyToPost(uid, next);
},
function(next) {
if (content) {
content = content.trim();
}
2013-12-29 23:33:28 -05:00
if (!content || content.length < meta.config.minimumPostLength) {
return next(new Error('content-too-short'));
}
2013-12-29 23:33:28 -05:00
2014-02-22 17:56:13 -05:00
posts.create({uid:uid, tid:tid, content:content, toPid:toPid}, next);
},
function(data, next) {
postData = data;
2014-01-30 13:16:45 -05:00
threadTools.notifyFollowers(tid, postData.pid, uid);
2014-02-11 18:58:46 -05:00
user.sendPostNotificationToFollowers(uid, tid, postData.pid);
2013-12-29 23:33:28 -05:00
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;
2014-02-10 13:29:59 -05:00
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);
2014-02-12 10:03:28 -05:00
};
2013-12-29 23:33:28 -05:00
2014-01-06 19:12:34 -05:00
Topics.createTopicFromPosts = function(uid, title, pids, callback) {
2014-01-06 17:37:42 -05:00
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];
2014-01-23 19:01:30 -05:00
async.parallel({
postData: function(callback) {
posts.getPostData(mainPid, callback);
},
cid: function(callback) {
posts.getCidByPid(mainPid, callback);
2014-01-03 21:33:41 -05:00
}
2014-01-23 19:01:30 -05:00
}, function(err, results) {
2014-02-20 02:05:49 -05:00
Topics.create({uid: results.postData.uid, title: title, cid: results.cid}, function(err, tid) {
2014-01-03 21:33:41 -05:00
if(err) {
return callback(err);
}
2014-01-23 19:01:30 -05:00
async.eachSeries(pids, move, function(err) {
2014-01-03 21:33:41 -05:00
if(err) {
return callback(err);
}
2014-01-23 19:01:30 -05:00
Topics.getTopicData(tid, callback);
});
function move(pid, next) {
postTools.privileges(pid, uid, function(err, privileges) {
2014-01-06 17:37:42 -05:00
if(err) {
2014-01-23 19:01:30 -05:00
return next(err);
2014-01-06 17:37:42 -05:00
}
2014-01-03 21:33:41 -05:00
2014-01-23 19:01:30 -05:00
if(privileges.editable) {
Topics.movePostToTopic(pid, tid, next);
} else {
next();
}
2014-01-06 17:37:42 -05:00
});
2014-01-23 19:01:30 -05:00
}
2014-01-03 21:33:41 -05:00
});
});
2014-02-12 10:03:28 -05:00
};
2014-01-03 21:33:41 -05:00
2014-01-07 15:00:05 -05:00
Topics.movePostToTopic = function(pid, tid, callback) {
threadTools.exists(tid, function(err, exists) {
if(err || !exists) {
return callback(err || new Error('Topic doesn\'t exist'));
2014-01-07 15:00:05 -05:00
}
2014-01-06 17:37:42 -05:00
2014-01-31 18:17:13 -05:00
posts.getPostFields(pid, ['deleted', 'tid', 'timestamp'], function(err, postData) {
2014-01-07 15:00:05 -05:00
if(err) {
return callback(err);
}
2014-01-07 17:30:29 -05:00
if(!postData) {
2014-01-07 15:00:05 -05:00
return callback(new Error('Post doesn\'t exist'));
}
2014-01-06 17:37:42 -05:00
2014-01-07 17:30:29 -05:00
Topics.removePostFromTopic(postData.tid, pid, function(err) {
2014-01-07 15:00:05 -05:00
if(err) {
return callback(err);
}
2014-01-31 18:17:13 -05:00
if(!parseInt(postData.deleted, 10)) {
Topics.decreasePostCount(postData.tid);
Topics.increasePostCount(tid);
}
2014-01-07 15:00:05 -05:00
posts.setPostField(pid, 'tid', tid);
2014-01-07 17:30:29 -05:00
Topics.addPostToTopic(tid, pid, postData.timestamp, callback);
2014-01-07 15:00:05 -05:00
});
});
});
2014-02-12 10:03:28 -05:00
};
2014-01-06 17:37:42 -05:00
2013-07-02 16:24:13 -04:00
Topics.getTopicData = function(tid, callback) {
2013-12-02 21:20:55 -05:00
db.getObject('topic:' + tid, function(err, data) {
if(err) {
return callback(err, null);
}
if(data) {
2014-02-19 17:23:25 -05:00
data.title = validator.escape(data.title);
data.relativeTime = utils.toISOString(data.timestamp);
}
callback(null, data);
2013-07-02 16:24:13 -04:00
});
2014-02-12 10:03:28 -05:00
};
2013-07-02 16:24:13 -04:00
2013-11-04 15:01:01 -05:00
Topics.getTopicDataWithUser = function(tid, callback) {
Topics.getTopicData(tid, function(err, topic) {
2014-02-25 14:03:47 -05:00
if (err || !topic) {
return callback(err || new Error('topic doesn\'t exist'));
}
2013-11-04 15:01:01 -05:00
user.getUserFields(topic.uid, ['username', 'userslug', 'picture'] , function(err, userData) {
2014-02-25 14:03:47 -05:00
if (err) {
return callback(err);
}
2014-02-25 14:03:47 -05:00
if (!userData) {
userData = {};
}
topic.username = userData.username || 'Anonymous';
topic.userslug = userData.userslug || '';
topic.picture = userData.picture || gravatar.url('', {}, true);
callback(null, topic);
2013-07-03 12:14:20 -04:00
});
2013-07-02 16:24:13 -04:00
});
2014-02-12 10:03:28 -05:00
};
2013-07-02 16:24:13 -04:00
2014-02-26 15:32:32 -05:00
Topics.getTopicPosts = function(tid, start, end, uid, reverse, callback) {
posts.getPostsByTid(tid, start, end, reverse, function(err, postData) {
2013-12-11 16:08:20 -05:00
if(err) {
return callback(err);
}
2013-11-29 13:09:26 -05:00
if (Array.isArray(postData) && !postData.length) {
2013-12-11 16:08:20 -05:00
return callback(null, []);
2013-11-29 13:09:26 -05:00
}
for(var i=0; i<postData.length; ++i) {
postData[i].index = start + i;
}
2014-02-26 20:38:49 -05:00
var pids = postData.map(function(post) {
2014-01-19 17:28:08 -05:00
return post.pid;
});
2013-07-03 12:14:20 -04:00
2014-02-26 15:32:32 -05:00
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);
2014-01-19 17:28:08 -05:00
}
2014-02-26 15:32:32 -05:00
}, function(err, results) {
2013-12-11 16:08:20 -05:00
if(err) {
return callback(err);
}
2013-09-17 13:09:37 -04:00
for (var i = 0; i < postData.length; ++i) {
2014-02-26 15:32:32 -05:00
postData[i].favourited = results.favourites[i];
postData[i].upvoted = results.voteData[i].upvoted;
postData[i].downvoted = results.voteData[i].downvoted;
2014-02-06 14:30:59 -05:00
postData[i].votes = postData[i].votes || 0;
2014-02-26 20:38:49 -05:00
postData[i].display_moderator_tools = parseInt(uid, 10) !== 0 && results.privileges[i].editable;
2014-02-26 15:32:32 -05:00
postData[i].display_move_tools = results.privileges[i].move;
2014-02-26 17:16:55 -05:00
2014-02-26 15:32:32 -05:00
if(parseInt(postData[i].deleted, 10) === 1 && !results.privileges[i].view_deleted) {
2014-01-24 20:00:56 -05:00
postData[i].content = 'This post is deleted!';
}
2013-07-03 12:14:20 -04:00
}
2013-08-23 13:14:36 -04:00
2013-12-11 16:08:20 -05:00
callback(null, postData);
2013-07-02 16:24:13 -04:00
});
2013-07-03 12:14:20 -04:00
});
2014-02-12 10:03:28 -05:00
};
2013-07-02 16:24:13 -04:00
2014-02-10 14:15:54 -05:00
Topics.getPageCount = function(tid, uid, callback) {
2014-01-24 20:00:56 -05:00
db.sortedSetCard('tid:' + tid + ':posts', function(err, postCount) {
if(err) {
return callback(err);
}
if(!parseInt(postCount, 10)) {
return callback(null, 1);
}
2014-02-10 14:15:54 -05:00
user.getSettings(uid, function(err, settings) {
if(err) {
return callback(err);
}
2014-01-24 20:00:56 -05:00
2014-02-10 14:15:54 -05:00
callback(null, Math.ceil(parseInt(postCount, 10) / settings.postsPerPage));
});
2014-01-24 20:00:56 -05:00
});
2014-02-12 10:03:28 -05:00
};
2014-01-24 20:00:56 -05:00
Topics.getCategoryData = function(tid, callback) {
Topics.getTopicField(tid, 'cid', function(err, cid) {
2014-01-23 19:01:30 -05:00
if(err) {
callback(err);
}
categories.getCategoryData(cid, callback);
});
2014-02-12 10:03:28 -05:00
};
2014-01-30 19:46:25 -05:00
function getTopics(set, uid, tids, callback) {
2014-02-14 13:38:10 -05:00
var returnTopics = {
topics: [],
nextStart: 0
2014-01-30 19:46:25 -05:00
};
if (!tids || !tids.length) {
2014-02-14 13:38:10 -05:00
return callback(null, returnTopics);
2014-01-30 19:46:25 -05:00
}
async.filter(tids, function(tid, next) {
threadTools.privileges(tid, uid, function(err, privileges) {
next(!err && privileges.read);
});
}, function(tids) {
2014-02-26 19:55:28 -05:00
Topics.getTopicsByTids(tids, uid, function(err, topicData) {
2014-01-30 19:46:25 -05:00
if(err) {
return callback(err);
}
if(!topicData || !topicData.length) {
2014-02-14 13:38:10 -05:00
return callback(null, returnTopics);
2014-01-30 19:46:25 -05:00
}
db.sortedSetRevRank(set, topicData[topicData.length - 1].tid, function(err, rank) {
if(err) {
2014-02-26 20:38:49 -05:00
return callback(err);
2014-01-30 19:46:25 -05:00
}
2014-02-14 13:38:10 -05:00
returnTopics.nextStart = parseInt(rank, 10) + 1;
returnTopics.topics = topicData;
callback(null, returnTopics);
2014-01-30 19:46:25 -05:00
});
});
});
}
2014-02-21 18:31:59 -05:00
Topics.getLatestTids = function(start, end, term, callback) {
var terms = {
day: 86400000,
week: 604800000,
2013-10-11 23:32:47 -04:00
month: 2592000000
};
2014-02-26 20:38:49 -05:00
var since = terms.day;
2014-01-26 16:23:37 -05:00
if(terms[term]) {
since = terms[term];
2014-01-26 16:23:37 -05:00
}
2014-02-21 18:31:59 -05:00
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) {
2013-11-22 11:42:42 -05:00
return callback(err);
}
2014-02-21 18:31:59 -05:00
getTopics('topics:recent', uid, tids, callback);
2014-01-30 19:46:25 -05:00
});
2014-02-12 10:03:28 -05:00
};
2014-01-30 19:46:25 -05:00
Topics.getTopicsFromSet = function(uid, set, start, end, callback) {
db.getSortedSetRevRange(set, start, end, function(err, tids) {
if(err) {
return callback(err);
}
2014-01-30 19:46:25 -05:00
getTopics(set, uid, tids, callback);
});
2014-02-12 10:03:28 -05:00
};
2013-08-23 13:14:36 -04:00
Topics.getTotalUnread = function(uid, callback) {
2014-02-21 18:31:59 -05:00
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;
2014-02-21 18:31:59 -05:00
uid = parseInt(uid, 10);
if(uid === 0) {
return callback(null, unreadTids);
}
2014-02-21 18:31:59 -05:00
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;
2014-02-21 18:31:59 -05:00
return callback();
}
2014-02-21 18:31:59 -05:00
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;
});
2014-01-26 17:17:34 -05:00
2014-02-21 18:31:59 -05:00
async.filter(newtids, function(tid, next) {
threadTools.privileges(tid, uid, function(err, privileges) {
next(!err && privileges.read);
});
}, function(newtids) {
unreadTids.push.apply(unreadTids, newtids);
2014-02-21 18:31:59 -05:00
start = stop + 1;
stop = start + 19;
2014-02-21 18:31:59 -05:00
callback();
});
2014-02-21 18:31:59 -05:00
});
});
}, function(err) {
callback(err, unreadTids);
});
};
Topics.getUnreadTopics = function(uid, start, stop, callback) {
var unreadTopics = {
no_topics_message: '',
show_markallread_button: 'hidden',
nextStart : 0,
topics: []
};
2013-08-26 16:04:31 -04:00
function sendUnreadTopics(topicIds) {
2013-12-30 16:09:07 -05:00
2014-02-26 19:55:28 -05:00
Topics.getTopicsByTids(topicIds, uid, function(err, topicData) {
2014-01-16 20:53:32 -05:00
if(err) {
return callback(err);
}
2014-01-26 17:17:34 -05:00
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' : '';
2014-01-26 17:17:34 -05:00
callback(null, unreadTopics);
});
2013-08-26 16:04:31 -04:00
});
}
2013-08-23 13:14:36 -04:00
Topics.getUnreadTids(uid, start, stop, function(err, unreadTids) {
if (err) {
2014-01-16 20:53:32 -05:00
return callback(err);
}
2013-08-23 13:14:36 -04:00
if (unreadTids.length) {
sendUnreadTopics(unreadTids);
} else {
callback(null, unreadTopics);
2013-07-29 13:33:54 -04:00
}
});
};
2013-10-28 00:08:44 -04:00
Topics.pushUnreadCount = function(uids, callback) {
var websockets = require('./socket.io');
if (!uids) {
uids = websockets.getConnectedClients();
} else if (!Array.isArray(uids)) {
uids = [uids];
}
2013-10-28 00:08:44 -04:00
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);
2013-12-05 14:48:27 -05:00
next();
});
}, function(err) {
2013-12-05 14:48:27 -05:00
if (err) {
winston.error(err.message);
}
2013-08-26 16:04:31 -04:00
if (callback) {
callback();
2013-07-29 13:33:54 -04:00
}
});
};
2014-02-26 19:55:28 -05:00
Topics.getTopicsByTids = function(tids, uid, callback) {
2013-09-17 13:09:37 -04:00
if (!Array.isArray(tids) || tids.length === 0) {
2014-02-12 00:09:02 -05:00
return callback(null, []);
}
2013-08-23 13:14:36 -04:00
function getTopicInfo(topicData, callback) {
2014-02-25 14:03:47 -05:00
async.parallel({
hasread : function (next) {
2014-02-26 16:43:21 -05:00
Topics.hasReadTopic(topicData.tid, uid, next);
2014-02-25 14:03:47 -05:00
},
teaser : function (next) {
Topics.getTeaser(topicData.tid, next);
},
privileges : function (next) {
2014-02-26 16:43:21 -05:00
categoryTools.privileges(topicData.cid, uid, next);
2014-02-25 14:03:47 -05:00
},
categoryData : function (next) {
categories.getCategoryFields(topicData.cid, ['name', 'slug', 'icon'], next);
2014-01-16 20:53:32 -05:00
}
2014-02-25 14:03:47 -05:00
}, callback);
}
function isTopicVisible(topicData, topicInfo) {
var deleted = parseInt(topicData.deleted, 10) !== 0;
2014-01-26 16:23:37 -05:00
2014-02-26 16:43:21 -05:00
return !deleted || (deleted && topicInfo.privileges.view_deleted) || parseInt(topicData.uid, 10) === parseInt(uid, 10);
}
2014-01-16 20:53:32 -05:00
function loadTopic(tid, next) {
2014-02-25 14:03:47 -05:00
Topics.getTopicDataWithUser(tid, function(err, topicData) {
2014-01-16 20:53:32 -05:00
if(err) {
return next(err);
}
2013-09-17 13:09:37 -04:00
if (!topicData) {
2014-01-16 20:53:32 -05:00
return next();
}
2013-08-23 13:14:36 -04:00
2014-01-16 20:53:32 -05:00
getTopicInfo(topicData, function(err, topicInfo) {
if(err) {
return next(err);
}
2014-02-12 00:09:02 -05:00
if (!isTopicVisible(topicData, topicInfo)) {
return next();
}
2014-02-25 14:03:47 -05:00
topicData.pinned = parseInt(topicData.pinned, 10) === 1;
topicData.locked = parseInt(topicData.locked, 10) === 1;
topicData.deleted = parseInt(topicData.deleted, 10) === 1;
2014-02-26 16:43:21 -05:00
topicData.unread = !(topicInfo.hasread && parseInt(uid, 10) !== 0);
2013-12-05 13:11:27 -05:00
topicData.unreplied = parseInt(topicData.postcount, 10) === 1;
2014-02-25 14:03:47 -05:00
topicData.category = topicInfo.categoryData;
topicData.teaser = topicInfo.teaser;
2014-02-12 00:09:02 -05:00
next(null, topicData);
});
});
}
2013-08-23 13:14:36 -04:00
2014-02-12 00:09:02 -05:00
async.map(tids, loadTopic, function(err, topics) {
if(err) {
return callback(err);
}
2014-02-12 00:09:02 -05:00
topics = topics.filter(function(topic) {
return !!topic;
});
callback(null, topics);
});
2014-02-12 10:03:28 -05:00
};
2014-02-26 16:43:21 -05:00
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'));
2013-11-29 13:09:26 -05:00
}
2013-07-02 16:24:13 -04:00
2014-02-26 16:43:21 -05:00
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);
}
2013-08-23 13:14:36 -04:00
2014-02-26 16:43:21 -05:00
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);
});
});
2014-02-12 10:03:28 -05:00
};
2014-02-14 13:38:10 -05:00
Topics.getAllTopics = function(start, end, callback) {
db.getSortedSetRevRange('topics:tid', start, end, function(err, tids) {
2014-02-14 13:40:11 -05:00
if(err) {
return callback(err);
}
2014-02-14 13:38:10 -05:00
async.map(tids, function(tid, next) {
Topics.getTopicDataWithUser(tid, next);
}, callback);
});
2014-02-12 10:03:28 -05:00
};
2013-08-23 13:14:36 -04:00
Topics.markAllRead = function(uid, callback) {
2014-02-21 18:31:59 -05:00
Topics.getLatestTids(0, -1, 'month', function(err, tids) {
2013-09-17 13:09:37 -04:00
if (err) {
2014-01-16 19:58:57 -05:00
return callback(err);
}
2013-08-23 13:14:36 -04:00
if(!tids || !tids.length) {
2014-02-21 18:31:59 -05:00
return callback();
}
2013-08-23 13:14:36 -04:00
function markRead(tid, next) {
Topics.markAsRead(tid, uid, next);
}
async.each(tids, markRead, callback);
});
2014-02-12 10:03:28 -05:00
};
Topics.getTitleByPid = function(pid, callback) {
2014-02-20 02:05:49 -05:00
Topics.getTopicFieldByPid('title', pid, callback);
};
Topics.getTopicFieldByPid = function(field, pid, callback) {
2013-11-15 14:57:50 -05:00
posts.getPostField(pid, 'tid', function(err, tid) {
2014-02-20 02:05:49 -05:00
Topics.getTopicField(tid, field, callback);
});
2014-02-12 10:03:28 -05:00
};
2014-02-20 02:05:49 -05:00
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) {
2014-02-11 18:58:46 -05:00
db.delete('tid:' + tid + ':read_by_uid', function(err) {
if(err) {
return callback(err);
}
2014-02-26 20:38:49 -05:00
Topics.markCategoryUnreadForAll(tid, callback);
2014-02-11 18:58:46 -05:00
});
2014-02-12 10:03:28 -05:00
};
2013-07-22 12:07:05 -04:00
Topics.markAsRead = function(tid, uid, callback) {
2013-08-23 13:14:36 -04:00
db.setAdd('tid:' + tid + ':read_by_uid', uid, function(err) {
if(callback) {
callback(err);
}
});
2013-08-23 13:14:36 -04:00
Topics.getTopicField(tid, 'cid', function(err, cid) {
2014-02-21 18:31:59 -05:00
categories.markAsRead(cid, uid);
});
user.notifications.getUnreadByUniqueId(uid, 'topic:' + tid, function(err, nids) {
2013-11-26 19:09:32 -05:00
notifications.mark_read_multiple(nids, uid, function() {
user.pushNotifCount(uid);
2013-11-26 19:09:32 -05:00
});
});
2014-02-12 10:03:28 -05:00
};
Topics.markCategoryUnreadForAll = function(tid, callback) {
Topics.getTopicField(tid, 'cid', function(err, cid) {
if(err) {
return callback(err);
}
categories.markAsUnreadForAll(cid, callback);
});
2014-02-12 10:03:28 -05:00
};
Topics.hasReadTopics = function(tids, uid, callback) {
2014-02-11 18:22:02 -05:00
if(!parseInt(uid, 10)) {
2014-02-21 15:54:51 -05:00
return callback(null, tids.map(function() {
2014-02-11 18:22:02 -05:00
return false;
}));
}
2013-12-02 21:20:55 -05:00
var sets = [];
2013-09-17 13:09:37 -04:00
for (var i = 0, ii = tids.length; i < ii; i++) {
2013-12-02 21:20:55 -05:00
sets.push('tid:' + tids[i] + ':read_by_uid');
}
2013-08-23 13:14:36 -04:00
2014-02-21 15:54:51 -05:00
db.isMemberOfSets(sets, uid, callback);
2014-02-12 10:03:28 -05:00
};
2013-07-04 13:41:13 -04:00
Topics.hasReadTopic = function(tid, uid, callback) {
2014-02-11 18:22:02 -05:00
if(!parseInt(uid, 10)) {
2014-02-21 15:54:51 -05:00
return callback(null, false);
2014-02-11 18:22:02 -05:00
}
2014-02-21 15:54:51 -05:00
db.isSetMember('tid:' + tid + ':read_by_uid', uid, callback);
2014-02-12 10:03:28 -05:00
};
2013-08-23 13:14:36 -04:00
2013-07-03 12:21:24 -04:00
Topics.getTeasers = function(tids, callback) {
2014-02-12 10:03:28 -05:00
if(!Array.isArray(tids)) {
return callback(null, []);
}
2014-02-26 20:38:49 -05:00
async.map(tids, Topics.getTeaser, callback);
2014-02-12 10:03:28 -05:00
};
2013-07-03 12:21:24 -04:00
Topics.getTeaser = function(tid, callback) {
threadTools.getLatestUndeletedPid(tid, function(err, pid) {
2013-11-15 14:57:50 -05:00
if (err) {
2014-02-12 10:03:28 -05:00
return callback(err);
2013-11-15 14:57:50 -05:00
}
2014-02-25 14:03:47 -05:00
if (!pid) {
return callback(null, null);
}
posts.getPostFields(pid, ['pid', 'uid', 'timestamp'], function(err, postData) {
2013-11-15 14:57:50 -05:00
if (err) {
2014-02-12 10:03:28 -05:00
return callback(err);
2013-11-15 14:57:50 -05:00
} else if(!postData) {
return callback(new Error('no-teaser-found'));
}
user.getUserFields(postData.uid, ['username', 'userslug', 'picture'], function(err, userData) {
if (err) {
2014-02-12 10:03:28 -05:00
return callback(err);
2013-11-15 14:57:50 -05:00
}
callback(null, {
pid: postData.pid,
username: userData.username || 'anonymous',
2014-02-25 14:03:47 -05:00
userslug: userData.userslug || '',
2014-01-25 12:05:48 -05:00
picture: userData.picture || gravatar.url('', {}, true),
2014-02-25 14:03:47 -05:00
timestamp: utils.toISOString(postData.timestamp)
});
});
2013-11-15 14:57:50 -05:00
});
});
2014-02-26 20:38:49 -05:00
};
2013-07-02 16:24:13 -04:00
Topics.getTopicField = function(tid, field, callback) {
2013-12-02 21:20:55 -05:00
db.getObjectField('topic:' + tid, field, callback);
2014-02-26 20:38:49 -05:00
};
2013-08-23 13:14:36 -04:00
Topics.getTopicFields = function(tid, fields, callback) {
2013-12-02 21:20:55 -05:00
db.getObjectFields('topic:' + tid, fields, callback);
2014-02-26 20:38:49 -05:00
};
2013-07-02 16:24:13 -04:00
2013-11-27 15:02:09 -05:00
Topics.setTopicField = function(tid, field, value, callback) {
2013-12-02 21:20:55 -05:00
db.setObjectField('topic:' + tid, field, value, callback);
2014-02-26 20:38:49 -05:00
};
2013-07-02 16:24:13 -04:00
2013-11-27 15:02:09 -05:00
Topics.increasePostCount = function(tid, callback) {
2014-01-30 19:46:25 -05:00
db.incrObjectField('topic:' + tid, 'postcount', function(err, value) {
if(err) {
return callback(err);
}
db.sortedSetAdd('topics:posts', value, tid, callback);
});
2014-02-26 20:38:49 -05:00
};
2013-07-02 16:24:13 -04:00
2014-01-03 21:33:41 -05:00
Topics.decreasePostCount = function(tid, callback) {
2014-01-30 19:46:25 -05:00
db.decrObjectField('topic:' + tid, 'postcount', function(err, value) {
if(err) {
return callback(err);
}
db.sortedSetAdd('topics:posts', value, tid, callback);
});
2014-02-26 20:38:49 -05:00
};
2014-01-03 21:33:41 -05:00
2013-11-27 15:02:09 -05:00
Topics.increaseViewCount = function(tid, callback) {
2014-01-30 19:46:25 -05:00
db.incrObjectField('topic:' + tid, 'viewcount', function(err, value) {
if(err) {
return callback(err);
}
db.sortedSetAdd('topics:views', value, tid, callback);
});
2014-02-26 20:38:49 -05:00
};
2013-10-23 12:26:24 -04:00
2013-07-02 16:24:13 -04:00
Topics.isLocked = function(tid, callback) {
Topics.getTopicField(tid, 'locked', function(err, locked) {
2013-11-15 14:57:50 -05:00
if(err) {
2014-02-25 14:41:14 -05:00
return callback(err);
2013-11-15 14:57:50 -05:00
}
2013-12-05 13:11:27 -05:00
callback(null, parseInt(locked, 10) === 1);
2013-07-02 16:24:13 -04:00
});
2014-02-26 20:38:49 -05:00
};
2013-07-02 16:24:13 -04:00
2013-07-18 15:14:06 -04:00
Topics.updateTimestamp = function(tid, timestamp) {
2013-12-02 21:20:55 -05:00
db.sortedSetAdd('topics:recent', timestamp, tid);
Topics.setTopicField(tid, 'lastposttime', timestamp);
2014-02-26 20:38:49 -05:00
};
2013-08-23 13:14:36 -04:00
2014-01-06 17:37:42 -05:00
Topics.onNewPostMade = function(tid, pid, timestamp, callback) {
2013-12-29 23:33:28 -05:00
Topics.increasePostCount(tid);
Topics.updateTimestamp(tid, timestamp);
2014-01-07 17:30:29 -05:00
Topics.addPostToTopic(tid, pid, timestamp, callback);
2014-02-26 20:38:49 -05:00
};
2013-12-29 23:33:28 -05:00
2014-01-07 17:30:29 -05:00
Topics.addPostToTopic = function(tid, pid, timestamp, callback) {
db.sortedSetAdd('tid:' + tid + ':posts', timestamp, pid, callback);
2014-02-26 20:38:49 -05:00
};
2013-07-22 12:07:05 -04:00
2014-01-07 15:00:05 -05:00
Topics.removePostFromTopic = function(tid, pid, callback) {
2014-01-07 17:30:29 -05:00
db.sortedSetRemove('tid:' + tid + ':posts', pid, callback);
2014-02-26 20:38:49 -05:00
};
2014-01-03 21:33:41 -05:00
2013-07-22 12:07:05 -04:00
Topics.getPids = function(tid, callback) {
2014-01-07 17:30:29 -05:00
db.getSortedSetRange('tid:' + tid + ':posts', 0, -1, callback);
2014-02-26 20:38:49 -05:00
};
2013-07-22 12:07:05 -04:00
2013-08-27 13:32:43 -04:00
Topics.getUids = function(tid, callback) {
var uids = {};
Topics.getPids(tid, function(err, pids) {
function getUid(pid, next) {
2013-11-15 14:57:50 -05:00
posts.getPostField(pid, 'uid', function(err, uid) {
2014-02-25 14:41:14 -05:00
if (err) {
2013-08-27 13:32:43 -04:00
return next(err);
2014-02-25 14:41:14 -05:00
}
2013-08-27 13:32:43 -04:00
uids[uid] = 1;
2014-02-25 14:41:14 -05:00
next();
2013-08-27 13:32:43 -04:00
});
}
async.each(pids, getUid, function(err) {
2014-02-25 14:41:14 -05:00
if (err) {
return callback(err);
}
2013-08-27 13:32:43 -04:00
callback(null, Object.keys(uids));
});
});
2014-02-26 20:38:49 -05:00
};
2013-08-27 13:32:43 -04:00
2014-02-24 16:23:11 -05:00
Topics.updateTopicCount = function(callback) {
db.sortedSetCard('topics:recent', function(err, count) {
if(err) {
return callback(err);
}
2014-02-24 16:24:21 -05:00
db.setObjectField('global', 'topicCount', count, callback);
2014-02-24 16:23:11 -05:00
});
};
2014-02-17 22:53:01 -05:00
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);
});
}
2014-02-24 16:23:11 -05:00
], function(err) {
if (err) {
return callback(err);
}
Topics.updateTopicCount(callback);
});
2014-02-17 22:53:01 -05:00
};
2013-08-26 16:04:31 -04:00
2014-02-17 22:53:01 -05:00
Topics.restore = function(tid, callback) {
2014-01-30 19:46:25 -05:00
Topics.getTopicFields(tid, ['lastposttime', 'postcount', 'viewcount'], function(err, topicData) {
2014-02-17 22:53:01 -05:00
if(err) {
return callback(err);
}
2014-02-17 22:53:01 -05:00
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);
});
}
2014-02-24 16:23:11 -05:00
], function(err) {
if (err) {
return callback(err);
}
Topics.updateTopicCount(callback);
});
});
2014-02-17 22:53:01 -05:00
};
2014-02-26 19:55:28 -05:00
}(exports));