{"record":{"id":"df12e39a849d9f43","repo":"RocketChat/Rocket.Chat","slug":"error-user-already-leader","errorCode":"error-user-already-leader","errorMessage":"User is already a leader","messagePattern":"User is already a leader","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"warning","filePath":"apps/meteor/server/meteor-methods/rooms/addRoomLeader.ts","lineNumber":48,"sourceCode":"\n\tconst user = await Users.findOneById(userId);\n\n\tif (!user?.username) {\n\t\tthrow new Meteor.Error('error-invalid-user', 'Invalid user', {\n\t\t\tmethod: 'addRoomLeader',\n\t\t});\n\t}\n\n\tconst subscription = await Subscriptions.findOneByRoomIdAndUserId(rid, user._id);\n\n\tif (!subscription) {\n\t\tthrow new Meteor.Error('error-user-not-in-room', 'User is not in this room', {\n\t\t\tmethod: 'addRoomLeader',\n\t\t});\n\t}\n\n\tif (subscription.roles && Array.isArray(subscription.roles) === true && subscription.roles.includes('leader') === true) {\n\t\tthrow new Meteor.Error('error-user-already-leader', 'User is already a leader', {\n\t\t\tmethod: 'addRoomLeader',\n\t\t});\n\t}\n\n\tconst addRoleResponse = await Subscriptions.addRoleById(subscription._id, 'leader');\n\tawait syncRoomRolePriorityForUserAndRoom(userId, rid, subscription.roles?.concat(['leader']) || ['leader']);\n\n\tif (addRoleResponse.modifiedCount) {\n\t\tvoid notifyOnSubscriptionChangedById(subscription._id);\n\t}\n\n\tconst fromUser = await Users.findOneById(fromUserId);\n\n\tif (!fromUser) {\n\t\tthrow new Meteor.Error('error-invalid-user', 'Invalid user', {\n\t\t\tmethod: 'addRoomLeader',\n\t\t});\n\t}","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/meteor-methods/rooms/addRoomLeader.ts#L30-L66","documentation":"If the target's subscription already lists 'leader' in its roles array, addRoomLeader throws error-user-already-leader instead of writing the role again. This is an idempotency guard: the desired end state is already in place, so the write is skipped. Semantically benign, but it still fails the method call.","triggerScenarios":"Double-submit of the 'Set as leader' action (double click, retry); two admins promoting the same user concurrently; stale UI that does not reflect the current role.","commonSituations":"Buttons without pending/disabled states; optimistic UI that assumes failure and re-triggers; websocket updates missed so the role appears unset.","solutions":["Treat the error as success - the user already has the role; refresh the UI state from the subscription.","Check the member's current roles before offering the action.","Disable the action while a role change is in flight."],"exampleFix":"// before\nawait Meteor.callAsync('addRoomLeader', rid, userId);\n\n// after\ntry {\n\tawait Meteor.callAsync('addRoomLeader', rid, userId);\n} catch (e: any) {\n\tif (e?.error === 'error-user-already-leader') return; // already done\n\tthrow e;\n}","handlingStrategy":"validation","validationCode":"// skip the call when the member already carries the role\nconst member = await fetch(`/api/v1/channels.members?roomId=${rid}`, { headers }).then((r) => r.json());\nconst alreadyLeader = member.members?.find((m: any) => m._id === userId)?.roles?.includes('leader');\nif (alreadyLeader) { /* nothing to do */ }","typeGuard":null,"tryCatchPattern":"try {\n\tawait Meteor.callAsync('addRoomLeader', rid, userId);\n} catch (e: any) {\n\tif (e?.error === 'error-user-already-leader') return; // idempotent success\n\tthrow e;\n}","preventionTips":["Read the subscription's current roles before offering 'Set as leader'.","Disable action buttons while a role change is pending (double-submit guard).","Treat already-role errors as success in retry wrappers."],"tags":["meteor-methods","rooms","roles","idempotency"],"backgroundTag":"duplicate-role-assignment","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}