Skip to content

Commit

Permalink
feat: Added WPP.chat.list function
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed May 28, 2022
1 parent 625b3b5 commit 8657dd1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/chat/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export { generateMessageID } from './generateMessageID';
export { get } from './get';
export { getMessageById } from './getMessageById';
export { getMessages, GetMessagesOptions } from './getMessages';
export { ChatListOptions, list } from './list';
export { markIsComposing } from './markIsComposing';
export { markIsPaused } from './markIsPaused';
export { markIsRead } from './markIsRead';
Expand Down
58 changes: 58 additions & 0 deletions src/chat/functions/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*!
* 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 { ChatModel, ChatStore } from '../../whatsapp';

export interface ChatListOptions {
onlyGroups?: boolean;
onlyUsers?: boolean;
onlyWithUnreadMessage?: boolean;
}

/**
* Return a list of chats
*
* @example
* ```javascript
* // All chats
* const chat = await WPP.chat.list();
*
* // Only users chats
* const chat = await WPP.chat.list({onlyUsers: true});
*
* // Only groups chats
* const chat = await WPP.chat.list({onlyGroups: true});
* ```
*
* @category Chat
*/
export async function list(options: ChatListOptions): Promise<ChatModel[]> {
let models = ChatStore.models;

if (options.onlyUsers) {
models = models.filter((c) => c.isUser);
}

if (options.onlyGroups) {
models = models.filter((c) => c.isGroup);
}

if (options.onlyWithUnreadMessage) {
models = models.filter((c) => c.hasUnread);
}

return models;
}

0 comments on commit 8657dd1

Please sign in to comment.