From 6bc2260dfecbe434adf308e2261f9c7e05fecade Mon Sep 17 00:00:00 2001 From: Edgard Date: Thu, 11 Nov 2021 19:40:15 -0300 Subject: [PATCH] feat: Added WPP.group.create function --- src/group/Group.ts | 59 ++++++++++++++++++++++- src/webpack.ts | 5 ++ src/whatsapp/functions/index.ts | 1 + src/whatsapp/functions/sendCreateGroup.ts | 38 +++++++++++++++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/whatsapp/functions/sendCreateGroup.ts diff --git a/src/group/Group.ts b/src/group/Group.ts index 1926951c56..0dc110db5f 100644 --- a/src/group/Group.ts +++ b/src/group/Group.ts @@ -18,14 +18,17 @@ import Debug from 'debug'; import Emittery from 'emittery'; import { assertGetChat, assertWid } from '../assert'; +import Chat from '../chat'; +import Contact from '../contact'; import { WPPError } from '../util'; import * as webpack from '../webpack'; -import { ParticipantModel, Wid } from '../whatsapp'; +import { ChatStore, ContactStore, ParticipantModel, Wid } from '../whatsapp'; import { addParticipants, demoteParticipants, promoteParticipants, removeParticipants, + sendCreateGroup, } from '../whatsapp/functions'; import { ChatEventTypes as GroupEventTypes } from './types'; @@ -256,4 +259,58 @@ export class Group extends Emittery { return demoteParticipants(groupChat, participants); } + + async create( + groupName: string, + participantsIds: (string | Wid) | (string | Wid)[] + ) { + if (!Array.isArray(participantsIds)) { + participantsIds = [participantsIds]; + } + + const participantsWids = participantsIds.map(assertWid); + + const wids: Wid[] = []; + + for (const wid of participantsWids) { + console.log('wid', wid); + const contact = ContactStore.get(wid); + if (contact) { + wids.push(contact.id); + continue; + } + + const info = await Contact.queryExists(wid); + + if (!info) { + throw new WPPError('participant_not_exists', 'Participant not exists', { + id: wid, + }); + } + + wids.push(info.wid); + } + + ChatStore.on('all', console.log); + + const result = await sendCreateGroup(groupName, wids); + + if (result.gid) { + const chatGroup = await Chat.find(result.gid); + + // Wait group meta to be not stale + if (chatGroup.groupMetadata?.stale !== false) { + await new Promise((resolve) => { + chatGroup.on('change:groupMetadata.stale', function fn() { + if (chatGroup.groupMetadata?.stale === false) { + resolve(); + chatGroup.off('change:groupMetadata.stale', fn); + } + }); + }); + } + } + + return result; + } } diff --git a/src/webpack.ts b/src/webpack.ts index 7cf50408ff..30a3e4c35c 100644 --- a/src/webpack.ts +++ b/src/webpack.ts @@ -122,6 +122,10 @@ export function searchId( ids = ids.reverse(); } + const timer = setTimeout(() => { + debug(`Searching for: ${condition.toString()}`); + }, 500); + const ignoreRE = /\w+\.PureComponent\s*\{/; for (const moduleId of ids) { @@ -135,6 +139,7 @@ export function searchId( if (condition(module, moduleId)) { debug(`Module found: ${moduleId} - ${condition.toString()}`); + clearTimeout(timer); return moduleId; } } catch (error) { diff --git a/src/whatsapp/functions/index.ts b/src/whatsapp/functions/index.ts index bc1cbf785d..e2f006b93c 100644 --- a/src/whatsapp/functions/index.ts +++ b/src/whatsapp/functions/index.ts @@ -23,6 +23,7 @@ export * from './isAuthenticated'; export * from './msgFindQuery'; export * from './randomId'; export * from './sendClear'; +export * from './sendCreateGroup'; export * from './sendDelete'; export * from './sendQueryExists'; export * from './sendTextMsgToChat'; diff --git a/src/whatsapp/functions/sendCreateGroup.ts b/src/whatsapp/functions/sendCreateGroup.ts new file mode 100644 index 0000000000..5ce2ba0680 --- /dev/null +++ b/src/whatsapp/functions/sendCreateGroup.ts @@ -0,0 +1,38 @@ +/*! + * Copyright 2021 WPPConnect Team + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Wid } from '..'; +import { exportModule } from '../exportModule'; + +/** @whatsapp 2.2142.12:53876 */ +export declare function sendCreateGroup( + groupName: string, + participants: Wid[], + ephemeral?: number, + dogfooding?: boolean +): Promise<{ + gid: Wid; + participants: { [key: string]: { code: string } }[]; + status: number; +}>; + +exportModule( + exports, + { + sendCreateGroup: 'sendCreateGroup', + }, + (m) => m.sendCreateGroup +);