Skip to content

Commit

Permalink
feat: add event that handles join approval requests (#802)
Browse files Browse the repository at this point in the history
* initial commit

* lint

* add type in method

* add more actions / fixes participant jid / rename event

* fixes

* more fixes

* fix typing

* change 'reject' to 'rejected'

* chore:linting

---------

Co-authored-by: Rajeh Taher <[email protected]>
  • Loading branch information
vinikjkkj and PurpShell committed Jun 2, 2024
1 parent 9065ab6 commit ffec4af
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
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 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
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 @@ -321,6 +322,15 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
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
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 @@ const processMessage = async(
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 processMessage = async(
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
}

} 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

0 comments on commit ffec4af

Please sign in to comment.