From 70f2391d51c6684dc22b84d7518b9ea9482ab83e Mon Sep 17 00:00:00 2001 From: "Pedro S. Lopez" Date: Sun, 15 Jan 2023 17:59:20 -0400 Subject: [PATCH] fix group methods (#1934) * fix: create group * fix set group subject, description, properties; exit group * fix participant methods * fix return type * fix invite methods --- src/Client.js | 18 +++++----- src/structures/GroupChat.js | 70 ++++++++++++++++++++++++------------- src/util/Injected.js | 12 +++---- 3 files changed, 60 insertions(+), 40 deletions(-) diff --git a/src/Client.js b/src/Client.js index d3c9bb4..7a09d7a 100644 --- a/src/Client.js +++ b/src/Client.js @@ -774,7 +774,7 @@ class Client extends EventEmitter { */ async getInviteInfo(inviteCode) { return await this.pupPage.evaluate(inviteCode => { - return window.Store.InviteInfo.sendQueryGroupInvite(inviteCode); + return window.Store.InviteInfo.queryGroupInvite(inviteCode); }, inviteCode); } @@ -784,11 +784,11 @@ class Client extends EventEmitter { * @returns {Promise} Id of the joined Chat */ async acceptInvite(inviteCode) { - const chatId = await this.pupPage.evaluate(async inviteCode => { - return await window.Store.Invite.sendJoinGroupViaInvite(inviteCode); + const res = await this.pupPage.evaluate(async inviteCode => { + return await window.Store.Invite.joinGroupViaInvite(inviteCode); }, inviteCode); - return chatId._serialized; + return res.gid._serialized; } /** @@ -1095,19 +1095,17 @@ class Client extends EventEmitter { const createRes = await this.pupPage.evaluate(async (name, participantIds) => { const participantWIDs = participantIds.map(p => window.Store.WidFactory.createWid(p)); - const id = window.Store.MsgKey.newId(); - const res = await window.Store.GroupUtils.sendCreateGroup(name, participantWIDs, undefined, id); - return res; + return await window.Store.GroupUtils.createGroup(name, participantWIDs, 0); }, name, participants); const missingParticipants = createRes.participants.reduce(((missing, c) => { - const id = Object.keys(c)[0]; - const statusCode = c[id].code; + const id = c.wid._serialized; + const statusCode = c.error ? c.error.toString() : '200'; if (statusCode != 200) return Object.assign(missing, { [id]: statusCode }); return missing; }), {}); - return { gid: createRes.gid, missingParticipants }; + return { gid: createRes.wid, missingParticipants }; } /** diff --git a/src/structures/GroupChat.js b/src/structures/GroupChat.js index c11f5a9..0be6b62 100644 --- a/src/structures/GroupChat.js +++ b/src/structures/GroupChat.js @@ -59,10 +59,15 @@ class GroupChat extends Chat { * @returns {Promise} */ async addParticipants(participantIds) { - return await this.client.pupPage.evaluate((chatId, participantIds) => { + return await this.client.pupPage.evaluate(async (chatId, participantIds) => { const chatWid = window.Store.WidFactory.createWid(chatId); - const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p)); - return window.Store.GroupParticipants.sendAddParticipants(chatWid, participantWids); + const chat = await window.Store.Chat.find(chatWid); + const participants = await Promise.all(participantIds.map(async p => { + const wid = window.Store.WidFactory.createWid(p); + return await window.Store.Contact.get(wid); + })); + await window.Store.GroupParticipants.addParticipants(chat, participants); + return { status: 200 }; }, this.id._serialized, participantIds); } @@ -72,10 +77,14 @@ class GroupChat extends Chat { * @returns {Promise} */ async removeParticipants(participantIds) { - return await this.client.pupPage.evaluate((chatId, participantIds) => { + return await this.client.pupPage.evaluate(async (chatId, participantIds) => { const chatWid = window.Store.WidFactory.createWid(chatId); - const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p)); - return window.Store.GroupParticipants.sendRemoveParticipants(chatWid, participantWids); + const chat = await window.Store.Chat.find(chatWid); + const participants = participantIds.map(p => { + return chat.groupMetadata.participants.get(p); + }).filter(p => Boolean(p)); + await window.Store.GroupParticipants.removeParticipants(chat, participants); + return { status: 200 }; }, this.id._serialized, participantIds); } @@ -85,10 +94,14 @@ class GroupChat extends Chat { * @returns {Promise<{ status: number }>} Object with status code indicating if the operation was successful */ async promoteParticipants(participantIds) { - return await this.client.pupPage.evaluate((chatId, participantIds) => { + return await this.client.pupPage.evaluate(async (chatId, participantIds) => { const chatWid = window.Store.WidFactory.createWid(chatId); - const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p)); - return window.Store.GroupParticipants.sendPromoteParticipants(chatWid, participantWids); + const chat = await window.Store.Chat.find(chatWid); + const participants = participantIds.map(p => { + return chat.groupMetadata.participants.get(p); + }).filter(p => Boolean(p)); + await window.Store.GroupParticipants.promoteParticipants(chat, participants); + return { status: 200 }; }, this.id._serialized, participantIds); } @@ -98,10 +111,14 @@ class GroupChat extends Chat { * @returns {Promise<{ status: number }>} Object with status code indicating if the operation was successful */ async demoteParticipants(participantIds) { - return await this.client.pupPage.evaluate((chatId, participantIds) => { + return await this.client.pupPage.evaluate(async (chatId, participantIds) => { const chatWid = window.Store.WidFactory.createWid(chatId); - const participantWids = participantIds.map(p => window.Store.WidFactory.createWid(p)); - return window.Store.GroupParticipants.sendDemoteParticipants(chatWid, participantWids); + const chat = await window.Store.Chat.find(chatWid); + const participants = participantIds.map(p => { + return chat.groupMetadata.participants.get(p); + }).filter(p => Boolean(p)); + await window.Store.GroupParticipants.demoteParticipants(chat, participants); + return { status: 200 }; }, this.id._serialized, participantIds); } @@ -114,7 +131,8 @@ class GroupChat extends Chat { const success = await this.client.pupPage.evaluate(async (chatId, subject) => { const chatWid = window.Store.WidFactory.createWid(chatId); try { - return await window.Store.GroupUtils.sendSetGroupSubject(chatWid, subject); + await window.Store.GroupUtils.setGroupSubject(chatWid, subject); + return true; } catch (err) { if(err.name === 'ServerStatusCodeError') return false; throw err; @@ -136,7 +154,8 @@ class GroupChat extends Chat { const chatWid = window.Store.WidFactory.createWid(chatId); let descId = window.Store.GroupMetadata.get(chatWid).descId; try { - return await window.Store.GroupUtils.sendSetGroupDescription(chatWid, description, window.Store.MsgKey.newId(), descId); + await window.Store.GroupUtils.setGroupDescription(chatWid, description, window.Store.MsgKey.newId(), descId); + return true; } catch (err) { if(err.name === 'ServerStatusCodeError') return false; throw err; @@ -157,7 +176,8 @@ class GroupChat extends Chat { const success = await this.client.pupPage.evaluate(async (chatId, adminsOnly) => { const chatWid = window.Store.WidFactory.createWid(chatId); try { - return await window.Store.GroupUtils.sendSetGroupProperty(chatWid, 'announcement', adminsOnly ? 1 : 0); + await window.Store.GroupUtils.setGroupProperty(chatWid, 'announcement', adminsOnly ? 1 : 0); + return true; } catch (err) { if(err.name === 'ServerStatusCodeError') return false; throw err; @@ -179,7 +199,8 @@ class GroupChat extends Chat { const success = await this.client.pupPage.evaluate(async (chatId, adminsOnly) => { const chatWid = window.Store.WidFactory.createWid(chatId); try { - return await window.Store.GroupUtils.sendSetGroupProperty(chatWid, 'restrict', adminsOnly ? 1 : 0); + await window.Store.GroupUtils.setGroupProperty(chatWid, 'restrict', adminsOnly ? 1 : 0); + return true; } catch (err) { if(err.name === 'ServerStatusCodeError') return false; throw err; @@ -197,12 +218,12 @@ class GroupChat extends Chat { * @returns {Promise} Group's invite code */ async getInviteCode() { - const code = await this.client.pupPage.evaluate(async chatId => { + const codeRes = await this.client.pupPage.evaluate(async chatId => { const chatWid = window.Store.WidFactory.createWid(chatId); - return window.Store.Invite.sendQueryGroupInviteCode(chatWid); + return window.Store.Invite.queryGroupInviteCode(chatWid); }, this.id._serialized); - return code; + return codeRes.code; } /** @@ -210,12 +231,12 @@ class GroupChat extends Chat { * @returns {Promise} New invite code */ async revokeInvite() { - const code = await this.client.pupPage.evaluate(chatId => { + const codeRes = await this.client.pupPage.evaluate(chatId => { const chatWid = window.Store.WidFactory.createWid(chatId); - return window.Store.Invite.sendRevokeGroupInviteCode(chatWid); + return window.Store.Invite.resetGroupInviteCode(chatWid); }, this.id._serialized); - return code; + return codeRes.code; } /** @@ -223,9 +244,10 @@ class GroupChat extends Chat { * @returns {Promise} */ async leave() { - await this.client.pupPage.evaluate(chatId => { + await this.client.pupPage.evaluate(async chatId => { const chatWid = window.Store.WidFactory.createWid(chatId); - return window.Store.GroupUtils.sendExitGroup(chatWid); + const chat = await window.Store.Chat.find(chatWid); + return window.Store.GroupUtils.sendExitGroup(chat); }, this.id._serialized); } diff --git a/src/util/Injected.js b/src/util/Injected.js index e1f3871..7acc145 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -14,8 +14,8 @@ exports.ExposeStore = (moduleRaidStr) => { window.Store.CryptoLib = window.mR.findModule('decryptE2EMedia')[0]; window.Store.DownloadManager = window.mR.findModule('downloadManager')[0].downloadManager; window.Store.GroupMetadata = window.mR.findModule('GroupMetadata')[0].default.GroupMetadata; - window.Store.Invite = window.mR.findModule('sendJoinGroupViaInvite')[0]; - window.Store.InviteInfo = window.mR.findModule('sendQueryGroupInvite')[0]; + window.Store.Invite = window.mR.findModule('resetGroupInviteCode')[0]; + window.Store.InviteInfo = window.mR.findModule('queryGroupInvite')[0]; window.Store.Label = window.mR.findModule('LabelCollection')[0].LabelCollection; window.Store.MediaPrep = window.mR.findModule('MediaPrep')[0]; window.Store.MediaObject = window.mR.findModule('getOrCreateMediaObject')[0]; @@ -41,7 +41,7 @@ exports.ExposeStore = (moduleRaidStr) => { window.Store.ProfilePic = window.mR.findModule('profilePicResync')[0]; window.Store.PresenceUtils = window.mR.findModule('sendPresenceAvailable')[0]; window.Store.ChatState = window.mR.findModule('sendChatStateComposing')[0]; - window.Store.GroupParticipants = window.mR.findModule('sendPromoteParticipants')[0]; + window.Store.GroupParticipants = window.mR.findModule('promoteParticipants')[1]; window.Store.JoinInviteV4 = window.mR.findModule('sendJoinGroupViaInviteV4')[0]; window.Store.findCommonGroups = window.mR.findModule('findCommonGroups')[0].findCommonGroups; window.Store.StatusUtils = window.mR.findModule('setMyStatus')[0]; @@ -60,9 +60,9 @@ exports.ExposeStore = (moduleRaidStr) => { }; window.Store.GroupUtils = { - ...window.mR.findModule('sendCreateGroup')[0], - ...window.mR.findModule('sendSetGroupSubject')[0], - ...window.mR.findModule('markExited')[0] + ...window.mR.findModule('createGroup')[0], + ...window.mR.findModule('setGroupDescription')[0], + ...window.mR.findModule('sendExitGroup')[0] }; if (!window.Store.Chat._find) {