From 07562c220473237e9575b8cd5b0e7ea6b05b5b26 Mon Sep 17 00:00:00 2001 From: SamuelScheit Date: Sun, 14 May 2023 17:44:56 +0200 Subject: [PATCH] feat: fetch groups if ib:dirty --- src/Socket/chats.ts | 16 +++++---- src/Socket/groups.ts | 85 ++++++++++++++++++++++++++------------------ 2 files changed, 60 insertions(+), 41 deletions(-) diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index 35cb8e3..ee40c33 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -340,8 +340,8 @@ export const makeChatsSocket = (config: SocketConfig) => { } } - const updateAccountSyncTimestamp = async(fromTimestamp: number | string) => { - logger.info({ fromTimestamp }, 'requesting account sync') + const cleanDirtyBits = async(type: 'account_sync' | 'groups', fromTimestamp?: number | string) => { + logger.info({ fromTimestamp }, 'clean dirty bits ' + type) await sendNode({ tag: 'iq', attrs: { @@ -354,8 +354,8 @@ export const makeChatsSocket = (config: SocketConfig) => { { tag: 'clean', attrs: { - type: 'account_sync', - timestamp: fromTimestamp.toString(), + type, + ...(fromTimestamp ? { timestamp: fromTimestamp.toString() } : null), } } ] @@ -879,13 +879,16 @@ export const makeChatsSocket = (config: SocketConfig) => { if(attrs.timestamp) { let { lastAccountSyncTimestamp } = authState.creds if(lastAccountSyncTimestamp) { - await updateAccountSyncTimestamp(lastAccountSyncTimestamp) + await cleanDirtyBits('account_sync', lastAccountSyncTimestamp) } lastAccountSyncTimestamp = +attrs.timestamp ev.emit('creds.update', { lastAccountSyncTimestamp }) } + break + case 'groups': + // handled in groups.ts break default: logger.info({ node }, 'received unknown sync') @@ -945,6 +948,7 @@ export const makeChatsSocket = (config: SocketConfig) => { updateDefaultDisappearingMode, getBusinessProfile, resyncAppState, - chatModify + chatModify, + cleanDirtyBits } } diff --git a/src/Socket/groups.ts b/src/Socket/groups.ts index 6a0dcca..7eb5aa7 100644 --- a/src/Socket/groups.ts +++ b/src/Socket/groups.ts @@ -29,6 +29,55 @@ export const makeGroupsSocket = (config: SocketConfig) => { return extractGroupMetadata(result) } + + const groupFetchAllParticipating = async() => { + const result = await query({ + tag: 'iq', + attrs: { + to: '@g.us', + xmlns: 'w:g2', + type: 'get', + }, + content: [ + { + tag: 'participating', + attrs: { }, + content: [ + { tag: 'participants', attrs: { } }, + { tag: 'description', attrs: { } } + ] + } + ] + }) + const data: { [_: string]: GroupMetadata } = { } + const groupsChild = getBinaryNodeChild(result, 'groups') + if(groupsChild) { + const groups = getBinaryNodeChildren(groupsChild, 'group') + for(const groupNode of groups) { + const meta = extractGroupMetadata({ + tag: 'result', + attrs: { }, + content: [groupNode] + }) + data[meta.id] = meta + } + } + + sock.ev.emit('groups.update', Object.values(data)) + + return data + } + + sock.ws.on('CB:ib,,dirty', async(node: BinaryNode) => { + const { attrs } = getBinaryNodeChild(node, 'dirty')! + if(attrs.type !== 'groups') { + return + } + + await groupFetchAllParticipating() + await sock.cleanDirtyBits('groups') + }) + return { ...sock, groupMetadata, @@ -211,41 +260,7 @@ export const makeGroupsSocket = (config: SocketConfig) => { groupSettingUpdate: async(jid: string, setting: 'announcement' | 'not_announcement' | 'locked' | 'unlocked') => { await groupQuery(jid, 'set', [ { tag: setting, attrs: { } } ]) }, - groupFetchAllParticipating: async() => { - const result = await query({ - tag: 'iq', - attrs: { - to: '@g.us', - xmlns: 'w:g2', - type: 'get', - }, - content: [ - { - tag: 'participating', - attrs: { }, - content: [ - { tag: 'participants', attrs: { } }, - { tag: 'description', attrs: { } } - ] - } - ] - }) - const data: { [_: string]: GroupMetadata } = { } - const groupsChild = getBinaryNodeChild(result, 'groups') - if(groupsChild) { - const groups = getBinaryNodeChildren(groupsChild, 'group') - for(const groupNode of groups) { - const meta = extractGroupMetadata({ - tag: 'result', - attrs: { }, - content: [groupNode] - }) - data[meta.id] = meta - } - } - - return data - } + groupFetchAllParticipating } }