Add user to group, from script / python.
-
From yesterday im trying to add/remove user from group, im new to mongodb and nodebb js.
Im using python as backend.
So i just checked how adding user to group is done in NodeBB.
https://github.com/NodeBB/NodeBB/blob/master/src/groups/join.js#L41const promises = [ db.sortedSetsAdd(groupsToJoin.map(groupName => 'group:' + groupName + ':members'), Date.now(), uid), db.incrObjectField(groupsToJoin.map(groupName => 'group:' + groupName), 'memberCount'), ]; if (isAdmin) { promises.push(db.setsAdd(groupsToJoin.map(groupName => 'group:' + groupName + ':owners'), uid)); }
module.sortedSetsAdd = async function (keys, scores, value) { ... var bulk = module.client.collection('objects').initializeUnorderedBulkOp(); for (var i = 0; i < keys.length; i += 1) { console.info("KEYS[i]: " + keys[i] + " value: " + value + " SCORE:" + scores); bulk.find({ _key: keys[i], value: value }).upsert().updateOne({ $set: { score: parseFloat(isArrayOfScores ? scores[i] : scores) } }); } await bulk.execute();
Just addded console.info that showed these keys when i was adding users from ACP:
KEYS[i]: group:group_A:members value: 2 SCORE:1596909662460 KEYS[i]: events:time value: 28 SCORE:1596909662470 KEYS[i]: events:time:group-add-member value: 28 SCORE:1596909662470
group_A is a group that i want to add user to, user id is 2.
and this is my python code:
import pymongo import urllib.parse username = urllib.parse.quote_plus('nodebb') password = urllib.parse.quote_plus('passwd"') database = 'nodebb' client = pymongo.MongoClient('mongodb://{0}:{1}@%2Fhome%2Fuser%2Fdb%2Fmongodb-27017.sock/{2}'.format(username, password, database)) db = client['nodebb'] collection = db['nodebb'] query={"_key":"group:group_A:members","value": "2"} values={"$set": {"score": float(1596909662470)}} x = collection.update(query, values) for y in x: print(y) query={"_key":"events:time","value": "28"} values={"$set": {"score": float(1596909662470)}} x = collection.update(query, values) for y in x: print(y) query={"_key":"events:time:group-add-member","value": "28"} values={"$set": {"score": float(1596909662470)}} x = collection.update(query, values) for y in x: print(y)
python output:
n nModified ok updatedExisting n nModified ok updatedExisting n nModified ok updatedExisting
I just cant get it, i think the _key might be wrong.
-
Hm, I thought it should show user in group after restarting NodeBB, or even when nodebb is turned off (then add user and start nodebb).
If i understand correct, script.js with
Groups.join('administrators', 2)
fired bynode script.js
should add user to group?path: /home/user/nodebb/src/groups/script.js
const winston = require('winston') winston.add(new winston.transports.File({ filename: 'logfile.log' })) const async = require('async'); const winston = require('winston'); const db = require('../database'); const user = require('../user'); const plugins = require('../plugins'); const cache = require('../cache'); require('./join')(); Groups.join('administrators', 2);
I'm don with this, its to much for me right now, but ...
anyway I'm going to try nodebb-plugin-write-api, i think is the right way, prepare for questions