2014-11-08 23:54:21 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-08-22 21:16:37 -04:00
|
|
|
const db = require('../database');
|
|
|
|
|
const topics = require('../topics');
|
|
|
|
|
const plugins = require('../plugins');
|
|
|
|
|
const meta = require('../meta');
|
|
|
|
|
const user = require('../user');
|
2014-11-08 23:54:21 -05:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Categories) {
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.getCategoryTopics = async function (data) {
|
2020-11-20 16:06:26 -05:00
|
|
|
let results = await plugins.hooks.fire('filter:category.topics.prepare', data);
|
2019-07-16 00:41:42 -04:00
|
|
|
const tids = await Categories.getTopicIds(results);
|
|
|
|
|
let topicsData = await topics.getTopicsByTids(tids, data.uid);
|
|
|
|
|
topicsData = await user.blocks.filter(data.uid, topicsData);
|
|
|
|
|
|
|
|
|
|
if (!topicsData.length) {
|
|
|
|
|
return { topics: [], uid: data.uid };
|
|
|
|
|
}
|
|
|
|
|
topics.calculateTopicIndices(topicsData, data.start);
|
|
|
|
|
|
2020-11-20 16:06:26 -05:00
|
|
|
results = await plugins.hooks.fire('filter:category.topics.get', { cid: data.cid, topics: topicsData, uid: data.uid });
|
2019-07-16 00:41:42 -04:00
|
|
|
return { topics: results.topics, nextStart: data.stop + 1 };
|
2016-01-27 20:36:40 +02:00
|
|
|
};
|
2015-09-25 01:09:14 -04:00
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.getTopicIds = async function (data) {
|
2020-10-26 10:43:18 -04:00
|
|
|
const dataForPinned = { ...data };
|
2019-07-16 00:41:42 -04:00
|
|
|
dataForPinned.start = 0;
|
|
|
|
|
dataForPinned.stop = -1;
|
|
|
|
|
|
|
|
|
|
const [pinnedTids, set, direction] = await Promise.all([
|
|
|
|
|
Categories.getPinnedTids(dataForPinned),
|
|
|
|
|
Categories.buildTopicsSortedSet(data),
|
|
|
|
|
Categories.getSortedSetRangeDirection(data.sort),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const totalPinnedCount = pinnedTids.length;
|
|
|
|
|
const pinnedTidsOnPage = pinnedTids.slice(data.start, data.stop !== -1 ? data.stop + 1 : undefined);
|
|
|
|
|
const pinnedCountOnPage = pinnedTidsOnPage.length;
|
|
|
|
|
const topicsPerPage = data.stop - data.start + 1;
|
|
|
|
|
const normalTidsToGet = Math.max(0, topicsPerPage - pinnedCountOnPage);
|
|
|
|
|
|
|
|
|
|
if (!normalTidsToGet && data.stop !== -1) {
|
|
|
|
|
return pinnedTidsOnPage;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-20 16:06:26 -05:00
|
|
|
if (plugins.hooks.hasListeners('filter:categories.getTopicIds')) {
|
|
|
|
|
const result = await plugins.hooks.fire('filter:categories.getTopicIds', {
|
2019-07-16 00:41:42 -04:00
|
|
|
tids: [],
|
|
|
|
|
data: data,
|
|
|
|
|
pinnedTids: pinnedTidsOnPage,
|
|
|
|
|
allPinnedTids: pinnedTids,
|
|
|
|
|
totalPinnedCount: totalPinnedCount,
|
|
|
|
|
normalTidsToGet: normalTidsToGet,
|
|
|
|
|
});
|
|
|
|
|
return result && result.tids;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-06 14:10:15 -07:00
|
|
|
let { start } = data;
|
2019-07-16 00:41:42 -04:00
|
|
|
if (start > 0 && totalPinnedCount) {
|
|
|
|
|
start -= totalPinnedCount - pinnedCountOnPage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const stop = data.stop === -1 ? data.stop : start + normalTidsToGet - 1;
|
|
|
|
|
let normalTids;
|
|
|
|
|
const reverse = direction === 'highest-to-lowest';
|
|
|
|
|
if (Array.isArray(set)) {
|
|
|
|
|
const weights = set.map((s, index) => (index ? 0 : 1));
|
|
|
|
|
normalTids = await db[reverse ? 'getSortedSetRevIntersect' : 'getSortedSetIntersect']({ sets: set, start: start, stop: stop, weights: weights });
|
|
|
|
|
} else {
|
|
|
|
|
normalTids = await db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop);
|
|
|
|
|
}
|
|
|
|
|
normalTids = normalTids.filter(tid => !pinnedTids.includes(tid));
|
2019-08-22 21:14:18 -04:00
|
|
|
return pinnedTidsOnPage.concat(normalTids);
|
2016-11-25 17:46:29 +03:00
|
|
|
};
|
|
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.getTopicCount = async function (data) {
|
2020-11-20 16:06:26 -05:00
|
|
|
if (plugins.hooks.hasListeners('filter:categories.getTopicCount')) {
|
|
|
|
|
const result = await plugins.hooks.fire('filter:categories.getTopicCount', {
|
2017-06-06 16:40:32 -04:00
|
|
|
topicCount: data.category.topic_count,
|
|
|
|
|
data: data,
|
|
|
|
|
});
|
2019-07-16 00:41:42 -04:00
|
|
|
return result && result.topicCount;
|
2017-06-06 16:40:32 -04:00
|
|
|
}
|
2019-07-16 00:41:42 -04:00
|
|
|
const set = await Categories.buildTopicsSortedSet(data);
|
|
|
|
|
if (Array.isArray(set)) {
|
|
|
|
|
return await db.sortedSetIntersectCard(set);
|
2020-09-21 22:24:38 -04:00
|
|
|
} else if (data.targetUid && set) {
|
|
|
|
|
return await db.sortedSetCard(set);
|
2019-07-16 00:41:42 -04:00
|
|
|
}
|
|
|
|
|
return data.category.topic_count;
|
2017-06-06 16:40:32 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.buildTopicsSortedSet = async function (data) {
|
2021-02-06 14:10:15 -07:00
|
|
|
const { cid } = data;
|
2021-02-03 23:59:08 -07:00
|
|
|
let set = `cid:${cid}:tids`;
|
2019-08-22 21:16:37 -04:00
|
|
|
const sort = data.sort || (data.settings && data.settings.categoryTopicSort) || meta.config.categoryTopicSort || 'newest_to_oldest';
|
2017-06-06 16:40:32 -04:00
|
|
|
|
|
|
|
|
if (sort === 'most_posts') {
|
2021-02-03 23:59:08 -07:00
|
|
|
set = `cid:${cid}:tids:posts`;
|
2017-12-10 11:02:48 -05:00
|
|
|
} else if (sort === 'most_votes') {
|
2021-02-03 23:59:08 -07:00
|
|
|
set = `cid:${cid}:tids:votes`;
|
2017-06-06 16:40:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.tag) {
|
|
|
|
|
if (Array.isArray(data.tag)) {
|
2021-02-03 23:59:08 -07:00
|
|
|
set = [set].concat(data.tag.map(tag => `tag:${tag}:topics`));
|
2017-06-06 16:40:32 -04:00
|
|
|
} else {
|
2021-02-03 23:59:08 -07:00
|
|
|
set = [set, `tag:${data.tag}:topics`];
|
2017-06-06 16:40:32 -04:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-17 13:14:55 -04:00
|
|
|
|
|
|
|
|
if (data.targetUid) {
|
|
|
|
|
set = (Array.isArray(set) ? set : [set]).concat([`cid:${cid}:uid:${data.targetUid}:tids`]);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-20 16:06:26 -05:00
|
|
|
const result = await plugins.hooks.fire('filter:categories.buildTopicsSortedSet', {
|
2017-07-13 16:09:01 -04:00
|
|
|
set: set,
|
|
|
|
|
data: data,
|
|
|
|
|
});
|
2019-07-16 00:41:42 -04:00
|
|
|
return result && result.set;
|
2017-06-06 16:40:32 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.getSortedSetRangeDirection = async function (sort) {
|
2017-06-06 16:40:32 -04:00
|
|
|
sort = sort || 'newest_to_oldest';
|
2019-08-22 21:16:37 -04:00
|
|
|
const direction = sort === 'newest_to_oldest' || sort === 'most_posts' || sort === 'most_votes' ? 'highest-to-lowest' : 'lowest-to-highest';
|
2020-11-20 16:06:26 -05:00
|
|
|
const result = await plugins.hooks.fire('filter:categories.getSortedSetRangeDirection', {
|
2017-07-13 16:09:01 -04:00
|
|
|
sort: sort,
|
|
|
|
|
direction: direction,
|
|
|
|
|
});
|
2019-07-16 00:41:42 -04:00
|
|
|
return result && result.direction;
|
2017-06-06 16:40:32 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.getAllTopicIds = async function (cid, start, stop) {
|
2021-02-03 23:59:08 -07:00
|
|
|
return await db.getSortedSetRange([`cid:${cid}:tids:pinned`, `cid:${cid}:tids`], start, stop);
|
2016-11-25 17:46:29 +03:00
|
|
|
};
|
|
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.getPinnedTids = async function (data) {
|
2020-11-20 16:06:26 -05:00
|
|
|
if (plugins.hooks.hasListeners('filter:categories.getPinnedTids')) {
|
|
|
|
|
const result = await plugins.hooks.fire('filter:categories.getPinnedTids', {
|
2017-06-26 16:51:45 -04:00
|
|
|
pinnedTids: [],
|
|
|
|
|
data: data,
|
|
|
|
|
});
|
2019-07-16 00:41:42 -04:00
|
|
|
return result && result.pinnedTids;
|
2017-06-26 16:51:45 -04:00
|
|
|
}
|
2021-02-03 23:59:08 -07:00
|
|
|
const pinnedTids = await db.getSortedSetRevRange(`cid:${data.cid}:tids:pinned`, data.start, data.stop);
|
2020-11-20 14:05:02 -05:00
|
|
|
return await topics.tools.checkPinExpiry(pinnedTids);
|
2016-11-25 17:46:29 +03:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Categories.modifyTopicsByPrivilege = function (topics, privileges) {
|
2021-03-21 21:38:08 -04:00
|
|
|
if (!Array.isArray(topics) || !topics.length || privileges.view_deleted) {
|
2016-01-27 20:36:40 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-04 00:01:39 -07:00
|
|
|
topics.forEach((topic) => {
|
2016-01-27 20:36:40 +02:00
|
|
|
if (topic.deleted && !topic.isOwner) {
|
|
|
|
|
topic.title = '[[topic:topic_is_deleted]]';
|
|
|
|
|
topic.slug = topic.tid;
|
|
|
|
|
topic.teaser = null;
|
|
|
|
|
topic.noAnchor = true;
|
2016-02-24 11:35:53 +02:00
|
|
|
topic.tags = [];
|
2016-01-27 20:36:40 +02:00
|
|
|
}
|
2014-11-16 16:15:49 -05:00
|
|
|
});
|
2014-11-08 23:54:21 -05:00
|
|
|
};
|
|
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.onNewPostMade = async function (cid, pinned, postData) {
|
2015-03-15 01:12:13 -04:00
|
|
|
if (!cid || !postData) {
|
2019-07-16 00:41:42 -04:00
|
|
|
return;
|
2015-03-15 01:12:13 -04:00
|
|
|
}
|
2019-07-16 00:41:42 -04:00
|
|
|
const promises = [
|
2021-02-03 23:59:08 -07:00
|
|
|
db.sortedSetAdd(`cid:${cid}:pids`, postData.timestamp, postData.pid),
|
|
|
|
|
db.incrObjectField(`category:${cid}`, 'post_count'),
|
2019-07-16 00:41:42 -04:00
|
|
|
];
|
|
|
|
|
if (!pinned) {
|
2021-02-03 23:59:08 -07:00
|
|
|
promises.push(db.sortedSetIncrBy(`cid:${cid}:tids:posts`, 1, postData.tid));
|
2019-07-16 00:41:42 -04:00
|
|
|
}
|
|
|
|
|
await Promise.all(promises);
|
2019-07-26 14:23:10 -04:00
|
|
|
await Categories.updateRecentTidForCid(cid);
|
2014-11-08 23:54:21 -05:00
|
|
|
};
|
|
|
|
|
};
|