fix(whatsapp): apply read receipts after intake policy

This commit is contained in:
Cyrus 2026-07-23 23:27:22 +01:00 committed by kshitij
parent 35afa8ce06
commit 652d858f2e
6 changed files with 209 additions and 14 deletions

View file

@ -654,15 +654,6 @@ async function startSocket() {
}
}
const receiptKeys = inboundReadReceiptKeys({ key: msg.key, enabled: SEND_READ_RECEIPTS });
if (receiptKeys.length > 0) {
try {
await sock.readMessages(receiptKeys);
} catch (err) {
console.warn('[bridge] failed to send read receipt:', err.message);
}
}
const messageContent = getMessageContent(msg);
if (messageContent.pollUpdateMessage) {
const pollUpdateMessage = messageContent.pollUpdateMessage;
@ -1058,6 +1049,30 @@ app.post('/typing', async (req, res) => {
}
});
// Mark an inbound message as read only after the Python adapter has accepted
// it through the authoritative DM/group/mention intake policy.
app.post('/read', async (req, res) => {
if (!sock || connectionState !== 'connected') {
return res.status(503).json({ error: 'Not connected' });
}
const receiptKeys = inboundReadReceiptKeys({
key: req.body?.key,
enabled: SEND_READ_RECEIPTS,
});
if (receiptKeys.length === 0) {
return res.json({ success: true, marked: false });
}
try {
await sock.readMessages(receiptKeys);
return res.json({ success: true, marked: true });
} catch (err) {
console.warn('[bridge] failed to send read receipt:', err.message);
return res.status(500).json({ error: 'Failed to send read receipt' });
}
});
// Chat info
app.get('/chat/:id', async (req, res) => {
const chatId = req.params.id;

View file

@ -118,6 +118,12 @@ import {
assert.equal(event.quotedParticipant, '15559998888@s.whatsapp.net');
assert.equal(event.quotedRemoteJid, '15551234567@s.whatsapp.net');
assert.equal(event.quotedText, 'approve deploy?');
assert.deepEqual(event.readReceiptKey, {
id: 'incoming-1',
remoteJid: '15551234567@s.whatsapp.net',
participant: '15550001111@s.whatsapp.net',
fromMe: false,
});
assert.equal(event.hasQuotedMessage, true);
assert.equal(event.body, 'approved');
console.log(' ✓ inbound quoted metadata includes quoted text');

View file

@ -483,6 +483,12 @@ export async function extractBridgeEvent({
quotedText,
hasQuotedMessage,
botIds,
readReceiptKey: {
remoteJid: msg.key.remoteJid || chatId,
id: msg.key.id,
participant: msg.key.participant || senderId,
fromMe: Boolean(msg.key.fromMe),
},
timestamp: msg.messageTimestamp,
};
}