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
This commit is contained in:
Pedro Lopez
2020-09-01 12:48:22 -04:00
parent e7c76fe069
commit ab0db80063
2 changed files with 12 additions and 9 deletions

View File

@@ -478,8 +478,8 @@ class Client extends EventEmitter {
* @returns {Promise<Array<Chat>>} * @returns {Promise<Array<Chat>>}
*/ */
async getChats() { async getChats() {
let chats = await this.pupPage.evaluate(() => { let chats = await this.pupPage.evaluate(async () => {
return window.WWebJS.getChats(); return await window.WWebJS.getChats();
}); });
return chats.map(chat => ChatFactory.create(this, chat)); return chats.map(chat => ChatFactory.create(this, chat));
@@ -491,8 +491,8 @@ class Client extends EventEmitter {
* @returns {Promise<Chat>} * @returns {Promise<Chat>}
*/ */
async getChatById(chatId) { async getChatById(chatId) {
let chat = await this.pupPage.evaluate(chatId => { let chat = await this.pupPage.evaluate(async chatId => {
return window.WWebJS.getChat(chatId); return await window.WWebJS.getChat(chatId);
}, chatId); }, chatId);
return ChatFactory.create(this, chat); return ChatFactory.create(this, chat);

View File

@@ -166,26 +166,29 @@ exports.LoadUtils = () => {
return mediaData; return mediaData;
}; };
window.WWebJS.getChatModel = chat => { window.WWebJS.getChatModel = async chat => {
let res = chat.serialize(); let res = chat.serialize();
res.isGroup = chat.isGroup; res.isGroup = chat.isGroup;
res.formattedTitle = chat.formattedTitle; res.formattedTitle = chat.formattedTitle;
if (chat.groupMetadata) { if (chat.groupMetadata) {
await window.Store.GroupMetadata.update(chat.id._serialized);
res.groupMetadata = chat.groupMetadata.serialize(); res.groupMetadata = chat.groupMetadata.serialize();
} }
return res; return res;
}; };
window.WWebJS.getChat = chatId => { window.WWebJS.getChat = async chatId => {
const chat = window.Store.Chat.get(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; 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 => { window.WWebJS.getContactModel = contact => {