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 3 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
8 changes: 8 additions & 0 deletions src/Socket/messages-recv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,20 @@

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

Check warning on line 318 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 = [ 'created', child.attrs.request_method ]
break
case 'revoked_membership_requests':
msg.messageStubType = WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD
msg.messageStubParameters = [ 'revoked' ]
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, 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'

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

export interface GroupMetadata {
id: string
owner: string | undefined
Expand Down
12 changes: 11 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 = (action: RequestJoinAction, method: RequestJoinMethod) => {
ev.emit('group-join-request', { id: jid, author: message.participant!, action, method: method! })
}

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

switch (message.messageStubType) {
Expand Down Expand Up @@ -357,7 +361,13 @@
const approvalMode = message.messageStubParameters?.[0]
emitGroupUpdate({ joinApprovalMode: approvalMode === 'on' })
break
case WAMessageStubType.GROUP_MEMBERSHIP_JOIN_APPROVAL_REQUEST_NON_ADMIN_ADD:
let action = message.messageStubParameters?.[0] as RequestJoinAction

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

View workflow job for this annotation

GitHub Actions / check-lint

'action' is never reassigned. Use 'const' instead
let method = message.messageStubParameters?.[1] as RequestJoinMethod

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

View workflow job for this annotation

GitHub Actions / check-lint

'method' is never reassigned. Use 'const' instead
emitGroupRequestJoin(action, method)
break

Check failure on line 368 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