From bb0c7fd22b971d2dc9e2a1d00a1daf1de158009b Mon Sep 17 00:00:00 2001 From: devlikepro Date: Sun, 19 Oct 2025 16:17:22 +0700 Subject: [PATCH] [core] NOWEB, GOWS - better handle message.edited fix #1394 --- src/core/engines/gows/session.gows.core.ts | 39 +++++++++----------- src/core/engines/noweb/session.noweb.core.ts | 33 +++++------------ src/core/utils/pwa.ts | 22 +++++++++++ 3 files changed, 49 insertions(+), 45 deletions(-) create mode 100644 src/core/utils/pwa.ts diff --git a/src/core/engines/gows/session.gows.core.ts b/src/core/engines/gows/session.gows.core.ts index a821d5d8..0d6ecbc8 100644 --- a/src/core/engines/gows/session.gows.core.ts +++ b/src/core/engines/gows/session.gows.core.ts @@ -1,4 +1,4 @@ -import type { WAMessageKey } from '@adiwajshing/baileys'; +import { normalizeMessageContent, WAMessageKey } from '@adiwajshing/baileys'; import { proto } from '@adiwajshing/baileys'; import * as grpc from '@grpc/grpc-js'; import { connectivityState } from '@grpc/grpc-js'; @@ -167,6 +167,7 @@ import { isLabelUpsertEvent, } from './labels.gows'; import esm from '@waha/vendor/esm'; +import { IsEditedMessage } from '@waha/core/utils/pwa'; enum WhatsMeowEvent { CONNECTED = 'gows.ConnectedEventData', @@ -492,20 +493,15 @@ export class WhatsappSessionGoWSCore extends WhatsappSession { // Handle edited messages const messagesEdited$ = messages$.pipe( - filter((msg) => { - return ( - msg?.Message?.protocolMessage?.type === - proto.Message.ProtocolMessage.Type.MESSAGE_EDIT && - msg?.Message?.protocolMessage?.editedMessage !== undefined - ); - }), + filter((message) => IsEditedMessage(message.Message)), mergeMap(async (message): Promise => { const waMessage = await this.toWAMessage(message); + const content = normalizeMessageContent(message.Message); // Extract the body from editedMessage using extractBody function - const body = - extractBody(message.Message.protocolMessage.editedMessage) || ''; + const body = extractBody(content.protocolMessage.editedMessage) || ''; // Extract the original message ID from protocolMessage.key - const editedMessageId = message.Message.protocolMessage.key?.ID; + // @ts-ignore + const editedMessageId = content.protocolMessage.key?.ID; return { ...waMessage, body: body, @@ -1950,17 +1946,18 @@ export class WhatsappSessionGoWSCore extends WhatsappSession { // if there is no text or media message if (!message) return; if (!message.Message) return; - // Ignore reactions, we have a dedicated handler for that - if (message.Message.reactionMessage) return; - // Ignore poll votes, we have a dedicated handler for that - if (message.Message.pollUpdateMessage) return; - // Ignore event response, we have a dedicated handler for that - if (message.Message.encEventResponseMessage) return; - // Ignore protocol messages - if (message.Message.protocolMessage) return; - const normalizedContent = esm.b.normalizeMessageContent(message.Message); - const contentType = esm.b.getContentType(normalizedContent); + const content = esm.b.normalizeMessageContent(message.Message); + // Ignore reactions, we have a dedicated handler for that + if (content.reactionMessage) return; + // Ignore poll votes, we have a dedicated handler for that + if (content.pollUpdateMessage) return; + // Ignore event response, we have a dedicated handler for that + if (content.encEventResponseMessage) return; + // Ignore protocol messages + if (content.protocolMessage) return; + + const contentType = esm.b.getContentType(content); // Ignore device sent message if (contentType == 'deviceSentMessage') { return; diff --git a/src/core/engines/noweb/session.noweb.core.ts b/src/core/engines/noweb/session.noweb.core.ts index 592543e4..2bc7645d 100644 --- a/src/core/engines/noweb/session.noweb.core.ts +++ b/src/core/engines/noweb/session.noweb.core.ts @@ -199,6 +199,7 @@ import { NowebPersistentStore } from './store/NowebPersistentStore'; import { NowebStorageFactoryCore } from './store/NowebStorageFactoryCore'; import { ensureNumber, extractMediaContent } from './utils'; import { Agents } from '@waha/core/engines/noweb/types'; +import { IsEditedMessage } from '@waha/core/utils/pwa'; // eslint-disable-next-line @typescript-eslint/no-var-requires const promiseRetry = require('promise-retry'); @@ -608,14 +609,9 @@ export class WhatsappSessionNoWebCore extends WhatsappSession { // https://github.com/WhiskeySockets/Baileys/pull/855/ this.sock.ev.on('messages.upsert', ({ messages }) => { for (const message of messages) { - const content = normalizeMessageContent(message.message); - const protocolMsg = content?.protocolMessage; - if ( - protocolMsg !== null && - protocolMsg !== undefined && - protocolMsg.editedMessage && - protocolMsg.key - ) { + if (IsEditedMessage(message.message)) { + const content = normalizeMessageContent(message.message); + const protocolMsg = content?.protocolMessage; this.sock?.ev.emit('messages.update', [ { key: { @@ -1897,22 +1893,15 @@ export class WhatsappSessionNoWebCore extends WhatsappSession { this.events2.get(WAHAEvents.MESSAGE_REVOKED).switch(messagesRevoked$); // Handle edited messages - // @ts-ignore const messagesEdited$ = messagesUpsert$.pipe( - filter( - (message) => - // @ts-ignore - message.message?.protocolMessage?.type === - proto.Message.ProtocolMessage.Type.MESSAGE_EDIT && - message.message?.protocolMessage?.editedMessage !== undefined, - ), + filter((message) => IsEditedMessage(message.message)), mergeMap(async (message): Promise => { const waMessage = this.toWAMessage(message); // Extract the body from editedMessage using extractBody function - const body = - extractBody(message.message.protocolMessage.editedMessage) || ''; + const content = normalizeMessageContent(message.message); + const body = extractBody(content.protocolMessage.editedMessage) || ''; // Extract the original message ID from protocolMessage.key - const editedMessageId = message.message.protocolMessage.key?.id; + const editedMessageId = content.protocolMessage.key?.id; return { ...waMessage, body: body, @@ -2208,11 +2197,7 @@ export class WhatsappSessionNoWebCore extends WhatsappSession { ) return; // Ignore edit, we have a dedicated event for that - if ( - message.message?.protocolMessage?.type === - proto.Message.ProtocolMessage.Type.MESSAGE_EDIT - ) - return; + if (IsEditedMessage(message.message)) return; if ( message.message?.protocolMessage?.type === proto.Message.ProtocolMessage.Type.EPHEMERAL_SYNC_RESPONSE diff --git a/src/core/utils/pwa.ts b/src/core/utils/pwa.ts new file mode 100644 index 00000000..afa70e75 --- /dev/null +++ b/src/core/utils/pwa.ts @@ -0,0 +1,22 @@ +/** + * Proto WhatsApp Utils + */ + +import { normalizeMessageContent, proto } from '@adiwajshing/baileys'; + +export function IsEditedMessage(message: proto.IMessage): boolean { + message = normalizeMessageContent(message); + if (!message) { + return false; + } + if ( + message?.protocolMessage?.type !== + proto.Message.ProtocolMessage.Type.MESSAGE_EDIT + ) { + return false; + } + if (message?.protocolMessage?.editedMessage == null) { + return false; + } + return true; +}