From 4e0f1d3d85a5b4e8b8e848ee019b29b22d370020 Mon Sep 17 00:00:00 2001 From: Edgard Date: Tue, 28 Sep 2021 20:19:44 -0300 Subject: [PATCH] feat: Added getMessages for Chat --- src/chat/Chat.ts | 58 ++++++++++++++++++++++++++ src/chat/types.ts | 6 +++ src/whatsapp/functions/index.ts | 1 + src/whatsapp/functions/msgFindQuery.ts | 44 +++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 src/whatsapp/functions/msgFindQuery.ts diff --git a/src/chat/Chat.ts b/src/chat/Chat.ts index 9ff2f017a2..91597ae1bd 100644 --- a/src/chat/Chat.ts +++ b/src/chat/Chat.ts @@ -26,6 +26,7 @@ import { ChatStore, ClockSkew, Constants, + Features, MsgKey, ReplyButtonModel, UserPrefs, @@ -34,10 +35,13 @@ import { import { addAndSendMsgToChat, findChat, + msgFindQuery, + MsgFindQueryParams, randomMessageId, } from '../whatsapp/functions'; import { ChatEventTypes, + GetMessagesOptions, ListMessageOptions, MessageButtonsOptions, RawMessage, @@ -77,6 +81,60 @@ export class Chat extends Emittery { return ChatStore.get(wid); } + /** + * Fetch messages from a chat + * + * @example + * ```javascript + * // Some messages + * WPP.chat.getMessages('@c.us', { + * count: 20, + * }); + * + * // All messages + * WPP.chat.getMessages('@c.us', { + * count: -1, + * }); + * + * // 20 messages before specific message + * WPP.chat.getMessages('@c.us', { + * count: 20, + * direction: 'before', + * id: '' + * }); + * ``` + * + * @return {RawMessage[]} List of raw messages + */ + getMessages( + chatId: string | Wid, + options: GetMessagesOptions = {} + ): Promise { + const chat = assertGetChat(chatId); + + let count = options.count || 20; + const direction = options.direction === 'after' ? 'after' : 'before'; + const id = options.id || chat.lastReceivedKey?.toString(); + + let params: MsgFindQueryParams = { + remote: chat.id, + } as any; + + if (id) { + params = MsgKey.fromString(id) as any; + } + + // Fix for multidevice + if (count === -1 && Features.supportsFeature('MD_BACKEND')) { + count = Infinity; + } + + params.count = count; + params.direction = direction; + + return msgFindQuery(direction, params); + } + prepareMessageButtons( message: RawMessage, options: MessageButtonsOptions diff --git a/src/chat/types.ts b/src/chat/types.ts index 97e69925ef..95f049d0f4 100644 --- a/src/chat/types.ts +++ b/src/chat/types.ts @@ -21,6 +21,12 @@ export interface ChatEventTypes { idle: undefined; } +export interface GetMessagesOptions { + count?: number; + direction?: 'after' | 'before'; + id?: string; +} + export interface SendMessageOptions { waitForAck?: boolean; createChat?: boolean; diff --git a/src/whatsapp/functions/index.ts b/src/whatsapp/functions/index.ts index 84365d0243..23a73ab603 100644 --- a/src/whatsapp/functions/index.ts +++ b/src/whatsapp/functions/index.ts @@ -18,5 +18,6 @@ export * from './addAndSendMsgToChat'; export * from './findChat'; export * from './getOrGenerate'; export * from './isAuthenticated'; +export * from './msgFindQuery'; export * from './randomId'; export * from './sendTextMsgToChat'; diff --git a/src/whatsapp/functions/msgFindQuery.ts b/src/whatsapp/functions/msgFindQuery.ts new file mode 100644 index 0000000000..7c562940ca --- /dev/null +++ b/src/whatsapp/functions/msgFindQuery.ts @@ -0,0 +1,44 @@ +/*! + * 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 { Wid } from '..'; +import { exportModule } from '../exportModule'; +import { ModelPropertiesContructor, MsgModel } from '../models'; + +export interface MsgFindQueryParams { + count: number; + direction: 'after' | 'before'; + remote: Wid; + fromMe?: boolean; + id?: string; + participant?: any; +} + +/** + * @whatsapp 2.2136.10:2495 + */ +export declare function msgFindQuery( + direction: 'after' | 'before', + params: MsgFindQueryParams +): Promise[]>; + +exportModule( + exports, + { + msgFindQuery: 'msgFindQuery', + }, + (m) => m.msgFindQuery && m.msgFindByIds +);