[core] NOWEB, GOWS - better handle message.edited

fix #1394
This commit is contained in:
devlikepro committed 2025-10-19 17:08:05 +07:00
1 parent c79958541e
commit bb0c7fd22b
3 files changed
+49 -45

No files matched your search

+18 -21
View File
@@ -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<WAMessageEditedBody> => {
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;
+9 -24
View File
@@ -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<WAMessageEditedBody> => {
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
+22
View File
@@ -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;
}