{"record":{"id":"228682d2588fa277","repo":"RocketChat/Rocket.Chat","slug":"error-no-message-for-unread","errorCode":"error-no-message-for-unread","errorMessage":"There are no messages to mark unread","messagePattern":"There are no messages to mark unread","errorType":"error_code","errorClass":"Meteor.Error","httpStatus":null,"severity":"warning","filePath":"apps/meteor/server/lib/messaging/unread/unreadMessages.ts","lineNumber":27,"sourceCode":"\ndeclare module '@rocket.chat/ddp-client' {\n\t// eslint-disable-next-line @typescript-eslint/naming-convention\n\tinterface ServerMethods {\n\t\tunreadMessages(firstUnreadMessage?: Pick<IMessage, '_id'>, room?: IRoom['_id']): void;\n\t}\n}\n\nexport const unreadMessages = async (userId: string, firstUnreadMessage?: Pick<IMessage, '_id'>, room?: IRoom['_id']): Promise<void> => {\n\tif (room && typeof room === 'string') {\n\t\tconst lastMessage = (\n\t\t\tawait Messages.findVisibleByRoomId(room, {\n\t\t\t\tlimit: 1,\n\t\t\t\tsort: { ts: -1 },\n\t\t\t}).toArray()\n\t\t)[0];\n\n\t\tif (!lastMessage) {\n\t\t\tthrow new Meteor.Error('error-no-message-for-unread', 'There are no messages to mark unread', {\n\t\t\t\tmethod: 'unreadMessages',\n\t\t\t\taction: 'Unread_messages',\n\t\t\t});\n\t\t}\n\n\t\tconst setAsUnreadResponse = await Subscriptions.setAsUnreadByRoomIdAndUserId(lastMessage.rid, userId, lastMessage.ts);\n\t\tif (setAsUnreadResponse.modifiedCount) {\n\t\t\tvoid notifyOnSubscriptionChangedByRoomIdAndUserId(lastMessage.rid, userId);\n\t\t}\n\n\t\treturn;\n\t}\n\n\tif (typeof firstUnreadMessage?._id !== 'string') {\n\t\tthrow new Meteor.Error('error-action-not-allowed', 'Not allowed', {\n\t\t\tmethod: 'unreadMessages',\n\t\t\taction: 'Unread_messages',\n\t\t});","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/messaging/unread/unreadMessages.ts#L9-L45","documentation":"When unreadMessages is called with a room id, it fetches the newest visible message via findVisibleByRoomId (limit 1, ts desc); if none exists it throws Meteor.Error('error-no-message-for-unread', 'There are no messages to mark unread'). The room has zero messages eligible for an unread marker - empty room, or every message invisible (deleted/hidden).","triggerScenarios":"Meteor.call('unreadMessages', undefined, roomId) on a room with no visible messages: a freshly created empty channel, a room whose only messages were deleted, or a retention purge that removed everything.","commonSituations":"Mark-as-unread offered on empty rooms; messages purged while the room was already read; UI not disabling the unread action when lastMessage is absent.","solutions":["Client: gate the mark-unread action on room.lastMessage being present","If messages were purged, acknowledge the room (mark read) instead of marking unread","Skip the call when the local room cache shows no visible history"],"exampleFix":"// before\nMeteor.call('unreadMessages', null, rid); // throws on empty rooms\n\n// after\nconst room = Rooms.findOne({ _id: rid });\nif (room?.lastMessage) {\n  Meteor.call('unreadMessages', null, rid);","handlingStrategy":"validation","validationCode":"// The rooms stream carries lastMessage - use it as the precondition\nconst room = Rooms.findOne({ _id: rid });\nif (room?.lastMessage) {\n  Meteor.call('unreadMessages', null, rid);\n} else {\n  markRoomAsRead(rid); // nothing to unread\n}","typeGuard":null,"tryCatchPattern":"Meteor.call('unreadMessages', null, rid, (err) => {\n  if (err?.error === 'error-no-message-for-unread') {\n    markRoomAsRead(rid); // benign - clear any unread badge and move on\n  }\n});","preventionTips":["Drive mark-unread availability from room.lastMessage","After retention purges, expect this error and fall back to read state","Do not queue unread calls for rooms whose history has not synced yet"],"tags":["unread","empty-room","ddp-method"],"backgroundTag":"no-messages-in-room","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","contentChangedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}