• 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

11.76
/src/socket.io/categories/search.js
1
'use strict';
2

3
const _ = require('lodash');
4✔
4

5
const meta = require('../../meta');
4✔
6
const categories = require('../../categories');
4✔
7
const privileges = require('../../privileges');
4✔
8
const controllersHelpers = require('../../controllers/helpers');
4✔
9
const plugins = require('../../plugins');
4✔
10

11
module.exports = function (SocketCategories) {
4✔
12
    // used by categorySearch module
13
    SocketCategories.categorySearch = async function (socket, data) {
4✔
14
        let cids = [];
×
15
        let matchedCids = [];
×
16
        const privilege = data.privilege || 'topics:read';
×
17
        data.states = (data.states || ['watching', 'notwatching', 'ignoring']).map(
×
18
            state => categories.watchStates[state]
×
19
        );
20

21
        if (data.search) {
×
22
            ({ cids, matchedCids } = await findMatchedCids(socket.uid, data));
×
23
        } else {
24
            cids = await loadCids(socket.uid, data.parentCid);
×
25
        }
26

27
        const visibleCategories = await controllersHelpers.getVisibleCategories({
×
28
            cids, uid: socket.uid, states: data.states, privilege, showLinks: data.showLinks, parentCid: data.parentCid,
29
        });
30

31
        if (Array.isArray(data.selectedCids)) {
×
32
            data.selectedCids = data.selectedCids.map(cid => parseInt(cid, 10));
×
33
        }
34

35
        let categoriesData = categories.buildForSelectCategories(visibleCategories, ['disabledClass'], data.parentCid);
×
36
        categoriesData = categoriesData.slice(0, 200);
×
37

38
        categoriesData.forEach((category) => {
×
39
            category.selected = data.selectedCids ? data.selectedCids.includes(category.cid) : false;
×
40
            if (matchedCids.includes(category.cid)) {
×
41
                category.match = true;
×
42
            }
43
        });
44
        const result = await plugins.hooks.fire('filter:categories.categorySearch', {
×
45
            categories: categoriesData,
46
            ...data,
47
            uid: socket.uid,
48
        });
49
        return result.categories;
×
50
    };
51

52
    async function findMatchedCids(uid, data) {
53
        const result = await categories.search({
×
54
            uid: uid,
55
            query: data.search,
56
            qs: data.query,
57
            paginate: false,
58
        });
59

60
        let matchedCids = result.categories.map(c => c.cid);
×
61
        // no need to filter if all 3 states are used
62
        const filterByWatchState = !Object.values(categories.watchStates)
×
63
            .every(state => data.states.includes(state));
×
64

65
        if (filterByWatchState) {
×
66
            const states = await categories.getWatchState(matchedCids, uid);
×
67
            matchedCids = matchedCids.filter((cid, index) => data.states.includes(states[index]));
×
68
        }
69

70
        const rootCids = _.uniq(_.flatten(await Promise.all(matchedCids.map(categories.getParentCids))));
×
71
        const allChildCids = _.uniq(_.flatten(await Promise.all(matchedCids.map(categories.getChildrenCids))));
×
72

73
        return {
×
74
            cids: _.uniq(rootCids.concat(allChildCids).concat(matchedCids)),
75
            matchedCids: matchedCids,
76
        };
77
    }
78

79
    async function loadCids(uid, parentCid) {
80
        let resultCids = [];
×
81
        async function getCidsRecursive(cids) {
82
            const categoryData = await categories.getCategoriesFields(cids, ['subCategoriesPerPage']);
×
83
            const cidToData = _.zipObject(cids, categoryData);
×
84
            await Promise.all(cids.map(async (cid) => {
×
85
                const allChildCids = await categories.getAllCidsFromSet(`cid:${cid}:children`);
×
86
                if (allChildCids.length) {
×
87
                    const childCids = await privileges.categories.filterCids('find', allChildCids, uid);
×
88
                    resultCids.push(...childCids.slice(0, cidToData[cid].subCategoriesPerPage));
×
89
                    await getCidsRecursive(childCids);
×
90
                }
91
            }));
92
        }
93

94
        const allRootCids = await categories.getAllCidsFromSet(`cid:${parentCid}:children`);
×
95
        const rootCids = await privileges.categories.filterCids('find', allRootCids, uid);
×
96
        const pageCids = rootCids.slice(0, meta.config.categoriesPerPage);
×
97
        resultCids = pageCids;
×
98
        await getCidsRecursive(pageCids);
×
99
        return resultCids;
×
100
    }
101
};
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