Skip to content

Commit 91cba73

Browse files
committed
fix(events): derive senderDevice from raw device suffix
jidNormalizedUser stripped the device segment before senderDeviceOf ran, so senderDevice was always 'android'. Keep the raw participant jid and decode the device from it.
1 parent 2e375bf commit 91cba73

4 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/events/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ export const buildMessageContext = (input: BuildContextInput): MessageContext =>
335335
senderId,
336336
senderLid: input.sender.lid ?? null,
337337
senderName: input.sender.pushName ?? null,
338-
senderDevice: senderDeviceOf(input.sender.jid),
338+
senderDevice: senderDeviceOf(input.sender.deviceJid ?? input.sender.jid),
339339
timestamp: epochSecondsToMs(input.message.messageTimestamp),
340340
text: input.text,
341341
mentions: input.mentions,

src/events/decoders/_shared.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export const extractSender = (
4545
const alt = altRaw != null ? extractJid(altRaw) : null
4646
const username = nonEmpty(key.participantUsername) ?? nonEmpty(key.remoteJidUsername)
4747
const sender: SenderInfo = { jid, isMe: key.fromMe === true }
48+
if (typeof raw === 'string' && raw.length > 0) sender.deviceJid = raw
4849
const candidates = alt !== null ? [jid, alt] : [jid]
4950
const lid = candidates.find(isLidJid)
5051
const pn = candidates.find(isPnJid)

src/events/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { MentionAllContext, MentionContext, MessageContext } from './contex
33

44
export interface SenderInfo {
55
jid: string
6+
deviceJid?: string
67
lid?: string
78
pn?: string
89
username?: string

tests/events/pipeline.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ describe('attachInboundPipeline — messages.upsert', () => {
169169
expect(seen.mock.calls[0]?.[0].mentions).toEqual(['66554863583429@lid'])
170170
})
171171

172+
it('derives senderDevice from the participant device suffix', () => {
173+
const { client, socket } = setup()
174+
const seen = vi.fn()
175+
client.on('text', seen)
176+
socket.triggerMessagesUpsert({
177+
messages: [
178+
textMsg('hi', { key: { remoteJid: '99-1@g.us', id: 'D1', fromMe: false, participant: '628000:3@s.whatsapp.net' } }),
179+
],
180+
type: 'notify',
181+
})
182+
expect(seen.mock.calls[0]?.[0].senderDevice).toBe('web')
183+
expect(seen.mock.calls[0]?.[0].senderId).toBe('628000@s.whatsapp.net')
184+
})
185+
172186
it('emits both text and mention when self mentioned (multi-decoder)', () => {
173187
const { client, socket } = setup()
174188
const text = vi.fn()

0 commit comments

Comments
 (0)