Skip to content

Commit

Permalink
Added constants of topics functionalities to answered.js and removed …
Browse files Browse the repository at this point in the history
…unused references in functions, minor spacing changes
  • Loading branch information
bingbhakdibhumi committed Feb 10, 2025
1 parent f37ac83 commit 450e190
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions src/topics/answered.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
'use strict';

const _ = require('lodash');

const db = require('../database');
const user = require('../user');
const categories = require('../categories');
const privileges = require('../privileges');
const plugins = require('../plugins');


module.exports = function (Topics) {
Topics.markAsAnswered = async function (uid, tid) {
const topicData = await Topics.getTopicFields(tid, ['answered']);
Expand Down Expand Up @@ -93,10 +100,8 @@ module.exports = function (Topics) {

params.cutoff = await Topics.unreadCutoff(params.uid);

const [followedTids, ignoredTids, categoryTids, userScores, tids_unread] = await Promise.all([
const [followedTids, userScores, tids_unread] = await Promise.all([
getFollowedTids(params),
user.getIgnoredTids(params.uid, 0, -1),
getCategoryTids(params),
db.getSortedSetRevRangeByScoreWithScores(`uid:${params.uid}:tids_read`, 0, -1, '+inf', params.cutoff),
db.getSortedSetRevRangeWithScores(`uid:${params.uid}:tids_unread`, 0, -1),
]);
Expand All @@ -114,26 +119,8 @@ module.exports = function (Topics) {
isTopicsFollowed[t.value] = unreadFollowed[i];
});

const unreadTopics = _.unionWith(categoryTids, followedTids, (a, b) => a.value === b.value)
.filter(t => !ignoredTids.includes(t.value) && (!userReadTimes[t.value] || t.score > userReadTimes[t.value]))
.concat(tids_unread.filter(t => !ignoredTids.includes(t.value)))
.sort((a, b) => b.score - a.score);

let tids = _.uniq(unreadTopics.map(topic => topic.value)).slice(0, 200);

if (!tids.length) {
return { counts, tids, tidsByFilter, unreadCids };
}

const blockedUids = await user.blocks.list(params.uid);

tids = await filterTidsThatHaveBlockedPosts({
uid: params.uid,
tids: tids,
blockedUids: blockedUids,
recentTids: categoryTids,
});

tids = await privileges.topics.filterTids('topics:read', tids, params.uid);

Check failure on line 124 in src/topics/answered.js

View workflow job for this annotation

GitHub Actions / test

'tids' is not defined

Check failure on line 124 in src/topics/answered.js

View workflow job for this annotation

GitHub Actions / test

'tids' is not defined
const topicData = (await Topics.getTopicsFields(tids, ['tid', 'cid', 'uid', 'postcount', 'deleted', 'scheduled', 'tags']))

Check failure on line 125 in src/topics/answered.js

View workflow job for this annotation

GitHub Actions / test

'tids' is not defined
.filter(t => t.scheduled || !t.deleted);
Expand Down Expand Up @@ -182,4 +169,13 @@ module.exports = function (Topics) {
unreadCids: unreadCids,
};
}
};
async function getFollowedTids(params) {
const keys = params.cid ?
params.cid.map(cid => `cid:${cid}:tids:lastposttime`) :
'topics:recent';

const recentTopicData = await db.getSortedSetRevRangeByScoreWithScores(keys, 0, -1, '+inf', params.cutoff);
const isFollowed = await db.isSortedSetMembers(`uid:${params.uid}:followed_tids`, recentTopicData.map(t => t.tid));
return recentTopicData.filter((t, i) => isFollowed[i]);
}
};

0 comments on commit 450e190

Please sign in to comment.