2014-03-21 17:04:15 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2016-02-24 21:35:35 +02:00
|
|
|
var async = require('async');
|
2017-10-30 15:26:12 -04:00
|
|
|
|
2016-02-24 21:35:35 +02:00
|
|
|
var db = require('../database');
|
2016-07-07 17:17:17 -04:00
|
|
|
var plugins = require('../plugins');
|
2016-11-03 18:02:10 +03:00
|
|
|
var privileges = require('../privileges');
|
|
|
|
|
var user = require('../user');
|
2017-04-18 14:21:28 -04:00
|
|
|
var meta = require('../meta');
|
2014-09-19 15:54:13 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Topics) {
|
2014-09-03 15:16:41 -04:00
|
|
|
var terms = {
|
|
|
|
|
day: 86400000,
|
|
|
|
|
week: 604800000,
|
|
|
|
|
month: 2592000000,
|
2017-02-17 19:31:21 -07:00
|
|
|
year: 31104000000,
|
2014-09-03 15:16:41 -04:00
|
|
|
};
|
2014-03-21 17:04:15 -04:00
|
|
|
|
2018-06-18 02:27:07 -04:00
|
|
|
Topics.getSortedTopics = function (params, callback) {
|
|
|
|
|
var data = {
|
2017-02-18 01:27:46 -07:00
|
|
|
nextStart: 0,
|
2018-06-18 02:27:07 -04:00
|
|
|
topicCount: 0,
|
2017-02-17 19:31:21 -07:00
|
|
|
topics: [],
|
2016-11-03 18:02:10 +03:00
|
|
|
};
|
2018-06-18 02:27:07 -04:00
|
|
|
|
|
|
|
|
params.term = params.term || 'alltime';
|
2018-09-06 16:23:40 -04:00
|
|
|
params.sort = params.sort || 'recent';
|
2018-06-18 02:27:07 -04:00
|
|
|
if (params.hasOwnProperty('cids') && params.cids && !Array.isArray(params.cids)) {
|
|
|
|
|
params.cids = [params.cids];
|
2017-10-30 15:26:12 -04:00
|
|
|
}
|
2018-06-18 02:27:07 -04:00
|
|
|
|
2016-11-03 18:02:10 +03:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
2018-06-18 02:27:07 -04:00
|
|
|
getTids(params, next);
|
|
|
|
|
},
|
|
|
|
|
function (tids, next) {
|
|
|
|
|
data.topicCount = tids.length;
|
|
|
|
|
getTopics(tids, params, next);
|
|
|
|
|
},
|
|
|
|
|
function (topicData, next) {
|
|
|
|
|
data.topics = topicData;
|
|
|
|
|
data.nextStart = params.stop + 1;
|
|
|
|
|
next(null, data);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function getTids(params, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
if (params.term === 'alltime') {
|
|
|
|
|
var key = 'topics:' + params.sort;
|
|
|
|
|
if (params.cids) {
|
|
|
|
|
key = params.cids.map(function (cid) {
|
|
|
|
|
if (params.sort === 'recent') {
|
|
|
|
|
return 'cid:' + cid + ':tids:lastposttime';
|
|
|
|
|
} else if (params.sort === 'votes') {
|
|
|
|
|
return 'cid:' + cid + ':tids:votes';
|
|
|
|
|
} else if (params.sort === 'posts') {
|
|
|
|
|
return 'cid:' + cid + ':tids:posts';
|
|
|
|
|
}
|
|
|
|
|
return 'cid:' + cid + ':tids';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.getSortedSetRevRange(key, 0, 199, next);
|
|
|
|
|
} else {
|
|
|
|
|
Topics.getLatestTidsFromSet('topics:tid', 0, -1, params.term, next);
|
2017-10-30 17:07:51 -04:00
|
|
|
}
|
2016-11-03 18:02:10 +03:00
|
|
|
},
|
|
|
|
|
function (tids, next) {
|
2018-06-18 02:27:07 -04:00
|
|
|
if (params.term !== 'alltime') {
|
|
|
|
|
sortTids(tids, params, next);
|
|
|
|
|
} else {
|
|
|
|
|
next(null, tids);
|
|
|
|
|
}
|
2016-11-03 18:02:10 +03:00
|
|
|
},
|
|
|
|
|
function (tids, next) {
|
2018-06-18 02:27:07 -04:00
|
|
|
filterTids(tids, params.uid, params.filter, params.cids, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sortTids(tids, params, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
Topics.getTopicsFields(tids, ['tid', 'lastposttime', 'upvotes', 'downvotes', 'postcount'], next);
|
|
|
|
|
},
|
|
|
|
|
function (topicData, next) {
|
|
|
|
|
var sortFn = sortRecent;
|
|
|
|
|
if (params.sort === 'posts') {
|
|
|
|
|
sortFn = sortPopular;
|
|
|
|
|
} else if (params.sort === 'votes') {
|
|
|
|
|
sortFn = sortVotes;
|
|
|
|
|
}
|
|
|
|
|
tids = topicData.sort(sortFn).map(function (topic) {
|
|
|
|
|
return topic && topic.tid;
|
|
|
|
|
});
|
|
|
|
|
next(null, tids);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sortRecent(a, b) {
|
|
|
|
|
return b.lastposttime - a.lastposttime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sortVotes(a, b) {
|
|
|
|
|
if (parseInt(a.votes, 10) !== parseInt(b.votes, 10)) {
|
|
|
|
|
return b.votes - a.votes;
|
|
|
|
|
}
|
|
|
|
|
return parseInt(b.postcount, 10) - parseInt(a.postcount, 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sortPopular(a, b) {
|
|
|
|
|
if (parseInt(a.postcount, 10) !== parseInt(b.postcount, 10)) {
|
|
|
|
|
return b.postcount - a.postcount;
|
|
|
|
|
}
|
|
|
|
|
return parseInt(b.viewcount, 10) - parseInt(a.viewcount, 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getTopics(tids, params, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
tids = tids.slice(params.start, params.stop !== -1 ? params.stop + 1 : undefined);
|
|
|
|
|
Topics.getTopicsByTids(tids, params.uid, next);
|
2016-11-03 18:02:10 +03:00
|
|
|
},
|
|
|
|
|
function (topicData, next) {
|
2018-05-31 00:16:52 -04:00
|
|
|
topicData.forEach(function (topicObj, i) {
|
2018-06-18 02:27:07 -04:00
|
|
|
topicObj.index = params.start + i;
|
2018-05-31 00:16:52 -04:00
|
|
|
});
|
2018-06-18 02:27:07 -04:00
|
|
|
next(null, topicData);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-11-03 18:02:10 +03:00
|
|
|
], callback);
|
2018-06-18 02:27:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Topics.getRecentTopics = function (cid, uid, start, stop, filter, callback) {
|
|
|
|
|
Topics.getSortedTopics({
|
|
|
|
|
cids: cid,
|
|
|
|
|
uid: uid,
|
|
|
|
|
start: start,
|
|
|
|
|
stop: stop,
|
|
|
|
|
filter: filter,
|
|
|
|
|
sort: 'recent',
|
|
|
|
|
}, callback);
|
2016-11-03 18:02:10 +03:00
|
|
|
};
|
|
|
|
|
|
2018-06-18 02:27:07 -04:00
|
|
|
function filterTids(tids, uid, filter, cids, callback) {
|
2016-11-03 18:02:10 +03:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
if (filter === 'watched') {
|
|
|
|
|
Topics.filterWatchedTids(tids, uid, next);
|
|
|
|
|
} else if (filter === 'new') {
|
|
|
|
|
Topics.filterNewTids(tids, uid, next);
|
2017-10-19 13:53:05 -04:00
|
|
|
} else if (filter === 'unreplied') {
|
|
|
|
|
Topics.filterUnrepliedTids(tids, next);
|
2016-11-03 18:02:10 +03:00
|
|
|
} else {
|
|
|
|
|
Topics.filterNotIgnoredTids(tids, uid, next);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
function (tids, next) {
|
|
|
|
|
privileges.topics.filterTids('read', tids, uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (tids, next) {
|
|
|
|
|
async.parallel({
|
|
|
|
|
ignoredCids: function (next) {
|
2017-04-18 14:21:28 -04:00
|
|
|
if (filter === 'watched' || parseInt(meta.config.disableRecentCategoryFilter, 10) === 1) {
|
2016-11-03 18:02:10 +03:00
|
|
|
return next(null, []);
|
|
|
|
|
}
|
|
|
|
|
user.getIgnoredCategories(uid, next);
|
|
|
|
|
},
|
|
|
|
|
topicData: function (next) {
|
2018-04-20 14:48:10 -04:00
|
|
|
Topics.getTopicsFields(tids, ['uid', 'tid', 'cid'], next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-11-03 18:02:10 +03:00
|
|
|
}, next);
|
|
|
|
|
},
|
2018-04-20 14:48:10 -04:00
|
|
|
function (results, next) {
|
|
|
|
|
user.blocks.filter(uid, results.topicData, function (err, filtered) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results.topicData = filtered;
|
|
|
|
|
next(null, results);
|
|
|
|
|
});
|
|
|
|
|
},
|
2016-11-03 18:02:10 +03:00
|
|
|
function (results, next) {
|
2018-06-18 02:27:07 -04:00
|
|
|
cids = cids && cids.map(String);
|
2016-11-03 18:02:10 +03:00
|
|
|
tids = results.topicData.filter(function (topic) {
|
2017-10-05 11:39:35 -04:00
|
|
|
if (topic && topic.cid) {
|
2018-06-18 02:27:07 -04:00
|
|
|
return !results.ignoredCids.includes(topic.cid.toString()) && (!cids || (cids.length && cids.includes(topic.cid.toString())));
|
2016-11-03 18:02:10 +03:00
|
|
|
}
|
2017-02-18 14:27:26 -07:00
|
|
|
return false;
|
2016-11-03 18:02:10 +03:00
|
|
|
}).map(function (topic) {
|
|
|
|
|
return topic.tid;
|
|
|
|
|
});
|
|
|
|
|
next(null, tids);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-11-03 18:02:10 +03:00
|
|
|
], callback);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-06 15:42:37 -04:00
|
|
|
/* not an orphan method, used in widget-essentials */
|
2016-10-13 11:43:39 +02:00
|
|
|
Topics.getLatestTopics = function (uid, start, stop, term, callback) {
|
2014-11-16 16:15:49 -05:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
2015-03-31 23:40:58 -04:00
|
|
|
Topics.getLatestTidsFromSet('topics:recent', start, stop, term, next);
|
2014-11-16 16:15:49 -05:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (tids, next) {
|
2014-11-16 16:15:49 -05:00
|
|
|
Topics.getTopics(tids, uid, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (topics, next) {
|
2017-02-18 12:30:49 -07:00
|
|
|
next(null, { topics: topics, nextStart: stop + 1 });
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2014-11-16 16:15:49 -05:00
|
|
|
], callback);
|
2014-03-21 17:04:15 -04:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Topics.getLatestTidsFromSet = function (set, start, stop, term, callback) {
|
2014-03-21 17:04:15 -04:00
|
|
|
var since = terms.day;
|
2014-09-03 15:16:41 -04:00
|
|
|
if (terms[term]) {
|
2014-03-21 17:04:15 -04:00
|
|
|
since = terms[term];
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 23:40:58 -04:00
|
|
|
var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1;
|
2014-03-21 17:04:15 -04:00
|
|
|
|
2015-01-07 13:35:49 -05:00
|
|
|
db.getSortedSetRevRangeByScore(set, start, count, '+inf', Date.now() - since, callback);
|
2014-03-21 17:04:15 -04:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Topics.updateTimestamp = function (tid, timestamp, callback) {
|
2014-09-19 15:54:13 -04:00
|
|
|
async.parallel([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2017-10-30 17:07:51 -04:00
|
|
|
var topicData;
|
2016-02-24 21:35:35 +02:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
2017-10-30 17:07:51 -04:00
|
|
|
Topics.getTopicFields(tid, ['cid', 'deleted'], next);
|
|
|
|
|
},
|
|
|
|
|
function (_topicData, next) {
|
|
|
|
|
topicData = _topicData;
|
|
|
|
|
db.sortedSetAdd('cid:' + topicData.cid + ':tids:lastposttime', timestamp, tid, next);
|
2016-02-24 21:35:35 +02:00
|
|
|
},
|
2017-10-30 17:07:51 -04:00
|
|
|
function (next) {
|
|
|
|
|
if (parseInt(topicData.deleted, 10) === 1) {
|
2016-02-24 21:35:35 +02:00
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
Topics.updateRecent(tid, timestamp, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-02-24 21:35:35 +02:00
|
|
|
], next);
|
2014-09-19 15:54:13 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2014-09-19 15:54:13 -04:00
|
|
|
Topics.setTopicField(tid, 'lastposttime', timestamp, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
], function (err) {
|
2015-09-01 12:38:26 -04:00
|
|
|
callback(err);
|
|
|
|
|
});
|
2014-09-05 17:39:24 -04:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Topics.updateRecent = function (tid, timestamp, callback) {
|
|
|
|
|
callback = callback || function () {};
|
2017-05-26 16:56:26 -04:00
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
if (plugins.hasListeners('filter:topics.updateRecent')) {
|
|
|
|
|
plugins.fireHook('filter:topics.updateRecent', { tid: tid, timestamp: timestamp }, next);
|
|
|
|
|
} else {
|
|
|
|
|
next(null, { tid: tid, timestamp: timestamp });
|
2016-07-08 01:26:48 +03:00
|
|
|
}
|
2017-05-26 16:56:26 -04:00
|
|
|
},
|
|
|
|
|
function (data, next) {
|
2016-07-08 01:26:48 +03:00
|
|
|
if (data && data.tid && data.timestamp) {
|
2017-05-26 16:56:26 -04:00
|
|
|
db.sortedSetAdd('topics:recent', data.timestamp, data.tid, next);
|
2016-07-08 01:26:48 +03:00
|
|
|
} else {
|
2017-05-26 16:56:26 -04:00
|
|
|
next();
|
2016-07-08 01:26:48 +03:00
|
|
|
}
|
2017-05-26 16:56:26 -04:00
|
|
|
},
|
|
|
|
|
], callback);
|
2014-09-05 17:39:24 -04:00
|
|
|
};
|
2014-03-21 17:04:15 -04:00
|
|
|
};
|