forked from wppconnect-team/wa-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added group management functions
- Loading branch information
1 parent
bac71d0
commit decae64
Showing
11 changed files
with
466 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,259 @@ | ||
/*! | ||
* 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 Debug from 'debug'; | ||
import Emittery from 'emittery'; | ||
|
||
import { assertGetChat, assertWid } from '../assert'; | ||
import { WPPError } from '../util'; | ||
import * as webpack from '../webpack'; | ||
import { ParticipantModel, Wid } from '../whatsapp'; | ||
import { | ||
addParticipants, | ||
demoteParticipants, | ||
promoteParticipants, | ||
removeParticipants, | ||
} from '../whatsapp/functions'; | ||
import { ChatEventTypes as GroupEventTypes } from './types'; | ||
|
||
const debug = Debug('WPP:group'); | ||
|
||
export class Group extends Emittery<GroupEventTypes> { | ||
constructor() { | ||
super(); | ||
webpack.onInjected(() => this.initialize()); | ||
} | ||
|
||
async initialize() { | ||
debug('initialized'); | ||
} | ||
|
||
private ensureGroup(groupId: string | Wid) { | ||
const groupChat = assertGetChat(groupId); | ||
|
||
if (!groupChat.isGroup) { | ||
throw new WPPError( | ||
'not_a_group', | ||
`Chat ${groupChat.id._serialized} is not a group` | ||
); | ||
} | ||
|
||
return groupChat; | ||
} | ||
|
||
iAmAdmin(groupId: string | Wid) { | ||
const groupChat = this.ensureGroup(groupId); | ||
return groupChat.groupMetadata!.participants.iAmAdmin(); | ||
} | ||
|
||
iAmMember(groupId: string | Wid) { | ||
const groupChat = this.ensureGroup(groupId); | ||
return groupChat.groupMetadata!.participants.iAmMember(); | ||
} | ||
|
||
iAmRestrictedMember(groupId: string | Wid) { | ||
const groupChat = this.ensureGroup(groupId); | ||
return groupChat.groupMetadata!.participants.iAmRestrictedMember(); | ||
} | ||
|
||
iAmSuperAdmin(groupId: string | Wid) { | ||
const groupChat = this.ensureGroup(groupId); | ||
return groupChat.groupMetadata!.participants.iAmMember(); | ||
} | ||
|
||
private ensureGroupAndParticipants( | ||
groupId: string | Wid, | ||
participantsIds: (string | Wid) | (string | Wid)[], | ||
createIfNotExists = false | ||
) { | ||
const groupChat = this.ensureGroup(groupId); | ||
|
||
if (!groupChat.groupMetadata!.participants.iAmAdmin()) { | ||
throw new WPPError( | ||
'group_you_are_not_admin', | ||
`You are not admin in ${groupChat.id._serialized}` | ||
); | ||
} | ||
|
||
if (!Array.isArray(participantsIds)) { | ||
participantsIds = [participantsIds]; | ||
} | ||
|
||
const wids = participantsIds.map(assertWid); | ||
|
||
const participants = wids.map<ParticipantModel>((wid) => { | ||
let participant = groupChat.groupMetadata?.participants.get(wid); | ||
|
||
if (!participant && createIfNotExists) { | ||
participant = new ParticipantModel({ | ||
id: wid, | ||
}); | ||
} | ||
|
||
if (!participant) { | ||
throw new WPPError( | ||
'group_participant_not_found', | ||
`Chat ${groupChat.id._serialized} is not a group` | ||
); | ||
} | ||
|
||
return participant; | ||
}); | ||
|
||
return { | ||
groupChat, | ||
participants, | ||
}; | ||
} | ||
|
||
canAdd(groupId: string | Wid) { | ||
const groupChat = this.ensureGroup(groupId); | ||
return groupChat.groupMetadata!.participants.canAdd(); | ||
} | ||
|
||
canDemote( | ||
groupId: string | Wid, | ||
participantsIds: (string | Wid) | (string | Wid)[] | ||
) { | ||
const { groupChat, participants } = this.ensureGroupAndParticipants( | ||
groupId, | ||
participantsIds | ||
); | ||
|
||
return participants.every((p) => | ||
groupChat.groupMetadata!.participants.canDemote(p) | ||
); | ||
} | ||
|
||
canPromote( | ||
groupId: string | Wid, | ||
participantsIds: (string | Wid) | (string | Wid)[] | ||
) { | ||
const { groupChat, participants } = this.ensureGroupAndParticipants( | ||
groupId, | ||
participantsIds | ||
); | ||
|
||
return participants.every((p) => | ||
groupChat.groupMetadata!.participants.canPromote(p) | ||
); | ||
} | ||
|
||
canRemove( | ||
groupId: string | Wid, | ||
participantsIds: (string | Wid) | (string | Wid)[] | ||
) { | ||
const { groupChat, participants } = this.ensureGroupAndParticipants( | ||
groupId, | ||
participantsIds | ||
); | ||
|
||
return participants.every((p) => | ||
groupChat.groupMetadata!.participants.canRemove(p) | ||
); | ||
} | ||
|
||
async addParticipants( | ||
groupId: string | Wid, | ||
participantsIds: (string | Wid) | (string | Wid)[] | ||
): Promise<void> { | ||
const { groupChat, participants } = this.ensureGroupAndParticipants( | ||
groupId, | ||
participantsIds, | ||
true | ||
); | ||
|
||
if ( | ||
participants.some((p) => groupChat.groupMetadata?.participants.get(p.id)) | ||
) { | ||
throw new WPPError( | ||
'group_participant_already_a_group_member', | ||
`Group ${groupChat.id._serialized}: Group participant already a group member` | ||
); | ||
} | ||
|
||
return addParticipants(groupChat, participants); | ||
} | ||
|
||
async removeParticipants( | ||
groupId: string | Wid, | ||
participantsIds: (string | Wid) | (string | Wid)[] | ||
): Promise<void> { | ||
const { groupChat, participants } = this.ensureGroupAndParticipants( | ||
groupId, | ||
participantsIds | ||
); | ||
|
||
if ( | ||
participants.some( | ||
(p) => !groupChat.groupMetadata?.participants.canRemove(p) | ||
) | ||
) { | ||
throw new WPPError( | ||
'group_participant_is_not_a_group_member', | ||
`Group ${groupChat.id._serialized}: Group participant is not a group member` | ||
); | ||
} | ||
|
||
return removeParticipants(groupChat, participants); | ||
} | ||
|
||
async promoteParticipants( | ||
groupId: string | Wid, | ||
participantsIds: (string | Wid) | (string | Wid)[] | ||
): Promise<void> { | ||
const { groupChat, participants } = this.ensureGroupAndParticipants( | ||
groupId, | ||
participantsIds | ||
); | ||
|
||
if ( | ||
participants.some( | ||
(p) => !groupChat.groupMetadata?.participants.canPromote(p) | ||
) | ||
) { | ||
throw new WPPError( | ||
'group_participant_is_already_a_group_admin', | ||
`Group ${groupChat.id._serialized}: Group participant is already a group admin` | ||
); | ||
} | ||
|
||
return promoteParticipants(groupChat, participants); | ||
} | ||
|
||
async demoteParticipants( | ||
groupId: string | Wid, | ||
participantsIds: (string | Wid) | (string | Wid)[] | ||
): Promise<void> { | ||
const { groupChat, participants } = this.ensureGroupAndParticipants( | ||
groupId, | ||
participantsIds | ||
); | ||
|
||
if ( | ||
participants.some( | ||
(p) => !groupChat.groupMetadata?.participants.canDemote(p) | ||
) | ||
) { | ||
throw new WPPError( | ||
'group_participant_is_already_not_a_group_admin', | ||
`Group ${groupChat.id._serialized}: Group participant is already not a group admin` | ||
); | ||
} | ||
|
||
return demoteParticipants(groupChat, participants); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/*! | ||
* 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 { Group } from './Group'; | ||
|
||
export * from './Group'; | ||
export * from './types'; | ||
|
||
export default new Group(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/*! | ||
* 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 { ModelPropertiesContructor, MsgModel } from '../whatsapp'; | ||
|
||
export interface ChatEventTypes { | ||
change: string; | ||
idle: undefined; | ||
} | ||
|
||
export interface GetMessagesOptions { | ||
count?: number; | ||
direction?: 'after' | 'before'; | ||
id?: string; | ||
} | ||
|
||
export interface SendMessageOptions { | ||
waitForAck?: boolean; | ||
createChat?: boolean; | ||
} | ||
|
||
export interface MessageButtonsOptions { | ||
buttons?: Array<{ | ||
id: string; | ||
text: string; | ||
}>; | ||
title?: string; | ||
footer?: string; | ||
} | ||
|
||
export interface ListMessageOptions extends SendMessageOptions { | ||
buttonText: string; | ||
description: string; | ||
sections: Array<{ | ||
title: string; | ||
rows: Array<{ | ||
rowId: string; | ||
title: string; | ||
description: string; | ||
}>; | ||
}>; | ||
} | ||
|
||
export type TextMessageOptions = SendMessageOptions & MessageButtonsOptions; | ||
|
||
export type AllMessageOptions = SendMessageOptions & | ||
MessageButtonsOptions & | ||
Partial<ListMessageOptions>; | ||
|
||
export type RawMessage = ModelPropertiesContructor<MsgModel>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.