fix(inbound): handle malformed JIDs in postUpsertChatId fallback

Addresses Copilot review on PR #392.

`jidNormalizedUser()` returns an empty string when `jidDecode()` fails
(e.g. a malformed JID missing the `@` separator). With the previous
ternary `rawChatId ? jidNormalizedUser(rawChatId) : 'unknown'`, a
truthy-but-malformed `rawChatId` produced `postUpsertChatId === ''`,
so the keyed mutex ran with an empty-string key instead of the
intended `'unknown'` fallback bucket — defeating the per-chat
serialization and silently lumping malformed-key messages into a
single anonymous queue.

Compute the normalized value first then apply the fallback only when
the result is empty.
This commit is contained in:
Renato Alcara
2026-04-26 15:13:42 -03:00
parent aef4973cac
commit d169a6f755
+6 -2
View File
@@ -1465,9 +1465,13 @@ export const makeChatsSocket = (config: SocketConfig) => {
// Use getChatId + jidNormalizedUser so the mutex key matches the chat-id
// scheme processMessage uses for chat updates (broadcasts target the
// participant). Falling back to a constant bucket avoids fragmenting the
// queue with per-message ids when the key is malformed.
// queue when the key is malformed.
// Compute the normalized value first then apply the fallback: a malformed
// but truthy rawChatId (e.g. missing `@`) makes jidNormalizedUser return
// '', which would otherwise pass through as an empty mutex key.
const rawChatId = getChatId(msg.key)
const postUpsertChatId = rawChatId ? jidNormalizedUser(rawChatId) : 'unknown'
const normalizedChatId = rawChatId ? jidNormalizedUser(rawChatId) : ''
const postUpsertChatId = normalizedChatId || 'unknown'
// Wrap in `postUpsertMutex(chatId)` (a SEPARATE keyed mutex from the outer
// `messageMutex` held by the inbound caller) so per-chat ordering of