Skip to content

Commit

Permalink
fix: Fixed getAllGroups method (fix #1089, fix #1188)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Jul 11, 2022
1 parent 3e0a33d commit 4efb0cf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/api/layers/retriever.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,23 @@ export class RetrieverLayer extends SenderLayer {
* @category Group
* @returns array of groups
*/
public async getAllGroups(withNewMessagesOnly = false) {
if (withNewMessagesOnly) {
// prettier-ignore
const chats = await evaluateAndReturn(this.page,() => WAPI.getAllChatsWithNewMsg());
return chats.filter((chat) => chat.isGroup);
} else {
const chats = await evaluateAndReturn(this.page, () =>
WAPI.getAllChats()
);
return chats.filter((chat) => chat.isGroup);
}
public async getAllGroups(withNewMessagesOnly = false): Promise<Chat[]> {
return await evaluateAndReturn(
this.page,
async ({ withNewMessagesOnly }) => {
const chats = await WPP.chat.list({
onlyGroups: true,
onlyWithUnreadMessage: withNewMessagesOnly,
});

const groups = await Promise.all(
chats.map((c) => WPP.group.ensureGroup(c.id))
);

return groups.map((g) => WAPI._serializeChatObj(g));
},
{ withNewMessagesOnly }
);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/types/WAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ interface WAPI {
waitNewAcknowledgements: (callback: Function) => void;
waitNewMessages: (rmCallback: boolean, callback: Function) => void;
_profilePicfunc: (contactId: string) => Promise<ProfilePicThumbObj>;
_serializeChatObj: (chat: any) => any;
}

declare global {
Expand Down

0 comments on commit 4efb0cf

Please sign in to comment.