@zervonex/baileys 0.0.0-stage → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3206 -3
- package/WAProto/WAProtoCompile.js +291 -0
- package/WAProto/fetcher.js +69 -0
- package/WAProto/index.js +88 -0
- package/WAProto/parser.js +313 -0
- package/engine-requirements.js +10 -0
- package/lib/Builders/AIRich.js +643 -0
- package/lib/Builders/base.js +130 -0
- package/lib/Builders/builders.js +449 -0
- package/lib/Builders/index.js +3 -0
- package/lib/Defaults/index.js +225 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Signal/index.js +2 -0
- package/lib/Signal/libsignal.js +523 -0
- package/lib/Signal/lid-mapping.js +283 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +84 -0
- package/lib/Socket/business.js +152 -0
- package/lib/Socket/chats.js +855 -0
- package/lib/Socket/communities.js +147 -0
- package/lib/Socket/groups.js +294 -0
- package/lib/Socket/index.js +15 -0
- package/lib/Socket/messages-recv.js +981 -0
- package/lib/Socket/messages-send.js +708 -0
- package/lib/Socket/mex.js +34 -0
- package/lib/Socket/newsletter.js +781 -0
- package/lib/Socket/registration.js +155 -0
- package/lib/Socket/socket.js +797 -0
- package/lib/Socket/spam-report.js +65 -0
- package/lib/Socket/username.js +82 -0
- package/lib/Socket/zervonex-handler.js +55 -0
- package/lib/Store/index.js +6 -0
- package/lib/Store/make-cache-manager-store.js +81 -0
- package/lib/Store/make-in-memory-store.js +425 -0
- package/lib/Store/make-ordered-dictionary.js +82 -0
- package/lib/Store/object-repository.js +31 -0
- package/lib/Store/reporting-info-store.js +38 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Bussines.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +8 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/Message.js +15 -0
- package/lib/Types/Newsletter.js +100 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +14 -0
- package/lib/Types/State.js +26 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +30 -0
- package/lib/Utils/auth-utils.js +142 -0
- package/lib/Utils/baileys-event-stream.js +33 -0
- package/lib/Utils/bot-capabilities.js +40 -0
- package/lib/Utils/bot-planning-replay.js +44 -0
- package/lib/Utils/browser-utils.js +26 -0
- package/lib/Utils/business.js +75 -0
- package/lib/Utils/chat-utils.js +317 -0
- package/lib/Utils/companion-reg-client-utils.js +20 -0
- package/lib/Utils/consumer-application.js +38 -0
- package/lib/Utils/crypto.js +52 -0
- package/lib/Utils/decode-wa-message.js +197 -0
- package/lib/Utils/event-buffer.js +273 -0
- package/lib/Utils/extras.js +186 -0
- package/lib/Utils/generics.js +195 -0
- package/lib/Utils/history.js +51 -0
- package/lib/Utils/identity-change-handler.js +19 -0
- package/lib/Utils/index.js +38 -0
- package/lib/Utils/link-preview.js +51 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.js +3 -0
- package/lib/Utils/make-mutex.js +16 -0
- package/lib/Utils/message-composer.js +59 -0
- package/lib/Utils/message-retry-manager.js +48 -0
- package/lib/Utils/messages-media.js +520 -0
- package/lib/Utils/messages.js +752 -0
- package/lib/Utils/meta-ai-msmsg.js +122 -0
- package/lib/Utils/meta-compositing.js +75 -0
- package/lib/Utils/modern-messages.js +235 -0
- package/lib/Utils/native-flow.js +55 -0
- package/lib/Utils/noise-handler.js +161 -0
- package/lib/Utils/pre-key-manager.js +76 -0
- package/lib/Utils/process-message.js +399 -0
- package/lib/Utils/reporting-utils.js +171 -0
- package/lib/Utils/signal.js +167 -0
- package/lib/Utils/spam-report-utils.js +135 -0
- package/lib/Utils/sync-action-utils.js +33 -0
- package/lib/Utils/tc-token-utils.js +154 -0
- package/lib/Utils/use-multi-file-auth-state.js +307 -0
- package/lib/Utils/validate-connection.js +181 -0
- package/lib/WABinary/constants.js +1321 -0
- package/lib/WABinary/decode.js +152 -0
- package/lib/WABinary/encode.js +140 -0
- package/lib/WABinary/generic-utils.js +179 -0
- package/lib/WABinary/index.js +6 -0
- package/lib/WABinary/jid-utils.js +235 -0
- package/lib/WABinary/types.js +4 -0
- package/lib/WAM/BinaryInfo.js +10 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/encode.js +150 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +48 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +23 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +15 -0
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +24 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +9 -0
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +21 -0
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +19 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +19 -0
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +22 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +16 -0
- package/lib/WAUSync/Protocols/index.js +12 -0
- package/lib/WAUSync/USyncQuery.js +100 -0
- package/lib/WAUSync/USyncUser.js +20 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/index.js +67 -0
- package/package.json +220 -5
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
import WAProto from '../../WAProto/index.js';
|
|
2
|
+
import * as Defaults from '../Defaults/index.js';
|
|
3
|
+
import { LabelAssociationType } from '../Types/LabelAssociation.js';
|
|
4
|
+
import * as Utils_1 from '../Utils/index.js';
|
|
5
|
+
import * as WABinary_1 from '../WABinary/index.js';
|
|
6
|
+
import { makeOrderedDictionary } from './make-ordered-dictionary.js';
|
|
7
|
+
import { ObjectRepository } from './object-repository.js';
|
|
8
|
+
|
|
9
|
+
const waChatKey = (pin) => ({
|
|
10
|
+
key: (c) => (pin ? (c.pinned ? '1' : '0') : '') + (c.archived ? '0' : '1') + (c.conversationTimestamp ? c.conversationTimestamp.toString(16).padStart(8, '0') : '') + c.id,
|
|
11
|
+
compare: (k1, k2) => k2.localeCompare(k1)
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const waMessageID = (m) => m.key.id || '';
|
|
15
|
+
|
|
16
|
+
const waLabelAssociationKey = {
|
|
17
|
+
key: (la) => (la.type === LabelAssociationType.Chat ? la.chatId + la.labelId : la.chatId + la.messageId + la.labelId),
|
|
18
|
+
compare: (k1, k2) => k2.localeCompare(k1)
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const makeMessagesDictionary = () => makeOrderedDictionary(waMessageID);
|
|
22
|
+
|
|
23
|
+
const makeInMemoryStore = (config) => {
|
|
24
|
+
const socket = config.socket
|
|
25
|
+
const chatKey = config.chatKey || waChatKey(true)
|
|
26
|
+
const labelAssociationKey = config.labelAssociationKey || waLabelAssociationKey
|
|
27
|
+
const logger = config.logger || Defaults.DEFAULT_CONNECTION_CONFIG.logger.child({ stream: 'in-mem-store' })
|
|
28
|
+
const KeyedDB = require('@adiwajshing/keyed-db').default
|
|
29
|
+
const chats = new KeyedDB(chatKey, c => c.id)
|
|
30
|
+
const messages = {}
|
|
31
|
+
const contacts = {}
|
|
32
|
+
const groupMetadata = {}
|
|
33
|
+
const presences = {}
|
|
34
|
+
const state = { connection: 'close' }
|
|
35
|
+
const labels = new ObjectRepository()
|
|
36
|
+
const labelAssociations = new KeyedDB(labelAssociationKey, labelAssociationKey.key)
|
|
37
|
+
const assertMessageList = (jid) => {
|
|
38
|
+
if (!messages[jid]) {
|
|
39
|
+
messages[jid] = makeMessagesDictionary()
|
|
40
|
+
}
|
|
41
|
+
return messages[jid]
|
|
42
|
+
}
|
|
43
|
+
const contactsUpsert = (newContacts) => {
|
|
44
|
+
const oldContacts = new Set(Object.keys(contacts))
|
|
45
|
+
for (const contact of newContacts) {
|
|
46
|
+
oldContacts.delete(contact.id)
|
|
47
|
+
contacts[contact.id] = Object.assign(contacts[contact.id] || {}, contact)
|
|
48
|
+
}
|
|
49
|
+
return oldContacts
|
|
50
|
+
}
|
|
51
|
+
const labelsUpsert = (newLabels) => {
|
|
52
|
+
for (const label of newLabels) {
|
|
53
|
+
labels.upsertById(label.id, label)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* binds to a BaileysEventEmitter.
|
|
58
|
+
* It listens to all events and constructs a state that you can query accurate data from.
|
|
59
|
+
* Eg. can use the store to fetch chats, contacts, messages etc.
|
|
60
|
+
* @param ev typically the event emitter from the socket connection
|
|
61
|
+
*/
|
|
62
|
+
const bind = (ev) => {
|
|
63
|
+
ev.on('connection.update', update => {
|
|
64
|
+
Object.assign(state, update)
|
|
65
|
+
})
|
|
66
|
+
ev.on('messaging-history.set', ({ chats: newChats, contacts: newContacts, messages: newMessages, isLatest, syncType }) => {
|
|
67
|
+
if (syncType === WAProto.proto.HistorySync.HistorySyncType.ON_DEMAND) {
|
|
68
|
+
return // FOR NOW,
|
|
69
|
+
//TODO: HANDLE
|
|
70
|
+
}
|
|
71
|
+
if (isLatest) {
|
|
72
|
+
chats.clear()
|
|
73
|
+
for (const id in messages) {
|
|
74
|
+
delete messages[id]
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const chatsAdded = chats.insertIfAbsent(...newChats).length
|
|
78
|
+
logger.debug({ chatsAdded }, 'synced chats')
|
|
79
|
+
const oldContacts = contactsUpsert(newContacts)
|
|
80
|
+
if (isLatest) {
|
|
81
|
+
for (const jid of oldContacts) {
|
|
82
|
+
delete contacts[jid]
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
logger.debug({ deletedContacts: isLatest ? oldContacts.size : 0, newContacts }, 'synced contacts')
|
|
86
|
+
for (const msg of newMessages) {
|
|
87
|
+
const jid = msg.key.remoteJid
|
|
88
|
+
const list = assertMessageList(jid)
|
|
89
|
+
list.upsert(msg, 'prepend')
|
|
90
|
+
}
|
|
91
|
+
logger.debug({ messages: newMessages.length }, 'synced messages')
|
|
92
|
+
})
|
|
93
|
+
ev.on('contacts.upsert', contacts => {
|
|
94
|
+
contactsUpsert(contacts)
|
|
95
|
+
})
|
|
96
|
+
ev.on('contacts.update', async (updates) => {
|
|
97
|
+
for (const update of updates) {
|
|
98
|
+
let contact
|
|
99
|
+
if (contacts[update.id]) {
|
|
100
|
+
contact = contacts[update.id]
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
const contactHashes = await Promise.all(Object.keys(contacts).map(async (contactId) => {
|
|
104
|
+
const { user } = WABinary_1.jidDecode(contactId)
|
|
105
|
+
return [contactId, (Utils_1.md5(Buffer.from(user + 'WA_ADD_NOTIF', 'utf8'))).toString('base64').slice(0, 3)]
|
|
106
|
+
}))
|
|
107
|
+
contact = contacts[contactHashes.find(([, b]) => b === update.id?.[0]) || ''] // find contact by attrs.hash, when user is not saved as a contact
|
|
108
|
+
}
|
|
109
|
+
if (!contact) {
|
|
110
|
+
// Create new contact entry if the update has meaningful data
|
|
111
|
+
if (update.notify || update.verifiedName || update.name) {
|
|
112
|
+
contacts[update.id] = { id: update.id }
|
|
113
|
+
contact = contacts[update.id]
|
|
114
|
+
} else {
|
|
115
|
+
logger.debug({ update }, 'got update for non-existant contact')
|
|
116
|
+
continue
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (update.imgUrl === 'changed') {
|
|
120
|
+
contact.imgUrl = socket ? await socket.profilePictureUrl(contact.id) : undefined
|
|
121
|
+
}
|
|
122
|
+
else if (update.imgUrl === 'removed') {
|
|
123
|
+
delete contact.imgUrl
|
|
124
|
+
}
|
|
125
|
+
// Persist pushName, verifiedName, and name from the update
|
|
126
|
+
if (update.notify) contact.notify = update.notify
|
|
127
|
+
if (update.verifiedName) contact.verifiedName = update.verifiedName
|
|
128
|
+
if (update.name) contact.name = update.name
|
|
129
|
+
Object.assign(contacts[contact.id], contact)
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
ev.on('chats.upsert', newChats => {
|
|
133
|
+
chats.upsert(...newChats)
|
|
134
|
+
})
|
|
135
|
+
ev.on('chats.update', updates => {
|
|
136
|
+
for (let update of updates) {
|
|
137
|
+
const result = chats.update(update.id, chat => {
|
|
138
|
+
if (update.unreadCount > 0) {
|
|
139
|
+
update = { ...update }
|
|
140
|
+
update.unreadCount = (chat.unreadCount || 0) + update.unreadCount
|
|
141
|
+
}
|
|
142
|
+
Object.assign(chat, update)
|
|
143
|
+
})
|
|
144
|
+
if (!result) {
|
|
145
|
+
logger.debug({ update }, 'got update for non-existant chat')
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
ev.on('labels.edit', (label) => {
|
|
150
|
+
if (label.deleted) {
|
|
151
|
+
return labels.deleteById(label.id)
|
|
152
|
+
}
|
|
153
|
+
// WhatsApp can store only up to 20 labels
|
|
154
|
+
if (labels.count() < 20) {
|
|
155
|
+
return labels.upsertById(label.id, label)
|
|
156
|
+
}
|
|
157
|
+
logger.error('Labels count exceed')
|
|
158
|
+
})
|
|
159
|
+
ev.on('labels.association', ({ type, association }) => {
|
|
160
|
+
switch (type) {
|
|
161
|
+
case 'add':
|
|
162
|
+
labelAssociations.upsert(association)
|
|
163
|
+
break
|
|
164
|
+
case 'remove':
|
|
165
|
+
labelAssociations.delete(association)
|
|
166
|
+
break
|
|
167
|
+
default:
|
|
168
|
+
console.error(`unknown operation type [${type}]`)
|
|
169
|
+
}
|
|
170
|
+
})
|
|
171
|
+
ev.on('presence.update', ({ id, presences: update }) => {
|
|
172
|
+
presences[id] = presences[id] || {}
|
|
173
|
+
Object.assign(presences[id], update)
|
|
174
|
+
})
|
|
175
|
+
ev.on('chats.delete', deletions => {
|
|
176
|
+
for (const item of deletions) {
|
|
177
|
+
if (chats.get(item)) {
|
|
178
|
+
chats.deleteById(item)
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
})
|
|
182
|
+
ev.on('messages.upsert', ({ messages: newMessages, type }) => {
|
|
183
|
+
switch (type) {
|
|
184
|
+
case 'append':
|
|
185
|
+
case 'notify':
|
|
186
|
+
for (const msg of newMessages) {
|
|
187
|
+
const jid = WABinary_1.jidNormalizedUser(msg.key.remoteJid)
|
|
188
|
+
const list = assertMessageList(jid)
|
|
189
|
+
list.upsert(msg, 'append')
|
|
190
|
+
if (type === 'notify' && !chats.get(jid)) {
|
|
191
|
+
ev.emit('chats.upsert', [
|
|
192
|
+
{
|
|
193
|
+
id: jid,
|
|
194
|
+
conversationTimestamp: Utils_1.toNumber(msg.messageTimestamp),
|
|
195
|
+
unreadCount: 1
|
|
196
|
+
}
|
|
197
|
+
])
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
break
|
|
201
|
+
}
|
|
202
|
+
})
|
|
203
|
+
ev.on('messages.update', updates => {
|
|
204
|
+
for (const { update, key } of updates) {
|
|
205
|
+
const list = assertMessageList(WABinary_1.jidNormalizedUser(key.remoteJid))
|
|
206
|
+
if (update?.status) {
|
|
207
|
+
const listStatus = list.get(key.id)?.status
|
|
208
|
+
if (listStatus && update?.status <= listStatus) {
|
|
209
|
+
logger.debug({ update, storedStatus: listStatus }, 'status stored newer then update')
|
|
210
|
+
delete update.status
|
|
211
|
+
logger.debug({ update }, 'new update object')
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const result = list.updateAssign(key.id, update)
|
|
215
|
+
if (!result) {
|
|
216
|
+
logger.debug({ update }, 'got update for non-existent message')
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
})
|
|
220
|
+
ev.on('messages.delete', item => {
|
|
221
|
+
if ('all' in item) {
|
|
222
|
+
const list = messages[item.jid]
|
|
223
|
+
list?.clear()
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
const jid = item.keys[0].remoteJid
|
|
227
|
+
const list = messages[jid]
|
|
228
|
+
if (list) {
|
|
229
|
+
const idSet = new Set(item.keys.map(k => k.id))
|
|
230
|
+
list.filter(m => !idSet.has(m.key.id))
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
})
|
|
234
|
+
ev.on('groups.update', updates => {
|
|
235
|
+
for (const update of updates) {
|
|
236
|
+
const id = update.id
|
|
237
|
+
if (groupMetadata[id]) {
|
|
238
|
+
Object.assign(groupMetadata[id], update)
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
logger.debug({ update }, 'got update for non-existant group metadata')
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
})
|
|
245
|
+
ev.on('group-participants.update', ({ id, participants, action }) => {
|
|
246
|
+
const metadata = groupMetadata[id]
|
|
247
|
+
if (metadata) {
|
|
248
|
+
switch (action) {
|
|
249
|
+
case 'add':
|
|
250
|
+
metadata.participants.push(...participants.map(id => ({ id, isAdmin: false, isSuperAdmin: false })))
|
|
251
|
+
break
|
|
252
|
+
case 'demote':
|
|
253
|
+
case 'promote':
|
|
254
|
+
for (const participant of metadata.participants) {
|
|
255
|
+
if (participants.includes(participant.id)) {
|
|
256
|
+
participant.isAdmin = action === 'promote'
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
break
|
|
260
|
+
case 'remove':
|
|
261
|
+
metadata.participants = metadata.participants.filter(p => !participants.includes(p.id))
|
|
262
|
+
break
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
})
|
|
266
|
+
ev.on('message-receipt.update', updates => {
|
|
267
|
+
for (const { key, receipt } of updates) {
|
|
268
|
+
const obj = messages[key.remoteJid]
|
|
269
|
+
const msg = obj?.get(key.id)
|
|
270
|
+
if (msg) {
|
|
271
|
+
Utils_1.updateMessageWithReceipt(msg, receipt)
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
})
|
|
275
|
+
ev.on('messages.reaction', (reactions) => {
|
|
276
|
+
for (const { key, reaction } of reactions) {
|
|
277
|
+
const obj = messages[key.remoteJid]
|
|
278
|
+
const msg = obj?.get(key.id)
|
|
279
|
+
if (msg) {
|
|
280
|
+
Utils_1.updateMessageWithReaction(msg, reaction)
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
})
|
|
284
|
+
}
|
|
285
|
+
const toJSON = () => ({
|
|
286
|
+
chats,
|
|
287
|
+
contacts,
|
|
288
|
+
messages,
|
|
289
|
+
labels,
|
|
290
|
+
labelAssociations
|
|
291
|
+
})
|
|
292
|
+
const fromJSON = (json) => {
|
|
293
|
+
chats.upsert(...json.chats)
|
|
294
|
+
labelAssociations.upsert(...json.labelAssociations || [])
|
|
295
|
+
contactsUpsert(Object.values(json.contacts))
|
|
296
|
+
labelsUpsert(Object.values(json.labels || {}))
|
|
297
|
+
for (const jid in json.messages) {
|
|
298
|
+
const list = assertMessageList(jid)
|
|
299
|
+
for (const msg of json.messages[jid]) {
|
|
300
|
+
list.upsert(WAProto.proto.WebMessageInfo.fromObject(msg), 'append')
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return {
|
|
305
|
+
chats,
|
|
306
|
+
contacts,
|
|
307
|
+
messages,
|
|
308
|
+
groupMetadata,
|
|
309
|
+
state,
|
|
310
|
+
presences,
|
|
311
|
+
labels,
|
|
312
|
+
labelAssociations,
|
|
313
|
+
bind,
|
|
314
|
+
/** loads messages from the store, if not found -- uses the legacy connection */
|
|
315
|
+
loadMessages: async (jid, count, cursor) => {
|
|
316
|
+
const list = assertMessageList(jid)
|
|
317
|
+
const mode = !cursor || 'before' in cursor ? 'before' : 'after'
|
|
318
|
+
const cursorKey = !!cursor ? ('before' in cursor ? cursor.before : cursor.after) : undefined
|
|
319
|
+
const cursorValue = cursorKey ? list.get(cursorKey.id) : undefined
|
|
320
|
+
let messages
|
|
321
|
+
if (list && mode === 'before' && (!cursorKey || cursorValue)) {
|
|
322
|
+
if (cursorValue) {
|
|
323
|
+
const msgIdx = list.array.findIndex(m => m.key.id === cursorKey?.id)
|
|
324
|
+
messages = list.array.slice(0, msgIdx)
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
messages = list.array
|
|
328
|
+
}
|
|
329
|
+
const diff = count - messages.length
|
|
330
|
+
if (diff < 0) {
|
|
331
|
+
messages = messages.slice(-count) // get the last X messages
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
messages = []
|
|
336
|
+
}
|
|
337
|
+
return messages
|
|
338
|
+
},
|
|
339
|
+
/**
|
|
340
|
+
* Get all available labels for profile
|
|
341
|
+
*
|
|
342
|
+
* Keep in mind that the list is formed from predefined tags and tags
|
|
343
|
+
* that were "caught" during their editing.
|
|
344
|
+
*/
|
|
345
|
+
getLabels: () => {
|
|
346
|
+
return labels
|
|
347
|
+
},
|
|
348
|
+
/**
|
|
349
|
+
* Get labels for chat
|
|
350
|
+
*
|
|
351
|
+
* @returns Label IDs
|
|
352
|
+
**/
|
|
353
|
+
getChatLabels: (chatId) => {
|
|
354
|
+
return labelAssociations.filter((la) => la.chatId === chatId).all()
|
|
355
|
+
},
|
|
356
|
+
/**
|
|
357
|
+
* Get labels for message
|
|
358
|
+
*
|
|
359
|
+
* @returns Label IDs
|
|
360
|
+
**/
|
|
361
|
+
getMessageLabels: (messageId) => {
|
|
362
|
+
const associations = labelAssociations
|
|
363
|
+
.filter((la) => la.messageId === messageId)
|
|
364
|
+
.all()
|
|
365
|
+
return associations.map(({ labelId }) => labelId)
|
|
366
|
+
},
|
|
367
|
+
loadMessage: async (jid, id) => messages[jid]?.get(id),
|
|
368
|
+
mostRecentMessage: async (jid) => {
|
|
369
|
+
const message = messages[jid]?.array.slice(-1)[0]
|
|
370
|
+
return message
|
|
371
|
+
},
|
|
372
|
+
fetchImageUrl: async (jid, sock) => {
|
|
373
|
+
const contact = contacts[jid]
|
|
374
|
+
if (!contact) {
|
|
375
|
+
return sock?.profilePictureUrl(jid)
|
|
376
|
+
}
|
|
377
|
+
if (typeof contact.imgUrl === 'undefined') {
|
|
378
|
+
contact.imgUrl = await sock?.profilePictureUrl(jid)
|
|
379
|
+
}
|
|
380
|
+
return contact.imgUrl
|
|
381
|
+
},
|
|
382
|
+
fetchGroupMetadata: async (jid, sock) => {
|
|
383
|
+
if (!groupMetadata[jid]) {
|
|
384
|
+
const metadata = await sock?.groupMetadata(jid)
|
|
385
|
+
if (metadata) {
|
|
386
|
+
groupMetadata[jid] = metadata
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return groupMetadata[jid]
|
|
390
|
+
},
|
|
391
|
+
// fetchBroadcastListInfo: async(jid: string, sock: WASocket | undefined) => {
|
|
392
|
+
// if(!groupMetadata[jid]) {
|
|
393
|
+
// const metadata = await sock?.getBroadcastListInfo(jid)
|
|
394
|
+
// if(metadata) {
|
|
395
|
+
// groupMetadata[jid] = metadata
|
|
396
|
+
// }
|
|
397
|
+
// }
|
|
398
|
+
// return groupMetadata[jid]
|
|
399
|
+
// },
|
|
400
|
+
fetchMessageReceipts: async ({ remoteJid, id }) => {
|
|
401
|
+
const list = messages[remoteJid]
|
|
402
|
+
const msg = list?.get(id)
|
|
403
|
+
return msg?.userReceipt
|
|
404
|
+
},
|
|
405
|
+
toJSON,
|
|
406
|
+
fromJSON,
|
|
407
|
+
writeToFile: (path) => {
|
|
408
|
+
// require fs here so that in case "fs" is not available -- the app does not crash
|
|
409
|
+
const { writeFileSync } = require('fs')
|
|
410
|
+
writeFileSync(path, JSON.stringify(toJSON()))
|
|
411
|
+
},
|
|
412
|
+
readFromFile: (path) => {
|
|
413
|
+
// require fs here so that in case "fs" is not available -- the app does not crash
|
|
414
|
+
const { readFileSync, existsSync } = require('fs')
|
|
415
|
+
if (existsSync(path)) {
|
|
416
|
+
logger.debug({ path }, 'reading from file')
|
|
417
|
+
const jsonStr = readFileSync(path, { encoding: 'utf-8' })
|
|
418
|
+
const json = JSON.parse(jsonStr)
|
|
419
|
+
fromJSON(json)
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
export { waChatKey, waMessageID, waLabelAssociationKey, makeInMemoryStore };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
function makeOrderedDictionary(idGetter) {
|
|
2
|
+
const array = [];
|
|
3
|
+
const dict = {};
|
|
4
|
+
|
|
5
|
+
const get = (id) => dict[id];
|
|
6
|
+
|
|
7
|
+
const update = (item) => {
|
|
8
|
+
const id = idGetter(item);
|
|
9
|
+
const idx = array.findIndex((i) => idGetter(i) === id);
|
|
10
|
+
if (idx >= 0) {
|
|
11
|
+
array[idx] = item;
|
|
12
|
+
dict[id] = item;
|
|
13
|
+
}
|
|
14
|
+
return false;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const upsert = (item, mode) => {
|
|
18
|
+
const id = idGetter(item);
|
|
19
|
+
if (get(id)) {
|
|
20
|
+
update(item);
|
|
21
|
+
} else {
|
|
22
|
+
if (mode === 'append') {
|
|
23
|
+
array.push(item);
|
|
24
|
+
} else {
|
|
25
|
+
array.splice(0, 0, item);
|
|
26
|
+
}
|
|
27
|
+
dict[id] = item;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const remove = (item) => {
|
|
32
|
+
const id = idGetter(item);
|
|
33
|
+
const idx = array.findIndex((i) => idGetter(i) === id);
|
|
34
|
+
if (idx >= 0) {
|
|
35
|
+
array.splice(idx, 1);
|
|
36
|
+
delete dict[id];
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
array,
|
|
44
|
+
get,
|
|
45
|
+
upsert,
|
|
46
|
+
update,
|
|
47
|
+
remove,
|
|
48
|
+
updateAssign: (id, update) => {
|
|
49
|
+
const item = get(id);
|
|
50
|
+
if (item) {
|
|
51
|
+
Object.assign(item, update);
|
|
52
|
+
delete dict[id];
|
|
53
|
+
dict[idGetter(item)] = item;
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
},
|
|
58
|
+
clear: () => {
|
|
59
|
+
array.splice(0, array.length);
|
|
60
|
+
for (const key of Object.keys(dict)) {
|
|
61
|
+
delete dict[key];
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
filter: (contain) => {
|
|
65
|
+
let i = 0;
|
|
66
|
+
while (i < array.length) {
|
|
67
|
+
if (!contain(array[i])) {
|
|
68
|
+
delete dict[idGetter(array[i])];
|
|
69
|
+
array.splice(i, 1);
|
|
70
|
+
} else {
|
|
71
|
+
i += 1;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
toJSON: () => array,
|
|
76
|
+
fromJSON: (newItems) => {
|
|
77
|
+
array.splice(0, array.length, ...newItems);
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { makeOrderedDictionary };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
class ObjectRepository {
|
|
2
|
+
constructor(entities = {}) {
|
|
3
|
+
this.entityMap = new Map(Object.entries(entities));
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
findById(id) {
|
|
7
|
+
return this.entityMap.get(id);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
findAll() {
|
|
11
|
+
return Array.from(this.entityMap.values());
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
upsertById(id, entity) {
|
|
15
|
+
return this.entityMap.set(id, { ...entity });
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
deleteById(id) {
|
|
19
|
+
return this.entityMap.delete(id);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
count() {
|
|
23
|
+
return this.entityMap.size;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
toJSON() {
|
|
27
|
+
return this.findAll();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { ObjectRepository };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const createReportingInfoStore = () => {
|
|
2
|
+
const byJid = new Map()
|
|
3
|
+
|
|
4
|
+
const normalizeKey = jid => (jid || '').toLowerCase()
|
|
5
|
+
|
|
6
|
+
return {
|
|
7
|
+
add(jid, entry) {
|
|
8
|
+
const key = normalizeKey(jid)
|
|
9
|
+
if (!key || !entry?.stanzaId || !entry?.reportingTag?.length) return
|
|
10
|
+
const list = byJid.get(key) || []
|
|
11
|
+
const idx = list.findIndex(e => e.stanzaId === entry.stanzaId)
|
|
12
|
+
if (idx >= 0) list[idx] = entry
|
|
13
|
+
else list.push(entry)
|
|
14
|
+
list.sort((a, b) => b.timestamp - a.timestamp)
|
|
15
|
+
byJid.set(key, list)
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
getForJid(jid, max = 5) {
|
|
19
|
+
return (byJid.get(normalizeKey(jid)) || []).slice(0, max)
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
clear(jid) {
|
|
23
|
+
byJid.delete(normalizeKey(jid))
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
listJids() {
|
|
27
|
+
return [...byJid.keys()]
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
size() {
|
|
31
|
+
let n = 0
|
|
32
|
+
for (const list of byJid.values()) n += list.length
|
|
33
|
+
return n
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { createReportingInfoStore }
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { proto } from '../../WAProto/index.js'
|
|
2
|
+
|
|
3
|
+
// Derived from proto.CollectionName — sorted by enum value, lowercased, UNKNOWN (0) excluded.
|
|
4
|
+
// Automatically picks up new collection names when the proto updates.
|
|
5
|
+
export const ALL_WA_PATCH_NAMES = Object.entries(proto.CollectionName ?? {})
|
|
6
|
+
.filter(([k, v]) => typeof v === 'number' && v !== 0)
|
|
7
|
+
.sort((a, b) => a[1] - b[1])
|
|
8
|
+
.map(([k]) => k.toLowerCase())
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** WhatsApp has 20 predefined colors */
|
|
2
|
+
export var LabelColor;
|
|
3
|
+
(function (LabelColor) {
|
|
4
|
+
LabelColor[LabelColor["Color1"] = 0] = "Color1";
|
|
5
|
+
LabelColor[LabelColor["Color2"] = 1] = "Color2";
|
|
6
|
+
LabelColor[LabelColor["Color3"] = 2] = "Color3";
|
|
7
|
+
LabelColor[LabelColor["Color4"] = 3] = "Color4";
|
|
8
|
+
LabelColor[LabelColor["Color5"] = 4] = "Color5";
|
|
9
|
+
LabelColor[LabelColor["Color6"] = 5] = "Color6";
|
|
10
|
+
LabelColor[LabelColor["Color7"] = 6] = "Color7";
|
|
11
|
+
LabelColor[LabelColor["Color8"] = 7] = "Color8";
|
|
12
|
+
LabelColor[LabelColor["Color9"] = 8] = "Color9";
|
|
13
|
+
LabelColor[LabelColor["Color10"] = 9] = "Color10";
|
|
14
|
+
LabelColor[LabelColor["Color11"] = 10] = "Color11";
|
|
15
|
+
LabelColor[LabelColor["Color12"] = 11] = "Color12";
|
|
16
|
+
LabelColor[LabelColor["Color13"] = 12] = "Color13";
|
|
17
|
+
LabelColor[LabelColor["Color14"] = 13] = "Color14";
|
|
18
|
+
LabelColor[LabelColor["Color15"] = 14] = "Color15";
|
|
19
|
+
LabelColor[LabelColor["Color16"] = 15] = "Color16";
|
|
20
|
+
LabelColor[LabelColor["Color17"] = 16] = "Color17";
|
|
21
|
+
LabelColor[LabelColor["Color18"] = 17] = "Color18";
|
|
22
|
+
LabelColor[LabelColor["Color19"] = 18] = "Color19";
|
|
23
|
+
LabelColor[LabelColor["Color20"] = 19] = "Color20";
|
|
24
|
+
})(LabelColor || (LabelColor = {}));
|
|
25
|
+
//# sourceMappingURL=Label.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Association type */
|
|
2
|
+
export var LabelAssociationType;
|
|
3
|
+
(function (LabelAssociationType) {
|
|
4
|
+
LabelAssociationType["Chat"] = "label_jid";
|
|
5
|
+
LabelAssociationType["Message"] = "label_message";
|
|
6
|
+
})(LabelAssociationType || (LabelAssociationType = {}));
|
|
7
|
+
//# sourceMappingURL=LabelAssociation.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { proto } from '../../WAProto/index.js'
|
|
2
|
+
|
|
3
|
+
export { proto as WAProto }
|
|
4
|
+
|
|
5
|
+
// Proto is the authoritative source. These Proxies let callers use
|
|
6
|
+
// WAMessageStubType.GROUP_CREATE or WAMessageStatus.READ exactly as before.
|
|
7
|
+
export const WAMessageStubType = new Proxy({}, { get: (_, k) => proto.WebMessageInfo?.StubType?.[k] })
|
|
8
|
+
export const WAMessageStatus = new Proxy({}, { get: (_, k) => proto.WebMessageInfo?.Status?.[k] })
|
|
9
|
+
|
|
10
|
+
export const WAMessageAddressingMode = Object.freeze({ PN: 'pn', LID: 'lid' })
|
|
11
|
+
|
|
12
|
+
// Resolve a numeric stub type to its string name.
|
|
13
|
+
// Proto is the true source — only falls back to UNKNOWN_STUB_N for values not yet in the compiled proto.
|
|
14
|
+
export const resolveStubType = (stubType) =>
|
|
15
|
+
proto.WebMessageInfo?.StubType?.[stubType] ?? `UNKNOWN_STUB_${stubType}`
|