Skip to content

Commit

Permalink
feat: Added WPP.group.create function
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Nov 11, 2021
1 parent 04262f5 commit 6bc2260
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
59 changes: 58 additions & 1 deletion src/group/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -256,4 +259,58 @@ export class Group extends Emittery<GroupEventTypes> {

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<void>((resolve) => {
chatGroup.on('change:groupMetadata.stale', function fn() {
if (chatGroup.groupMetadata?.stale === false) {
resolve();
chatGroup.off('change:groupMetadata.stale', fn);
}
});
});
}
}

return result;
}
}
5 changes: 5 additions & 0 deletions src/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -135,6 +139,7 @@ export function searchId(

if (condition(module, moduleId)) {
debug(`Module found: ${moduleId} - ${condition.toString()}`);
clearTimeout(timer);
return moduleId;
}
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions src/whatsapp/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
38 changes: 38 additions & 0 deletions src/whatsapp/functions/sendCreateGroup.ts
Original file line number Diff line number Diff line change
@@ -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
);

0 comments on commit 6bc2260

Please sign in to comment.