Skip to content

Commit

Permalink
feat: Added getMessages for Chat
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Sep 28, 2021
1 parent 851e0e7 commit 4e0f1d3
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/chat/Chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
ChatStore,
ClockSkew,
Constants,
Features,
MsgKey,
ReplyButtonModel,
UserPrefs,
Expand All @@ -34,10 +35,13 @@ import {
import {
addAndSendMsgToChat,
findChat,
msgFindQuery,
MsgFindQueryParams,
randomMessageId,
} from '../whatsapp/functions';
import {
ChatEventTypes,
GetMessagesOptions,
ListMessageOptions,
MessageButtonsOptions,
RawMessage,
Expand Down Expand Up @@ -77,6 +81,60 @@ export class Chat extends Emittery<ChatEventTypes> {
return ChatStore.get(wid);
}

/**
* Fetch messages from a chat
*
* @example
* ```javascript
* // Some messages
* WPP.chat.getMessages('<number>@c.us', {
* count: 20,
* });
*
* // All messages
* WPP.chat.getMessages('<number>@c.us', {
* count: -1,
* });
*
* // 20 messages before specific message
* WPP.chat.getMessages('<number>@c.us', {
* count: 20,
* direction: 'before',
* id: '<full message id>'
* });
* ```
*
* @return {RawMessage[]} List of raw messages
*/
getMessages(
chatId: string | Wid,
options: GetMessagesOptions = {}
): Promise<RawMessage[]> {
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
Expand Down
6 changes: 6 additions & 0 deletions src/chat/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/whatsapp/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
44 changes: 44 additions & 0 deletions src/whatsapp/functions/msgFindQuery.ts
Original file line number Diff line number Diff line change
@@ -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<ModelPropertiesContructor<MsgModel>[]>;

exportModule(
exports,
{
msgFindQuery: 'msgFindQuery',
},
(m) => m.msgFindQuery && m.msgFindByIds
);

0 comments on commit 4e0f1d3

Please sign in to comment.