Files
NodeBB/src/topics/recent.js

174 lines
4.5 KiB
JavaScript
Raw Normal View History

2014-03-21 17:04:15 -04:00
'use strict';
var async = require('async');
2017-10-30 15:26:12 -04:00
var db = require('../database');
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
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
2016-11-03 18:02:10 +03:00
Topics.getRecentTopics = function (cid, uid, start, stop, filter, callback) {
var recentTopics = {
2017-02-18 01:27:46 -07:00
nextStart: 0,
2017-02-17 19:31:21 -07:00
topics: [],
2016-11-03 18:02:10 +03:00
};
2017-10-30 15:26:12 -04:00
if (cid && !Array.isArray(cid)) {
cid = [cid];
}
2016-11-03 18:02:10 +03:00
async.waterfall([
function (next) {
2017-10-30 17:07:51 -04:00
var key = 'topics:recent';
if (cid) {
key = cid.map(function (cid) {
return 'cid:' + cid + ':tids:lastposttime';
});
}
db.getSortedSetRevRange(key, 0, 199, next);
2016-11-03 18:02:10 +03:00
},
function (tids, next) {
2017-10-30 15:26:12 -04:00
filterTids(tids, uid, filter, cid, next);
2016-11-03 18:02:10 +03:00
},
function (tids, next) {
recentTopics.topicCount = tids.length;
tids = tids.slice(start, stop + 1);
Topics.getTopicsByTids(tids, uid, next);
},
function (topicData, next) {
recentTopics.topics = topicData;
recentTopics.nextStart = stop + 1;
next(null, recentTopics);
2017-02-17 19:31:21 -07:00
},
2016-11-03 18:02:10 +03:00
], callback);
};
2017-10-30 15:26:12 -04:00
function filterTids(tids, uid, filter, cid, 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) {
Topics.getTopicsFields(tids, ['tid', 'cid'], next);
2017-02-17 19:31:21 -07:00
},
2016-11-03 18:02:10 +03:00
}, next);
},
function (results, next) {
2017-10-30 15:26:12 -04:00
cid = cid && cid.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) {
2017-10-30 15:26:12 -04:00
return results.ignoredCids.indexOf(topic.cid.toString()) === -1 && (!cid || (cid.length && cid.indexOf(topic.cid.toString()) !== -1));
2016-11-03 18:02:10 +03: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 */
Topics.getLatestTopics = function (uid, start, stop, term, callback) {
async.waterfall([
function (next) {
Topics.getLatestTidsFromSet('topics:recent', start, stop, term, next);
},
function (tids, next) {
Topics.getTopics(tids, uid, next);
},
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
},
], callback);
2014-03-21 17:04:15 -04: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];
}
var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1;
2014-03-21 17:04:15 -04:00
db.getSortedSetRevRangeByScore(set, start, count, '+inf', Date.now() - since, callback);
2014-03-21 17:04:15 -04:00
};
Topics.updateTimestamp = function (tid, timestamp, callback) {
2014-09-19 15:54:13 -04:00
async.parallel([
function (next) {
2017-10-30 17:07:51 -04:00
var topicData;
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);
},
2017-10-30 17:07:51 -04:00
function (next) {
if (parseInt(topicData.deleted, 10) === 1) {
return next();
}
Topics.updateRecent(tid, timestamp, next);
2017-02-17 19:31:21 -07:00
},
], next);
2014-09-19 15:54:13 -04: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
},
], function (err) {
callback(err);
});
2014-09-05 17:39:24 -04: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
};