|
|
|
@ -170,96 +170,83 @@ var async = require('async'),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Topics.getTopicsByTids = function(tids, uid, callback) {
|
|
|
|
|
if (!Array.isArray(tids) || tids.length === 0) {
|
|
|
|
|
if (!Array.isArray(tids) || !tids.length) {
|
|
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var categoryCache = {},
|
|
|
|
|
privilegeCache = {},
|
|
|
|
|
userCache = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function loadTopicInfo(topicData, next) {
|
|
|
|
|
if (!topicData) {
|
|
|
|
|
return next(null, null);
|
|
|
|
|
Topics.getTopicsData(tids, function(err, topics) {
|
|
|
|
|
function mapFilter(array, field) {
|
|
|
|
|
return array.map(function(topic) {
|
|
|
|
|
return topic[field];
|
|
|
|
|
}).filter(function(value, index, array) {
|
|
|
|
|
return array.indexOf(value) === index;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isTopicVisible(topicData, topicInfo) {
|
|
|
|
|
if (parseInt(topicInfo.categoryData.disabled, 10) === 1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
var deleted = parseInt(topicData.deleted, 10) !== 0;
|
|
|
|
|
return !deleted || (deleted && topicInfo.privileges.view_deleted) || parseInt(topicData.uid, 10) === parseInt(uid, 10);
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var uids = mapFilter(topics, 'uid');
|
|
|
|
|
var cids = mapFilter(topics, 'cid');
|
|
|
|
|
|
|
|
|
|
async.parallel({
|
|
|
|
|
hasread: function(next) {
|
|
|
|
|
Topics.hasReadTopic(topicData.tid, uid, next);
|
|
|
|
|
users: function(next) {
|
|
|
|
|
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
|
|
|
|
|
},
|
|
|
|
|
teaser: function(next) {
|
|
|
|
|
Topics.getTeaser(topicData.tid, next);
|
|
|
|
|
categories: function(next) {
|
|
|
|
|
categories.getMultipleCategoryFields(cids, ['cid', 'name', 'slug', 'icon', 'bgColor', 'color', 'disabled'], next);
|
|
|
|
|
},
|
|
|
|
|
privileges: function(next) {
|
|
|
|
|
if (privilegeCache[topicData.cid]) {
|
|
|
|
|
return next(null, privilegeCache[topicData.cid]);
|
|
|
|
|
}
|
|
|
|
|
privileges.categories.get(topicData.cid, uid, next);
|
|
|
|
|
hasRead: function(next) {
|
|
|
|
|
Topics.hasReadTopics(tids, uid, next);
|
|
|
|
|
},
|
|
|
|
|
categoryData: function(next) {
|
|
|
|
|
if (categoryCache[topicData.cid]) {
|
|
|
|
|
return next(null, categoryCache[topicData.cid]);
|
|
|
|
|
}
|
|
|
|
|
categories.getCategoryFields(topicData.cid, ['name', 'slug', 'icon', 'bgColor', 'color', 'disabled'], next);
|
|
|
|
|
isAdminOrMod: function(next) {
|
|
|
|
|
privileges.categories.isAdminOrMod(cids, uid, next);
|
|
|
|
|
},
|
|
|
|
|
user: function(next) {
|
|
|
|
|
if (userCache[topicData.uid]) {
|
|
|
|
|
return next(null, userCache[topicData.uid]);
|
|
|
|
|
}
|
|
|
|
|
user.getUserFields(topicData.uid, ['username', 'userslug', 'picture'], next);
|
|
|
|
|
teasers: function(next) {
|
|
|
|
|
Topics.getTeasers(tids, next);
|
|
|
|
|
},
|
|
|
|
|
tags: function(next) {
|
|
|
|
|
Topics.getTopicTagsObjects(topicData.tid, next);
|
|
|
|
|
Topics.getTopicsTagsObjects(tids, next);
|
|
|
|
|
}
|
|
|
|
|
}, function(err, topicInfo) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}, function(err, results) {
|
|
|
|
|
function arrayToObject(array, field) {
|
|
|
|
|
var obj = {};
|
|
|
|
|
for (var i=0; i<array.length; ++i) {
|
|
|
|
|
obj[array[i][field]] = array[i];
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
privilegeCache[topicData.cid] = topicInfo.privileges;
|
|
|
|
|
categoryCache[topicData.cid] = topicInfo.categoryData;
|
|
|
|
|
userCache[topicData.uid] = topicInfo.user;
|
|
|
|
|
|
|
|
|
|
if (!isTopicVisible(topicData, topicInfo)) {
|
|
|
|
|
return next(null, null);
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
topicData.tags = topicInfo.tags;
|
|
|
|
|
|
|
|
|
|
next(null, topicData);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Topics.getTopicsData(tids, function(err, topics) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
var users = arrayToObject(results.users, 'uid');
|
|
|
|
|
var categories = arrayToObject(results.categories, 'cid');
|
|
|
|
|
var isAdminOrMod = {};
|
|
|
|
|
cids.forEach(function(cid, index) {
|
|
|
|
|
isAdminOrMod[cid] = results.isAdminOrMod[index];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
async.mapSeries(topics, loadTopicInfo, function(err, topics) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
for (var i=0; i<topics.length; ++i) {
|
|
|
|
|
topics[i].category = categories[topics[i].cid];
|
|
|
|
|
topics[i].category.disabled = parseInt(topics[i].category.disabled, 10) === 1;
|
|
|
|
|
topics[i].user = users[topics[i].uid];
|
|
|
|
|
topics[i].teaser = results.teasers[i];
|
|
|
|
|
topics[i].tags = results.tags[i];
|
|
|
|
|
|
|
|
|
|
topics[i].pinned = parseInt(topics[i].pinned, 10) === 1;
|
|
|
|
|
topics[i].locked = parseInt(topics[i].locked, 10) === 1;
|
|
|
|
|
topics[i].deleted = parseInt(topics[i].deleted, 10) === 1;
|
|
|
|
|
topics[i].unread = !(results.hasRead[i] && parseInt(uid, 10) !== 0);
|
|
|
|
|
topics[i].unreplied = parseInt(topics[i].postcount, 10) <= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
topics = topics.filter(function(topic) {
|
|
|
|
|
return !!topic;
|
|
|
|
|
return !topic.category.disabled ||
|
|
|
|
|
!topic.deleted || (topic.deleted && isAdminOrMod[topic.cid]) ||
|
|
|
|
|
parseInt(topic.uid, 10) === parseInt(uid, 10);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
plugins.fireHook('filter:topics.get', topics, callback);
|
|
|
|
@ -336,12 +323,71 @@ var async = require('async'),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Topics.getTeasers = function(tids, callback) {
|
|
|
|
|
|
|
|
|
|
if(!Array.isArray(tids)) {
|
|
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.map(tids, Topics.getTeaser, callback);
|
|
|
|
|
async.map(tids, function(tid, next) {
|
|
|
|
|
db.getSortedSetRevRange('tid:' + tid + ':posts', 0, 0, function(err, data) {
|
|
|
|
|
next(err, Array.isArray(data) && data.length ? data[0] : null);
|
|
|
|
|
});
|
|
|
|
|
}, function(err, pids) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var postKeys = pids.map(function(pid) {
|
|
|
|
|
return 'post:' + pid;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
async.parallel({
|
|
|
|
|
indices: function(next) {
|
|
|
|
|
var sets = tids.map(function(tid) {
|
|
|
|
|
return 'tid:' + tid + ':posts';
|
|
|
|
|
});
|
|
|
|
|
db.sortedSetsRanks(sets, pids, next);
|
|
|
|
|
},
|
|
|
|
|
posts: function(next) {
|
|
|
|
|
db.getObjectsFields(postKeys, ['pid', 'uid', 'timestamp'], next);
|
|
|
|
|
}
|
|
|
|
|
}, function(err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var indices = results.indices.map(function(index) {
|
|
|
|
|
if (!utils.isNumber(index)) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return parseInt(index, 10) + 2;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var uids = results.posts.map(function(post) {
|
|
|
|
|
return post.uid;
|
|
|
|
|
}).filter(function(uid, index, array) {
|
|
|
|
|
return array.indexOf(uid) === index;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], function(err, userData) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var users = {};
|
|
|
|
|
userData.forEach(function(user) {
|
|
|
|
|
users[user.uid] = user;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
results.posts.forEach(function(post, index) {
|
|
|
|
|
post.user = users[post.uid];
|
|
|
|
|
post.index = indices[index];
|
|
|
|
|
post.timestamp = utils.toISOString(post.timestamp);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
callback(err, results.posts);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Topics.getTeaser = function(tid, callback) {
|
|
|
|
|