mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
chore: format everything
This commit is contained in:
@@ -1,62 +1,70 @@
|
||||
import { proto } from '../../WAProto'
|
||||
import { GroupMetadata, GroupParticipant, ParticipantAction, SocketConfig, WAMessageKey, WAMessageStubType } from '../Types'
|
||||
import {
|
||||
GroupMetadata,
|
||||
GroupParticipant,
|
||||
ParticipantAction,
|
||||
SocketConfig,
|
||||
WAMessageKey,
|
||||
WAMessageStubType
|
||||
} from '../Types'
|
||||
import { generateMessageIDV2, unixTimestampSeconds } from '../Utils'
|
||||
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, jidEncode, jidNormalizedUser } from '../WABinary'
|
||||
import {
|
||||
BinaryNode,
|
||||
getBinaryNodeChild,
|
||||
getBinaryNodeChildren,
|
||||
getBinaryNodeChildString,
|
||||
jidEncode,
|
||||
jidNormalizedUser
|
||||
} from '../WABinary'
|
||||
import { makeChatsSocket } from './chats'
|
||||
|
||||
export const makeGroupsSocket = (config: SocketConfig) => {
|
||||
const sock = makeChatsSocket(config)
|
||||
const { authState, ev, query, upsertMessage } = sock
|
||||
|
||||
const groupQuery = async(jid: string, type: 'get' | 'set', content: BinaryNode[]) => (
|
||||
const groupQuery = async (jid: string, type: 'get' | 'set', content: BinaryNode[]) =>
|
||||
query({
|
||||
tag: 'iq',
|
||||
attrs: {
|
||||
type,
|
||||
xmlns: 'w:g2',
|
||||
to: jid,
|
||||
to: jid
|
||||
},
|
||||
content
|
||||
})
|
||||
)
|
||||
|
||||
const groupMetadata = async(jid: string) => {
|
||||
const result = await groupQuery(
|
||||
jid,
|
||||
'get',
|
||||
[ { tag: 'query', attrs: { request: 'interactive' } } ]
|
||||
)
|
||||
const groupMetadata = async (jid: string) => {
|
||||
const result = await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }])
|
||||
return extractGroupMetadata(result)
|
||||
}
|
||||
|
||||
|
||||
const groupFetchAllParticipating = async() => {
|
||||
const groupFetchAllParticipating = async () => {
|
||||
const result = await query({
|
||||
tag: 'iq',
|
||||
attrs: {
|
||||
to: '@g.us',
|
||||
xmlns: 'w:g2',
|
||||
type: 'get',
|
||||
type: 'get'
|
||||
},
|
||||
content: [
|
||||
{
|
||||
tag: 'participating',
|
||||
attrs: { },
|
||||
attrs: {},
|
||||
content: [
|
||||
{ tag: 'participants', attrs: { } },
|
||||
{ tag: 'description', attrs: { } }
|
||||
{ tag: 'participants', attrs: {} },
|
||||
{ tag: 'description', attrs: {} }
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
const data: { [_: string]: GroupMetadata } = { }
|
||||
const data: { [_: string]: GroupMetadata } = {}
|
||||
const groupsChild = getBinaryNodeChild(result, 'groups')
|
||||
if(groupsChild) {
|
||||
if (groupsChild) {
|
||||
const groups = getBinaryNodeChildren(groupsChild, 'group')
|
||||
for(const groupNode of groups) {
|
||||
for (const groupNode of groups) {
|
||||
const meta = extractGroupMetadata({
|
||||
tag: 'result',
|
||||
attrs: { },
|
||||
attrs: {},
|
||||
content: [groupNode]
|
||||
})
|
||||
data[meta.id] = meta
|
||||
@@ -68,9 +76,9 @@ export const makeGroupsSocket = (config: SocketConfig) => {
|
||||
return data
|
||||
}
|
||||
|
||||
sock.ws.on('CB:ib,,dirty', async(node: BinaryNode) => {
|
||||
sock.ws.on('CB:ib,,dirty', async (node: BinaryNode) => {
|
||||
const { attrs } = getBinaryNodeChild(node, 'dirty')!
|
||||
if(attrs.type !== 'groups') {
|
||||
if (attrs.type !== 'groups') {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -81,89 +89,69 @@ export const makeGroupsSocket = (config: SocketConfig) => {
|
||||
return {
|
||||
...sock,
|
||||
groupMetadata,
|
||||
groupCreate: async(subject: string, participants: string[]) => {
|
||||
groupCreate: async (subject: string, participants: string[]) => {
|
||||
const key = generateMessageIDV2()
|
||||
const result = await groupQuery(
|
||||
'@g.us',
|
||||
'set',
|
||||
[
|
||||
{
|
||||
tag: 'create',
|
||||
attrs: {
|
||||
subject,
|
||||
key
|
||||
},
|
||||
content: participants.map(jid => ({
|
||||
tag: 'participant',
|
||||
attrs: { jid }
|
||||
}))
|
||||
}
|
||||
]
|
||||
)
|
||||
const result = await groupQuery('@g.us', 'set', [
|
||||
{
|
||||
tag: 'create',
|
||||
attrs: {
|
||||
subject,
|
||||
key
|
||||
},
|
||||
content: participants.map(jid => ({
|
||||
tag: 'participant',
|
||||
attrs: { jid }
|
||||
}))
|
||||
}
|
||||
])
|
||||
return extractGroupMetadata(result)
|
||||
},
|
||||
groupLeave: async(id: string) => {
|
||||
await groupQuery(
|
||||
'@g.us',
|
||||
'set',
|
||||
[
|
||||
{
|
||||
tag: 'leave',
|
||||
attrs: { },
|
||||
content: [
|
||||
{ tag: 'group', attrs: { id } }
|
||||
]
|
||||
}
|
||||
]
|
||||
)
|
||||
groupLeave: async (id: string) => {
|
||||
await groupQuery('@g.us', 'set', [
|
||||
{
|
||||
tag: 'leave',
|
||||
attrs: {},
|
||||
content: [{ tag: 'group', attrs: { id } }]
|
||||
}
|
||||
])
|
||||
},
|
||||
groupUpdateSubject: async(jid: string, subject: string) => {
|
||||
await groupQuery(
|
||||
jid,
|
||||
'set',
|
||||
[
|
||||
{
|
||||
tag: 'subject',
|
||||
attrs: { },
|
||||
content: Buffer.from(subject, 'utf-8')
|
||||
}
|
||||
]
|
||||
)
|
||||
groupUpdateSubject: async (jid: string, subject: string) => {
|
||||
await groupQuery(jid, 'set', [
|
||||
{
|
||||
tag: 'subject',
|
||||
attrs: {},
|
||||
content: Buffer.from(subject, 'utf-8')
|
||||
}
|
||||
])
|
||||
},
|
||||
groupRequestParticipantsList: async(jid: string) => {
|
||||
const result = await groupQuery(
|
||||
jid,
|
||||
'get',
|
||||
[
|
||||
{
|
||||
tag: 'membership_approval_requests',
|
||||
attrs: {}
|
||||
}
|
||||
]
|
||||
)
|
||||
groupRequestParticipantsList: async (jid: string) => {
|
||||
const result = await groupQuery(jid, 'get', [
|
||||
{
|
||||
tag: 'membership_approval_requests',
|
||||
attrs: {}
|
||||
}
|
||||
])
|
||||
const node = getBinaryNodeChild(result, 'membership_approval_requests')
|
||||
const participants = getBinaryNodeChildren(node, 'membership_approval_request')
|
||||
return participants.map(v => v.attrs)
|
||||
},
|
||||
groupRequestParticipantsUpdate: async(jid: string, participants: string[], action: 'approve' | 'reject') => {
|
||||
const result = await groupQuery(
|
||||
jid,
|
||||
'set',
|
||||
[{
|
||||
groupRequestParticipantsUpdate: async (jid: string, participants: string[], action: 'approve' | 'reject') => {
|
||||
const result = await groupQuery(jid, 'set', [
|
||||
{
|
||||
tag: 'membership_requests_action',
|
||||
attrs: {},
|
||||
content: [
|
||||
content: [
|
||||
{
|
||||
tag: action,
|
||||
attrs: { },
|
||||
attrs: {},
|
||||
content: participants.map(jid => ({
|
||||
tag: 'participant',
|
||||
attrs: { jid }
|
||||
}))
|
||||
}
|
||||
]
|
||||
}]
|
||||
)
|
||||
}
|
||||
])
|
||||
const node = getBinaryNodeChild(result, 'membership_requests_action')
|
||||
const nodeAction = getBinaryNodeChild(node, action)
|
||||
const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant')
|
||||
@@ -171,63 +159,49 @@ export const makeGroupsSocket = (config: SocketConfig) => {
|
||||
return { status: p.attrs.error || '200', jid: p.attrs.jid }
|
||||
})
|
||||
},
|
||||
groupParticipantsUpdate: async(
|
||||
jid: string,
|
||||
participants: string[],
|
||||
action: ParticipantAction
|
||||
) => {
|
||||
const result = await groupQuery(
|
||||
jid,
|
||||
'set',
|
||||
[
|
||||
{
|
||||
tag: action,
|
||||
attrs: { },
|
||||
content: participants.map(jid => ({
|
||||
tag: 'participant',
|
||||
attrs: { jid }
|
||||
}))
|
||||
}
|
||||
]
|
||||
)
|
||||
groupParticipantsUpdate: async (jid: string, participants: string[], action: ParticipantAction) => {
|
||||
const result = await groupQuery(jid, 'set', [
|
||||
{
|
||||
tag: action,
|
||||
attrs: {},
|
||||
content: participants.map(jid => ({
|
||||
tag: 'participant',
|
||||
attrs: { jid }
|
||||
}))
|
||||
}
|
||||
])
|
||||
const node = getBinaryNodeChild(result, action)
|
||||
const participantsAffected = getBinaryNodeChildren(node, 'participant')
|
||||
return participantsAffected.map(p => {
|
||||
return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p }
|
||||
})
|
||||
},
|
||||
groupUpdateDescription: async(jid: string, description?: string) => {
|
||||
groupUpdateDescription: async (jid: string, description?: string) => {
|
||||
const metadata = await groupMetadata(jid)
|
||||
const prev = metadata.descId ?? null
|
||||
|
||||
await groupQuery(
|
||||
jid,
|
||||
'set',
|
||||
[
|
||||
{
|
||||
tag: 'description',
|
||||
attrs: {
|
||||
...(description ? { id: generateMessageIDV2() } : { delete: 'true' }),
|
||||
...(prev ? { prev } : {})
|
||||
},
|
||||
content: description ? [
|
||||
{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }
|
||||
] : undefined
|
||||
}
|
||||
]
|
||||
)
|
||||
await groupQuery(jid, 'set', [
|
||||
{
|
||||
tag: 'description',
|
||||
attrs: {
|
||||
...(description ? { id: generateMessageIDV2() } : { delete: 'true' }),
|
||||
...(prev ? { prev } : {})
|
||||
},
|
||||
content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
|
||||
}
|
||||
])
|
||||
},
|
||||
groupInviteCode: async(jid: string) => {
|
||||
groupInviteCode: async (jid: string) => {
|
||||
const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }])
|
||||
const inviteNode = getBinaryNodeChild(result, 'invite')
|
||||
return inviteNode?.attrs.code
|
||||
},
|
||||
groupRevokeInvite: async(jid: string) => {
|
||||
groupRevokeInvite: async (jid: string) => {
|
||||
const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }])
|
||||
const inviteNode = getBinaryNodeChild(result, 'invite')
|
||||
return inviteNode?.attrs.code
|
||||
},
|
||||
groupAcceptInvite: async(code: string) => {
|
||||
groupAcceptInvite: async (code: string) => {
|
||||
const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }])
|
||||
const result = getBinaryNodeChild(results, 'group')
|
||||
return result?.attrs.jid
|
||||
@@ -239,8 +213,10 @@ export const makeGroupsSocket = (config: SocketConfig) => {
|
||||
* @param invitedJid jid of person you invited
|
||||
* @returns true if successful
|
||||
*/
|
||||
groupRevokeInviteV4: async(groupJid: string, invitedJid: string) => {
|
||||
const result = await groupQuery(groupJid, 'set', [{ tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }])
|
||||
groupRevokeInviteV4: async (groupJid: string, invitedJid: string) => {
|
||||
const result = await groupQuery(groupJid, 'set', [
|
||||
{ tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }
|
||||
])
|
||||
return !!result
|
||||
},
|
||||
|
||||
@@ -249,87 +225,90 @@ export const makeGroupsSocket = (config: SocketConfig) => {
|
||||
* @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
|
||||
* @param inviteMessage the message to accept
|
||||
*/
|
||||
groupAcceptInviteV4: ev.createBufferedFunction(async(key: string | WAMessageKey, inviteMessage: proto.Message.IGroupInviteMessage) => {
|
||||
key = typeof key === 'string' ? { remoteJid: key } : key
|
||||
const results = await groupQuery(inviteMessage.groupJid!, 'set', [{
|
||||
tag: 'accept',
|
||||
attrs: {
|
||||
code: inviteMessage.inviteCode!,
|
||||
expiration: inviteMessage.inviteExpiration!.toString(),
|
||||
admin: key.remoteJid!
|
||||
}
|
||||
}])
|
||||
|
||||
// if we have the full message key
|
||||
// update the invite message to be expired
|
||||
if(key.id) {
|
||||
// create new invite message that is expired
|
||||
inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage)
|
||||
inviteMessage.inviteExpiration = 0
|
||||
inviteMessage.inviteCode = ''
|
||||
ev.emit('messages.update', [
|
||||
groupAcceptInviteV4: ev.createBufferedFunction(
|
||||
async (key: string | WAMessageKey, inviteMessage: proto.Message.IGroupInviteMessage) => {
|
||||
key = typeof key === 'string' ? { remoteJid: key } : key
|
||||
const results = await groupQuery(inviteMessage.groupJid!, 'set', [
|
||||
{
|
||||
key,
|
||||
update: {
|
||||
message: {
|
||||
groupInviteMessage: inviteMessage
|
||||
}
|
||||
tag: 'accept',
|
||||
attrs: {
|
||||
code: inviteMessage.inviteCode!,
|
||||
expiration: inviteMessage.inviteExpiration!.toString(),
|
||||
admin: key.remoteJid!
|
||||
}
|
||||
}
|
||||
])
|
||||
}
|
||||
|
||||
// generate the group add message
|
||||
await upsertMessage(
|
||||
{
|
||||
key: {
|
||||
remoteJid: inviteMessage.groupJid,
|
||||
id: generateMessageIDV2(sock.user?.id),
|
||||
fromMe: false,
|
||||
// if we have the full message key
|
||||
// update the invite message to be expired
|
||||
if (key.id) {
|
||||
// create new invite message that is expired
|
||||
inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage)
|
||||
inviteMessage.inviteExpiration = 0
|
||||
inviteMessage.inviteCode = ''
|
||||
ev.emit('messages.update', [
|
||||
{
|
||||
key,
|
||||
update: {
|
||||
message: {
|
||||
groupInviteMessage: inviteMessage
|
||||
}
|
||||
}
|
||||
}
|
||||
])
|
||||
}
|
||||
|
||||
// generate the group add message
|
||||
await upsertMessage(
|
||||
{
|
||||
key: {
|
||||
remoteJid: inviteMessage.groupJid,
|
||||
id: generateMessageIDV2(sock.user?.id),
|
||||
fromMe: false,
|
||||
participant: key.remoteJid
|
||||
},
|
||||
messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
|
||||
messageStubParameters: [authState.creds.me!.id],
|
||||
participant: key.remoteJid,
|
||||
messageTimestamp: unixTimestampSeconds()
|
||||
},
|
||||
messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
|
||||
messageStubParameters: [
|
||||
authState.creds.me!.id
|
||||
],
|
||||
participant: key.remoteJid,
|
||||
messageTimestamp: unixTimestampSeconds()
|
||||
},
|
||||
'notify'
|
||||
)
|
||||
'notify'
|
||||
)
|
||||
|
||||
return results.attrs.from
|
||||
}),
|
||||
groupGetInviteInfo: async(code: string) => {
|
||||
return results.attrs.from
|
||||
}
|
||||
),
|
||||
groupGetInviteInfo: async (code: string) => {
|
||||
const results = await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }])
|
||||
return extractGroupMetadata(results)
|
||||
},
|
||||
groupToggleEphemeral: async(jid: string, ephemeralExpiration: number) => {
|
||||
const content: BinaryNode = ephemeralExpiration ?
|
||||
{ tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } } :
|
||||
{ tag: 'not_ephemeral', attrs: { } }
|
||||
groupToggleEphemeral: async (jid: string, ephemeralExpiration: number) => {
|
||||
const content: BinaryNode = ephemeralExpiration
|
||||
? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
|
||||
: { tag: 'not_ephemeral', attrs: {} }
|
||||
await groupQuery(jid, 'set', [content])
|
||||
},
|
||||
groupSettingUpdate: async(jid: string, setting: 'announcement' | 'not_announcement' | 'locked' | 'unlocked') => {
|
||||
await groupQuery(jid, 'set', [ { tag: setting, attrs: { } } ])
|
||||
groupSettingUpdate: async (jid: string, setting: 'announcement' | 'not_announcement' | 'locked' | 'unlocked') => {
|
||||
await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }])
|
||||
},
|
||||
groupMemberAddMode: async(jid: string, mode: 'admin_add' | 'all_member_add') => {
|
||||
await groupQuery(jid, 'set', [ { tag: 'member_add_mode', attrs: { }, content: mode } ])
|
||||
groupMemberAddMode: async (jid: string, mode: 'admin_add' | 'all_member_add') => {
|
||||
await groupQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }])
|
||||
},
|
||||
groupJoinApprovalMode: async(jid: string, mode: 'on' | 'off') => {
|
||||
await groupQuery(jid, 'set', [ { tag: 'membership_approval_mode', attrs: { }, content: [ { tag: 'group_join', attrs: { state: mode } } ] } ])
|
||||
groupJoinApprovalMode: async (jid: string, mode: 'on' | 'off') => {
|
||||
await groupQuery(jid, 'set', [
|
||||
{ tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }
|
||||
])
|
||||
},
|
||||
groupFetchAllParticipating
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const extractGroupMetadata = (result: BinaryNode) => {
|
||||
const group = getBinaryNodeChild(result, 'group')!
|
||||
const descChild = getBinaryNodeChild(group, 'description')
|
||||
let desc: string | undefined
|
||||
let descId: string | undefined
|
||||
if(descChild) {
|
||||
if (descChild) {
|
||||
desc = getBinaryNodeChildString(descChild, 'body')
|
||||
descId = descChild.attrs.id
|
||||
}
|
||||
@@ -355,14 +334,12 @@ export const extractGroupMetadata = (result: BinaryNode) => {
|
||||
isCommunityAnnounce: !!getBinaryNodeChild(group, 'default_sub_group'),
|
||||
joinApprovalMode: !!getBinaryNodeChild(group, 'membership_approval_mode'),
|
||||
memberAddMode,
|
||||
participants: getBinaryNodeChildren(group, 'participant').map(
|
||||
({ attrs }) => {
|
||||
return {
|
||||
id: attrs.jid,
|
||||
admin: (attrs.type || null) as GroupParticipant['admin'],
|
||||
}
|
||||
participants: getBinaryNodeChildren(group, 'participant').map(({ attrs }) => {
|
||||
return {
|
||||
id: attrs.jid,
|
||||
admin: (attrs.type || null) as GroupParticipant['admin']
|
||||
}
|
||||
),
|
||||
}),
|
||||
ephemeralDuration: eph ? +eph : undefined
|
||||
}
|
||||
return metadata
|
||||
|
||||
Reference in New Issue
Block a user