Skip to content

Commit

Permalink
fix: Fixed WPP.group.addParticipants function for WhatsApp >= 2.2320.4 (
Browse files Browse the repository at this point in the history
fix #1114)
  • Loading branch information
edgardmessias committed May 23, 2023
1 parent 92176a0 commit fb7f4c6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -53,6 +53,7 @@
"@typescript-eslint/parser": "^5.59.7",
"@wppconnect/wa-version": "^1.2.41",
"buffer": "^6.0.3",
"compare-versions": "^6.0.0-rc.1",
"compressorjs": "^1.2.1",
"conventional-changelog-cli": "^2.2.2",
"debug": "^4.3.4",
Expand Down
42 changes: 36 additions & 6 deletions src/group/functions/addParticipants.ts
Expand Up @@ -14,14 +14,24 @@
* limitations under the License.
*/

import { compare } from 'compare-versions';

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

declare global {
interface Window {
Debug: {
VERSION: string;
};
}
}

const messageCodes: {
[key: number]: string;
} = {
200: 'OK',
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.",
};
Expand Down Expand Up @@ -71,10 +81,30 @@ export async function addParticipants(
true
);

const result = await wa_functions.sendAddParticipants(
groupChat.id,
participants.map((p) => p.id)
);
let members: any[] = [];

if (compare(self.Debug.VERSION, '2.2320.0', '>=')) {
if (groupChat.groupMetadata?.isLidAddressingMode) {
members = participants.map((p: ParticipantModel) => ({
phoneNumber: p.id,
lid: functions.getCurrentLid(p.id),
}));
} else {
members = participants.map((p: ParticipantModel) => ({
phoneNumber: p.id,
}));
}
} else {
if (groupChat.groupMetadata?.isLidAddressingMode) {
members = participants.map((p: ParticipantModel) =>
functions.getCurrentLid(p.id)
);
} else {
members = participants.map((p: ParticipantModel) => p.id);
}
}

const result = await functions.sendAddParticipants(groupChat.id, members);

if (result.status >= 400) {
throw new WPPError(
Expand Down
2 changes: 1 addition & 1 deletion src/whatsapp/functions/sendGroupParticipants.ts
Expand Up @@ -22,7 +22,7 @@ import { exportModule } from '../exportModule';
*/
export declare function sendAddParticipants(
group: Wid,
participants: Wid[]
participants: (Wid | { phoneNumber: Wid; lid?: Wid })[]
): Promise<{
participants?: (
| {
Expand Down
1 change: 1 addition & 0 deletions src/whatsapp/models/GroupMetadataModel.ts
Expand Up @@ -39,6 +39,7 @@ interface Props {
size?: any;
support?: any;
uniqueShortNameMap?: any;
isLidAddressingMode: boolean;
}

interface Session {
Expand Down

1 comment on commit fb7f4c6

@edgardmessias
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on #1119

Please sign in to comment.