forked from WhiskeySockets/Baileys
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathUSyncQuery.ts
More file actions
146 lines (122 loc) · 3.6 KB
/
USyncQuery.ts
File metadata and controls
146 lines (122 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import type { USyncQueryProtocol } from '../Types/USync'
import { type BinaryNode, getBinaryNodeChild } from '../WABinary'
import { USyncBotProfileProtocol } from './Protocols/UsyncBotProfileProtocol'
import { USyncLIDProtocol } from './Protocols/UsyncLIDProtocol'
import {
USyncContactProtocol,
USyncDeviceProtocol,
USyncDisappearingModeProtocol,
USyncStatusProtocol,
USyncUsernameProtocol
} from './Protocols'
import { USyncUser } from './USyncUser'
export type USyncQueryResultList = { [protocol: string]: unknown; id: string; rawId?: number }
export type USyncQueryResult = {
list: USyncQueryResultList[]
sideList: USyncQueryResultList[]
}
export class USyncQuery {
protocols: USyncQueryProtocol[]
users: USyncUser[]
context: string
mode: string
constructor() {
this.protocols = []
this.users = []
this.context = 'interactive'
this.mode = 'query'
}
withMode(mode: string) {
this.mode = mode
return this
}
withContext(context: string) {
this.context = context
return this
}
withUser(user: USyncUser) {
this.users.push(user)
return this
}
parseUSyncQueryResult(result: BinaryNode | undefined): USyncQueryResult | undefined {
if (result?.attrs.type !== 'result') {
return
}
const protocolMap = Object.fromEntries(
this.protocols.map(protocol => {
return [protocol.name, protocol.parser]
})
)
const queryResult: USyncQueryResult = {
// TODO: implement errors etc.
list: [],
sideList: []
}
const usyncNode = getBinaryNodeChild(result, 'usync')
//TODO: implement error backoff, refresh etc.
//TODO: see if there are any errors in the result node
//const resultNode = getBinaryNodeChild(usyncNode, 'result')
const parseNodeList = (content: BinaryNode[]): USyncQueryResultList[] =>
content.reduce((acc: USyncQueryResultList[], node) => {
const id = node?.attrs.jid
if (id) {
const data = Array.isArray(node?.content)
? Object.fromEntries(
node.content
.map(content => {
const protocol = content.tag
const parser = protocolMap[protocol]
if (parser) {
return [protocol, parser(content)]
} else {
return [protocol, null]
}
})
.filter(([, b]) => b !== null) as [string, unknown][]
)
: {}
const rawIdAttr = node?.attrs?.['raw_id']
const rawId = rawIdAttr !== undefined ? Number(rawIdAttr) : undefined
acc.push({ ...data, id, ...(rawId !== undefined && !isNaN(rawId) ? { rawId } : {}) })
}
return acc
}, [])
const listNode = usyncNode ? getBinaryNodeChild(usyncNode, 'list') : undefined
if (listNode?.content && Array.isArray(listNode.content)) {
queryResult.list = parseNodeList(listNode.content)
}
const sideListNode = usyncNode ? getBinaryNodeChild(usyncNode, 'side_list') : undefined
if (sideListNode?.content && Array.isArray(sideListNode.content)) {
queryResult.sideList = parseNodeList(sideListNode.content)
}
return queryResult
}
withDeviceProtocol() {
this.protocols.push(new USyncDeviceProtocol())
return this
}
withContactProtocol() {
this.protocols.push(new USyncContactProtocol())
return this
}
withStatusProtocol() {
this.protocols.push(new USyncStatusProtocol())
return this
}
withDisappearingModeProtocol() {
this.protocols.push(new USyncDisappearingModeProtocol())
return this
}
withBotProfileProtocol() {
this.protocols.push(new USyncBotProfileProtocol())
return this
}
withLIDProtocol() {
this.protocols.push(new USyncLIDProtocol())
return this
}
withUsernameProtocol() {
this.protocols.push(new USyncUsernameProtocol())
return this
}
}