@nerxa/socketon 0.0.0-stage → 1.21.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.
Files changed (289) hide show
  1. package/EXPERIMENTAL.md +350 -0
  2. package/LICENSE +24 -0
  3. package/README.md +6317 -2
  4. package/WAProto/index.d.ts +17301 -0
  5. package/WAProto/index.js +125088 -0
  6. package/engine-requirements.js +14 -0
  7. package/package.json +100 -4
  8. package/src/Defaults/index.d.ts +152 -0
  9. package/src/Defaults/index.js +164 -0
  10. package/src/MessageBuilder/bot-signature.d.ts +22 -0
  11. package/src/MessageBuilder/bot-signature.js +122 -0
  12. package/src/MessageBuilder/extras.d.ts +449 -0
  13. package/src/MessageBuilder/extras.js +1356 -0
  14. package/src/MessageBuilder/index.d.ts +210 -0
  15. package/src/MessageBuilder/index.js +3594 -0
  16. package/src/MessageBuilder/metaai.d.ts +438 -0
  17. package/src/MessageBuilder/metaai.js +890 -0
  18. package/src/Signal/Group/ciphertext-message.d.ts +9 -0
  19. package/src/Signal/Group/ciphertext-message.js +11 -0
  20. package/src/Signal/Group/group-session-builder.d.ts +7 -0
  21. package/src/Signal/Group/group-session-builder.js +27 -0
  22. package/src/Signal/Group/group_cipher.d.ts +11 -0
  23. package/src/Signal/Group/group_cipher.js +78 -0
  24. package/src/Signal/Group/index.d.ts +11 -0
  25. package/src/Signal/Group/index.js +11 -0
  26. package/src/Signal/Group/keyhelper.d.ts +6 -0
  27. package/src/Signal/Group/keyhelper.js +17 -0
  28. package/src/Signal/Group/sender-chain-key.d.ts +13 -0
  29. package/src/Signal/Group/sender-chain-key.js +25 -0
  30. package/src/Signal/Group/sender-key-distribution-message.d.ts +16 -0
  31. package/src/Signal/Group/sender-key-distribution-message.js +62 -0
  32. package/src/Signal/Group/sender-key-message.d.ts +18 -0
  33. package/src/Signal/Group/sender-key-message.js +65 -0
  34. package/src/Signal/Group/sender-key-name.d.ts +11 -0
  35. package/src/Signal/Group/sender-key-name.js +47 -0
  36. package/src/Signal/Group/sender-key-record.d.ts +12 -0
  37. package/src/Signal/Group/sender-key-record.js +40 -0
  38. package/src/Signal/Group/sender-key-state.d.ts +16 -0
  39. package/src/Signal/Group/sender-key-state.js +83 -0
  40. package/src/Signal/Group/sender-message-key.d.ts +11 -0
  41. package/src/Signal/Group/sender-message-key.js +25 -0
  42. package/src/Signal/libsignal.d.ts +54 -0
  43. package/src/Signal/libsignal.js +427 -0
  44. package/src/Signal/lid-mapping.d.ts +20 -0
  45. package/src/Signal/lid-mapping.js +274 -0
  46. package/src/Socket/Client/index.d.ts +2 -0
  47. package/src/Socket/Client/index.js +3 -0
  48. package/src/Socket/Client/tcp.js +177 -0
  49. package/src/Socket/Client/types.d.ts +5 -0
  50. package/src/Socket/Client/types.js +9 -0
  51. package/src/Socket/Client/websocket.d.ts +12 -0
  52. package/src/Socket/Client/websocket.js +53 -0
  53. package/src/Socket/business.d.ts +431 -0
  54. package/src/Socket/business.js +379 -0
  55. package/src/Socket/chats.d.ts +247 -0
  56. package/src/Socket/chats.js +1484 -0
  57. package/src/Socket/communities.d.ts +598 -0
  58. package/src/Socket/communities.js +421 -0
  59. package/src/Socket/email.js +145 -0
  60. package/src/Socket/groups.d.ts +298 -0
  61. package/src/Socket/groups.js +339 -0
  62. package/src/Socket/index.d.ts +581 -0
  63. package/src/Socket/index.js +37 -0
  64. package/src/Socket/messages-recv.d.ts +370 -0
  65. package/src/Socket/messages-recv.js +1864 -0
  66. package/src/Socket/messages-send.d.ts +390 -0
  67. package/src/Socket/messages-send.js +1574 -0
  68. package/src/Socket/mex.d.ts +1 -0
  69. package/src/Socket/mex.js +44 -0
  70. package/src/Socket/mobile-builders.js +72 -0
  71. package/src/Socket/mobile.js +424 -0
  72. package/src/Socket/newsletter.d.ts +468 -0
  73. package/src/Socket/newsletter.js +671 -0
  74. package/src/Socket/socket.d.ts +77 -0
  75. package/src/Socket/socket.js +1106 -0
  76. package/src/Store/index.d.ts +3 -0
  77. package/src/Store/index.js +3 -0
  78. package/src/Store/make-in-memory-store.d.ts +62 -0
  79. package/src/Store/make-in-memory-store.js +396 -0
  80. package/src/Store/make-ordered-dictionary.d.ts +12 -0
  81. package/src/Store/make-ordered-dictionary.js +78 -0
  82. package/src/Store/object-repository.d.ts +10 -0
  83. package/src/Store/object-repository.js +23 -0
  84. package/src/Types/Auth.d.ts +1 -0
  85. package/src/Types/Auth.js +1 -0
  86. package/src/Types/Bussines.d.ts +1 -0
  87. package/src/Types/Bussines.js +1 -0
  88. package/src/Types/Call.d.ts +1 -0
  89. package/src/Types/Call.js +1 -0
  90. package/src/Types/Chat.d.ts +2 -0
  91. package/src/Types/Chat.js +24 -0
  92. package/src/Types/Contact.d.ts +1 -0
  93. package/src/Types/Contact.js +1 -0
  94. package/src/Types/Events.d.ts +1 -0
  95. package/src/Types/Events.js +0 -0
  96. package/src/Types/GroupMetadata.d.ts +5 -0
  97. package/src/Types/GroupMetadata.js +6 -0
  98. package/src/Types/Label.d.ts +2 -0
  99. package/src/Types/Label.js +24 -0
  100. package/src/Types/LabelAssociation.d.ts +2 -0
  101. package/src/Types/LabelAssociation.js +6 -0
  102. package/src/Types/Message.d.ts +13 -0
  103. package/src/Types/Message.js +23 -0
  104. package/src/Types/Mex.d.ts +2 -0
  105. package/src/Types/Mex.js +95 -0
  106. package/src/Types/Product.d.ts +1 -0
  107. package/src/Types/Product.js +1 -0
  108. package/src/Types/Signal.d.ts +1 -0
  109. package/src/Types/Signal.js +0 -0
  110. package/src/Types/Socket.d.ts +1 -0
  111. package/src/Types/Socket.js +0 -0
  112. package/src/Types/State.d.ts +5 -0
  113. package/src/Types/State.js +54 -0
  114. package/src/Types/USync.d.ts +1 -0
  115. package/src/Types/USync.js +0 -0
  116. package/src/Types/index.d.ts +13 -0
  117. package/src/Types/index.js +25 -0
  118. package/src/Utils/auth-store.d.ts +29 -0
  119. package/src/Utils/auth-store.js +76 -0
  120. package/src/Utils/auth-utils.d.ts +58 -0
  121. package/src/Utils/auth-utils.js +273 -0
  122. package/src/Utils/browser-utils.d.ts +9 -0
  123. package/src/Utils/browser-utils.js +28 -0
  124. package/src/Utils/business.d.ts +49 -0
  125. package/src/Utils/business.js +226 -0
  126. package/src/Utils/chat-utils.d.ts +408 -0
  127. package/src/Utils/chat-utils.js +846 -0
  128. package/src/Utils/companion-reg-client-utils.d.ts +4 -0
  129. package/src/Utils/companion-reg-client-utils.js +39 -0
  130. package/src/Utils/crypto.d.ts +39 -0
  131. package/src/Utils/crypto.js +110 -0
  132. package/src/Utils/decode-wa-message.d.ts +99 -0
  133. package/src/Utils/decode-wa-message.js +373 -0
  134. package/src/Utils/event-buffer.d.ts +22 -0
  135. package/src/Utils/event-buffer.js +624 -0
  136. package/src/Utils/generics.d.ts +63 -0
  137. package/src/Utils/generics.js +375 -0
  138. package/src/Utils/group-metadata-cache.d.ts +4 -0
  139. package/src/Utils/group-metadata-cache.js +25 -0
  140. package/src/Utils/history.d.ts +75 -0
  141. package/src/Utils/history.js +131 -0
  142. package/src/Utils/html-app.d.ts +19 -0
  143. package/src/Utils/html-app.js +96 -0
  144. package/src/Utils/html-multiplayer.d.ts +10 -0
  145. package/src/Utils/html-multiplayer.js +94 -0
  146. package/src/Utils/identity-change-handler.d.ts +13 -0
  147. package/src/Utils/identity-change-handler.js +48 -0
  148. package/src/Utils/index.d.ts +44 -0
  149. package/src/Utils/index.js +44 -0
  150. package/src/Utils/link-preview-metadata.d.ts +47 -0
  151. package/src/Utils/link-preview-metadata.js +88 -0
  152. package/src/Utils/link-preview.d.ts +12 -0
  153. package/src/Utils/link-preview.js +79 -0
  154. package/src/Utils/logger.d.ts +2 -0
  155. package/src/Utils/logger.js +2 -0
  156. package/src/Utils/lt-hash.d.ts +6 -0
  157. package/src/Utils/lt-hash.js +3 -0
  158. package/src/Utils/make-mutex.d.ts +6 -0
  159. package/src/Utils/make-mutex.js +32 -0
  160. package/src/Utils/message-retry-manager.d.ts +79 -0
  161. package/src/Utils/message-retry-manager.js +255 -0
  162. package/src/Utils/messages-media.d.ts +136 -0
  163. package/src/Utils/messages-media.js +814 -0
  164. package/src/Utils/messages.d.ts +62 -0
  165. package/src/Utils/messages.js +2223 -0
  166. package/src/Utils/mobile-companion-crypto.js +191 -0
  167. package/src/Utils/modern-messages.d.ts +150 -0
  168. package/src/Utils/modern-messages.js +496 -0
  169. package/src/Utils/native-flow.d.ts +11 -0
  170. package/src/Utils/native-flow.js +115 -0
  171. package/src/Utils/newsletter-status.d.ts +158 -0
  172. package/src/Utils/newsletter-status.js +651 -0
  173. package/src/Utils/noise-handler.d.ts +20 -0
  174. package/src/Utils/noise-handler.js +200 -0
  175. package/src/Utils/offline-node-processor.d.ts +9 -0
  176. package/src/Utils/offline-node-processor.js +33 -0
  177. package/src/Utils/optional-media.d.ts +3 -0
  178. package/src/Utils/optional-media.js +45 -0
  179. package/src/Utils/payment-messages.d.ts +66 -0
  180. package/src/Utils/payment-messages.js +160 -0
  181. package/src/Utils/pre-key-manager.d.ts +25 -0
  182. package/src/Utils/pre-key-manager.js +95 -0
  183. package/src/Utils/process-message.d.ts +60 -0
  184. package/src/Utils/process-message.js +688 -0
  185. package/src/Utils/reporting-utils.d.ts +12 -0
  186. package/src/Utils/reporting-utils.js +256 -0
  187. package/src/Utils/scheduled-message.d.ts +51 -0
  188. package/src/Utils/scheduled-message.js +112 -0
  189. package/src/Utils/sender-key-memory.d.ts +19 -0
  190. package/src/Utils/sender-key-memory.js +51 -0
  191. package/src/Utils/signal.d.ts +82 -0
  192. package/src/Utils/signal.js +193 -0
  193. package/src/Utils/stanza-ack.d.ts +16 -0
  194. package/src/Utils/stanza-ack.js +30 -0
  195. package/src/Utils/status-stickers.d.ts +127 -0
  196. package/src/Utils/status-stickers.js +260 -0
  197. package/src/Utils/sync-action-utils.d.ts +2 -0
  198. package/src/Utils/sync-action-utils.js +44 -0
  199. package/src/Utils/tc-token-utils.d.ts +29 -0
  200. package/src/Utils/tc-token-utils.js +155 -0
  201. package/src/Utils/ttl-cache.d.ts +28 -0
  202. package/src/Utils/ttl-cache.js +120 -0
  203. package/src/Utils/use-mongo-auth-state.d.ts +21 -0
  204. package/src/Utils/use-mongo-auth-state.js +67 -0
  205. package/src/Utils/use-multi-file-auth-state.d.ts +11 -0
  206. package/src/Utils/use-multi-file-auth-state.js +106 -0
  207. package/src/Utils/use-mysql-auth-state.d.ts +19 -0
  208. package/src/Utils/use-mysql-auth-state.js +79 -0
  209. package/src/Utils/use-postgres-auth-state.d.ts +19 -0
  210. package/src/Utils/use-postgres-auth-state.js +79 -0
  211. package/src/Utils/use-redis-auth-state.d.ts +19 -0
  212. package/src/Utils/use-redis-auth-state.js +89 -0
  213. package/src/Utils/use-single-file-auth-state.d.ts +10 -0
  214. package/src/Utils/use-single-file-auth-state.js +108 -0
  215. package/src/Utils/use-sqlite-auth-state.d.ts +12 -0
  216. package/src/Utils/use-sqlite-auth-state.js +120 -0
  217. package/src/Utils/useNekoDBAuth.d.ts +19 -0
  218. package/src/Utils/useNekoDBAuth.js +111 -0
  219. package/src/Utils/username.d.ts +12 -0
  220. package/src/Utils/username.js +58 -0
  221. package/src/Utils/validate-connection.d.ts +43 -0
  222. package/src/Utils/validate-connection.js +277 -0
  223. package/src/Utils/voice-recognition.d.ts +53 -0
  224. package/src/Utils/voice-recognition.js +219 -0
  225. package/src/Voip/audio-feeder.d.ts +24 -0
  226. package/src/Voip/audio-feeder.js +268 -0
  227. package/src/Voip/index.d.ts +88 -0
  228. package/src/Voip/index.js +936 -0
  229. package/src/Voip/relay-transport.d.ts +5 -0
  230. package/src/Voip/relay-transport.js +824 -0
  231. package/src/Voip/signaling.d.ts +11 -0
  232. package/src/Voip/signaling.js +1099 -0
  233. package/src/Voip/types.d.ts +2 -0
  234. package/src/Voip/types.js +12 -0
  235. package/src/Voip/video-feeder.d.ts +24 -0
  236. package/src/Voip/video-feeder.js +268 -0
  237. package/src/Voip/wasm-engine.d.ts +26 -0
  238. package/src/Voip/wasm-engine.js +1307 -0
  239. package/src/Voip/worker-bootstrap.js +1023 -0
  240. package/src/WABinary/constants.d.ts +22 -0
  241. package/src/WABinary/constants.js +1300 -0
  242. package/src/WABinary/decode.d.ts +9 -0
  243. package/src/WABinary/decode.js +261 -0
  244. package/src/WABinary/encode.d.ts +2 -0
  245. package/src/WABinary/encode.js +219 -0
  246. package/src/WABinary/generic-utils.d.ts +76 -0
  247. package/src/WABinary/generic-utils.js +214 -0
  248. package/src/WABinary/index.d.ts +5 -0
  249. package/src/WABinary/index.js +5 -0
  250. package/src/WABinary/jid-utils.d.ts +30 -0
  251. package/src/WABinary/jid-utils.js +97 -0
  252. package/src/WABinary/types.d.ts +1 -0
  253. package/src/WABinary/types.js +0 -0
  254. package/src/WAM/BinaryInfo.d.ts +7 -0
  255. package/src/WAM/BinaryInfo.js +9 -0
  256. package/src/WAM/constants.d.ts +34926 -0
  257. package/src/WAM/constants.js +22852 -0
  258. package/src/WAM/encode.d.ts +1 -0
  259. package/src/WAM/encode.js +148 -0
  260. package/src/WAM/index.d.ts +3 -0
  261. package/src/WAM/index.js +3 -0
  262. package/src/WAUSync/Protocols/USyncContactProtocol.d.ts +36 -0
  263. package/src/WAUSync/Protocols/USyncContactProtocol.js +50 -0
  264. package/src/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
  265. package/src/WAUSync/Protocols/USyncDeviceProtocol.js +51 -0
  266. package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
  267. package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
  268. package/src/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
  269. package/src/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
  270. package/src/WAUSync/Protocols/USyncUsernameProtocol.d.ts +9 -0
  271. package/src/WAUSync/Protocols/USyncUsernameProtocol.js +23 -0
  272. package/src/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +38 -0
  273. package/src/WAUSync/Protocols/UsyncBotProfileProtocol.js +49 -0
  274. package/src/WAUSync/Protocols/UsyncLIDProtocol.d.ts +14 -0
  275. package/src/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
  276. package/src/WAUSync/Protocols/index.d.ts +5 -0
  277. package/src/WAUSync/Protocols/index.js +5 -0
  278. package/src/WAUSync/USyncQuery.d.ts +20 -0
  279. package/src/WAUSync/USyncQuery.js +93 -0
  280. package/src/WAUSync/USyncUser.d.ts +16 -0
  281. package/src/WAUSync/USyncUser.js +30 -0
  282. package/src/WAUSync/index.d.ts +3 -0
  283. package/src/WAUSync/index.js +3 -0
  284. package/src/assets/wasm/README.md +76 -0
  285. package/src/assets/wasm/loader.js +5 -0
  286. package/src/assets/wasm/whatsapp.wasm +0 -0
  287. package/src/assets/wasm/worker-modules.js +273 -0
  288. package/src/index.d.ts +43 -0
  289. package/src/index.js +48 -0
