Users to whom the category is available

NodeBB Development
  • Hey! I am just starting to deal with development and I have a task that I cannot cope with yet. I need to get a list of all users to whom the category is available. For now, I don't want to do this as a widget, but just use the filter: category.build hook in plugin.json and call the method in which I am experimenting. In general, I have not yet found a suitable ready-made method in the code of NodeBB.
    In general, I see the task like this ... I need to write a function that will receive the user ID and category ID and the answer will return true or false, that is, whether the user can see this category or not

  • If you have the userId and categoryId you can use the privilege methods to check if that user has a specific privilege on that category. The function looks like this

    await privileges.categories.can('topics:read', cid, uid);

    This will return true if the user can read topics in category cid

  • @baris
    Thank you! I'll check it out today.
    But I still need help getting data about all users. I tried the method described in this post, but it doesn't work for me. Always gives an empty array.
    https://community.nodebb.org/topic/8908/retrieve-select-info-of-all-users-in-database

  • @matvey what exactly have you tried? Show us the code

  • @pitaj
    Actually I just need a simple method to get all user IDs. I have now used the following code:

    const nextUid = await db.getObjectField('global', 'nextUid');
    const allUids = [];
    
    for (let i = 1; i <= nextUid; i++) {
         allUids.push(i);
    }
    

    But I'm not sure if this is correct.

  • could it be better this way?

    user.getUidsFromSet ('users: joindate', 0, -1)
    
  • As a result, I got such a script, maybe it will be useful to someone.

        const Theme = {}
        const user = require.main.require('./src/user')
        const privileges = require.main.require('./src/privileges')
    
        Theme.getUsersByCategory = async function(data) {
            const { templateData: { cid } } = data
    
            const allUserIds = await user.getUidsFromSet('users:joindate', 0, -1)
    
            const asyncFilter = async (arr, predicate) => {
                const results = await Promise.all(arr.map(predicate))
    
                return arr.filter((element, index) => results[index])
            }
    
            const availableUserIds = await asyncFilter(allUserIds, async (id) => {
                return await privileges.categories.can('read', cid, id)
            })
    
            data.templateData.availableUsers = await user.getUsersData(availableUserIds)
    
            return data
        }
    
        module.exports = Theme
    


Suggested Topics


| | | |