{"record":{"id":"fe00fae7f148b02a","repo":"RocketChat/Rocket.Chat","slug":"invalid-user-fe00fa","errorCode":"invalid-user","errorMessage":"Invalid user","messagePattern":"Invalid user","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/rooms/setRoomAvatar.ts","lineNumber":12,"sourceCode":"import { api, Message } from '@rocket.chat/core-services';\nimport type { IUser } from '@rocket.chat/core-typings';\nimport { isRegisterUser } from '@rocket.chat/core-typings';\nimport { Avatars, Rooms } from '@rocket.chat/models';\nimport { Meteor } from 'meteor/meteor';\n\nimport { RocketChatFile } from '../media/file';\nimport { FileUpload } from '../media/file-upload';\n\nexport const setRoomAvatar = async function (rid: string, dataURI: string, user: IUser): Promise<void> {\n\tif (!isRegisterUser(user)) {\n\t\tthrow new Meteor.Error('invalid-user', 'Invalid user', {\n\t\t\tfunction: 'RocketChat.setRoomAvatar',\n\t\t});\n\t}\n\n\tconst fileStore = FileUpload.getStore('Avatars');\n\n\tconst current = await Avatars.findOneByRoomId(rid);\n\n\tif (!dataURI) {\n\t\tawait fileStore.deleteByRoomId(rid);\n\t\tawait Message.saveSystemMessage('room_changed_avatar', rid, '', user);\n\t\tvoid api.broadcast('room.avatarUpdate', { _id: rid });\n\t\tawait Rooms.unsetAvatarData(rid);\n\t\treturn;\n\t}\n\n\tconst fileData = RocketChatFile.dataURIParse(dataURI);\n","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/rooms/setRoomAvatar.ts#L1-L30","documentation":"Thrown by setRoomAvatar when isRegisterUser(user) is false (setRoomAvatar.ts:12). isRegisterUser (packages/core-typings/src/IUser.ts:255) requires BOTH user.username and user.name to be defined — so this fires for incomplete user objects, not just missing accounts. Code is 'invalid-user' (no error- prefix), details { function: 'RocketChat.setRoomAvatar' }.","triggerScenarios":"Passing a partial user (e.g. picked from a session with only _id); app/bot users whose document lacks name; a user object constructed by hand in tests without name; code that passed userId string instead of the IUser document.","commonSituations":"Custom avatar endpoints resolving only {_id, username} from a token; importers creating users without a display name; refactors that swapped the full user param for AtLeast<IUser,'_id'>.","solutions":["Fetch the full user document before calling: Users.findOneById(userId)","Ensure the user has both username and name set (update the profile if name is empty)","Pre-check with the same helper: isRegisterUser(user) from '@rocket.chat/core-typings'","Note the code is 'invalid-user', not 'error-invalid-user' — match on the exact string when catching"],"exampleFix":"// before\nawait setRoomAvatar(rid, dataURI, { _id: uid } as IUser);\n\n// after\nconst user = await Users.findOneById(uid);\nif (!user || !isRegisterUser(user)) {\n\tthrow new Meteor.Error('invalid-user', 'Invalid user');\n}\nawait setRoomAvatar(rid, dataURI, user);","handlingStrategy":"type-guard","validationCode":"const user = await Users.findOneById(userId);\nif (!user || !isRegisterUser(user)) {\n\tthrow new Meteor.Error('invalid-user', 'A registered user (with username and name) is required');\n}\nawait setRoomAvatar(rid, dataURI, user);","typeGuard":"import { isRegisterUser } from '@rocket.chat/core-typings';\n// isRegisterUser: user.username !== undefined && user.name !== undefined\nconst assertRegisterUser = (u: IUser): void => {\n\tif (!isRegisterUser(u)) throw new Meteor.Error('invalid-user', 'User lacks username or name');\n};","tryCatchPattern":null,"preventionTips":["Always fetch the full user document instead of building stubs","Remember isRegisterUser needs BOTH username and name","Catch on the exact code 'invalid-user' (no error- prefix)"],"tags":["avatar","user","validation","type-guard"],"backgroundTag":"incomplete-user-object","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}