Skip to content

Node.js based forum software built for the modern web

2.9 KB
Raw Blame

'use strict';

const db = require('../database');
const plugins = require('../plugins');
const posts = require('../posts');
const utils = require('../utils');

module.exports = function (Topics) {
	const terms = {
		day: 86400000,
		week: 604800000,
		month: 2592000000,
		year: 31104000000,
	};

	Topics.getRecentTopics = async function (cid, uid, start, stop, filter) {
		return await Topics.getSortedTopics({
			cids: cid,
			uid: uid,
			start: start,
			stop: stop,
			filter: filter,
			sort: 'recent',
		});
	};

	/* not an orphan method, used in widget-essentials */
	Topics.getLatestTopics = async function (options) {
		// uid, start, stop, term
		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 };
	};

	Topics.getSinceFromTerm = function (term) {
		if (terms.hasOwnProperty(term)) {
			return terms[term];
		}
		return terms.day;
	};

	Topics.getLatestTidsFromSet = async function (set, start, stop, term) {
		const since = Topics.getSinceFromTerm(term);
		const count = parseInt(stop, 10) === -1 ? stop : stop - start + 1;
		return await db.getSortedSetRevRangeByScore(set, start, count, '+inf', Date.now() - since);
	};

	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);
	};

	Topics.updateLastPostTime = async function (tid, lastposttime) {
		await Topics.setTopicField(tid, 'lastposttime', lastposttime);
		const topicData = await Topics.getTopicFields(tid, ['cid', 'deleted', 'pinned']);
		const crossposts = await Topics.crossposts.get(tid);
		const crosspostCids = crossposts.map(({ cid }) => cid);
		const cids = [topicData.cid, ...crosspostCids];

		await db.sortedSetsAdd(cids.map(cid => `cid:${cid}:tids:lastposttime`), lastposttime, tid);

		await Topics.updateRecent(tid, lastposttime);

		if (!topicData.pinned) {
			// crossposted categories are bumped too, otherwise the topic is stuck at
			// the position it had when it was crossposted and sinks as others are replied to
			await db.sortedSetsAdd(cids.map(cid => `cid:${cid}:tids`), lastposttime, tid);
		}
	};

	Topics.updateRecent = async function (tid, timestamp) {
		let data = { tid, timestamp };

		// Topics in /world are excluded from /recent
		const cid = await Topics.getTopicField(tid, 'cid');
		if (!utils.isNumber(cid) || cid === -1) {
			return await db.sortedSetRemove('topics:recent', data.tid);
		}

		if (plugins.hooks.hasListeners('filter:topics.updateRecent')) {
			data = await plugins.hooks.fire('filter:topics.updateRecent', data);
		}
		if (data && data.tid && data.timestamp) {
			await db.sortedSetAdd('topics:recent', data.timestamp, data.tid);
		}
	};
};