{"record":{"id":"1ea5876a732cb213","repo":"RocketChat/Rocket.Chat","slug":"error-user-not-in-room-1ea587","errorCode":"error-user-not-in-room","errorMessage":"User is not in this room","messagePattern":"User is not in this room","errorType":"error_code","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/meteor-methods/rooms/addRoomModerator.ts","lineNumber":58,"sourceCode":"\t\t});\n\t}\n\n\tif (isFederated && !isFederationEnabled()) {\n\t\tthrow new FederationMatrixInvalidConfigurationError('unable to change room owners');\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: 'addRoomModerator',\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: 'addRoomModerator',\n\t\t});\n\t}\n\n\tif (subscription.roles && Array.isArray(subscription.roles) === true && subscription.roles.includes('moderator') === true) {\n\t\tthrow new Meteor.Error('error-user-already-moderator', 'User is already a moderator', {\n\t\t\tmethod: 'addRoomModerator',\n\t\t});\n\t}\n\n\tawait beforeChangeRoomRole.run({ fromUserId, userId, room, role: 'moderator' });\n\n\tconst addRoleResponse = await Subscriptions.addRoleById(subscription._id, 'moderator');\n\tawait syncRoomRolePriorityForUserAndRoom(userId, rid, subscription.roles?.concat(['moderator']) || ['moderator']);\n\n\tif (addRoleResponse.modifiedCount) {\n\t\tvoid notifyOnSubscriptionChangedById(subscription._id);\n\t}","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/meteor-methods/rooms/addRoomModerator.ts#L40-L76","documentation":"addRoomModerator looks up the target's membership with Subscriptions.findOneByRoomIdAndUserId(rid, user._id); no subscription means the user is not in the room, so they cannot be made moderator. The role write attaches to the subscription document, so membership is a hard precondition.","triggerScenarios":"The target left/was removed from the room before the promotion; promoting a user who was never a member.","commonSituations":"Leave/join races with admin actions; unrefreshed member lists in moderation UIs.","solutions":["Invite the user to the room first (channels.invite / addUserToRoom), then promote.","Re-check membership before offering the moderator action.","On the error, refresh membership state instead of retrying blindly."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// confirm membership 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('addRoomModerator', rid, userId);\n} catch (e: any) {\n\tif (e?.error === 'error-user-not-in-room') {\n\t\t// invite first (channels.invite), then retry the promotion\n\t}\n}","preventionTips":["Verify membership immediately before role changes.","Keep member pickers synced with subscription add/remove events.","Order workflows as invite-then-promote."],"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"}