Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): skip a poll vote whose options cannot be named

Yudhi Armyndharis3be700cfb9c5
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index 0cdd2e524..6928df92c 100644
--- a/docs/06-api-specification.md
+++ b/docs/06-api-specification.md
@@ -7354,7 +7354,7 @@ Existing stored mappings from older releases have mixed origins, so an upgrade r
> }
> ```
>
-> `selectedOptions` are the option texts exactly as they appear on the poll, the same strings [`vote-poll`](#post-apisessionssessionidmessagesvote-poll) takes. It is the voter's whole current selection, not a delta: `[]` means they cleared their vote, and the event with the latest `timestamp` per `voterId` is the current state. Order by `timestamp` rather than by arrival. `pollName` is present only when WhatsApp Web still holds the poll creation message; without it whatsapp-web.js cannot name the options either, and an option it cannot name is left out of `selectedOptions`. Vote counts are not kept by the gateway: tally them from these events. Baileys accepts the subscription and never fires it.
+> `selectedOptions` are the option texts exactly as they appear on the poll, the same strings [`vote-poll`](#post-apisessionssessionidmessagesvote-poll) takes. It is the voter's whole current selection, not a delta: `[]` means they cleared their vote, and the event with the latest `timestamp` per `voterId` is the current state. Order by `timestamp` rather than by arrival. `pollName` is present only when WhatsApp Web still holds the poll creation message; without it whatsapp-web.js cannot name the options either, so a vote on such a poll is not reported (a cleared vote still is, with `[]`). Vote counts are not kept by the gateway: tally them from these events. Baileys accepts the subscription and never fires it.
> WhatsApp also sends a contact's edit, and a reaction in some chats, encrypted under a secret carried by the message it changes. On Baileys they are read with the stored copy of that message, so `message.edited` fires for a message **this session received** and `message.reaction` for one it **received or sent**, while its message store still holds it; an edit or reaction of one the store no longer holds (evicted past `BAILEYS_MESSAGE_STORE_LIMIT`, or from before the number was linked) is not delivered. An edit the account makes from its own phone to its own message is applied under the account's own id the same way, whether it arrives as a plain or as an encrypted edit. whatsapp-web.js gets both already decrypted from WhatsApp Web.
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
index dfb2c5867..a906c37c2 100644
--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
@@ -4060,14 +4060,11 @@ describe('WhatsAppWebJsAdapter vote_update (poll.vote)', () => {
expect(onPollVote).toHaveBeenCalledWith(expect.objectContaining({ selectedOptions: [] }));
});
- it('falls back to the poll key when WhatsApp Web no longer holds the poll, leaving unnamed options out', () => {
+ it('falls back to the poll key for a cleared vote when WhatsApp Web no longer holds the poll', () => {
const { onPollVote, client } = wireVoteHandler();
- // Without the creation message, PollVote builds an empty Message and keeps only local option ids.
- client.emit(
- 'vote_update',
- vote({ parentMessage: {}, selectedOptions: [{ name: undefined, localId: 1 }], voter: { $1: '[email protected]' } }),
- );
+ // Without the creation message, PollVote builds an empty Message from the poll key alone.
+ client.emit('vote_update', vote({ parentMessage: {}, selectedOptions: [], voter: { $1: '[email protected]' } }));
const event = (onPollVote.mock.calls as Array<[Record<string, unknown>]>)[0][0];
expect(event).toEqual({
@@ -4080,6 +4077,18 @@ describe('WhatsAppWebJsAdapter vote_update (poll.vote)', () => {
expect(event).not.toHaveProperty('pollName');
});
+ it('skips a vote whose options WhatsApp Web cannot name, rather than reporting it as a cleared vote', () => {
+ const { onPollVote, client } = wireVoteHandler();
+
+ // Without the creation message PollVote keeps only local option ids, so the choice is unknown.
+ client.emit(
+ 'vote_update',
+ vote({ parentMessage: {}, selectedOptions: [{ name: undefined, localId: 1 }], voter: { $1: '[email protected]' } }),
+ );
+
+ expect(onPollVote).not.toHaveBeenCalled();
+ });
+
it('logs and drops a malformed vote instead of throwing into the client', () => {
const { onPollVote, client } = wireVoteHandler();
diff --git a/src/engine/adapters/wwebjs-message-events.ts b/src/engine/adapters/wwebjs-message-events.ts
index ffa38cc02..7e300c904 100644
--- a/src/engine/adapters/wwebjs-message-events.ts
+++ b/src/engine/adapters/wwebjs-message-events.ts
@@ -384,16 +384,24 @@ export function registerWwebjsMessageEvents(client: Client, host: WwebjsEngineHo
};
const poll = vote.parentMessage as Partial<Message> | undefined;
const pollId = poll?.id as unknown as SerializedWid | undefined;
+ // Without the creation message whatsapp-web.js keeps only the local option ids, which no route
+ // of this API takes, so an option it could not name is left out. A selection with no nameable
+ // option is no report of what was chosen, and `[]` means a cleared vote, so it is skipped.
+ const selectedOptions = (vote.selectedOptions ?? [])
+ .map(option => option.name)
+ .filter((name): name is string => typeof name === 'string');
+ if ((vote.selectedOptions?.length ?? 0) > 0 && selectedOptions.length === 0) {
+ host.logger.debug('Ignoring a poll vote whose options WhatsApp Web cannot name', {
+ pollMessageId: readWid(pollId) ?? readWid(raw.parentMsgKey),
+ });
+ return;
+ }
const event: PollVoteEvent = {
pollMessageId: readWid(pollId) ?? readWid(raw.parentMsgKey) ?? '',
// Direction flag, as Message._getChatId (see message_revoke_everyone).
chatId: (poll?.from ? (poll.fromMe ? poll.to : poll.from) : readWid(raw.parentMsgKey?.remote)) ?? '',
voterId: readWid(raw.voter) ?? '',
- // Without the creation message whatsapp-web.js keeps only the local option ids, which no route
- // of this API takes, so an option it could not name is left out.
- selectedOptions: (vote.selectedOptions ?? [])
- .map(option => option.name)
- .filter((name): name is string => typeof name === 'string'),
+ selectedOptions,
...(poll?.pollName ? { pollName: poll.pollName } : {}),
timestamp: Math.floor((Number(vote.interractedAtTs) || Date.now()) / 1000),
};