Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted src/categories/topics from JS to TS #94

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 43 additions & 43 deletions src/categories/topics.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
'use strict';

"use strict";
// plugins.d.ts
const db = require('../database');
const topics = require('../topics');
const plugins = require('../plugins');
const meta = require('../meta');
const privileges = require('../privileges');
const user = require('../user');

module.exports = function (Categories) {
async function filterScheduledTids(tids) {
const scores = await db.sortedSetScores('topics:scheduled', tids);
const now = Date.now();
return tids.filter((tid, index) => tid && (!scores[index] || scores[index] <= now));
}
Categories.getCategoryTopics = async function (data) {
let results = await plugins.hooks.fire('filter:category.topics.prepare', data);
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);

results = await plugins.hooks.fire('filter:category.topics.get', { cid: data.cid, topics: topicsData, uid: data.uid });
results = await plugins.hooks.fire('filter:category.topics.get', {
cid: data.cid,
topics: topicsData,
uid: data.uid,
});
return { topics: results.topics, nextStart: data.stop + 1 };
};

Categories.getTopicIds = async function (data) {
const dataForPinned = { ...data };
const dataForPinned = Object.assign({}, data);
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;
}

if (plugins.hooks.hasListeners('filter:categories.getTopicIds')) {
const result = await plugins.hooks.fire('filter:categories.getTopicIds', {
tids: [],
Expand All @@ -55,25 +56,23 @@ module.exports = function (Categories) {
});
return result && result.tids;
}

let { start } = data;
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 {
}
else {
normalTids = await db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop);
}
normalTids = normalTids.filter(tid => !pinnedTids.includes(tid));
normalTids = normalTids.filter(tid => pinnedTids.indexOf(tid) === -1);
return pinnedTidsOnPage.concat(normalTids);
};

Categories.getTopicCount = async function (data) {
if (plugins.hooks.hasListeners('filter:categories.getTopicCount')) {
const result = await plugins.hooks.fire('filter:categories.getTopicCount', {
Expand All @@ -85,58 +84,67 @@ module.exports = function (Categories) {
const set = await Categories.buildTopicsSortedSet(data);
if (Array.isArray(set)) {
return await db.sortedSetIntersectCard(set);
} else if (data.targetUid && set) {
}
else if (data.targetUid && set) {
return await db.sortedSetCard(set);
}
return data.category.topic_count;
};

Categories.buildTopicsSortedSet = async function (data) {
const { cid } = data;
let set = `cid:${cid}:tids`;
const sort = data.sort || (data.settings && data.settings.categoryTopicSort) || meta.config.categoryTopicSort || 'newest_to_oldest';

const sort = data.sort ||
(data.settings && data.settings.categoryTopicSort) ||
meta.config.categoryTopicSort ||
'newest_to_oldest';
if (sort === 'most_posts') {
set = `cid:${cid}:tids:posts`;
} else if (sort === 'most_votes') {
}
else if (sort === 'most_votes') {
set = `cid:${cid}:tids:votes`;
} else if (sort === 'most_views') {
}
else if (sort === 'most_views') {
set = `cid:${cid}:tids:views`;
}

if (data.tag) {
if (Array.isArray(data.tag)) {
set = [set].concat(data.tag.map(tag => `tag:${tag}:topics`));
} else {
}
else {
set = [set, `tag:${data.tag}:topics`];
}
}

if (data.targetUid) {
set = (Array.isArray(set) ? set : [set]).concat([`cid:${cid}:uid:${data.targetUid}:tids`]);
set = (Array.isArray(set) ? set : [set]).concat([
`cid:${cid}:uid:${data.targetUid}:tids`,
]);
}

const result = await plugins.hooks.fire('filter:categories.buildTopicsSortedSet', {
set: set,
data: data,
});
return result && result.set;
};

Categories.getSortedSetRangeDirection = async function (sort) {
sort = sort || 'newest_to_oldest';
const direction = ['newest_to_oldest', 'most_posts', 'most_votes', 'most_views'].includes(sort) ? 'highest-to-lowest' : 'lowest-to-highest';
const directionOptions = [
'newest_to_oldest',
'most_posts',
'most_votes',
'most_views',
];
const direction = directionOptions.indexOf(sort) !== -1 ?
'highest-to-lowest' :
'lowest-to-highest';
const result = await plugins.hooks.fire('filter:categories.getSortedSetRangeDirection', {
sort: sort,
direction: direction,
});
return result && result.direction;
};

Categories.getAllTopicIds = async function (cid, start, stop) {
return await db.getSortedSetRange([`cid:${cid}:tids:pinned`, `cid:${cid}:tids`], start, stop);
};

Categories.getPinnedTids = async function (data) {
if (plugins.hooks.hasListeners('filter:categories.getPinnedTids')) {
const result = await plugins.hooks.fire('filter:categories.getPinnedTids', {
Expand All @@ -149,16 +157,15 @@ module.exports = function (Categories) {
db.getSortedSetRevRange(`cid:${data.cid}:tids:pinned`, data.start, data.stop),
privileges.categories.can('topics:schedule', data.cid, data.uid),
]);
const pinnedTids = canSchedule ? allPinnedTids : await filterScheduledTids(allPinnedTids);

const pinnedTids = canSchedule ?
allPinnedTids :
await filterScheduledTids(allPinnedTids);
return await topics.tools.checkPinExpiry(pinnedTids);
};

Categories.modifyTopicsByPrivilege = function (topics, privileges) {
if (!Array.isArray(topics) || !topics.length || privileges.view_deleted) {
return;
}

topics.forEach((topic) => {
if (!topic.scheduled && topic.deleted && !topic.isOwner) {
topic.title = '[[topic:topic_is_deleted]]';
Expand All @@ -172,7 +179,6 @@ module.exports = function (Categories) {
}
});
};

Categories.onNewPostMade = async function (cid, pinned, postData) {
if (!cid || !postData) {
return;
Expand All @@ -187,10 +193,4 @@ module.exports = function (Categories) {
await Promise.all(promises);
await Categories.updateRecentTidForCid(cid);
};

async function filterScheduledTids(tids) {
const scores = await db.sortedSetScores('topics:scheduled', tids);
const now = Date.now();
return tids.filter((tid, index) => tid && (!scores[index] || scores[index] <= now));
}
};
Loading