feat: fetch groups if ib:dirty

This commit is contained in:
SamuelScheit
2023-05-14 17:44:56 +02:00
parent 68a6fe40c8
commit 07562c2204
2 changed files with 60 additions and 41 deletions

View File

@@ -340,8 +340,8 @@ export const makeChatsSocket = (config: SocketConfig) => {
} }
} }
const updateAccountSyncTimestamp = async(fromTimestamp: number | string) => { const cleanDirtyBits = async(type: 'account_sync' | 'groups', fromTimestamp?: number | string) => {
logger.info({ fromTimestamp }, 'requesting account sync') logger.info({ fromTimestamp }, 'clean dirty bits ' + type)
await sendNode({ await sendNode({
tag: 'iq', tag: 'iq',
attrs: { attrs: {
@@ -354,8 +354,8 @@ export const makeChatsSocket = (config: SocketConfig) => {
{ {
tag: 'clean', tag: 'clean',
attrs: { attrs: {
type: 'account_sync', type,
timestamp: fromTimestamp.toString(), ...(fromTimestamp ? { timestamp: fromTimestamp.toString() } : null),
} }
} }
] ]
@@ -879,13 +879,16 @@ export const makeChatsSocket = (config: SocketConfig) => {
if(attrs.timestamp) { if(attrs.timestamp) {
let { lastAccountSyncTimestamp } = authState.creds let { lastAccountSyncTimestamp } = authState.creds
if(lastAccountSyncTimestamp) { if(lastAccountSyncTimestamp) {
await updateAccountSyncTimestamp(lastAccountSyncTimestamp) await cleanDirtyBits('account_sync', lastAccountSyncTimestamp)
} }
lastAccountSyncTimestamp = +attrs.timestamp lastAccountSyncTimestamp = +attrs.timestamp
ev.emit('creds.update', { lastAccountSyncTimestamp }) ev.emit('creds.update', { lastAccountSyncTimestamp })
} }
break
case 'groups':
// handled in groups.ts
break break
default: default:
logger.info({ node }, 'received unknown sync') logger.info({ node }, 'received unknown sync')
@@ -945,6 +948,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
updateDefaultDisappearingMode, updateDefaultDisappearingMode,
getBusinessProfile, getBusinessProfile,
resyncAppState, resyncAppState,
chatModify chatModify,
cleanDirtyBits
} }
} }

View File

@@ -29,6 +29,55 @@ export const makeGroupsSocket = (config: SocketConfig) => {
return extractGroupMetadata(result) 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 { return {
...sock, ...sock,
groupMetadata, groupMetadata,
@@ -211,41 +260,7 @@ export const makeGroupsSocket = (config: SocketConfig) => {
groupSettingUpdate: async(jid: string, setting: 'announcement' | 'not_announcement' | 'locked' | 'unlocked') => { groupSettingUpdate: async(jid: string, setting: 'announcement' | 'not_announcement' | 'locked' | 'unlocked') => {
await groupQuery(jid, 'set', [ { tag: setting, attrs: { } } ]) await groupQuery(jid, 'set', [ { tag: setting, attrs: { } } ])
}, },
groupFetchAllParticipating: async() => { groupFetchAllParticipating
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
}
} }
} }