Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): let the sender change a received list message by its chat

Yudhi Armyndharis03438074a94d
diff --git a/src/modules/session/message-mutation-guard.spec.ts b/src/modules/session/message-mutation-guard.spec.ts
index 812019742..100bfd1a7 100644
--- a/src/modules/session/message-mutation-guard.spec.ts
+++ b/src/modules/session/message-mutation-guard.spec.ts
@@ -49,10 +49,26 @@ describe('mutationTargetsForeignRow', () => {
expect(mutationTargetsForeignRow({ chatId: list, direction: OUT }, { kind: 'edit', chatId: direct })).toBe(
'chat',
);
- expect(mutationTargetsForeignRow({ chatId: list, direction: IN }, { kind: 'reaction', chatId: direct })).toBe(
- 'chat',
- );
+ expect(
+ mutationTargetsForeignRow(
+ { chatId: list, direction: IN, author: '[email protected]' },
+ { kind: 'reaction', chatId: direct },
+ ),
+ ).toBe('chat');
+ }
+ });
+
+ // Earlier versions filed a message received through a contact's list under the list id, with its sender as
+ // the author, and it is shown in the chat with that sender: the sender's change names that chat.
+ it('lets the sender of a received list message change it from their own chat', () => {
+ const row = { chatId: '1700000009@broadcast', direction: IN, author: direct };
+ for (const kind of ['edit', 'revoke', 'reaction'] as const) {
+ expect(mutationTargetsForeignRow(row, { kind, chatId: direct, fromMe: false })).toBeNull();
}
+ // The account's own phone reacting to it names the sender's chat too, and another person's chat is foreign.
+ expect(mutationTargetsForeignRow(row, { kind: 'reaction', chatId: direct, fromMe: true })).toBeNull();
+ expect(mutationTargetsForeignRow(row, { kind: 'reaction', chatId: '[email protected]' })).toBe('chat');
+ expect(mutationTargetsForeignRow(row, { kind: 'edit', chatId: group, fromMe: false })).toBe('chat');
});
// A REST send keeps the number as its caller wrote it ('+', spaces, with or without the mobile marker
diff --git a/src/modules/session/message-mutation-guard.ts b/src/modules/session/message-mutation-guard.ts
index e019b1001..786d3703c 100644
--- a/src/modules/session/message-mutation-guard.ts
+++ b/src/modules/session/message-mutation-guard.ts
@@ -56,6 +56,20 @@ function isOwnBroadcast(row: StoredMessageRef): boolean {
return row.direction === MessageDirection.OUTGOING && (kind === 'broadcast' || kind === 'status');
}
+/**
+ * A message received through a contact's broadcast list was filed under the list id by earlier versions
+ * (its row says `<id>@broadcast`, incoming), though the dashboard and the engines show it in the one-to-one
+ * chat with its sender. A change to it names the sender's chat, which is the chat of that message as well,
+ * for as long as the row keeps the list id.
+ */
+function receivedThroughList(row: StoredMessageRef, chatId: string): boolean {
+ return (
+ row.direction === MessageDirection.INCOMING &&
+ entityOf(row.chatId)?.kind === 'broadcast' &&
+ !provablyDifferent(row.author, chatId)
+ );
+}
+
/**
* Whether two ids provably name different chats or people, compared by the entity they name rather than
* by dialect or spelling. A lid against a phone number may be one person.
@@ -79,7 +93,8 @@ export function provablyDifferent(a?: string | null, b?: string | null): boolean
* author, so applying it would make the gateway's record say what no client shows.
*
* - `chat`: a message id belongs to one chat. A reaction to a message the account sent to a broadcast
- * list or posted as a status is the exception: its recipients react from their own chats.
+ * list or posted as a status is the exception: its recipients react from their own chats. So is any
+ * change to a received list message that an earlier version filed under the list, from its sender's chat.
* - `direction`: an edit, and a revoke in a one-to-one chat, come from the author only, so a change
* made by the counterparty cannot name a message the account sent, nor the reverse. Group revokes are
* exempt, since an admin may delete anyone's message.
@@ -89,9 +104,8 @@ export function mutationTargetsForeignRow(
row: StoredMessageRef,
source: MutationSource,
): 'chat' | 'direction' | 'author' | null {
- if (!(source.kind === 'reaction' && isOwnBroadcast(row)) && provablyDifferent(row.chatId, source.chatId)) {
- return 'chat';
- }
+ const listed = (source.kind === 'reaction' && isOwnBroadcast(row)) || receivedThroughList(row, source.chatId);
+ if (!listed && provablyDifferent(row.chatId, source.chatId)) return 'chat';
const group = source.chatId.endsWith('@g.us') || row.chatId.endsWith('@g.us');
const authorOnly = source.kind === 'edit' || (source.kind === 'revoke' && !group);
if (authorOnly && source.fromMe !== undefined) {