Bulk move users to group?
-
Hi,
in our football forum we have a ticket exchange category. In the last time more and more users register and offer tickets with the first post. To get rid of scammers, we want to restrict access to this category.
I know that there is the rewards plugin, where I could configure to add users to an "access group", let's say when they reach 10 posts. The problem ist that already registered users with >=10 posts would have to be added manually to this group and I can only find an option to add users one by one. So for 335 users I would have to check every single profile for the post count and add the user to the access group.
Is there any way to handle this more efficient? Maybe a mongo call for all users if user post count >= 10 then add to group. And for all new users (or users that didn't write 10 posts yet) the plugin should do it's job right?
-
You would have to run a custom script like below. Place it in your nodebb folder, change the group name your special access group and run it with
node myscript.js
. It will add all users who have 10 posts or more into that group.'use strict'; const nconf = require('nconf'); nconf.file({ file: 'config.json', }); nconf.defaults({ base_dir: __dirname, views_dir: './build/public/templates', upload_path: 'public/uploads', }); const db = require('./src/database'); const groupName = '<Replace with your groupname>'; db.init((err) => { if (err) { console.log(`NodeBB could not connect to your database. Error: ${err.message}`); process.exit(); } addUsersToGroup((err) => { if (err) { console.error(err); process.exit(); } console.log('done'); process.exit(); }); }); async function addUsersToGroup(callback) { const user = require('./src/user'); const groups = require('./src/groups'); const batch = require('./src/batch'); // check if target group exists const exists = await groups.exists(groupName); if (!exists) { return callback(new Error('group does not exist')); } try { const now = Date.now(); await batch.processSortedSet('users:joindate', async (uids) => { const userData = (await user.getUsersData(uids)) .filter(u => u && u.postcount >= 10); await db.sortedSetAdd( `group:${groupName}:members`, userData.map(() => now), userData.map(u => u.uid) ); }, { batch: 500, }); const memberCount = await db.sortedSetCard(`group:${groupName}:members`); console.log('total count', memberCount); await db.setObjectField(`group:${groupName}`, 'memberCount', memberCount); await db.sortedSetAdd( 'groups:visible:memberCount', memberCount, groupName, ); callback(); } catch (err) { callback(err); } }
-