Skip to content

Commit

Permalink
Optimize contact retrive
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcardeenas committed Jun 10, 2019
1 parent f0b1d1d commit e75afbf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 35 deletions.
1 change: 1 addition & 0 deletions src/api/model/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Id } from './id';




export interface Contact {
formattedName: string;

Expand Down
27 changes: 26 additions & 1 deletion src/api/whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare module WAPI {
const getAllChatsWithNewMsg: () => Chat[];
const getAllGroups: () => Chat[];
const getGroupParticipantIDs: (groupId: string) => Promise<Id[]>;
const getContact: (contactId: string) => Contact;
}

export class Whatsapp {
Expand Down Expand Up @@ -85,10 +86,34 @@ export class Whatsapp {
* Retrieves group members as [Id] objects
* @param groupId group id
*/
public async getGroupMembersId(groupId) {
public async getGroupMembersId(groupId: string) {
return await this.page.evaluate(
groupId => WAPI.getGroupParticipantIDs(groupId),
groupId
);
}

/**
* Returns group members [Contact] objects
* @param groupId
*/
public async getGroupMembers(groupId: string) {
const membersIds = await this.getGroupMembersId(groupId);
const actions = membersIds.map(memberId => {
return this.getContact(memberId._serialized);
});
return await Promise.all(actions);
}

/**
* Retrieves contact detail object of given contact id
* @param contactId
* @returns contact detial as promise
*/
public async getContact(contactId: string) {
return await this.page.evaluate(
contactId => WAPI.getContact(contactId),
contactId
);
}
}
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ async function start() {
// Group
const groups = await client.getAllGroups();
const group = groups.find(g => g.name === 'NEGATIVE VIBES/SHIT TALK');
const metadata = await client.getGroupMembersId(group.id._serialized);
const metadata = await client.getGroupMembers(group.id._serialized);
console.log(metadata);

// const contacts = await client.getAllContacts();
// const berni = contacts.find(c => c.name === 'Berni');
// console.log(berni.name);
}

Expand Down
31 changes: 0 additions & 31 deletions src/lib/wapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,37 +295,6 @@ window.WAPI.getChatByName = function (name, done) {
return found;
};

window.WAPI.sendImageFromDatabasePicBot = function (picId, chatId, caption) {
var chatDatabase = window.WAPI.getChatByName('DATABASEPICBOT');
var msgWithImg = chatDatabase.msgs.find((msg) => msg.caption == picId);

if (msgWithImg === undefined) {
return false;
}
var chatSend = WAPI.getChat(chatId);
if (chatSend === undefined) {
return false;
}
const oldCaption = msgWithImg.caption;

msgWithImg.id.id = window.WAPI.getNewId();
msgWithImg.id.remote = chatId;
msgWithImg.t = Math.ceil(new Date().getTime() / 1000);
msgWithImg.to = chatId;

if (caption !== undefined && caption !== '') {
msgWithImg.caption = caption;
} else {
msgWithImg.caption = '';
}

msgWithImg.collection.send(msgWithImg).then(function (e) {
msgWithImg.caption = oldCaption;
});

return true;
};

window.WAPI.sendMessageWithThumb = function (thumb, url, title, description, chatId, done) {
var chatSend = WAPI.getChat(chatId);
if (chatSend === undefined) {
Expand Down

0 comments on commit e75afbf

Please sign in to comment.