Skip to content

Commit

Permalink
feat: Added option to get only media, url and docs for WPP.chat.getMe…
Browse files Browse the repository at this point in the history
…ssages
  • Loading branch information
icleitoncosta authored and edgardmessias committed Aug 10, 2022
1 parent f4746df commit a74ac3c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
43 changes: 42 additions & 1 deletion src/chat/functions/getMessages.ts
Expand Up @@ -25,6 +25,7 @@ export interface GetMessagesOptions {
direction?: 'after' | 'before';
id?: string;
onlyUnread?: boolean;
media?: 'url' | 'document' | 'all' | 'image';
}

/**
Expand Down Expand Up @@ -60,6 +61,30 @@ export interface GetMessagesOptions {
* direction: 'before',
* id: '<full message id>'
* });
*
* // Only media messages (url, document and links)
* WPP.chat.getMessages('[number]@c.us', {
* count: 20,
* media: 'all',
* });
*
* // Only image messages
* WPP.chat.getMessages('[number]@c.us', {
* count: 20,
* media: 'image',
* });
*
* // Only document messages
* WPP.chat.getMessages('[number]@c.us', {
* count: 20,
* media: 'document',
* });
*
* // Only link (url) messages
* WPP.chat.getMessages('[number]@c.us', {
* count: 20,
* media: 'url',
* });
* ```
* @category Message
* @return {RawMessage[]} List of raw messages
Expand Down Expand Up @@ -107,7 +132,23 @@ export async function getMessages(
params.count = count;
params.direction = direction;

const msgs = await msgFindQuery(direction, params);
let msgs = [];
if (options.media === 'all') {
const { messages } = await msgFindQuery('media', params);
msgs = messages;
} else if (options.media === 'image') {
const { messages } = await msgFindQuery('media', params);
for (const Msg of messages) {
if (Msg.type === 'image') {
msgs.push(Msg);
}
}
} else if (options.media !== undefined) {
params.media = options.media;
msgs = await msgFindQuery('media', params);
} else {
msgs = await msgFindQuery(direction, params);
}

if (!options.id && id) {
const msg = MsgStore.get(id);
Expand Down
5 changes: 3 additions & 2 deletions src/whatsapp/functions/msgFindQuery.ts
Expand Up @@ -25,13 +25,14 @@ export interface MsgFindQueryParams {
fromMe?: boolean;
id?: string;
participant?: any;
media?: 'url' | 'document';
}

/** @whatsapp 76581 */
export declare function msgFindQuery(
direction: 'after' | 'before',
direction: 'after' | 'before' | 'media',
params: MsgFindQueryParams
): Promise<ModelPropertiesContructor<MsgModel>[]>;
): Promise<ModelPropertiesContructor<MsgModel>[] | any>;

exportModule(
exports,
Expand Down

0 comments on commit a74ac3c

Please sign in to comment.