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');
|
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
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2017-07-06 15:42:37 -04:00
|
|
|
/* not an orphan method, used in widget-essentials */
|
2019-02-22 14:39:05 -05:00
|
|
|
Topics.getLatestTopics = function (options, callback) {
|
|
|
|
|
// uid, start, stop, term
|
2014-11-16 16:15:49 -05:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
2019-02-22 14:39:05 -05:00
|
|
|
Topics.getLatestTidsFromSet('topics:recent', options.start, options.stop, options.term, next);
|
2014-11-16 16:15:49 -05:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (tids, next) {
|
2019-02-22 14:39:05 -05:00
|
|
|
Topics.getTopics(tids, options, next);
|
2014-11-16 16:15:49 -05:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (topics, next) {
|
2019-02-22 14:39:05 -05:00
|
|
|
next(null, { topics: topics, nextStart: options.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
|
|
|
};
|
|
|
|
|
|
2018-10-11 16:00:22 -04:00
|
|
|
Topics.updateLastPostTimeFromLastPid = function (tid, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
Topics.getLatestUndeletedPid(tid, next);
|
|
|
|
|
},
|
|
|
|
|
function (pid, next) {
|
2018-10-23 21:36:00 -04:00
|
|
|
if (!pid) {
|
2018-10-11 16:00:22 -04:00
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
posts.getPostField(pid, 'timestamp', next);
|
|
|
|
|
},
|
|
|
|
|
function (timestamp, next) {
|
2018-10-23 21:36:00 -04:00
|
|
|
if (!timestamp) {
|
2018-10-11 16:00:22 -04:00
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
Topics.updateLastPostTime(tid, timestamp, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Topics.updateLastPostTime = function (tid, lastposttime, callback) {
|
2018-10-13 01:06:07 -04:00
|
|
|
async.waterfall([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2018-10-13 01:06:07 -04:00
|
|
|
Topics.setTopicField(tid, 'lastposttime', lastposttime, next);
|
2014-09-19 15:54:13 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2018-10-13 01:06:07 -04:00
|
|
|
Topics.getTopicFields(tid, ['cid', 'deleted', 'pinned'], next);
|
|
|
|
|
},
|
|
|
|
|
function (topicData, next) {
|
|
|
|
|
var tasks = [
|
|
|
|
|
async.apply(db.sortedSetAdd, 'cid:' + topicData.cid + ':tids:lastposttime', lastposttime, tid),
|
|
|
|
|
];
|
|
|
|
|
|
2018-10-25 17:02:59 -04:00
|
|
|
if (!topicData.deleted) {
|
2018-10-13 01:06:07 -04:00
|
|
|
tasks.push(async.apply(Topics.updateRecent, tid, lastposttime));
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 17:02:59 -04:00
|
|
|
if (!topicData.pinned) {
|
2018-10-13 01:06:07 -04:00
|
|
|
tasks.push(async.apply(db.sortedSetAdd, 'cid:' + topicData.cid + ':tids', lastposttime, tid));
|
|
|
|
|
}
|
|
|
|
|
async.series(tasks, 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
|
|
|
};
|