Added function for updateUserImage + bugfix

Noah Zalev committed Jan 9, 2022 at 20:29 UTC f262dd9aa1e08a681963d0bfc91f9dcb5aec2a75
1 file changed +37 -37
meshuser.js
+37 -37
@@ -1331,43 +1331,6 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
1331
1332 // OK Response
1333 if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'edituser', responseid: command.responseid, result: 'ok' })); } catch (ex) { } }
1334 - break;
1335 - }
1336 - case 'updateUserImage':
1337 - {
1338 - if (req.session.loginToken != null) break; // Do not allow this command when logged in using a login token
1339 -
1340 - var uid = user._id;
1341 - if ((typeof command.userid == 'string') && ((user.siteadmin & SITERIGHT_MANAGEUSERS) != 0)) { uid = command.userid; }
1342 -
1343 - var chguser = parent.users[uid], flags = 0, change = 0;
1344 - if (chguser == null) break;
1345 - if (typeof chguser.flags == 'number') { flags = chguser.flags; }
1346 -
1347 - if (command.image == 0) {
1348 - // Delete the image
1349 - db.Remove('im' + uid);
1350 - if ((flags & 1) != 0) { flags -= 1; change = 1; }
1351 - } else if ((typeof command.image == 'string') && (command.image.length < 600000) && ((command.image.startsWith('data:image/png;base64,') || (command.image.startsWith('data:image/jpeg;base64,'))))) {
1352 - // Save the new image
1353 - db.Set({ _id: 'im' + uid, image: command.image });
1354 - if ((flags & 1) == 0) { flags += 1; }
1355 - change = 1;
1356 - }
1357 -
1358 - // Update the user if needed
1359 - if (change == 1) {
1360 - chguser.flags = flags;
1361 - db.SetUser(chguser);
1362 -
1363 - // Event the change
1364 - var targets = ['*', 'server-users', user._id, chguser._id];
1365 - if (allTargetGroups) { for (var i in allTargetGroups) { targets.push('server-users:' + i); } }
1366 - var event = { etype: 'user', userid: uid, username: chguser.name, account: parent.CloneSafeUser(chguser), action: 'accountchange', msgid: 66, msgArgs: [chguser.name], msg: 'Account changed: ' + chguser.name, domain: domain.id, accountImageChange: 1 };
1367 - if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come.
1368 - parent.parent.DispatchEvent(targets, obj, event);
1369 - }
1370 -
1334 break;
1335 }
1336 case 'usergroups':
@@ -5007,6 +4970,7 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
4970 'servertimelinestats': serverCommandServerTimelineStats,
4971 'serverupdate': serverCommandServerUpdate,
4972 'serverversion': serverCommandServerVersion,
4973 + 'updateUserImage': serverCommandUpdateUserImage,
4974 'urlargs': serverCommandUrlArgs,
4975 'users': serverCommandUsers,
4976 'verifyemail': serverCommandVerifyEmail,
@@ -6099,6 +6063,42 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
6063 parent.parent.getServerTags(function (tags, err) { try { ws.send(JSON.stringify({ action: 'serverversion', tags: tags })); } catch (ex) { } });
6064 }
6065
6066 + function serverCommandUpdateUserImage(command) {
6067 + if (req.session.loginToken != null) return; // Do not allow this command when logged in using a login token
6068 +
6069 + var uid = user._id;
6070 + if ((typeof command.userid == 'string') && ((user.siteadmin & SITERIGHT_MANAGEUSERS) != 0)) { uid = command.userid; }
6071 +
6072 + var chguser = parent.users[uid], flags = 0, change = 0;
6073 + if (chguser == null) return;
6074 + if (typeof chguser.flags == 'number') { flags = chguser.flags; }
6075 +
6076 + if (command.image == 0) {
6077 + // Delete the image
6078 + db.Remove('im' + uid);
6079 + if ((flags & 1) != 0) { flags -= 1; change = 1; }
6080 + } else if ((typeof command.image == 'string') && (command.image.length < 600000) && ((command.image.startsWith('data:image/png;base64,') || (command.image.startsWith('data:image/jpeg;base64,'))))) {
6081 + // Save the new image
6082 + db.Set({ _id: 'im' + uid, image: command.image });
6083 + if ((flags & 1) == 0) { flags += 1; }
6084 + change = 1;
6085 + }
6086 +
6087 + // Update the user if needed
6088 + if (change == 1) {
6089 + chguser.flags = flags;
6090 + db.SetUser(chguser);
6091 +
6092 + // Event the change
6093 + var targets = ['*', 'server-users', user._id, chguser._id];
6094 + var allTargetGroups = chguser.groups;
6095 + if (allTargetGroups) { for (var i in allTargetGroups) { targets.push('server-users:' + i); } }
6096 + var event = { etype: 'user', userid: uid, username: chguser.name, account: parent.CloneSafeUser(chguser), action: 'accountchange', msgid: 66, msgArgs: [chguser.name], msg: 'Account changed: ' + chguser.name, domain: domain.id, accountImageChange: 1 };
6097 + if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the user. Another event will come.
6098 + parent.parent.DispatchEvent(targets, obj, event);
6099 + }
6100 + }
6101 +
6102 function serverCommandUrlArgs(command) {
6103 console.log(req.query);
6104 console.log(command.args);