From cdc00e934d3c7c2461e0b49ae6f59fc17675bd3f Mon Sep 17 00:00:00 2001 From: JoseHM8A <70950705+JoseHM8A@users.noreply.github.com> Date: Sat, 5 Jun 2021 21:10:47 +0000 Subject: [PATCH] feat: acceptGroupV4Invite (#677) * Added option client.acceptGroupV4Invite() * Update Client.js * Update index.d.ts Added a description to the inviteV4 object Renamed "invite" by "inviteV4" * Update Client.js * Update Message.js renamed "invite" to "inviteV4" * Update Client.js * Added message.acceptGroupV4Invite method * Update index.d.ts acceptGroupV4Invite * better typings, refactor some names Co-authored-by: Pedro Lopez --- index.d.ts | 17 +++++++++++++++++ src/Client.js | 14 ++++++++++++++ src/structures/Message.js | 21 +++++++++++++++++++++ src/util/Constants.js | 3 ++- 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 5737af4..03c223c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -19,6 +19,9 @@ declare namespace WAWebJS { /**Accepts an invitation to join a group */ acceptInvite(inviteCode: string): Promise + /** Accepts a private invitation to join a group (v4 invite) */ + acceptGroupV4Invite: (inviteV4: InviteV4Data) => Promise<{status: number}> + /**Returns an object with information about the invite code's group */ getInviteInfo(inviteCode: string): Promise @@ -419,6 +422,7 @@ declare namespace WAWebJS { ORDER = 'order', PRODUCT = 'product', UNKNOWN = 'unknown', + GROUP_INVITE = 'groups_v4_invite', } /** Client status */ @@ -453,6 +457,15 @@ declare namespace WAWebJS { readRemaining: number } + export type InviteV4Data = { + inviteCode: string, + inviteCodeExp: number, + groupId: string, + groupName?: string, + fromId: string, + toId: string + } + /** * Represents a Message on WhatsApp * @@ -510,6 +523,8 @@ declare namespace WAWebJS { location: Location, /** List of vCards contained in the message */ vCards: string[], + /** Invite v4 info */ + inviteV4?: InviteV4Data, /** MediaKey that represents the sticker 'ID' */ mediaKey?: string, /** Indicates the mentions in the message body. */ @@ -536,6 +551,8 @@ declare namespace WAWebJS { businessOwnerJid?: string, /** Product JID */ productId?: string, + /** Accept the Group V4 Invite in message */ + acceptGroupV4Invite: () => Promise<{status: number}>, /** Deletes the message from the chat */ delete: (everyone?: boolean) => Promise, /** Downloads and returns the attatched message media */ diff --git a/src/Client.js b/src/Client.js index 3116953..690a5b2 100644 --- a/src/Client.js +++ b/src/Client.js @@ -607,6 +607,20 @@ class Client extends EventEmitter { return chatId._serialized; } + /** + * Accepts a private invitation to join a group + * @param {object} inviteV4 Invite V4 Info + * @returns {Promise} + */ + async acceptGroupV4Invite(inviteInfo) { + if(!inviteInfo.inviteCode) throw 'Invalid invite code, try passing the message.inviteV4 object'; + if (inviteInfo.inviteCodeExp == 0) throw 'Expired invite code'; + return await this.pupPage.evaluate(async inviteInfo => { + let { groupId, fromId, inviteCode, inviteCodeExp, toId } = inviteInfo; + return await window.Store.Wap.acceptGroupV4Invite(groupId, fromId, inviteCode, String(inviteCodeExp), toId); + }, inviteInfo); + } + /** * Sets the current user's status message * @param {string} status New status message diff --git a/src/structures/Message.js b/src/structures/Message.js index 783050b..d7a21b0 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -136,6 +136,19 @@ class Message extends Base { */ this.vCards = data.type === MessageTypes.CONTACT_CARD_MULTI ? data.vcardList.map((c) => c.vcard) : data.type === MessageTypes.CONTACT_CARD ? [data.body] : []; + /** + * Group Invite Data + * @type {object} + */ + this.inviteV4 = data.type === MessageTypes.GROUP_INVITE ? { + inviteCode: data.inviteCode, + inviteCodeExp: data.inviteCodeExp, + groupId: data.inviteGrp, + groupName: data.inviteGrpName, + fromId: data.from._serialized, + toId: data.to._serialized + } : undefined; + /** * Indicates the mentions in the message body. * @type {Array} @@ -252,6 +265,14 @@ class Message extends Base { return this.client.sendMessage(chatId, content, options); } + /** + * Accept Group V4 Invite + * @returns {Promise} + */ + async acceptGroupV4Invite() { + return await this.client.acceptGroupV4Invite(this.inviteV4); + } + /** * Forwards this message to another chat * diff --git a/src/util/Constants.js b/src/util/Constants.js index 1e32dc0..0fc3966 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -72,7 +72,8 @@ exports.MessageTypes = { ORDER: 'order', REVOKED: 'revoked', PRODUCT: 'product', - UNKNOWN: 'unknown' + UNKNOWN: 'unknown', + GROUP_INVITE: 'groups_v4_invite' }; /**