Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add event that handles join approval requests #802

Merged
merged 10 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Socket/messages-recv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@
child: BinaryNode,
msg: Partial<proto.IWebMessageInfo>
) => {
const participantJid = getBinaryNodeChild(child, 'participant')?.attrs?.jid || participant
switch (child?.tag) {
case 'create':
const metadata = extractGroupMetadata(child)
Expand Down Expand Up @@ -315,12 +316,21 @@

break
case 'membership_approval_mode':
const approvalMode: any = getBinaryNodeChild(child, 'group_join')

Check warning on line 319 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Unexpected any. Specify a different type
if(approvalMode) {
msg.messageStubType = WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_MODE
msg.messageStubParameters = [ approvalMode.attrs.state ]
}

break
case 'created_membership_requests':
msg.messageStubType = WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD
msg.messageStubParameters = [ participantJid, 'created', child.attrs.request_method ]
break
case 'revoked_membership_requests':
const isDenied = areJidsSameUser(participantJid, participant)
msg.messageStubType = WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD
msg.messageStubParameters = [ participantJid, isDenied ? 'revoked' : 'rejected' ]
break
}
}
Expand Down Expand Up @@ -574,7 +584,7 @@
participant: attrs.participant
}

if(shouldIgnoreJid(remoteJid)) {

Check failure on line 587 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

Expected '!==' and instead saw '!='
logger.debug({ remoteJid }, 'ignoring receipt from jid')
await sendMessageAck(node)
return
Expand Down Expand Up @@ -683,7 +693,7 @@
}

const handleMessage = async(node: BinaryNode) => {
const { fullMessage: msg, category, author, decrypt } = decryptMessageNode(

Check warning on line 696 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

This assertion is unnecessary since it does not change the type of the expression

Check warning on line 696 in src/Socket/messages-recv.ts

View workflow job for this annotation

GitHub Actions / check-lint

This assertion is unnecessary since it does not change the type of the expression
node,
authState.creds.me!.id,
authState.creds.me!.lid || '',
Expand Down
3 changes: 2 additions & 1 deletion src/Types/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AuthenticationCreds } from './Auth'
import { WACallEvent } from './Call'
import { Chat, ChatUpdate, PresenceData } from './Chat'
import { Contact } from './Contact'
import { GroupMetadata, ParticipantAction } from './GroupMetadata'
import { GroupMetadata, ParticipantAction, RequestJoinAction, RequestJoinMethod } from './GroupMetadata'
import { Label } from './Label'
import { LabelAssociation } from './LabelAssociation'
import { MessageUpsertType, MessageUserReceiptUpdate, WAMessage, WAMessageKey, WAMessageUpdate } from './Message'
Expand Down Expand Up @@ -52,6 +52,7 @@ export type BaileysEventMap = {
'groups.update': Partial<GroupMetadata>[]
/** apply an action to participants in a group */
'group-participants.update': { id: string, author: string, participants: string[], action: ParticipantAction }
'group.join-request': { id: string, author: string, participant: string, action: RequestJoinAction, method: RequestJoinMethod }

'blocklist.set': { blocklist: string[] }
'blocklist.update': { blocklist: string[], type: 'add' | 'remove' }
Expand Down
4 changes: 4 additions & 0 deletions src/Types/GroupMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export type GroupParticipant = (Contact & { isAdmin?: boolean, isSuperAdmin?: bo

export type ParticipantAction = 'add' | 'remove' | 'promote' | 'demote'

export type RequestJoinAction = 'created' | 'revoked' | 'rejected'

export type RequestJoinMethod = 'invite_link' | 'linked_group_join' | 'non_admin_add' | undefined

export interface GroupMetadata {
id: string
owner: string | undefined
Expand Down
13 changes: 12 additions & 1 deletion src/Utils/process-message.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AxiosRequestConfig } from 'axios'
import type { Logger } from 'pino'
import { proto } from '../../WAProto'
import { AuthenticationCreds, BaileysEventEmitter, Chat, GroupMetadata, ParticipantAction, SignalKeyStoreWithTransaction, SocketConfig, WAMessageStubType } from '../Types'
import { AuthenticationCreds, BaileysEventEmitter, Chat, GroupMetadata, ParticipantAction, RequestJoinAction, RequestJoinMethod, SignalKeyStoreWithTransaction, SocketConfig, WAMessageStubType } from '../Types'
import { getContentType, normalizeMessageContent } from '../Utils/messages'
import { areJidsSameUser, isJidBroadcast, isJidStatusBroadcast, jidNormalizedUser } from '../WABinary'
import { aesDecryptGCM, hmacSign } from './crypto'
Expand Down Expand Up @@ -301,6 +301,10 @@
ev.emit('groups.update', [{ id: jid, ...update, author: message.participant ?? undefined }])
}

const emitGroupRequestJoin = (participant: string, action: RequestJoinAction, method: RequestJoinMethod) => {
ev.emit('group.join-request', { id: jid, author: message.participant!, participant, action, method: method! })
}

const participantsIncludesMe = () => participants.find(jid => areJidsSameUser(meId, jid))

switch (message.messageStubType) {
Expand Down Expand Up @@ -357,7 +361,14 @@
const approvalMode = message.messageStubParameters?.[0]
emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' })
break
case WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD:
const participant = message.messageStubParameters?.[0] as string
const action = message.messageStubParameters?.[1] as RequestJoinAction
const method = message.messageStubParameters?.[2] as RequestJoinMethod
emitGroupRequestJoin(participant, action, method)
break

Check failure on line 369 in src/Utils/process-message.ts

View workflow job for this annotation

GitHub Actions / check-lint

Expected indentation of 3 tabs but found 2
}

} else if(content?.pollUpdateMessage) {
const creationMsgKey = content.pollUpdateMessage.pollCreationMessageKey!
// we need to fetch the poll creation message to get the poll enc key
Expand Down
Loading