{"record":{"id":"e1220b35e4ceff4c","repo":"RocketChat/Rocket.Chat","slug":"error-user-not-in-room","errorCode":"error-user-not-in-room","errorMessage":"User is not in this room","messagePattern":"User is not in this room","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/meteor-methods/rooms/addRoomLeader.ts","lineNumber":42,"sourceCode":"\n\tif (!(await hasPermissionAsync(fromUserId, 'set-leader', rid))) {\n\t\tthrow new Meteor.Error('error-not-allowed', 'Not allowed', {\n\t\t\tmethod: 'addRoomLeader',\n\t\t});\n\t}\n\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);","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/meteor-methods/rooms/addRoomLeader.ts#L24-L60","documentation":"addRoomLeader then looks up the target's membership with Subscriptions.findOneByRoomIdAndUserId(rid, user._id); a null subscription means the user is not in the room, so they cannot be promoted to leader. The role must be attached to an existing subscription.","triggerScenarios":"The target left or was removed from the channel before the role change landed; calling addRoomLeader for a user who was never added to the room.","commonSituations":"Races between a user leaving and an admin promoting them; member lists not refreshed after removals.","solutions":["Add the user to the room first (addUserToRoom / channels.invite), then re-run the role change.","Refresh membership state in the UI before offering role actions.","Treat the error as a prompt to re-check membership rather than retrying unchanged."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// confirm the target is a member of the room before promoting\nconst sub = RocketChat.models?.Subscriptions?.findOne({ rid, 'u._id': userId });\nif (!sub) {\n\tthrow new Error('User is not in this room - invite first');\n}","typeGuard":null,"tryCatchPattern":"try {\n\tawait Meteor.callAsync('addRoomLeader', rid, userId);\n} catch (e: any) {\n\tif (e?.error === 'error-user-not-in-room') {\n\t\t// invite the user first (channels.invite), then retry the promotion\n\t}\n}","preventionTips":["Verify membership right before role changes.","Handle subscription-removed events to keep member pickers accurate.","Sequence invite-then-promote in workflows instead of assuming membership."],"tags":["meteor-methods","rooms","membership","roles"],"backgroundTag":"user-not-in-room","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}