@@ -0,0 +1,3 @@
1
+ export * from "./make-in-memory-store.js";
2
+ export * from "./make-ordered-dictionary.js";
3
+ export * from "./object-repository.js";
@@ -0,0 +1,3 @@
1
+ export * from './make-in-memory-store.js';
2
+ export * from './make-ordered-dictionary.js';
3
+ export * from './object-repository.js';
@@ -0,0 +1,62 @@
1
+ export function waChatKey(pin: any): {
2
+ key: (c: any) => string;
3
+ compare: (k1: any, k2: any) => any;
4
+ };
5
+ export function waMessageID(m: any): any;
6
+ export namespace waLabelAssociationKey {
7
+ function key(la: any): any;
8
+ function compare(k1: any, k2: any): any;
9
+ }
10
+ export function makeInMemoryStore(config?: {}): {
11
+ chats: any;
12
+ contacts: {};
13
+ messages: {};
14
+ groupMetadata: {};
15
+ state: {
16
+ connection: string;
17
+ };
18
+ presences: {};
19
+ labels: ObjectRepository;
20
+ labelAssociations: any;
21
+ bind: (ev: any) => void;
22
+ /** loads messages from the store, if not found -- uses the legacy connection */
23
+ loadMessages: (jid: any, count: any, cursor: any) => Promise<any>;
24
+ /**
25
+ * Get all available labels for profile
26
+ *
27
+ * Keep in mind that the list is formed from predefined tags and tags
28
+ * that were "caught" during their editing.
29
+ */
30
+ getLabels: () => ObjectRepository;
31
+ /**
32
+ * Get labels for chat
33
+ *
34
+ * @returns Label IDs
35
+ **/
36
+ getChatLabels: (chatId: any) => any;
37
+ /**
38
+ * Get labels for message
39
+ *
40
+ * @returns Label IDs
41
+ **/
42
+ getMessageLabels: (messageId: any) => any;
43
+ loadMessage: (jid: any, id: any) => Promise<any>;
44
+ mostRecentMessage: (jid: any) => Promise<any>;
45
+ fetchImageUrl: (jid: any, sock: any) => Promise<any>;
46
+ fetchGroupMetadata: (jid: any, sock: any) => Promise<any>;
47
+ fetchMessageReceipts: ({ remoteJid, id }: {
48
+ remoteJid: any;
49
+ id: any;
50
+ }) => Promise<any>;
51
+ toJSON: () => {
52
+ chats: any;
53
+ contacts: {};
54
+ messages: {};
55
+ labels: ObjectRepository;
56
+ labelAssociations: any;
57
+ };
58
+ fromJSON: (json: any) => void;
59
+ writeToFile: (path: any) => void;
60
+ readFromFile: (path: any) => void;
61
+ };
62
+ import { ObjectRepository } from './object-repository.js';
@@ -0,0 +1,396 @@
1
+ import keyedDB from '@adiwajshing/keyed-db';
2
+ import { writeFileSync, readFileSync, existsSync } from 'fs';
3
+ import { WAProto } from '../Types/index.js';
4
+ import { LabelAssociationType } from '../Types/LabelAssociation.js';
5
+ import { DEFAULT_CONNECTION_CONFIG } from '../Defaults/index.js';
6
+ import { md5, toNumber, updateMessageWithReceipt, updateMessageWithReaction } from '../Utils/index.js';
7
+ import { jidDecode, jidNormalizedUser } from '../WABinary/index.js';
8
+ import { makeOrderedDictionary } from './make-ordered-dictionary.js';
9
+ import { ObjectRepository } from './object-repository.js';
10
+ export const waChatKey = (pin) => ({
11
+ key: (c) => (pin ? (c.pinned ? '1' : '0') : '') + (c.archived ? '0' : '1') + (c.conversationTimestamp ? c.conversationTimestamp.toString(16).padStart(8, '0') : '') + c.id,
12
+ compare: (k1, k2) => k2.localeCompare(k1)
13
+ });
14
+ export const waMessageID = (m) => m.key.id || '';
15
+ export const waLabelAssociationKey = {
16
+ key: (la) => (la.type === LabelAssociationType.Chat ? la.chatId + la.labelId : la.chatId + la.messageId + la.labelId),
17
+ compare: (k1, k2) => k2.localeCompare(k1)
18
+ };
19
+ const makeMessagesDictionary = () => makeOrderedDictionary(waMessageID);
20
+ export const makeInMemoryStore = (config = {}) => {
21
+ const socket = config.socket;
22
+ const chatKey = config.chatKey || waChatKey(true);
23
+ const labelAssociationKey = config.labelAssociationKey || waLabelAssociationKey;
24
+ const logger = config.logger || DEFAULT_CONNECTION_CONFIG.logger.child({ stream: 'in-mem-store' });
25
+ const KeyedDB = keyedDB?.default ?? keyedDB;
26
+ const chats = new KeyedDB(chatKey, c => c.id);
27
+ const messages = {};
28
+ const contacts = {};
29
+ const groupMetadata = {};
30
+ const presences = {};
31
+ const state = { connection: 'close' };
32
+ const labels = new ObjectRepository();
33
+ const labelAssociations = new KeyedDB(labelAssociationKey, labelAssociationKey.key);
34
+ const assertMessageList = (jid) => {
35
+ if (!messages[jid]) {
36
+ messages[jid] = makeMessagesDictionary();
37
+ }
38
+ return messages[jid];
39
+ };
40
+ const contactsUpsert = (newContacts) => {
41
+ const oldContacts = new Set(Object.keys(contacts));
42
+ for (const contact of newContacts) {
43
+ oldContacts.delete(contact.id);
44
+ contacts[contact.id] = Object.assign(contacts[contact.id] || {}, contact);
45
+ }
46
+ return oldContacts;
47
+ };
48
+ const labelsUpsert = (newLabels) => {
49
+ for (const label of newLabels) {
50
+ labels.upsertById(label.id, label);
51
+ }
52
+ };
53
+
54
+ const bind = (ev) => {
55
+ ev.on('connection.update', update => {
56
+ Object.assign(state, update);
57
+ });
58
+ ev.on('messaging-history.set', ({ chats: newChats, contacts: newContacts, messages: newMessages, isLatest, syncType }) => {
59
+ if (syncType === WAProto.HistorySync.HistorySyncType.ON_DEMAND) {
60
+ return;
61
+
62
+ }
63
+ if (isLatest) {
64
+ chats.clear();
65
+ for (const id in messages) {
66
+ delete messages[id];
67
+ }
68
+ }
69
+ const chatsAdded = chats.insertIfAbsent(...newChats).length;
70
+ logger.debug({ chatsAdded }, 'synced chats');
71
+ const oldContacts = contactsUpsert(newContacts);
72
+ if (isLatest) {
73
+ for (const jid of oldContacts) {
74
+ delete contacts[jid];
75
+ }
76
+ }
77
+ logger.debug({ deletedContacts: isLatest ? oldContacts.size : 0, newContacts }, 'synced contacts');
78
+ for (const msg of newMessages) {
79
+ const jid = msg.key.remoteJidAlt || msg.key.remoteJid;
80
+ const list = assertMessageList(jid);
81
+ list.upsert(msg, 'prepend');
82
+ }
83
+ logger.debug({ messages: newMessages.length }, 'synced messages');
84
+ });
85
+ ev.on('contacts.upsert', contacts => {
86
+ contactsUpsert(contacts);
87
+ });
88
+ ev.on('contacts.update', async (updates) => {
89
+ var _a;
90
+ for (const update of updates) {
91
+ let contact;
92
+ if (contacts[update.id]) {
93
+ contact = contacts[update.id];
94
+ }
95
+ else {
96
+ const contactHashes = await Promise.all(Object.keys(contacts).map(async (contactId) => {
97
+ const { user } = jidDecode(contactId);
98
+ return [contactId, (await md5(Buffer.from(user + 'WA_ADD_NOTIF', 'utf8'))).toString('base64').slice(0, 3)];
99
+ }));
100
+ contact = contacts[((_a = contactHashes.find(([, b]) => b === update.id)) === null || _a === void 0 ? void 0 : _a[0]) || ''];
101
+ }
102
+ if (contact) {
103
+ if (update.imgUrl === 'changed') {
104
+ contact.imgUrl = socket ? await (socket === null || socket === void 0 ? void 0 : socket.profilePictureUrl(contact.id)) : undefined;
105
+ }
106
+ else if (update.imgUrl === 'removed') {
107
+ delete contact.imgUrl;
108
+ }
109
+ }
110
+ else {
111
+ return logger.debug({ update }, 'got update for non-existant contact');
112
+ }
113
+ Object.assign(contacts[contact.id], contact);
114
+ }
115
+ });
116
+ ev.on('chats.upsert', newChats => {
117
+ chats.upsert(...newChats);
118
+ });
119
+ ev.on('chats.update', updates => {
120
+ for (let update of updates) {
121
+ const result = chats.update(update.id, chat => {
122
+ if (update.unreadCount > 0) {
123
+ update = { ...update };
124
+ update.unreadCount = (chat.unreadCount || 0) + update.unreadCount;
125
+ }
126
+ Object.assign(chat, update);
127
+ });
128
+ if (!result) {
129
+ logger.debug({ update }, 'got update for non-existant chat');
130
+ }
131
+ }
132
+ });
133
+ ev.on('labels.edit', (label) => {
134
+ if (label.deleted) {
135
+ return labels.deleteById(label.id);
136
+ }
137
+
138
+ if (labels.count() < 20) {
139
+ return labels.upsertById(label.id, label);
140
+ }
141
+ logger.error('Labels count exceed');
142
+ });
143
+ ev.on('labels.association', ({ type, association }) => {
144
+ switch (type) {
145
+ case 'add':
146
+ labelAssociations.upsert(association);
147
+ break;
148
+ case 'remove':
149
+ labelAssociations.delete(association);
150
+ break;
151
+ default:
152
+ console.error(`unknown operation type [${type}]`);
153
+ }
154
+ });
155
+ ev.on('presence.update', ({ id, presences: update }) => {
156
+ presences[id] = presences[id] || {};
157
+ Object.assign(presences[id], update);
158
+ });
159
+ ev.on('chats.delete', deletions => {
160
+ for (const item of deletions) {
161
+ if (chats.get(item)) {
162
+ chats.deleteById(item);
163
+ }
164
+ }
165
+ });
166
+ ev.on('messages.upsert', ({ messages: newMessages, type }) => {
167
+ switch (type) {
168
+ case 'append':
169
+ case 'notify':
170
+ for (const msg of newMessages) {
171
+ const jid = jidNormalizedUser(msg.key.remoteJidAlt || msg.key.remoteJid);
172
+ const list = assertMessageList(jid);
173
+ list.upsert(msg, 'append');
174
+ if (type === 'notify' && !chats.get(jid)) {
175
+ ev.emit('chats.upsert', [
176
+ {
177
+ id: jid,
178
+ conversationTimestamp: toNumber(msg.messageTimestamp),
179
+ unreadCount: 1
180
+ }
181
+ ]);
182
+ }
183
+ }
184
+ break;
185
+ }
186
+ });
187
+ ev.on('messages.update', updates => {
188
+ var _a;
189
+ for (const { update, key } of updates) {
190
+ const list = assertMessageList(jidNormalizedUser(key.remoteJid));
191
+ if (update === null || update === void 0 ? void 0 : update.status) {
192
+ const listStatus = (_a = list.get(key.id)) === null || _a === void 0 ? void 0 : _a.status;
193
+ if (listStatus && (update === null || update === void 0 ? void 0 : update.status) <= listStatus) {
194
+ logger.debug({ update, storedStatus: listStatus }, 'status stored newer then update');
195
+ delete update.status;
196
+ logger.debug({ update }, 'new update object');
197
+ }
198
+ }
199
+ const result = list.updateAssign(key.id, update);
200
+ if (!result) {
201
+ logger.debug({ update }, 'got update for non-existent message');
202
+ }
203
+ }
204
+ });
205
+ ev.on('messages.delete', item => {
206
+ if ('all' in item) {
207
+ const list = messages[item.jid];
208
+ list === null || list === void 0 ? void 0 : list.clear();
209
+ }
210
+ else {
211
+ const jid = item.keys[0].remoteJidAlt || item.keys[0].remoteJid;
212
+ const list = messages[jid];
213
+ if (list) {
214
+ const idSet = new Set(item.keys.map(k => k.id));
215
+ list.filter(m => !idSet.has(m.key.id));
216
+ }
217
+ }
218
+ });
219
+ ev.on('groups.update', updates => {
220
+ for (const update of updates) {
221
+ const id = update.id;
222
+ if (groupMetadata[id]) {
223
+ Object.assign(groupMetadata[id], update);
224
+ }
225
+ else {
226
+ logger.debug({ update }, 'got update for non-existant group metadata');
227
+ }
228
+ }
229
+ });
230
+ ev.on('group-participants.update', ({ id, participants, action }) => {
231
+ const metadata = groupMetadata[id];
232
+ if (metadata) {
233
+ switch (action) {
234
+ case 'add':
235
+ metadata.participants.push(...participants.map(participant => ({ id: participant.id, phoneNumber: participant.phoneNumber, admin: participant.admin })));
236
+ break;
237
+ case 'demote':
238
+ case 'promote':
239
+ for (const participant of metadata.participants) {
240
+ for (const participantData of participants) {
241
+ if (participantData.id === participant.id || participantData.phoneNumber === participant.phoneNumber) {
242
+ participant.admin = action === 'promote' && 'admin';
243
+ }
244
+ }
245
+ }
246
+ break;
247
+ case 'remove':
248
+ const removeSet = new Set();
249
+ for (const p of participants) {
250
+ if (p.id)
251
+ removeSet.add(p.id);
252
+ if (p.phoneNumber)
253
+ removeSet.add(p.phoneNumber);
254
+ }
255
+ metadata.participants = metadata.participants.reduce((acc, p) => {
256
+ if (!removeSet.has(p.id) && !removeSet.has(p.phoneNumber))
257
+ acc.push(p);
258
+ return acc;
259
+ }, []);
260
+ break;
261
+ }
262
+ }
263
+ });
264
+ ev.on('message-receipt.update', updates => {
265
+ for (const { key, receipt } of updates) {
266
+ const obj = messages[key.remoteJidAlt || key.remoteJid];
267
+ const msg = obj === null || obj === void 0 ? void 0 : obj.get(key.id);
268
+ if (msg) {
269
+ updateMessageWithReceipt(msg, receipt);
270
+ }
271
+ }
272
+ });
273
+ ev.on('messages.reaction', (reactions) => {
274
+ for (const { key, reaction } of reactions) {
275
+ const obj = messages[key.remoteJidAlt || key.remoteJid];
276
+ const msg = obj === null || obj === void 0 ? void 0 : obj.get(key.id);
277
+ if (msg) {
278
+ updateMessageWithReaction(msg, reaction);
279
+ }
280
+ }
281
+ });
282
+ };
283
+ const toJSON = () => ({
284
+ chats,
285
+ contacts,
286
+ messages,
287
+ labels,
288
+ labelAssociations
289
+ });
290
+ const fromJSON = (json) => {
291
+ chats.upsert(...json.chats);
292
+ labelAssociations.upsert(...json.labelAssociations || []);
293
+ contactsUpsert(Object.values(json.contacts));
294
+ labelsUpsert(Object.values(json.labels || {}));
295
+ for (const jid in json.messages) {
296
+ const list = assertMessageList(jid);
297
+ for (const msg of json.messages[jid]) {
298
+ list.upsert(WAProto.WebMessageInfo.fromObject(msg), 'append');
299
+ }
300
+ }
301
+ };
302
+ return {
303
+ chats,
304
+ contacts,
305
+ messages,
306
+ groupMetadata,
307
+ state,
308
+ presences,
309
+ labels,
310
+ labelAssociations,
311
+ bind,
312
+
313
+ loadMessages: async (jid, count, cursor) => {
314
+ const list = assertMessageList(jid);
315
+ const mode = !cursor || 'before' in cursor ? 'before' : 'after';
316
+ const cursorKey = !!cursor ? ('before' in cursor ? cursor.before : cursor.after) : undefined;
317
+ const cursorValue = cursorKey ? list.get(cursorKey.id) : undefined;
318
+ let messages;
319
+ if (list && mode === 'before' && (!cursorKey || cursorValue)) {
320
+ if (cursorValue) {
321
+ const msgIdx = list.array.findIndex(m => m.key.id === (cursorKey === null || cursorKey === void 0 ? void 0 : cursorKey.id));
322
+ messages = list.array.slice(0, msgIdx);
323
+ }
324
+ else {
325
+ messages = list.array;
326
+ }
327
+ const diff = count - messages.length;
328
+ if (diff < 0) {
329
+ messages = messages.slice(-count);
330
+ }
331
+ }
332
+ else {
333
+ messages = [];
334
+ }
335
+ return messages;
336
+ },
337
+
338
+ getLabels: () => {
339
+ return labels;
340
+ },
341
+
342
+ getChatLabels: (chatId) => {
343
+ return labelAssociations.filter((la) => la.chatId === chatId).all();
344
+ },
345
+
346
+ getMessageLabels: (messageId) => {
347
+ const associations = labelAssociations
348
+ .filter((la) => la.messageId === messageId)
349
+ .all();
350
+ return associations.map(({ labelId }) => labelId);
351
+ },
352
+ loadMessage: async (jid, id) => { var _a; return (_a = messages[jid]) === null || _a === void 0 ? void 0 : _a.get(id); },
353
+ mostRecentMessage: async (jid) => {
354
+ var _a;
355
+ const message = (_a = messages[jid]) === null || _a === void 0 ? void 0 : _a.array.slice(-1)[0];
356
+ return message;
357
+ },
358
+ fetchImageUrl: async (jid, sock) => {
359
+ const contact = contacts[jid];
360
+ if (!contact) {
361
+ return sock === null || sock === void 0 ? void 0 : sock.profilePictureUrl(jid);
362
+ }
363
+ if (typeof contact.imgUrl === 'undefined') {
364
+ contact.imgUrl = await (sock === null || sock === void 0 ? void 0 : sock.profilePictureUrl(jid));
365
+ }
366
+ return contact.imgUrl;
367
+ },
368
+ fetchGroupMetadata: async (jid, sock) => {
369
+ if (!groupMetadata[jid]) {
370
+ const metadata = await (sock === null || sock === void 0 ? void 0 : sock.groupMetadata(jid));
371
+ if (metadata) {
372
+ groupMetadata[jid] = metadata;
373
+ }
374
+ }
375
+ return groupMetadata[jid];
376
+ },
377
+ fetchMessageReceipts: async ({ remoteJid, id }) => {
378
+ const list = messages[remoteJid];
379
+ const msg = list === null || list === void 0 ? void 0 : list.get(id);
380
+ return msg === null || msg === void 0 ? void 0 : msg.userReceipt;
381
+ },
382
+ toJSON,
383
+ fromJSON,
384
+ writeToFile: (path) => {
385
+ writeFileSync(path, JSON.stringify(toJSON()));
386
+ },
387
+ readFromFile: (path) => {
388
+ if (existsSync(path)) {
389
+ logger.debug({ path }, 'reading from file');
390
+ const jsonStr = readFileSync(path, { encoding: 'utf-8' });
391
+ const json = JSON.parse(jsonStr);
392
+ fromJSON(json);
393
+ }
394
+ }
395
+ };
396
+ };
@@ -0,0 +1,12 @@
1
+ export function makeOrderedDictionary(idGetter: any): {
2
+ array: any[];
3
+ get: (id: any) => any;
4
+ upsert: (item: any, mode: any) => void;
5
+ update: (item: any) => boolean;
6
+ remove: (item: any) => boolean;
7
+ updateAssign: (id: any, update: any) => boolean;
8
+ clear: () => void;
9
+ filter: (contain: any) => void;
10
+ toJSON: () => any[];
11
+ fromJSON: (newItems: any) => void;
12
+ };
@@ -0,0 +1,78 @@
1
+ export function makeOrderedDictionary(idGetter) {
2
+ const array = [];
3
+ const dict = {};
4
+ const get = (id) => dict[id];
5
+ const update = (item) => {
6
+ const id = idGetter(item);
7
+ const idx = array.findIndex(i => idGetter(i) === id);
8
+ if (idx >= 0) {
9
+ array[idx] = item;
10
+ dict[id] = item;
11
+ }
12
+ return false;
13
+ };
14
+ const upsert = (item, mode) => {
15
+ const id = idGetter(item);
16
+ if (get(id)) {
17
+ update(item);
18
+ }
19
+ else {
20
+ if (mode === 'append') {
21
+ array.push(item);
22
+ }
23
+ else {
24
+ array.splice(0, 0, item);
25
+ }
26
+ dict[id] = item;
27
+ }
28
+ };
29
+ const remove = (item) => {
30
+ const id = idGetter(item);
31
+ const idx = array.findIndex(i => idGetter(i) === id);
32
+ if (idx >= 0) {
33
+ array.splice(idx, 1);
34
+ delete dict[id];
35
+ return true;
36
+ }
37
+ return false;
38
+ };
39
+ return {
40
+ array,
41
+ get,
42
+ upsert,
43
+ update,
44
+ remove,
45
+ updateAssign: (id, update) => {
46
+ const item = get(id);
47
+ if (item) {
48
+ Object.assign(item, update);
49
+ delete dict[id];
50
+ dict[idGetter(item)] = item;
51
+ return true;
52
+ }
53
+ return false;
54
+ },
55
+ clear: () => {
56
+ array.splice(0, array.length);
57
+ for (const key of Object.keys(dict)) {
58
+ delete dict[key];
59
+ }
60
+ },
61
+ filter: (contain) => {
62
+ let i = 0;
63
+ while (i < array.length) {
64
+ if (!contain(array[i])) {
65
+ delete dict[idGetter(array[i])];
66
+ array.splice(i, 1);
67
+ }
68
+ else {
69
+ i += 1;
70
+ }
71
+ }
72
+ },
73
+ toJSON: () => array,
74
+ fromJSON: (newItems) => {
75
+ array.splice(0, array.length, ...newItems);
76
+ }
77
+ };
78
+ }
@@ -0,0 +1,10 @@
1
+ export class ObjectRepository {
2
+ constructor(entities?: {});
3
+ entityMap: Map<string, any>;
4
+ findById(id: any): any;
5
+ findAll(): any[];
6
+ upsertById(id: any, entity: any): Map<string, any>;
7
+ deleteById(id: any): boolean;
8
+ count(): number;
9
+ toJSON(): any[];
10
+ }
@@ -0,0 +1,23 @@
1
+ export class ObjectRepository {
2
+ constructor(entities = {}) {
3
+ this.entityMap = new Map(Object.entries(entities));
4
+ }
5
+ findById(id) {
6
+ return this.entityMap.get(id);
7
+ }
8
+ findAll() {
9
+ return Array.from(this.entityMap.values());
10
+ }
11
+ upsertById(id, entity) {
12
+ return this.entityMap.set(id, { ...entity });
13
+ }
14
+ deleteById(id) {
15
+ return this.entityMap.delete(id);
16
+ }
17
+ count() {
18
+ return this.entityMap.size;
19
+ }
20
+ toJSON() {
21
+ return this.findAll();
22
+ }
23
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export const ALL_WA_PATCH_NAMES: string[];
2
+ export const SPAM_FLOWS: Readonly<Record<string, string>>;
@@ -0,0 +1,24 @@
1
+ export const ALL_WA_PATCH_NAMES = [
2
+ 'critical_block',
3
+ 'critical_unblock_low',
4
+ 'regular_high',
5
+ 'regular_low',
6
+ 'regular'
7
+ ];
8
+ export const SPAM_FLOWS = Object.freeze({
9
+ Block: 'block_dialog',
10
+ OverflowMenuBlock: 'overflow_menu_block',
11
+ OverflowMenuReport: 'overflow_menu_report',
12
+ MediaViewer: 'media_viewer',
13
+ MessageMenu: 'message_menu',
14
+ CommunityHome: 'community_home',
15
+ AccountInfoReport: 'account_info_report',
16
+ GroupInfoReport: 'group_info_report',
17
+ GroupSpamBannerReport: 'group_spam_banner_report',
18
+ OneToOneChatSpamBannerReport: '1_1_spam_banner_report',
19
+ BizSpamBannerBlock: 'biz_spam_banner_block',
20
+ ChatListBlock: 'chat_list_block',
21
+ NotificationBlock: 'notification_block',
22
+ StatusPostReport: 'status_post_report',
23
+ NewsletterInfoReport: 'newsletter_info_report'
24
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
File without changes
@@ -0,0 +1,5 @@
1
+ export declare enum GroupHistoryToggleMode {
2
+ DEFAULT = 0,
3
+ ON = 1,
4
+ OFF = 2
5
+ }
@@ -0,0 +1,6 @@
1
+ export var GroupHistoryToggleMode;
2
+ (function (GroupHistoryToggleMode) {
3
+ GroupHistoryToggleMode[GroupHistoryToggleMode["DEFAULT"] = 0] = "DEFAULT";
4
+ GroupHistoryToggleMode[GroupHistoryToggleMode["ON"] = 1] = "ON";
5
+ GroupHistoryToggleMode[GroupHistoryToggleMode["OFF"] = 2] = "OFF";
6
+ })(GroupHistoryToggleMode || (GroupHistoryToggleMode = {}));
@@ -0,0 +1,2 @@
1
+ /** WhatsApp has 20 predefined colors */
2
+ export const LabelColor: any;