Skip to content

Commit

Permalink
fix: getchats
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed Feb 21, 2021
1 parent 50885b5 commit c4fade7
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 49 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
70 changes: 43 additions & 27 deletions src/api/layers/retriever.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,70 @@ 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);
});
}

/**
* Retrieves all grups
* @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);
});
}

/**
* Retrieves all chats Contacts
* @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);
});
}

/**
* Retrieves all chats Transmission list
* @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);
});
}

/**
Expand All @@ -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
Expand Down
36 changes: 18 additions & 18 deletions src/api/model/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 10 additions & 1 deletion src/api/model/group-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,22 @@ 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;
_serialized: string;
};
participants: any[];
pendingParticipants: any[];
restrict: boolean;
support: boolean;
}
96 changes: 96 additions & 0 deletions src/lib/wapi/helper/fix-chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
mMMMMMMMMMNNNmmNNNMMNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
mmNMMNMMMMNNNNNmmmddhdddNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
mddNMMNy:/odNmmddmmNNmdhhddmNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
Nmmd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MMM
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;
}
}
1 change: 1 addition & 0 deletions src/lib/wapi/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
5 changes: 5 additions & 0 deletions src/lib/wapi/wapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ import {
generateMediaKey,
getFileHash,
arrayBufferToBase64,
fixChat,
} from './helper';
import {
addNewMessagesListener,
Expand Down Expand Up @@ -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;
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 @@ -95,6 +95,7 @@ interface WAPI {
skipMyMessages: boolean
) => any;
getAllChats: () => Chat[];
fixChat: (chats: Chat[]) => Promise<Chat[] | Boolean>;
getAllChatsWithMessages: (withNewMessageOnly?: boolean) => Chat[];
getAllChatsWithNewMsg: () => Chat[];
getAllContacts: () => Contact[];
Expand Down

0 comments on commit c4fade7

Please sign in to comment.