2014-03-21 17:04:15 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2016-02-24 21:35:35 +02:00
|
|
|
var db = require('../database');
|
2016-07-07 17:17:17 -04:00
|
|
|
var plugins = require('../plugins');
|
2018-10-11 16:00:22 -04:00
|
|
|
var posts = require('../posts');
|
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
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
Topics.getRecentTopics = async function (cid, uid, start, stop, filter) {
|
|
|
|
|
return await Topics.getSortedTopics({
|
2018-06-18 02:27:07 -04:00
|
|
|
cids: cid,
|
|
|
|
|
uid: uid,
|
|
|
|
|
start: start,
|
|
|
|
|
stop: stop,
|
|
|
|
|
filter: filter,
|
|
|
|
|
sort: 'recent',
|
2019-07-09 12:46:49 -04:00
|
|
|
});
|
2016-11-03 18:02:10 +03:00
|
|
|
};
|
|
|
|
|
|
2017-07-06 15:42:37 -04:00
|
|
|
/* not an orphan method, used in widget-essentials */
|
2019-07-09 12:46:49 -04:00
|
|
|
Topics.getLatestTopics = async function (options) {
|
2019-02-22 14:39:05 -05:00
|
|
|
// uid, start, stop, term
|
2019-07-09 12:46:49 -04:00
|
|
|
const tids = await Topics.getLatestTidsFromSet('topics:recent', options.start, options.stop, options.term);
|
|
|
|
|
const topics = await Topics.getTopics(tids, options);
|
|
|
|
|
return { topics: topics, nextStart: options.stop + 1 };
|
2014-03-21 17:04:15 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
Topics.getLatestTidsFromSet = async function (set, start, stop, term) {
|
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;
|
2019-07-09 12:46:49 -04:00
|
|
|
return await db.getSortedSetRevRangeByScore(set, start, count, '+inf', Date.now() - since);
|
2014-03-21 17:04:15 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
Topics.updateLastPostTimeFromLastPid = async function (tid) {
|
|
|
|
|
const pid = await Topics.getLatestUndeletedPid(tid);
|
|
|
|
|
if (!pid) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const timestamp = await posts.getPostField(pid, 'timestamp');
|
|
|
|
|
if (!timestamp) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
await Topics.updateLastPostTime(tid, timestamp);
|
2018-10-11 16:00:22 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
Topics.updateLastPostTime = async function (tid, lastposttime) {
|
|
|
|
|
await Topics.setTopicField(tid, 'lastposttime', lastposttime);
|
|
|
|
|
const topicData = await Topics.getTopicFields(tid, ['cid', 'deleted', 'pinned']);
|
2018-10-13 01:06:07 -04:00
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
await db.sortedSetAdd('cid:' + topicData.cid + ':tids:lastposttime', lastposttime, tid);
|
2018-10-13 01:06:07 -04:00
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
if (!topicData.deleted) {
|
|
|
|
|
await Topics.updateRecent(tid, lastposttime);
|
|
|
|
|
}
|
2014-09-05 17:39:24 -04:00
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
if (!topicData.pinned) {
|
|
|
|
|
await db.sortedSetAdd('cid:' + topicData.cid + ':tids', lastposttime, tid);
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-05-26 16:56:26 -04:00
|
|
|
|
2019-07-09 12:46:49 -04:00
|
|
|
Topics.updateRecent = async function (tid, timestamp) {
|
|
|
|
|
let data = { tid: tid, timestamp: timestamp };
|
|
|
|
|
if (plugins.hasListeners('filter:topics.updateRecent')) {
|
|
|
|
|
data = await plugins.fireHook('filter:topics.updateRecent', { tid: tid, timestamp: timestamp });
|
|
|
|
|
}
|
|
|
|
|
if (data && data.tid && data.timestamp) {
|
|
|
|
|
await db.sortedSetAdd('topics:recent', data.timestamp, data.tid);
|
|
|
|
|
}
|
2014-09-05 17:39:24 -04:00
|
|
|
};
|
2014-03-21 17:04:15 -04:00
|
|
|
};
|