• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CMU-17313Q / fall23-nodebb-inshallah-a / 6239420468

19 Sep 2023 06:18PM UTC coverage: 77.199% (+0.01%) from 77.188%
6239420468

push

github

web-flow
Merge pull request #59 from CMU-17313Q/Adding-isAnonymous-if-conditions

Adding is anonymous if conditions

9091 of 13356 branches covered (0.0%)

Branch coverage included in aggregate %.

20876 of 25462 relevant lines covered (81.99%)

2463.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

13.46
/src/topics/suggested.js
1

2
'use strict';
3

4
const _ = require('lodash');
20✔
5

6
const db = require('../database');
20✔
7
const user = require('../user');
20✔
8
const privileges = require('../privileges');
20✔
9
const search = require('../search');
20✔
10

11
module.exports = function (Topics) {
20✔
12
    Topics.getSuggestedTopics = async function (tid, uid, start, stop, cutoff = 0) {
20!
13
        let tids;
14
        tid = parseInt(tid, 10);
×
15
        cutoff = cutoff === 0 ? cutoff : (cutoff * 2592000000);
×
16
        const [tagTids, searchTids] = await Promise.all([
×
17
            getTidsWithSameTags(tid, cutoff),
18
            getSearchTids(tid, uid, cutoff),
19
        ]);
20

21
        tids = _.uniq(tagTids.concat(searchTids));
×
22

23
        let categoryTids = [];
×
24
        if (stop !== -1 && tids.length < stop - start + 1) {
×
25
            categoryTids = await getCategoryTids(tid, cutoff);
×
26
        }
27
        tids = _.shuffle(_.uniq(tids.concat(categoryTids)));
×
28
        tids = await privileges.topics.filterTids('topics:read', tids, uid);
×
29

30
        let topicData = await Topics.getTopicsByTids(tids, uid);
×
31
        topicData = topicData.filter(topic => topic && topic.tid !== tid);
×
32
        topicData = await user.blocks.filter(uid, topicData);
×
33
        topicData = topicData.slice(start, stop !== -1 ? stop + 1 : undefined)
×
34
            .sort((t1, t2) => t2.timestamp - t1.timestamp);
×
35
        return topicData;
×
36
    };
37

38
    async function getTidsWithSameTags(tid, cutoff) {
39
        const tags = await Topics.getTopicTags(tid);
×
40
        let tids = cutoff === 0 ?
×
41
            await db.getSortedSetRevRange(tags.map(tag => `tag:${tag}:topics`), 0, -1) :
×
42
            await db.getSortedSetRevRangeByScore(tags.map(tag => `tag:${tag}:topics`), 0, -1, '+inf', Date.now() - cutoff);
×
43
        tids = tids.filter(_tid => _tid !== tid); // remove self
×
44
        return _.shuffle(_.uniq(tids)).slice(0, 10).map(Number);
×
45
    }
46

47
    async function getSearchTids(tid, uid, cutoff) {
48
        const topicData = await Topics.getTopicFields(tid, ['title', 'cid']);
×
49
        const data = await search.search({
×
50
            query: topicData.title,
51
            searchIn: 'titles',
52
            matchWords: 'any',
53
            categories: [topicData.cid],
54
            uid: uid,
55
            returnIds: true,
56
            timeRange: cutoff !== 0 ? cutoff / 1000 : 0,
×
57
            timeFilter: 'newer',
58
        });
59
        data.tids = data.tids.filter(_tid => _tid !== tid); // remove self
×
60
        return _.shuffle(data.tids).slice(0, 10).map(Number);
×
61
    }
62

63
    async function getCategoryTids(tid, cutoff) {
64
        const cid = await Topics.getTopicField(tid, 'cid');
×
65
        const tids = cutoff === 0 ?
×
66
            await db.getSortedSetRevRange(`cid:${cid}:tids:lastposttime`, 0, 9) :
67
            await db.getSortedSetRevRangeByScore(`cid:${cid}:tids:lastposttime`, 0, 9, '+inf', Date.now() - cutoff);
68
        return _.shuffle(tids.map(Number).filter(_tid => _tid !== tid));
×
69
    }
70
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc