@sixcore/baileys 1.0.0
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 +606 -0
- package/WAProto/GenerateStatics.sh +4 -0
- package/WAProto/WAProto.proto +4357 -0
- package/WAProto/index.d.ts +50383 -0
- package/WAProto/index.js +155693 -0
- package/WASignalGroup/GroupProtocol.js +1697 -0
- package/WASignalGroup/ciphertext_message.js +16 -0
- package/WASignalGroup/generate-proto.sh +1 -0
- package/WASignalGroup/group.proto +42 -0
- package/WASignalGroup/group_cipher.js +120 -0
- package/WASignalGroup/group_session_builder.js +46 -0
- package/WASignalGroup/index.js +5 -0
- package/WASignalGroup/keyhelper.js +21 -0
- package/WASignalGroup/protobufs.js +3 -0
- package/WASignalGroup/queue_job.js +69 -0
- package/WASignalGroup/sender_chain_key.js +50 -0
- package/WASignalGroup/sender_key_distribution_message.js +78 -0
- package/WASignalGroup/sender_key_message.js +92 -0
- package/WASignalGroup/sender_key_name.js +70 -0
- package/WASignalGroup/sender_key_record.js +56 -0
- package/WASignalGroup/sender_key_state.js +129 -0
- package/WASignalGroup/sender_message_key.js +39 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/index.d.ts +53 -0
- package/lib/Defaults/index.js +108 -0
- package/lib/Signal/libsignal.d.ts +3 -0
- package/lib/Signal/libsignal.js +152 -0
- package/lib/Socket/Client/abstract-socket-client.d.ts +17 -0
- package/lib/Socket/Client/abstract-socket-client.js +13 -0
- package/lib/Socket/Client/index.d.ts +3 -0
- package/lib/Socket/Client/index.js +19 -0
- package/lib/Socket/Client/mobile-socket-client.d.ts +13 -0
- package/lib/Socket/Client/mobile-socket-client.js +65 -0
- package/lib/Socket/Client/web-socket-client.d.ts +12 -0
- package/lib/Socket/Client/web-socket-client.js +62 -0
- package/lib/Socket/business.d.ts +170 -0
- package/lib/Socket/business.js +260 -0
- package/lib/Socket/chats.d.ts +81 -0
- package/lib/Socket/chats.js +950 -0
- package/lib/Socket/groups.d.ts +115 -0
- package/lib/Socket/groups.js +315 -0
- package/lib/Socket/index.d.ts +172 -0
- package/lib/Socket/index.js +10 -0
- package/lib/Socket/messages-recv.d.ts +158 -0
- package/lib/Socket/messages-recv.js +972 -0
- package/lib/Socket/messages-send.d.ts +155 -0
- package/lib/Socket/messages-send.js +1087 -0
- package/lib/Socket/newsletter.d.ts +132 -0
- package/lib/Socket/newsletter.js +236 -0
- package/lib/Socket/registration.d.ts +264 -0
- package/lib/Socket/registration.js +166 -0
- package/lib/Socket/socket.d.ts +44 -0
- package/lib/Socket/socket.js +643 -0
- package/lib/Socket/usync.d.ts +37 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/index.js +10 -0
- package/lib/Store/make-cache-manager-store.d.ts +14 -0
- package/lib/Store/make-cache-manager-store.js +83 -0
- package/lib/Store/make-in-memory-store.d.ts +118 -0
- package/lib/Store/make-in-memory-store.js +431 -0
- package/lib/Store/make-ordered-dictionary.d.ts +13 -0
- package/lib/Store/make-ordered-dictionary.js +81 -0
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.d.ts +109 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Call.d.ts +13 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.d.ts +107 -0
- package/lib/Types/Chat.js +4 -0
- package/lib/Types/Contact.d.ts +19 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.d.ts +172 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.d.ts +56 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.d.ts +46 -0
- package/lib/Types/Label.js +27 -0
- package/lib/Types/LabelAssociation.d.ts +29 -0
- package/lib/Types/LabelAssociation.js +9 -0
- package/lib/Types/Message.d.ts +433 -0
- package/lib/Types/Message.js +9 -0
- package/lib/Types/Newsletter.d.ts +92 -0
- package/lib/Types/Newsletter.js +32 -0
- package/lib/Types/Product.d.ts +78 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.d.ts +57 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.d.ts +116 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.d.ts +27 -0
- package/lib/Types/State.js +2 -0
- package/lib/Types/USync.d.ts +25 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.d.ts +66 -0
- package/lib/Types/index.js +42 -0
- package/lib/Utils/auth-utils.d.ts +18 -0
- package/lib/Utils/auth-utils.js +227 -0
- package/lib/Utils/baileys-event-stream.d.ts +16 -0
- package/lib/Utils/baileys-event-stream.js +63 -0
- package/lib/Utils/business.d.ts +22 -0
- package/lib/Utils/business.js +234 -0
- package/lib/Utils/chat-utils.d.ts +70 -0
- package/lib/Utils/chat-utils.js +745 -0
- package/lib/Utils/crypto.d.ts +40 -0
- package/lib/Utils/crypto.js +199 -0
- package/lib/Utils/decode-wa-message.d.ts +36 -0
- package/lib/Utils/decode-wa-message.js +234 -0
- package/lib/Utils/event-buffer.d.ts +35 -0
- package/lib/Utils/event-buffer.js +517 -0
- package/lib/Utils/generics.d.ts +88 -0
- package/lib/Utils/generics.js +402 -0
- package/lib/Utils/history.d.ts +19 -0
- package/lib/Utils/history.js +94 -0
- package/lib/Utils/index.d.ts +17 -0
- package/lib/Utils/index.js +33 -0
- package/lib/Utils/link-preview.d.ts +21 -0
- package/lib/Utils/link-preview.js +93 -0
- package/lib/Utils/logger.d.ts +2 -0
- package/lib/Utils/logger.js +7 -0
- package/lib/Utils/lt-hash.d.ts +12 -0
- package/lib/Utils/lt-hash.js +51 -0
- package/lib/Utils/make-mutex.d.ts +7 -0
- package/lib/Utils/make-mutex.js +43 -0
- package/lib/Utils/messages-media.d.ts +113 -0
- package/lib/Utils/messages-media.js +643 -0
- package/lib/Utils/messages.d.ts +77 -0
- package/lib/Utils/messages.js +1221 -0
- package/lib/Utils/noise-handler.d.ts +20 -0
- package/lib/Utils/noise-handler.js +160 -0
- package/lib/Utils/process-message.d.ts +41 -0
- package/lib/Utils/process-message.js +373 -0
- package/lib/Utils/signal.d.ts +33 -0
- package/lib/Utils/signal.js +159 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +12 -0
- package/lib/Utils/use-multi-file-auth-state.js +295 -0
- package/lib/Utils/use-single-file-auth-state.d.ts +12 -0
- package/lib/Utils/use-single-file-auth-state.js +75 -0
- package/lib/Utils/validate-connection.d.ts +11 -0
- package/lib/Utils/validate-connection.js +205 -0
- package/lib/WABinary/constants.d.ts +27 -0
- package/lib/WABinary/constants.js +40 -0
- package/lib/WABinary/decode.d.ts +6 -0
- package/lib/WABinary/decode.js +264 -0
- package/lib/WABinary/encode.d.ts +2 -0
- package/lib/WABinary/encode.js +252 -0
- package/lib/WABinary/generic-utils.d.ts +14 -0
- package/lib/WABinary/generic-utils.js +110 -0
- package/lib/WABinary/index.d.ts +5 -0
- package/lib/WABinary/index.js +21 -0
- package/lib/WABinary/jid-utils.d.ts +31 -0
- package/lib/WABinary/jid-utils.js +62 -0
- package/lib/WABinary/types.d.ts +18 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.d.ts +8 -0
- package/lib/WAM/BinaryInfo.js +13 -0
- package/lib/WAM/constants.d.ts +38 -0
- package/lib/WAM/constants.js +15350 -0
- package/lib/WAM/encode.d.ts +2 -0
- package/lib/WAM/encode.js +155 -0
- package/lib/WAM/index.d.ts +3 -0
- package/lib/WAM/index.js +19 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +25 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +8 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
- package/lib/WAUSync/Protocols/index.d.ts +4 -0
- package/lib/WAUSync/Protocols/index.js +20 -0
- package/lib/WAUSync/USyncQuery.d.ts +28 -0
- package/lib/WAUSync/USyncQuery.js +89 -0
- package/lib/WAUSync/USyncUser.d.ts +10 -0
- package/lib/WAUSync/USyncUser.js +26 -0
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/WAUSync/index.js +19 -0
- package/lib/index.js +31 -0
- package/package.json +51 -0
- package/src/Defaults/baileys-version.json +3 -0
- package/src/Defaults/index.ts +133 -0
- package/src/Signal/Group/ciphertext-message.ts +9 -0
- package/src/Signal/Group/group-session-builder.ts +56 -0
- package/src/Signal/Group/group_cipher.ts +117 -0
- package/src/Signal/Group/index.ts +11 -0
- package/src/Signal/Group/keyhelper.ts +28 -0
- package/src/Signal/Group/sender-chain-key.ts +34 -0
- package/src/Signal/Group/sender-key-distribution-message.ts +95 -0
- package/src/Signal/Group/sender-key-message.ts +96 -0
- package/src/Signal/Group/sender-key-name.ts +66 -0
- package/src/Signal/Group/sender-key-record.ts +69 -0
- package/src/Signal/Group/sender-key-state.ts +134 -0
- package/src/Signal/Group/sender-message-key.ts +36 -0
- package/src/Signal/libsignal.ts +447 -0
- package/src/Signal/lid-mapping.ts +209 -0
- package/src/Socket/Client/index.ts +2 -0
- package/src/Socket/Client/types.ts +22 -0
- package/src/Socket/Client/websocket.ts +56 -0
- package/src/Socket/business.ts +421 -0
- package/src/Socket/chats.ts +1223 -0
- package/src/Socket/communities.ts +477 -0
- package/src/Socket/groups.ts +361 -0
- package/src/Socket/index.ts +22 -0
- package/src/Socket/messages-recv.ts +1563 -0
- package/src/Socket/messages-send.ts +1210 -0
- package/src/Socket/mex.ts +58 -0
- package/src/Socket/newsletter.ts +229 -0
- package/src/Socket/socket.ts +1072 -0
- package/src/Types/Auth.ts +115 -0
- package/src/Types/Bussines.ts +20 -0
- package/src/Types/Call.ts +14 -0
- package/src/Types/Chat.ts +138 -0
- package/src/Types/Contact.ts +24 -0
- package/src/Types/Events.ts +132 -0
- package/src/Types/GroupMetadata.ts +70 -0
- package/src/Types/Label.ts +48 -0
- package/src/Types/LabelAssociation.ts +35 -0
- package/src/Types/Message.ts +424 -0
- package/src/Types/Newsletter.ts +98 -0
- package/src/Types/Product.ts +85 -0
- package/src/Types/Signal.ts +76 -0
- package/src/Types/Socket.ts +150 -0
- package/src/Types/State.ts +43 -0
- package/src/Types/USync.ts +27 -0
- package/src/Types/globals.d.ts +8 -0
- package/src/Types/index.ts +67 -0
- package/src/Utils/auth-utils.ts +331 -0
- package/src/Utils/browser-utils.ts +31 -0
- package/src/Utils/business.ts +286 -0
- package/src/Utils/chat-utils.ts +933 -0
- package/src/Utils/crypto.ts +184 -0
- package/src/Utils/decode-wa-message.ts +355 -0
- package/src/Utils/event-buffer.ts +662 -0
- package/src/Utils/generics.ts +470 -0
- package/src/Utils/history.ts +114 -0
- package/src/Utils/index.ts +18 -0
- package/src/Utils/link-preview.ts +111 -0
- package/src/Utils/logger.ts +13 -0
- package/src/Utils/lt-hash.ts +65 -0
- package/src/Utils/make-mutex.ts +45 -0
- package/src/Utils/message-retry-manager.ts +229 -0
- package/src/Utils/messages-media.ts +820 -0
- package/src/Utils/messages.ts +1137 -0
- package/src/Utils/noise-handler.ts +192 -0
- package/src/Utils/pre-key-manager.ts +126 -0
- package/src/Utils/process-message.ts +622 -0
- package/src/Utils/signal.ts +214 -0
- package/src/Utils/use-multi-file-auth-state.ts +136 -0
- package/src/Utils/validate-connection.ts +253 -0
- package/src/WABinary/constants.ts +1305 -0
- package/src/WABinary/decode.ts +281 -0
- package/src/WABinary/encode.ts +253 -0
- package/src/WABinary/generic-utils.ts +127 -0
- package/src/WABinary/index.ts +5 -0
- package/src/WABinary/jid-utils.ts +128 -0
- package/src/WABinary/types.ts +17 -0
- package/src/WAM/BinaryInfo.ts +12 -0
- package/src/WAM/constants.ts +22889 -0
- package/src/WAM/encode.ts +169 -0
- package/src/WAM/index.ts +3 -0
- package/src/WAUSync/Protocols/USyncContactProtocol.ts +32 -0
- package/src/WAUSync/Protocols/USyncDeviceProtocol.ts +78 -0
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.ts +35 -0
- package/src/WAUSync/Protocols/USyncStatusProtocol.ts +44 -0
- package/src/WAUSync/Protocols/UsyncBotProfileProtocol.ts +76 -0
- package/src/WAUSync/Protocols/UsyncLIDProtocol.ts +33 -0
- package/src/WAUSync/Protocols/index.ts +4 -0
- package/src/WAUSync/USyncQuery.ts +133 -0
- package/src/WAUSync/USyncUser.ts +32 -0
- package/src/WAUSync/index.ts +3 -0
- package/src/index.ts +13 -0
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
import EventEmitter from 'events'
|
|
2
|
+
import type {
|
|
3
|
+
BaileysEvent,
|
|
4
|
+
BaileysEventEmitter,
|
|
5
|
+
BaileysEventMap,
|
|
6
|
+
BufferedEventData,
|
|
7
|
+
Chat,
|
|
8
|
+
ChatUpdate,
|
|
9
|
+
Contact,
|
|
10
|
+
WAMessage,
|
|
11
|
+
WAMessageKey
|
|
12
|
+
} from '../Types'
|
|
13
|
+
import { WAMessageStatus } from '../Types'
|
|
14
|
+
import { trimUndefined } from './generics'
|
|
15
|
+
import type { ILogger } from './logger'
|
|
16
|
+
import { updateMessageWithReaction, updateMessageWithReceipt } from './messages'
|
|
17
|
+
import { isRealMessage, shouldIncrementChatUnread } from './process-message'
|
|
18
|
+
|
|
19
|
+
const BUFFERABLE_EVENT = [
|
|
20
|
+
'messaging-history.set',
|
|
21
|
+
'chats.upsert',
|
|
22
|
+
'chats.update',
|
|
23
|
+
'chats.delete',
|
|
24
|
+
'contacts.upsert',
|
|
25
|
+
'contacts.update',
|
|
26
|
+
'messages.upsert',
|
|
27
|
+
'messages.update',
|
|
28
|
+
'messages.delete',
|
|
29
|
+
'messages.reaction',
|
|
30
|
+
'message-receipt.update',
|
|
31
|
+
'groups.update'
|
|
32
|
+
] as const
|
|
33
|
+
|
|
34
|
+
type BufferableEvent = (typeof BUFFERABLE_EVENT)[number]
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A map that contains a list of all events that have been triggered
|
|
38
|
+
*
|
|
39
|
+
* Note, this can contain different type of events
|
|
40
|
+
* this can make processing events extremely efficient -- since everything
|
|
41
|
+
* can be done in a single transaction
|
|
42
|
+
*/
|
|
43
|
+
type BaileysEventData = Partial<BaileysEventMap>
|
|
44
|
+
|
|
45
|
+
const BUFFERABLE_EVENT_SET = new Set<BaileysEvent>(BUFFERABLE_EVENT)
|
|
46
|
+
|
|
47
|
+
type BaileysBufferableEventEmitter = BaileysEventEmitter & {
|
|
48
|
+
/** Use to process events in a batch */
|
|
49
|
+
process(handler: (events: BaileysEventData) => void | Promise<void>): () => void
|
|
50
|
+
/**
|
|
51
|
+
* starts buffering events, call flush() to release them
|
|
52
|
+
* */
|
|
53
|
+
buffer(): void
|
|
54
|
+
/** buffers all events till the promise completes */
|
|
55
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
56
|
+
createBufferedFunction<A extends any[], T>(work: (...args: A) => Promise<T>): (...args: A) => Promise<T>
|
|
57
|
+
/**
|
|
58
|
+
* flushes all buffered events
|
|
59
|
+
* @returns returns true if the flush actually happened, otherwise false
|
|
60
|
+
*/
|
|
61
|
+
flush(): boolean
|
|
62
|
+
/** is there an ongoing buffer */
|
|
63
|
+
isBuffering(): boolean
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The event buffer logically consolidates different events into a single event
|
|
68
|
+
* making the data processing more efficient.
|
|
69
|
+
*/
|
|
70
|
+
export const makeEventBuffer = (logger: ILogger): BaileysBufferableEventEmitter => {
|
|
71
|
+
const ev = new EventEmitter()
|
|
72
|
+
const historyCache = new Set<string>()
|
|
73
|
+
|
|
74
|
+
let data = makeBufferData()
|
|
75
|
+
let isBuffering = false
|
|
76
|
+
let bufferTimeout: NodeJS.Timeout | null = null
|
|
77
|
+
let bufferCount = 0
|
|
78
|
+
const MAX_HISTORY_CACHE_SIZE = 10000 // Limit the history cache size to prevent memory bloat
|
|
79
|
+
const BUFFER_TIMEOUT_MS = 30000 // 30 seconds
|
|
80
|
+
|
|
81
|
+
// take the generic event and fire it as a baileys event
|
|
82
|
+
ev.on('event', (map: BaileysEventData) => {
|
|
83
|
+
for (const event in map) {
|
|
84
|
+
ev.emit(event, map[event as keyof BaileysEventMap])
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
function buffer() {
|
|
89
|
+
if (!isBuffering) {
|
|
90
|
+
logger.debug('Event buffer activated')
|
|
91
|
+
isBuffering = true
|
|
92
|
+
bufferCount++
|
|
93
|
+
|
|
94
|
+
// Auto-flush after a timeout to prevent infinite buffering
|
|
95
|
+
if (bufferTimeout) {
|
|
96
|
+
clearTimeout(bufferTimeout)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
bufferTimeout = setTimeout(() => {
|
|
100
|
+
if (isBuffering) {
|
|
101
|
+
logger.warn('Buffer timeout reached, auto-flushing')
|
|
102
|
+
flush()
|
|
103
|
+
}
|
|
104
|
+
}, BUFFER_TIMEOUT_MS)
|
|
105
|
+
} else {
|
|
106
|
+
bufferCount++
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function flush() {
|
|
111
|
+
if (!isBuffering) {
|
|
112
|
+
return false
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
logger.debug({ bufferCount }, 'Flushing event buffer')
|
|
116
|
+
isBuffering = false
|
|
117
|
+
bufferCount = 0
|
|
118
|
+
|
|
119
|
+
// Clear timeout
|
|
120
|
+
if (bufferTimeout) {
|
|
121
|
+
clearTimeout(bufferTimeout)
|
|
122
|
+
bufferTimeout = null
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Clear history cache if it exceeds the max size
|
|
126
|
+
if (historyCache.size > MAX_HISTORY_CACHE_SIZE) {
|
|
127
|
+
logger.debug({ cacheSize: historyCache.size }, 'Clearing history cache')
|
|
128
|
+
historyCache.clear()
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const newData = makeBufferData()
|
|
132
|
+
const chatUpdates = Object.values(data.chatUpdates)
|
|
133
|
+
let conditionalChatUpdatesLeft = 0
|
|
134
|
+
for (const update of chatUpdates) {
|
|
135
|
+
if (update.conditional) {
|
|
136
|
+
conditionalChatUpdatesLeft += 1
|
|
137
|
+
newData.chatUpdates[update.id!] = update
|
|
138
|
+
delete data.chatUpdates[update.id!]
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const consolidatedData = consolidateEvents(data)
|
|
143
|
+
if (Object.keys(consolidatedData).length) {
|
|
144
|
+
ev.emit('event', consolidatedData)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
data = newData
|
|
148
|
+
|
|
149
|
+
logger.trace({ conditionalChatUpdatesLeft }, 'released buffered events')
|
|
150
|
+
|
|
151
|
+
return true
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
process(handler) {
|
|
156
|
+
const listener = async (map: BaileysEventData) => {
|
|
157
|
+
await handler(map)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
ev.on('event', listener)
|
|
161
|
+
return () => {
|
|
162
|
+
ev.off('event', listener)
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
emit<T extends BaileysEvent>(event: BaileysEvent, evData: BaileysEventMap[T]) {
|
|
166
|
+
if (isBuffering && BUFFERABLE_EVENT_SET.has(event)) {
|
|
167
|
+
append(data, historyCache, event as BufferableEvent, evData, logger)
|
|
168
|
+
return true
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return ev.emit('event', { [event]: evData })
|
|
172
|
+
},
|
|
173
|
+
isBuffering() {
|
|
174
|
+
return isBuffering
|
|
175
|
+
},
|
|
176
|
+
buffer,
|
|
177
|
+
flush,
|
|
178
|
+
createBufferedFunction(work) {
|
|
179
|
+
return async (...args) => {
|
|
180
|
+
buffer()
|
|
181
|
+
try {
|
|
182
|
+
const result = await work(...args)
|
|
183
|
+
// If this is the only buffer, flush after a small delay
|
|
184
|
+
if (bufferCount === 1) {
|
|
185
|
+
setTimeout(() => {
|
|
186
|
+
if (isBuffering && bufferCount === 1) {
|
|
187
|
+
flush()
|
|
188
|
+
}
|
|
189
|
+
}, 100) // Small delay to allow nested buffers
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return result
|
|
193
|
+
} catch (error) {
|
|
194
|
+
throw error
|
|
195
|
+
} finally {
|
|
196
|
+
bufferCount = Math.max(0, bufferCount - 1)
|
|
197
|
+
if (bufferCount === 0) {
|
|
198
|
+
// Auto-flush when no other buffers are active
|
|
199
|
+
setTimeout(flush, 100)
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
},
|
|
204
|
+
on: (...args) => ev.on(...args),
|
|
205
|
+
off: (...args) => ev.off(...args),
|
|
206
|
+
removeAllListeners: (...args) => ev.removeAllListeners(...args)
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const makeBufferData = (): BufferedEventData => {
|
|
211
|
+
return {
|
|
212
|
+
historySets: {
|
|
213
|
+
chats: {},
|
|
214
|
+
messages: {},
|
|
215
|
+
contacts: {},
|
|
216
|
+
isLatest: false,
|
|
217
|
+
empty: true
|
|
218
|
+
},
|
|
219
|
+
chatUpserts: {},
|
|
220
|
+
chatUpdates: {},
|
|
221
|
+
chatDeletes: new Set(),
|
|
222
|
+
contactUpserts: {},
|
|
223
|
+
contactUpdates: {},
|
|
224
|
+
messageUpserts: {},
|
|
225
|
+
messageUpdates: {},
|
|
226
|
+
messageReactions: {},
|
|
227
|
+
messageDeletes: {},
|
|
228
|
+
messageReceipts: {},
|
|
229
|
+
groupUpdates: {}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function append<E extends BufferableEvent>(
|
|
234
|
+
data: BufferedEventData,
|
|
235
|
+
historyCache: Set<string>,
|
|
236
|
+
event: E,
|
|
237
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
238
|
+
eventData: any,
|
|
239
|
+
logger: ILogger
|
|
240
|
+
) {
|
|
241
|
+
switch (event) {
|
|
242
|
+
case 'messaging-history.set':
|
|
243
|
+
for (const chat of eventData.chats as Chat[]) {
|
|
244
|
+
const id = chat.id || ''
|
|
245
|
+
const existingChat = data.historySets.chats[id]
|
|
246
|
+
if (existingChat) {
|
|
247
|
+
existingChat.endOfHistoryTransferType = chat.endOfHistoryTransferType
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (!existingChat && !historyCache.has(id)) {
|
|
251
|
+
data.historySets.chats[id] = chat
|
|
252
|
+
historyCache.add(id)
|
|
253
|
+
|
|
254
|
+
absorbingChatUpdate(chat)
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
for (const contact of eventData.contacts as Contact[]) {
|
|
259
|
+
const existingContact = data.historySets.contacts[contact.id]
|
|
260
|
+
if (existingContact) {
|
|
261
|
+
Object.assign(existingContact, trimUndefined(contact))
|
|
262
|
+
} else {
|
|
263
|
+
const historyContactId = `c:${contact.id}`
|
|
264
|
+
const hasAnyName = contact.notify || contact.name || contact.verifiedName
|
|
265
|
+
if (!historyCache.has(historyContactId) || hasAnyName) {
|
|
266
|
+
data.historySets.contacts[contact.id] = contact
|
|
267
|
+
historyCache.add(historyContactId)
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
for (const message of eventData.messages as WAMessage[]) {
|
|
273
|
+
const key = stringifyMessageKey(message.key)
|
|
274
|
+
const existingMsg = data.historySets.messages[key]
|
|
275
|
+
if (!existingMsg && !historyCache.has(key)) {
|
|
276
|
+
data.historySets.messages[key] = message
|
|
277
|
+
historyCache.add(key)
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
data.historySets.empty = false
|
|
282
|
+
data.historySets.syncType = eventData.syncType
|
|
283
|
+
data.historySets.progress = eventData.progress
|
|
284
|
+
data.historySets.peerDataRequestSessionId = eventData.peerDataRequestSessionId
|
|
285
|
+
data.historySets.isLatest = eventData.isLatest || data.historySets.isLatest
|
|
286
|
+
|
|
287
|
+
break
|
|
288
|
+
case 'chats.upsert':
|
|
289
|
+
for (const chat of eventData as Chat[]) {
|
|
290
|
+
const id = chat.id || ''
|
|
291
|
+
let upsert = data.chatUpserts[id]
|
|
292
|
+
if (id && !upsert) {
|
|
293
|
+
upsert = data.historySets.chats[id]
|
|
294
|
+
if (upsert) {
|
|
295
|
+
logger.debug({ chatId: id }, 'absorbed chat upsert in chat set')
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (upsert) {
|
|
300
|
+
upsert = concatChats(upsert, chat)
|
|
301
|
+
} else {
|
|
302
|
+
upsert = chat
|
|
303
|
+
data.chatUpserts[id] = upsert
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
absorbingChatUpdate(upsert)
|
|
307
|
+
|
|
308
|
+
if (data.chatDeletes.has(id)) {
|
|
309
|
+
data.chatDeletes.delete(id)
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
break
|
|
314
|
+
case 'chats.update':
|
|
315
|
+
for (const update of eventData as ChatUpdate[]) {
|
|
316
|
+
const chatId = update.id!
|
|
317
|
+
const conditionMatches = update.conditional ? update.conditional(data) : true
|
|
318
|
+
if (conditionMatches) {
|
|
319
|
+
delete update.conditional
|
|
320
|
+
|
|
321
|
+
// if there is an existing upsert, merge the update into it
|
|
322
|
+
const upsert = data.historySets.chats[chatId] || data.chatUpserts[chatId]
|
|
323
|
+
if (upsert) {
|
|
324
|
+
concatChats(upsert, update)
|
|
325
|
+
} else {
|
|
326
|
+
// merge the update into the existing update
|
|
327
|
+
const chatUpdate = data.chatUpdates[chatId] || {}
|
|
328
|
+
data.chatUpdates[chatId] = concatChats(chatUpdate, update)
|
|
329
|
+
}
|
|
330
|
+
} else if (conditionMatches === undefined) {
|
|
331
|
+
// condition yet to be fulfilled
|
|
332
|
+
data.chatUpdates[chatId] = update
|
|
333
|
+
}
|
|
334
|
+
// otherwise -- condition not met, update is invalid
|
|
335
|
+
|
|
336
|
+
// if the chat has been updated
|
|
337
|
+
// ignore any existing chat delete
|
|
338
|
+
if (data.chatDeletes.has(chatId)) {
|
|
339
|
+
data.chatDeletes.delete(chatId)
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
break
|
|
344
|
+
case 'chats.delete':
|
|
345
|
+
for (const chatId of eventData as string[]) {
|
|
346
|
+
if (!data.chatDeletes.has(chatId)) {
|
|
347
|
+
data.chatDeletes.add(chatId)
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// remove any prior updates & upserts
|
|
351
|
+
if (data.chatUpdates[chatId]) {
|
|
352
|
+
delete data.chatUpdates[chatId]
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (data.chatUpserts[chatId]) {
|
|
356
|
+
delete data.chatUpserts[chatId]
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (data.historySets.chats[chatId]) {
|
|
360
|
+
delete data.historySets.chats[chatId]
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
break
|
|
365
|
+
case 'contacts.upsert':
|
|
366
|
+
for (const contact of eventData as Contact[]) {
|
|
367
|
+
let upsert = data.contactUpserts[contact.id]
|
|
368
|
+
if (!upsert) {
|
|
369
|
+
upsert = data.historySets.contacts[contact.id]
|
|
370
|
+
if (upsert) {
|
|
371
|
+
logger.debug({ contactId: contact.id }, 'absorbed contact upsert in contact set')
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (upsert) {
|
|
376
|
+
upsert = Object.assign(upsert, trimUndefined(contact))
|
|
377
|
+
} else {
|
|
378
|
+
upsert = contact
|
|
379
|
+
data.contactUpserts[contact.id] = upsert
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if (data.contactUpdates[contact.id]) {
|
|
383
|
+
upsert = Object.assign(data.contactUpdates[contact.id]!, trimUndefined(contact)) as Contact
|
|
384
|
+
delete data.contactUpdates[contact.id]
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
break
|
|
389
|
+
case 'contacts.update':
|
|
390
|
+
const contactUpdates = eventData as BaileysEventMap['contacts.update']
|
|
391
|
+
for (const update of contactUpdates) {
|
|
392
|
+
const id = update.id!
|
|
393
|
+
// merge into prior upsert
|
|
394
|
+
const upsert = data.historySets.contacts[id] || data.contactUpserts[id]
|
|
395
|
+
if (upsert) {
|
|
396
|
+
Object.assign(upsert, update)
|
|
397
|
+
} else {
|
|
398
|
+
// merge into prior update
|
|
399
|
+
const contactUpdate = data.contactUpdates[id] || {}
|
|
400
|
+
data.contactUpdates[id] = Object.assign(contactUpdate, update)
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
break
|
|
405
|
+
case 'messages.upsert':
|
|
406
|
+
const { messages, type } = eventData as BaileysEventMap['messages.upsert']
|
|
407
|
+
for (const message of messages) {
|
|
408
|
+
const key = stringifyMessageKey(message.key)
|
|
409
|
+
let existing = data.messageUpserts[key]?.message
|
|
410
|
+
if (!existing) {
|
|
411
|
+
existing = data.historySets.messages[key]
|
|
412
|
+
if (existing) {
|
|
413
|
+
logger.debug({ messageId: key }, 'absorbed message upsert in message set')
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (existing) {
|
|
418
|
+
message.messageTimestamp = existing.messageTimestamp
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (data.messageUpdates[key]) {
|
|
422
|
+
logger.debug('absorbed prior message update in message upsert')
|
|
423
|
+
Object.assign(message, data.messageUpdates[key].update)
|
|
424
|
+
delete data.messageUpdates[key]
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
if (data.historySets.messages[key]) {
|
|
428
|
+
data.historySets.messages[key] = message
|
|
429
|
+
} else {
|
|
430
|
+
data.messageUpserts[key] = {
|
|
431
|
+
message,
|
|
432
|
+
type: type === 'notify' || data.messageUpserts[key]?.type === 'notify' ? 'notify' : type
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
break
|
|
438
|
+
case 'messages.update':
|
|
439
|
+
const msgUpdates = eventData as BaileysEventMap['messages.update']
|
|
440
|
+
for (const { key, update } of msgUpdates) {
|
|
441
|
+
const keyStr = stringifyMessageKey(key)
|
|
442
|
+
const existing = data.historySets.messages[keyStr] || data.messageUpserts[keyStr]?.message
|
|
443
|
+
if (existing) {
|
|
444
|
+
Object.assign(existing, update)
|
|
445
|
+
// if the message was received & read by us
|
|
446
|
+
// the chat counter must have been incremented
|
|
447
|
+
// so we need to decrement it
|
|
448
|
+
if (update.status === WAMessageStatus.READ && !key.fromMe) {
|
|
449
|
+
decrementChatReadCounterIfMsgDidUnread(existing)
|
|
450
|
+
}
|
|
451
|
+
} else {
|
|
452
|
+
const msgUpdate = data.messageUpdates[keyStr] || { key, update: {} }
|
|
453
|
+
Object.assign(msgUpdate.update, update)
|
|
454
|
+
data.messageUpdates[keyStr] = msgUpdate
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
break
|
|
459
|
+
case 'messages.delete':
|
|
460
|
+
const deleteData = eventData as BaileysEventMap['messages.delete']
|
|
461
|
+
if ('keys' in deleteData) {
|
|
462
|
+
const { keys } = deleteData
|
|
463
|
+
for (const key of keys) {
|
|
464
|
+
const keyStr = stringifyMessageKey(key)
|
|
465
|
+
if (!data.messageDeletes[keyStr]) {
|
|
466
|
+
data.messageDeletes[keyStr] = key
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (data.messageUpserts[keyStr]) {
|
|
470
|
+
delete data.messageUpserts[keyStr]
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if (data.messageUpdates[keyStr]) {
|
|
474
|
+
delete data.messageUpdates[keyStr]
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
} else {
|
|
478
|
+
// TODO: add support
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
break
|
|
482
|
+
case 'messages.reaction':
|
|
483
|
+
const reactions = eventData as BaileysEventMap['messages.reaction']
|
|
484
|
+
for (const { key, reaction } of reactions) {
|
|
485
|
+
const keyStr = stringifyMessageKey(key)
|
|
486
|
+
const existing = data.messageUpserts[keyStr]
|
|
487
|
+
if (existing) {
|
|
488
|
+
updateMessageWithReaction(existing.message, reaction)
|
|
489
|
+
} else {
|
|
490
|
+
data.messageReactions[keyStr] = data.messageReactions[keyStr] || { key, reactions: [] }
|
|
491
|
+
updateMessageWithReaction(data.messageReactions[keyStr], reaction)
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
break
|
|
496
|
+
case 'message-receipt.update':
|
|
497
|
+
const receipts = eventData as BaileysEventMap['message-receipt.update']
|
|
498
|
+
for (const { key, receipt } of receipts) {
|
|
499
|
+
const keyStr = stringifyMessageKey(key)
|
|
500
|
+
const existing = data.messageUpserts[keyStr]
|
|
501
|
+
if (existing) {
|
|
502
|
+
updateMessageWithReceipt(existing.message, receipt)
|
|
503
|
+
} else {
|
|
504
|
+
data.messageReceipts[keyStr] = data.messageReceipts[keyStr] || { key, userReceipt: [] }
|
|
505
|
+
updateMessageWithReceipt(data.messageReceipts[keyStr], receipt)
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
break
|
|
510
|
+
case 'groups.update':
|
|
511
|
+
const groupUpdates = eventData as BaileysEventMap['groups.update']
|
|
512
|
+
for (const update of groupUpdates) {
|
|
513
|
+
const id = update.id!
|
|
514
|
+
const groupUpdate = data.groupUpdates[id] || {}
|
|
515
|
+
if (!data.groupUpdates[id]) {
|
|
516
|
+
data.groupUpdates[id] = Object.assign(groupUpdate, update)
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
break
|
|
521
|
+
default:
|
|
522
|
+
throw new Error(`"${event}" cannot be buffered`)
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function absorbingChatUpdate(existing: Chat) {
|
|
526
|
+
const chatId = existing.id || ''
|
|
527
|
+
const update = data.chatUpdates[chatId]
|
|
528
|
+
if (update) {
|
|
529
|
+
const conditionMatches = update.conditional ? update.conditional(data) : true
|
|
530
|
+
if (conditionMatches) {
|
|
531
|
+
delete update.conditional
|
|
532
|
+
logger.debug({ chatId }, 'absorbed chat update in existing chat')
|
|
533
|
+
Object.assign(existing, concatChats(update as Chat, existing))
|
|
534
|
+
delete data.chatUpdates[chatId]
|
|
535
|
+
} else if (conditionMatches === false) {
|
|
536
|
+
logger.debug({ chatId }, 'chat update condition fail, removing')
|
|
537
|
+
delete data.chatUpdates[chatId]
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
function decrementChatReadCounterIfMsgDidUnread(message: WAMessage) {
|
|
543
|
+
// decrement chat unread counter
|
|
544
|
+
// if the message has already been marked read by us
|
|
545
|
+
const chatId = message.key.remoteJid!
|
|
546
|
+
const chat = data.chatUpdates[chatId] || data.chatUpserts[chatId]
|
|
547
|
+
if (
|
|
548
|
+
isRealMessage(message) &&
|
|
549
|
+
shouldIncrementChatUnread(message) &&
|
|
550
|
+
typeof chat?.unreadCount === 'number' &&
|
|
551
|
+
chat.unreadCount > 0
|
|
552
|
+
) {
|
|
553
|
+
logger.debug({ chatId: chat.id }, 'decrementing chat counter')
|
|
554
|
+
chat.unreadCount -= 1
|
|
555
|
+
if (chat.unreadCount === 0) {
|
|
556
|
+
delete chat.unreadCount
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
function consolidateEvents(data: BufferedEventData) {
|
|
563
|
+
const map: BaileysEventData = {}
|
|
564
|
+
|
|
565
|
+
if (!data.historySets.empty) {
|
|
566
|
+
map['messaging-history.set'] = {
|
|
567
|
+
chats: Object.values(data.historySets.chats),
|
|
568
|
+
messages: Object.values(data.historySets.messages),
|
|
569
|
+
contacts: Object.values(data.historySets.contacts),
|
|
570
|
+
syncType: data.historySets.syncType,
|
|
571
|
+
progress: data.historySets.progress,
|
|
572
|
+
isLatest: data.historySets.isLatest,
|
|
573
|
+
peerDataRequestSessionId: data.historySets.peerDataRequestSessionId
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
const chatUpsertList = Object.values(data.chatUpserts)
|
|
578
|
+
if (chatUpsertList.length) {
|
|
579
|
+
map['chats.upsert'] = chatUpsertList
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
const chatUpdateList = Object.values(data.chatUpdates)
|
|
583
|
+
if (chatUpdateList.length) {
|
|
584
|
+
map['chats.update'] = chatUpdateList
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
const chatDeleteList = Array.from(data.chatDeletes)
|
|
588
|
+
if (chatDeleteList.length) {
|
|
589
|
+
map['chats.delete'] = chatDeleteList
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
const messageUpsertList = Object.values(data.messageUpserts)
|
|
593
|
+
if (messageUpsertList.length) {
|
|
594
|
+
const type = messageUpsertList[0]!.type
|
|
595
|
+
map['messages.upsert'] = {
|
|
596
|
+
messages: messageUpsertList.map(m => m.message),
|
|
597
|
+
type
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
const messageUpdateList = Object.values(data.messageUpdates)
|
|
602
|
+
if (messageUpdateList.length) {
|
|
603
|
+
map['messages.update'] = messageUpdateList
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
const messageDeleteList = Object.values(data.messageDeletes)
|
|
607
|
+
if (messageDeleteList.length) {
|
|
608
|
+
map['messages.delete'] = { keys: messageDeleteList }
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const messageReactionList = Object.values(data.messageReactions).flatMap(({ key, reactions }) =>
|
|
612
|
+
reactions.flatMap(reaction => ({ key, reaction }))
|
|
613
|
+
)
|
|
614
|
+
if (messageReactionList.length) {
|
|
615
|
+
map['messages.reaction'] = messageReactionList
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
const messageReceiptList = Object.values(data.messageReceipts).flatMap(({ key, userReceipt }) =>
|
|
619
|
+
userReceipt.flatMap(receipt => ({ key, receipt }))
|
|
620
|
+
)
|
|
621
|
+
if (messageReceiptList.length) {
|
|
622
|
+
map['message-receipt.update'] = messageReceiptList
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
const contactUpsertList = Object.values(data.contactUpserts)
|
|
626
|
+
if (contactUpsertList.length) {
|
|
627
|
+
map['contacts.upsert'] = contactUpsertList
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
const contactUpdateList = Object.values(data.contactUpdates)
|
|
631
|
+
if (contactUpdateList.length) {
|
|
632
|
+
map['contacts.update'] = contactUpdateList
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
const groupUpdateList = Object.values(data.groupUpdates)
|
|
636
|
+
if (groupUpdateList.length) {
|
|
637
|
+
map['groups.update'] = groupUpdateList
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
return map
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
function concatChats<C extends Partial<Chat>>(a: C, b: Partial<Chat>) {
|
|
644
|
+
if (
|
|
645
|
+
b.unreadCount === null && // neutralize unread counter
|
|
646
|
+
a.unreadCount! < 0
|
|
647
|
+
) {
|
|
648
|
+
a.unreadCount = undefined
|
|
649
|
+
b.unreadCount = undefined
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
if (typeof a.unreadCount === 'number' && typeof b.unreadCount === 'number') {
|
|
653
|
+
b = { ...b }
|
|
654
|
+
if (b.unreadCount! >= 0) {
|
|
655
|
+
b.unreadCount = Math.max(b.unreadCount!, 0) + Math.max(a.unreadCount, 0)
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
return Object.assign(a, b)
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
const stringifyMessageKey = (key: WAMessageKey) => `${key.remoteJid},${key.id},${key.fromMe ? '1' : '0'}`
|