From ab0db800634eee449c445ca2959da9f707434d49 Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Tue, 1 Sep 2020 12:48:22 -0400 Subject: [PATCH] fix: reliably get group metadata GroupMetadata was only loaded for the first couple chats, so you could not access it on older group chats without performing an action that would update its state close #264 --- src/Client.js | 8 ++++---- src/util/Injected.js | 13 ++++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Client.js b/src/Client.js index e9966a2..52a277a 100644 --- a/src/Client.js +++ b/src/Client.js @@ -478,8 +478,8 @@ class Client extends EventEmitter { * @returns {Promise>} */ async getChats() { - let chats = await this.pupPage.evaluate(() => { - return window.WWebJS.getChats(); + let chats = await this.pupPage.evaluate(async () => { + return await window.WWebJS.getChats(); }); return chats.map(chat => ChatFactory.create(this, chat)); @@ -491,8 +491,8 @@ class Client extends EventEmitter { * @returns {Promise} */ async getChatById(chatId) { - let chat = await this.pupPage.evaluate(chatId => { - return window.WWebJS.getChat(chatId); + let chat = await this.pupPage.evaluate(async chatId => { + return await window.WWebJS.getChat(chatId); }, chatId); return ChatFactory.create(this, chat); diff --git a/src/util/Injected.js b/src/util/Injected.js index b473155..9a2ed1b 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -166,26 +166,29 @@ exports.LoadUtils = () => { return mediaData; }; - window.WWebJS.getChatModel = chat => { + window.WWebJS.getChatModel = async chat => { let res = chat.serialize(); res.isGroup = chat.isGroup; res.formattedTitle = chat.formattedTitle; if (chat.groupMetadata) { + await window.Store.GroupMetadata.update(chat.id._serialized); res.groupMetadata = chat.groupMetadata.serialize(); } return res; }; - window.WWebJS.getChat = chatId => { + window.WWebJS.getChat = async chatId => { const chat = window.Store.Chat.get(chatId); - return window.WWebJS.getChatModel(chat); + return await window.WWebJS.getChatModel(chat); }; - window.WWebJS.getChats = () => { + window.WWebJS.getChats = async () => { const chats = window.Store.Chat.models; - return chats.map(chat => window.WWebJS.getChatModel(chat)); + + const chatPromises = chats.map(chat => window.WWebJS.getChatModel(chat)); + return await Promise.all(chatPromises); }; window.WWebJS.getContactModel = contact => {