From cde0034d932e8f52baa4d1eafebf81249e9062dc Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sun, 8 Sep 2019 03:18:40 -0400 Subject: [PATCH] [+] Implement function to get all open chats. Close #14 --- example.js | 3 +++ src/Client.js | 9 ++++----- src/util/Injected.js | 5 +++++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/example.js b/example.js index 2c1e437..2ec9e1a 100644 --- a/example.js +++ b/example.js @@ -78,6 +78,9 @@ client.on('message', async msg => { } else { msg.reply('This command can only be used in a group!'); } + } else if(msg.body == '!chats') { + const chats = await client.getChats(); + client.sendMessage(msg.from, `The bot has ${chats.length} chats open.`); } }); diff --git a/src/Client.js b/src/Client.js index ee7aa68..caa3b75 100644 --- a/src/Client.js +++ b/src/Client.js @@ -148,12 +148,11 @@ class Client extends EventEmitter { * Get all current chat instances */ async getChats() { - // let chats = await this.pupPage.evaluate(() => { - // return Store.Chat.serialize() - // }); + let chats = await this.pupPage.evaluate(() => { + return WWebJS.getChats(); + }); - // return chats.map(chatData => ChatFactory.create(this, chatData)); - throw new Error('NOT IMPLEMENTED') + return chats.map(chat => ChatFactory.create(this, chat)); } /** diff --git a/src/util/Injected.js b/src/util/Injected.js index 2109c90..17faa27 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -37,6 +37,11 @@ exports.LoadCustomSerializers = () => { const chat = Store.Chat.get(chatId); return WWebJS.getChatModel(chat); } + + window.WWebJS.getChats = () => { + const chats = Store.Chat.models; + return chats.map(chat => WWebJS.getChatModel(chat)); + } } exports.MarkAllRead = () => {