Skip to content

Commit

Permalink
fix: Fixed the return for WPP.group.addParticipants function
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Jun 18, 2022
1 parent 26840ab commit 7f01c85
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 13 deletions.
76 changes: 67 additions & 9 deletions src/group/functions/addParticipants.ts
Expand Up @@ -14,29 +14,87 @@
* limitations under the License.
*/

import { WPPError } from '../../util';
import { Wid } from '../../whatsapp';
import { createWid, WPPError } from '../../util';
import { ContactStore, Wid } from '../../whatsapp';
import * as wa_functions from '../../whatsapp/functions';
import { ensureGroupAndParticipants } from './ensureGroupAndParticipants';

const messageCodes: {
[key: number]: string;
} = {
403: "Can't join this group because the number was restricted it.",
409: "Can't join this group because the number is already a member of it.",
};

/**
* Add one or more participants to a group
*
* The method return a object with the result of each participant as the key
*
* @example
* ```javascript
* await WPP.group.addParticipants('[group@g.us]', [number@c.us]);
* ```
*
* @category Group
*/
export async function addParticipants(
groupId: string | Wid,
participantsIds: (string | Wid) | (string | Wid)[]
): Promise<void> {
): Promise<{
[key: `${number}@c.us`]: {
code: number;
message: string;
invite_code: string | null;
invite_code_exp: number | null;
};
}> {
const { groupChat, participants } = await ensureGroupAndParticipants(
groupId,
participantsIds,
true
);

if (
participants.some((p) => groupChat.groupMetadata?.participants.get(p.id))
) {
const result = await wa_functions.sendAddParticipants(
groupChat.id,
participants.map((p) => p.id)
);

if (result.status >= 400) {
throw new WPPError(
'group_participant_already_a_group_member',
`Group ${groupChat.id._serialized}: Group participant already a group member`
'group_add_participant_error',
'Failed to add participants to the group',
{ groupId, participantsIds }
);
}

return wa_functions.addParticipants(groupChat, participants);
const data: {
[key: `${number}@c.us`]: {
code: number;
message: string;
invite_code: string | null;
invite_code_exp: number | null;
};
} = {};

for (const r of result.participants || []) {
const firstKey = Object.keys(r)[0] as `${number}@c.us`;

const d = r[firstKey];

if (d.code !== '403') {
try {
ContactStore.gadd(createWid(firstKey) as any, { silent: true });
} catch (error) {}
}

data[firstKey] = {
code: Number(d.code),
message: messageCodes[Number(d.code)] || "Can't Join.",
invite_code: d.invite_code,
invite_code_exp: Number(d.invite_code) || null,
};
}

return data;
}
2 changes: 1 addition & 1 deletion src/whatsapp/collections/BaseCollection.ts
Expand Up @@ -51,7 +51,7 @@ export declare class BaseCollection<C, A = C | C[]> extends Collection<C> {

handle(values: A): void;

gadd(id: C | Stringable): C;
gadd(id: C | Stringable, options?: any): C;

gaddUp(id: C | Stringable): C;

Expand Down
5 changes: 2 additions & 3 deletions src/whatsapp/collections/ContactCollection.ts
Expand Up @@ -16,14 +16,13 @@

import { exportModule } from '../exportModule';
import { ContactModel } from '../models';
import { Collection } from './Collection';
import { BaseCollection } from './BaseCollection';

/** @whatsapp 19380
* @whatsapp 719380 >= 2.2222.8
*/
export declare class ContactCollection extends Collection<ContactModel> {
export declare class ContactCollection extends BaseCollection<ContactModel> {
static model: ContactModel;
static cachePolicy?: any;
checksum?: any;
ensureSorted(): any;
contactAdded(e?: any): any;
Expand Down
1 change: 1 addition & 0 deletions src/whatsapp/functions/index.ts
Expand Up @@ -43,6 +43,7 @@ export * from './sendClear';
export * from './sendCreateGroup';
export * from './sendDelete';
export * from './sendExitGroup';
export * from './sendGroupParticipants';
export * from './sendJoinGroupViaInvite';
export * from './sendQueryExists';
export * from './sendQueryGroupInvite';
Expand Down
75 changes: 75 additions & 0 deletions src/whatsapp/functions/sendGroupParticipants.ts
@@ -0,0 +1,75 @@
/*!
* 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 437722 >= 2.2222.8
*/
export declare function sendAddParticipants(
group: Wid,
participants: Wid[]
): Promise<{
participants?: {
[key: `${number}@c.us`]: {
code: string;
invite_code: string | null;
invite_code_exp: string | null;
};
}[];
status: number;
[key: `${number}@c.us`]: number;
}>;

/**
* @whatsapp 437722 >= 2.2222.8
*/
export declare function sendRemoveParticipants(
group: Wid,
participants: Wid[]
): Promise<void>;

/**
* @whatsapp 437722 >= 2.2222.8
*/
export declare function sendPromoteParticipants(
group: Wid,
participants: Wid[]
): Promise<void>;

/**
* @whatsapp 437722 >= 2.2222.8
*/
export declare function sendDemoteParticipants(
group: Wid,
participants: Wid[]
): Promise<void>;

exportModule(
exports,
{
sendAddParticipants: 'sendAddParticipants',
sendRemoveParticipants: 'sendRemoveParticipants',
sendPromoteParticipants: 'sendPromoteParticipants',
sendDemoteParticipants: 'sendDemoteParticipants',
},
(m) =>
m.sendAddParticipants &&
m.sendRemoveParticipants &&
m.sendPromoteParticipants &&
m.sendDemoteParticipants
);

0 comments on commit 7f01c85

Please sign in to comment.