From c4fade7a8b95537a1921c091475465cef4ba519e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3nalan=20de=20Lima?= Date: Sun, 21 Feb 2021 00:52:05 -0300 Subject: [PATCH] fix: getchats --- README.md | 3 - src/api/layers/retriever.layer.ts | 70 +++++++++++++--------- src/api/model/chat.ts | 36 ++++++------ src/api/model/group-metadata.ts | 11 +++- src/lib/wapi/helper/fix-chat.js | 96 +++++++++++++++++++++++++++++++ src/lib/wapi/helper/index.js | 1 + src/lib/wapi/wapi.js | 5 ++ src/types/WAPI.d.ts | 1 + 8 files changed, 174 insertions(+), 49 deletions(-) create mode 100644 src/lib/wapi/helper/fix-chat.js diff --git a/README.md b/README.md index 00ad0082c..a9a7ada88 100644 --- a/README.md +++ b/README.md @@ -622,9 +622,6 @@ const messages = await client.getAllUnreadMessages(); // Retrieve all chats const chats = await client.getAllChats(); -// Retrieve all groups -const chats = await client.getAllGroups(); - // Retrieve profile fic (as url) const url = await client.getProfilePicFromServer('000000000000@c.us'); diff --git a/src/api/layers/retriever.layer.ts b/src/api/layers/retriever.layer.ts index 12ba97c78..1b830c216 100644 --- a/src/api/layers/retriever.layer.ts +++ b/src/api/layers/retriever.layer.ts @@ -111,7 +111,10 @@ export class RetrieverLayer extends SenderLayer { * @returns array of [Chat] */ public async getAllChats() { - return await this.page.evaluate(() => WAPI.getAllChats()); + return await this.page.evaluate(() => { + let chats = WAPI.getAllChats(); + return WAPI.fixChat(chats); + }); } /** @@ -119,8 +122,11 @@ export class RetrieverLayer extends SenderLayer { * @returns array of [Chat] */ public async getAllChatsGroups() { - const chats = await this.page.evaluate(() => WAPI.getAllChats()); - return chats.filter((chat) => chat.kind === 'group'); + return await this.page.evaluate(() => { + let chats = WAPI.getAllChats(), + filter = chats.filter((chat) => chat.kind === 'group'); + return WAPI.fixChat(filter); + }); } /** @@ -128,8 +134,11 @@ export class RetrieverLayer extends SenderLayer { * @returns array of [Chat] */ public async getAllChatsContacts() { - const chats = await this.page.evaluate(() => WAPI.getAllChats()); - return chats.filter((chat) => chat.kind === 'chat'); + return await this.page.evaluate(() => { + let chats = WAPI.getAllChats(), + filter = chats.filter((chat) => chat.kind === 'chat'); + return WAPI.fixChat(filter); + }); } /** @@ -137,8 +146,35 @@ export class RetrieverLayer extends SenderLayer { * @returns array of [Chat] */ public async getAllChatsTransmission() { - const chats = await this.page.evaluate(() => WAPI.getAllChats()); - return chats.filter((chat) => chat.kind === 'broadcast'); + return await this.page.evaluate(() => { + let chats = WAPI.getAllChats(), + filter = chats.filter((chat) => chat.kind === 'broadcast'); + return WAPI.fixChat(filter); + }); + } + + /** + * Retrieve all groups new messages + * @returns array of groups + */ + public async getChatGroupNewMsg() { + return await this.page.evaluate(() => { + var chats = WAPI.getAllChatsWithNewMsg(), + filter = chats.filter((chat) => chat.kind === 'group'); + return WAPI.fixChat(filter); + }); + } + + /** + * Retrieve all contacts new messages + * @returns array of groups + */ + public async getChatContactNewMsg() { + return await this.page.evaluate(() => { + var chats = WAPI.getAllChatsWithNewMsg(), + filter = chats.filter((chat) => chat.kind === 'chat'); + return WAPI.fixChat(filter); + }); } /** @@ -165,26 +201,6 @@ export class RetrieverLayer extends SenderLayer { ); } - /** - * Retrieve all groups new messages - * @returns array of groups - */ - public async getChatGroupNewMsg() { - // prettier-ignore - const chats = await this.page.evaluate(() => WAPI.getAllChatsWithNewMsg()); - return chats.filter((chat) => chat.kind === 'group'); - } - - /** - * Retrieve all contacts new messages - * @returns array of groups - */ - public async getChatContactNewMsg() { - // prettier-ignore - const chats = await this.page.evaluate(() => WAPI.getAllChatsWithNewMsg()); - return chats.filter((chat) => chat.kind === 'chat'); - } - /** * Retrieves contact detail object of given contact id * @param contactId diff --git a/src/api/model/chat.ts b/src/api/model/chat.ts index 9a6977b1a..742e807cb 100644 --- a/src/api/model/chat.ts +++ b/src/api/model/chat.ts @@ -57,31 +57,31 @@ import { GroupMetadata } from './group-metadata'; import { Id } from './id'; export interface Chat { - id: Id; - pendingMsgs: boolean; - lastReceivedKey: LastReceivedKey; - t: number; - unreadCount: number; archive: boolean; + changeNumberNewJid: Id; + changeNumberOldJid: Id; + contact: Contact; + ephemeralDuration: number; + ephemeralSettingTimestamp: number; + groupMetadata: GroupMetadata; + id: Id; + isAnnounceGrpRestrict: boolean; + isGroup: boolean; + isOnline: null | boolean; isReadOnly: boolean; + kind: string; + lastReceivedKey: LastReceivedKey; + lastSeen: null | number | boolean; + modifyTag: number; + msgs: null; muteExpiration: number; name: string; notSpam: boolean; + pendingMsgs: boolean; pin: number; - msgs: null; - kind: string; - isGroup: boolean; - contact: Contact; - groupMetadata: GroupMetadata; presence: Presence; - /** - * @deprecated This is unreliable. Use the method {@link Whatsapp.getChatIsOnline} instead. - */ - isOnline: null | boolean; - /** - * @deprecated This is unreliable. Use the method {@link Whatsapp.getLastSeen} instead. - */ - lastSeen: null | number | boolean; + t: number; + unreadCount: number; } export interface ProfilePicThumbObj { diff --git a/src/api/model/group-metadata.ts b/src/api/model/group-metadata.ts index 4bf0d4de8..b51134a91 100644 --- a/src/api/model/group-metadata.ts +++ b/src/api/model/group-metadata.ts @@ -55,8 +55,15 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM import { Id } from '.'; export interface GroupMetadata { - id: Id; + announce: boolean; creation: number; + desc: string; + descId: string; + desOwner: Id; + descTime: number; + ephemeralDuration: number; + id: Id; + noFrequentlyForwarded: boolean; owner: { server: string; user: string; @@ -64,4 +71,6 @@ export interface GroupMetadata { }; participants: any[]; pendingParticipants: any[]; + restrict: boolean; + support: boolean; } diff --git a/src/lib/wapi/helper/fix-chat.js b/src/lib/wapi/helper/fix-chat.js new file mode 100644 index 000000000..6ba97093d --- /dev/null +++ b/src/lib/wapi/helper/fix-chat.js @@ -0,0 +1,96 @@ +/* +NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN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.yN- d.oNMMMMMMMMd d- mNh-`.`+mN/ yMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMN/ . +NMMN- oNMMMMMNdN. dMMMd:`/. ds.dNMMMMMMm::M- dMMNy/dMMN/ yMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMN/ +NMMMN- /yyyyyys d. dMMMMNo` dNy-+ymmmho-+NN- dMMMMMMMMN/ yMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMNyNMMMMN+::::::::::m+/mMMMMMMd: dMMNho///+'ymMMN+/mMMMMMMMMNs/hMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMNMMMMMMMMMMMMMMMMMMMMMMMMMMMMNsmMMMMMMMMMMMMMMNNNNMMNNNMMNNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMMMMMMMMMMMMMMMsMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +*/ +export async function fixChat(chatData) { + if ( + typeof chatData === 'object' && + Array.isArray(chatData) && + chatData.length + ) { + const rows = chatData.length, + result = chatData; + for (let i = 0; i < rows; i++) { + result[i].sender = + typeof result[i].sender === 'object' + ? await result.sender + : result.sender; + + if (result[i].contact) { + result[i].contact = + typeof result[i].contact === 'object' + ? await result[i].contact + : result[i].contact; + } + if (result[i].chat) { + result[i].chat = + typeof result[i].chat === 'object' + ? await result[i].chat + : result[i].chat; + } + if (result[i].chat && result[i].chat.contact) { + result[i].chat.contact = + typeof result[i].chat.contact === 'object' + ? await result[i].chat.contact + : result[i].chat.contact; + } + + if (result[i].hasOwnProperty('msgUnsyncedButtonReplyMsgs')) { + result[i].msgUnsyncedButtonReplyMsgs = 'undefined'; + } + } + return result; + } else { + return false; + } +} diff --git a/src/lib/wapi/helper/index.js b/src/lib/wapi/helper/index.js index 49f2650b1..f18dedd2f 100644 --- a/src/lib/wapi/helper/index.js +++ b/src/lib/wapi/helper/index.js @@ -56,3 +56,4 @@ export { base64ToFile } from './base64-to-file'; export { getFileHash } from './get-file-hash'; export { generateMediaKey } from './generate-media-key'; export { arrayBufferToBase64 } from './array-buffer-to-base64'; +export { fixChat } from './fix-chat'; diff --git a/src/lib/wapi/wapi.js b/src/lib/wapi/wapi.js index cf7f51f95..e76a54bcc 100644 --- a/src/lib/wapi/wapi.js +++ b/src/lib/wapi/wapi.js @@ -161,6 +161,7 @@ import { generateMediaKey, getFileHash, arrayBufferToBase64, + fixChat, } from './helper'; import { addNewMessagesListener, @@ -221,12 +222,16 @@ if (typeof window.WAPI === 'undefined') { lastRead: {}, }; + //helpers + window.WAPI.fixChat = fixChat; + //class window.WAPI.callbackWile = new callbackWile(); //others window.WAPI.interfaceMute = interfaceMute; window.WAPI.isInsideChat = isInsideChat; + //Profile window.WAPI.setProfilePic = setProfilePic; window.WAPI.getSessionTokenBrowser = getSessionTokenBrowser; diff --git a/src/types/WAPI.d.ts b/src/types/WAPI.d.ts index bc4514052..6c10279c1 100644 --- a/src/types/WAPI.d.ts +++ b/src/types/WAPI.d.ts @@ -95,6 +95,7 @@ interface WAPI { skipMyMessages: boolean ) => any; getAllChats: () => Chat[]; + fixChat: (chats: Chat[]) => Promise; getAllChatsWithMessages: (withNewMessageOnly?: boolean) => Chat[]; getAllChatsWithNewMsg: () => Chat[]; getAllContacts: () => Contact[];