Skip to content

Commit

Permalink
Add retrievers, deprecate some unneeded functions
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcardeenas committed Apr 10, 2020
1 parent b35cc77 commit d41bb44
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 57 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,37 @@ await client.stopTyping(chatId);
await client.setChatState(chatId, 0 | 1 | 2);
```

### Retrieving data

```javascript
// Retrieve contacts
const contacts = await client.getAllContacts();

// Retrieve all messages in chat
const allMessages = await client.loadAndGetAllMessagesInChat(chatId);

// Retrieve contact status
const status = await client.getStatus(contactId);

// Retrieve user profile
const user = await client.getNumberProfile(contactId);

// Retrieve all unread message
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(chatId);

// Retrieve chat/conversation
const chat = await client.getChat(chatId);
```

### Group functions

```javascript
Expand Down
51 changes: 14 additions & 37 deletions src/api/layers/retriever.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ContactStatus,
Message,
PartialMessage,
WhatsappProfile,
} from '../model';
import { SenderLayer } from './sender.layer';

Expand All @@ -17,12 +18,10 @@ declare module WAPI {
const getAllContacts: () => Contact[];
const getChatById: (contactId: string) => Chat;
const getChat: (contactId: string) => Chat;
const getProfilePicFromServer: (chatId: string) => any;
const getProfilePicFromServer: (chatId: string) => string;
const loadEarlierMessages: (contactId: string) => Message[];
const getStatus: (contactId: string) => ContactStatus;
const asyncLoadAllEarlierMessages: (contactId: string) => void;
const loadAllEarlierMessages: (contactId: string) => void;
const getNumberProfile: (contactId: string) => any;
const getNumberProfile: (contactId: string) => WhatsappProfile;
const getUnreadMessages: (
includeMe: boolean,
includeNotifications: boolean,
Expand Down Expand Up @@ -50,7 +49,7 @@ export class RetrieverLayer extends SenderLayer {
* Retrieves all chats
* @returns array of [Chat]
*/
public async getAllChatsgetAllChats(withNewMessageOnly = false) {
public async getAllChats(withNewMessageOnly = false) {
if (withNewMessageOnly) {
return this.page.evaluate(() => WAPI.getAllChatsWithNewMsg());
} else {
Expand Down Expand Up @@ -121,18 +120,16 @@ export class RetrieverLayer extends SenderLayer {
* Retrieves chat object of given contact id
* @param contactId
* @returns contact detial as promise
* @deprecated
*/
public async getChat(contactId: string) {
return this.page.evaluate(
(contactId) => WAPI.getChat(contactId),
contactId
);
return this.getChatById(contactId);
}

/**
* Retrieves chat picture
* @param chatId Chat id
* @returns Url of the chat picture or undefined if there is no picture for the chat.
* @returns url of the chat picture or undefined if there is no picture for the chat.
*/
public async getProfilePicFromServer(chatId: string) {
return this.page.evaluate(
Expand All @@ -145,6 +142,7 @@ export class RetrieverLayer extends SenderLayer {
* Load more messages in chat object from server. Use this in a while loop
* @param contactId
* @returns contact detial as promise
* @deprecated
*/
public async loadEarlierMessages(contactId: string) {
return this.page.evaluate(
Expand All @@ -164,30 +162,6 @@ export class RetrieverLayer extends SenderLayer {
);
}

/**
* Load all messages in chat object from server.
* @param contactId
* @returns contact detial as promise
*/
public async asyncLoadAllEarlierMessages(contactId: string) {
return this.page.evaluate(
(contactId) => WAPI.asyncLoadAllEarlierMessages(contactId),
contactId
);
}

/**
* Load all messages in chat object from server.
* @param contactId
* @returns contact detial as promise
*/
public async loadAllEarlierMessages(contactId: string) {
return this.page.evaluate(
(contactId) => WAPI.loadAllEarlierMessages(contactId),
contactId
);
}

/**
* Checks if a number is a valid whatsapp number
* @param contactId, you need to include the @c.us at the end.
Expand All @@ -206,6 +180,7 @@ export class RetrieverLayer extends SenderLayer {
* @param includeNotifications
* @param useUnreadCount
* @returns any
* @deprecated
*/
public async getUnreadMessages(
includeMe: boolean,
Expand All @@ -230,13 +205,15 @@ export class RetrieverLayer extends SenderLayer {
/**
* Retrieves all new messages (where isNewMsg is true)
* @returns List of messages
* @deprecated Use getAllUnreadMessages
*/
public async getAllNewMessages() {
return this.page.evaluate(() => WAPI.getAllNewMessages());
}

/**
* Retrieves all Messages in a chat
* Retrieves all messages already loaded in a chat
* For loading every message use loadAndGetAllMessagesInChat
* @param chatId, the chat to get the messages from
* @param includeMe, include my own messages? boolean
* @param includeNotifications
Expand All @@ -263,8 +240,8 @@ export class RetrieverLayer extends SenderLayer {
*/
public async loadAndGetAllMessagesInChat(
chatId: string,
includeMe: boolean,
includeNotifications: boolean
includeMe = false,
includeNotifications = false
) {
return await this.page.evaluate(
({ chatId, includeMe, includeNotifications }) =>
Expand Down
73 changes: 54 additions & 19 deletions src/api/model/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,64 @@ import { GroupMetadata } from './group-metadata';
import { Id } from './id';

export interface Chat {
archive: boolean;
changeNumberNewJid: any;
changeNumberOldJid: any;
contact: Contact;
groupMetadata: GroupMetadata;
id: Id;
isAnnounceGrpRestrict: any;
isGroup: boolean;
pendingMsgs: boolean;
lastReceivedKey: LastReceivedKey;
t: number;
unreadCount: number;
archive: boolean;
isReadOnly: boolean;
kind: string;
labels: any;
lastReceivedKey: any;
modifyTag: number;
msgs: any;
muteExpiration: number;
name: string;
notSpam: boolean;
pendingMsgs: boolean;
pin: number;
presence: any;
t: number;
unreadCount: number;
ack?: any;
isOnline?: any;
lastSeen?: any;
msgs: null;
kind: string;
isGroup: boolean;
contact: Contact;
groupMetadata: GroupMetadata;
presence: Presence;
isOnline: null;
lastSeen: null;
}

export interface Contact {
id: Id;
name: string;
shortName: string;
pushname: string;
type: string;
isBusiness: boolean;
isEnterprise: boolean;
statusMute: boolean;
labels: any[];
formattedName: string;
isMe: boolean;
isMyContact: boolean;
isPSA: boolean;
isUser: boolean;
isWAContact: boolean;
profilePicThumbObj: ProfilePicThumbObj;
msgs: null;
}

export interface ProfilePicThumbObj {
eurl: string;
id: Id;
img: string;
imgFull: string;
raw: null;
tag: string;
}

export interface LastReceivedKey {
fromMe: boolean;
remote: Id;
id: string;
_serialized: string;
}

export interface Presence {
id: Id;
chatstates: any[];
}
1 change: 1 addition & 0 deletions src/api/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export { HostDevice } from './host-device';
export { LiveLocation } from './live-location';
export { ContactStatus } from './contact-status';
export { GroupCreation } from './group-creation';
export { WhatsappProfile } from './whatsapp-profile';
9 changes: 9 additions & 0 deletions src/api/model/whatsapp-profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Id } from './id';

export interface WhatsappProfile {
id: Id;
status: number;
isBusiness: boolean;
canReceiveMessage: boolean;
numberExists: boolean;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {
LiveLocation,
PartialMessage,
ParticipantEvent,
WhatsappProfile,
} from './api/model';
export {
AckType,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/wapi/functions/load-all-earlier-chat-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export async function loadAllEarlierMessages(id, done) {
* @param {Funciton} done Optional callback
*/
export function asyncLoadAllEarlierMessages(id, done) {
done();
loadAllEarlierMessages(id);
done();
}

0 comments on commit d41bb44

Please sign in to comment.