From febaf0c122c054b5238dd42347d023defdb7df83 Mon Sep 17 00:00:00 2001 From: icleitoncosta Date: Thu, 14 Nov 2024 20:13:48 -0300 Subject: [PATCH] fix: Fixed error when send msg to some lids (close #2472, close #2480) --- src/chat/patch.ts | 18 ++++++++++ .../functions/findOrCreateLatestChat.ts | 34 +++++++++++++++++++ src/whatsapp/functions/index.ts | 1 + 3 files changed, 53 insertions(+) create mode 100644 src/whatsapp/functions/findOrCreateLatestChat.ts diff --git a/src/chat/patch.ts b/src/chat/patch.ts index a17f41c157..03151c9540 100644 --- a/src/chat/patch.ts +++ b/src/chat/patch.ts @@ -18,6 +18,7 @@ import * as webpack from '../webpack'; import { ChatModel, functions } from '../whatsapp'; import { wrapModuleFunction } from '../whatsapp/exportModule'; import { + findOrCreateLatestChat, isUnreadTypeMsg, mediaTypeFromProtobuf, typeAttributeFromProtobuf, @@ -81,6 +82,23 @@ function applyPatch() { return func(...args); }); + + /** + * Fixed error on try send message to some lids + */ + wrapModuleFunction(findOrCreateLatestChat, async (func, ...args) => { + const [chat, type] = args; + + if (chat.isLid() && type != 'username_contactless_search') { + try { + return await func(...args); + } catch (error) { + return await func(chat, 'username_contactless_search'); + } + } + + return await func(...args); + }); } function applyPatchModel() { diff --git a/src/whatsapp/functions/findOrCreateLatestChat.ts b/src/whatsapp/functions/findOrCreateLatestChat.ts new file mode 100644 index 0000000000..23d769f23e --- /dev/null +++ b/src/whatsapp/functions/findOrCreateLatestChat.ts @@ -0,0 +1,34 @@ +/*! + * Copyright 2021 WPPConnect Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { exportModule } from '../exportModule'; +import { Wid } from '../misc'; +import { ChatModel } from '../models'; + +/** @whatsapp WAWebFindChatAction + */ +export declare function findOrCreateLatestChat( + wid: Wid, + type?: 'username_contactless_search' +): Promise; + +exportModule( + exports, + { + findOrCreateLatestChat: 'findOrCreateLatestChat', + }, + (m) => m.findOrCreateLatestChat +); diff --git a/src/whatsapp/functions/index.ts b/src/whatsapp/functions/index.ts index 434951a0c9..a29f2c4d3f 100644 --- a/src/whatsapp/functions/index.ts +++ b/src/whatsapp/functions/index.ts @@ -48,6 +48,7 @@ export * from './fetchLinkPreview'; export * from './findChat'; export * from './findCommonGroups'; export * from './findFirstWebLink'; +export * from './findOrCreateLatestChat'; export * from './forwardMessagesToChats'; export * from './frontendFireAndForget'; export * from './genBotMsgSecretFromMsgSecret';