@barz-dev/baileys 8.4.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 (200) hide show
  1. package/README.md +393 -0
  2. package/WAProto/fix-import.js +29 -0
  3. package/WAProto/index.js +169661 -0
  4. package/engine-requirements.js +11 -0
  5. package/lib/Defaults/baileys-version.json +3 -0
  6. package/lib/Defaults/index.d.ts +53 -0
  7. package/lib/Defaults/index.js +147 -0
  8. package/lib/Defaults/phonenumber-mcc.json +223 -0
  9. package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
  10. package/lib/Signal/Group/ciphertext-message.js +15 -0
  11. package/lib/Signal/Group/group-session-builder.d.ts +14 -0
  12. package/lib/Signal/Group/group-session-builder.js +64 -0
  13. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  14. package/lib/Signal/Group/group_cipher.js +96 -0
  15. package/lib/Signal/Group/index.d.ts +11 -0
  16. package/lib/Signal/Group/index.js +57 -0
  17. package/lib/Signal/Group/keyhelper.d.ts +10 -0
  18. package/lib/Signal/Group/keyhelper.js +55 -0
  19. package/lib/Signal/Group/queue-job.d.ts +1 -0
  20. package/lib/Signal/Group/queue-job.js +57 -0
  21. package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
  22. package/lib/Signal/Group/sender-chain-key.js +34 -0
  23. package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
  24. package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
  25. package/lib/Signal/Group/sender-key-message.d.ts +18 -0
  26. package/lib/Signal/Group/sender-key-message.js +69 -0
  27. package/lib/Signal/Group/sender-key-name.d.ts +17 -0
  28. package/lib/Signal/Group/sender-key-name.js +51 -0
  29. package/lib/Signal/Group/sender-key-record.d.ts +30 -0
  30. package/lib/Signal/Group/sender-key-record.js +53 -0
  31. package/lib/Signal/Group/sender-key-state.d.ts +38 -0
  32. package/lib/Signal/Group/sender-key-state.js +99 -0
  33. package/lib/Signal/Group/sender-message-key.d.ts +11 -0
  34. package/lib/Signal/Group/sender-message-key.js +29 -0
  35. package/lib/Signal/libsignal.d.ts +3 -0
  36. package/lib/Signal/libsignal.js +174 -0
  37. package/lib/Signal/p +1 -0
  38. package/lib/Socket/Client/abstract-socket-client.d.ts +17 -0
  39. package/lib/Socket/Client/abstract-socket-client.js +13 -0
  40. package/lib/Socket/Client/index.d.ts +3 -0
  41. package/lib/Socket/Client/index.js +19 -0
  42. package/lib/Socket/Client/mobile-socket-client.d.ts +13 -0
  43. package/lib/Socket/Client/mobile-socket-client.js +65 -0
  44. package/lib/Socket/Client/web-socket-client.d.ts +12 -0
  45. package/lib/Socket/Client/web-socket-client.js +62 -0
  46. package/lib/Socket/business.d.ts +171 -0
  47. package/lib/Socket/business.js +260 -0
  48. package/lib/Socket/chats.d.ts +267 -0
  49. package/lib/Socket/chats.js +987 -0
  50. package/lib/Socket/communities.d.ts +180 -0
  51. package/lib/Socket/communities.js +421 -0
  52. package/lib/Socket/dugong.d.ts +254 -0
  53. package/lib/Socket/dugong.js +484 -0
  54. package/lib/Socket/groups.d.ts +115 -0
  55. package/lib/Socket/groups.js +380 -0
  56. package/lib/Socket/index.d.ts +173 -0
  57. package/lib/Socket/index.js +11 -0
  58. package/lib/Socket/luxu.d.ts +266 -0
  59. package/lib/Socket/luxu.js +541 -0
  60. package/lib/Socket/messages-recv.d.ts +161 -0
  61. package/lib/Socket/messages-recv.js +1110 -0
  62. package/lib/Socket/messages-send.d.ts +183 -0
  63. package/lib/Socket/messages-send.js +1084 -0
  64. package/lib/Socket/newsletter.d.ts +134 -0
  65. package/lib/Socket/newsletter.js +441 -0
  66. package/lib/Socket/registration.d.ts +267 -0
  67. package/lib/Socket/registration.js +166 -0
  68. package/lib/Socket/socket.d.ts +44 -0
  69. package/lib/Socket/socket.js +736 -0
  70. package/lib/Socket/usync.d.ts +36 -0
  71. package/lib/Socket/usync.js +70 -0
  72. package/lib/Store/index.d.ts +3 -0
  73. package/lib/Store/index.js +10 -0
  74. package/lib/Store/make-cache-manager-store.d.ts +13 -0
  75. package/lib/Store/make-cache-manager-store.js +83 -0
  76. package/lib/Store/make-in-memory-store.d.ts +118 -0
  77. package/lib/Store/make-in-memory-store.js +427 -0
  78. package/lib/Store/make-ordered-dictionary.d.ts +13 -0
  79. package/lib/Store/make-ordered-dictionary.js +81 -0
  80. package/lib/Store/object-repository.d.ts +10 -0
  81. package/lib/Store/object-repository.js +27 -0
  82. package/lib/Types/Auth.d.ts +110 -0
  83. package/lib/Types/Auth.js +2 -0
  84. package/lib/Types/Call.d.ts +13 -0
  85. package/lib/Types/Call.js +2 -0
  86. package/lib/Types/Chat.d.ts +102 -0
  87. package/lib/Types/Chat.js +4 -0
  88. package/lib/Types/Contact.d.ts +19 -0
  89. package/lib/Types/Contact.js +2 -0
  90. package/lib/Types/Events.d.ts +157 -0
  91. package/lib/Types/Events.js +2 -0
  92. package/lib/Types/GroupMetadata.d.ts +55 -0
  93. package/lib/Types/GroupMetadata.js +2 -0
  94. package/lib/Types/Label.d.ts +35 -0
  95. package/lib/Types/Label.js +27 -0
  96. package/lib/Types/LabelAssociation.d.ts +29 -0
  97. package/lib/Types/LabelAssociation.js +9 -0
  98. package/lib/Types/Message.d.ts +273 -0
  99. package/lib/Types/Message.js +9 -0
  100. package/lib/Types/Newsletter.d.ts +103 -0
  101. package/lib/Types/Newsletter.js +38 -0
  102. package/lib/Types/Product.d.ts +78 -0
  103. package/lib/Types/Product.js +2 -0
  104. package/lib/Types/Signal.d.ts +57 -0
  105. package/lib/Types/Signal.js +2 -0
  106. package/lib/Types/Socket.d.ts +111 -0
  107. package/lib/Types/Socket.js +2 -0
  108. package/lib/Types/State.d.ts +27 -0
  109. package/lib/Types/State.js +2 -0
  110. package/lib/Types/USync.d.ts +25 -0
  111. package/lib/Types/USync.js +2 -0
  112. package/lib/Types/index.d.ts +57 -0
  113. package/lib/Types/index.js +42 -0
  114. package/lib/Utils/auth-utils.d.ts +18 -0
  115. package/lib/Utils/auth-utils.js +206 -0
  116. package/lib/Utils/baileys-event-stream.d.ts +16 -0
  117. package/lib/Utils/baileys-event-stream.js +63 -0
  118. package/lib/Utils/business.d.ts +22 -0
  119. package/lib/Utils/business.js +234 -0
  120. package/lib/Utils/chat-utils.d.ts +71 -0
  121. package/lib/Utils/chat-utils.js +729 -0
  122. package/lib/Utils/crypto.d.ts +41 -0
  123. package/lib/Utils/crypto.js +151 -0
  124. package/lib/Utils/decode-wa-message.d.ts +19 -0
  125. package/lib/Utils/decode-wa-message.js +198 -0
  126. package/lib/Utils/event-buffer.d.ts +35 -0
  127. package/lib/Utils/event-buffer.js +514 -0
  128. package/lib/Utils/generics.d.ts +92 -0
  129. package/lib/Utils/generics.js +498 -0
  130. package/lib/Utils/history.d.ts +15 -0
  131. package/lib/Utils/history.js +96 -0
  132. package/lib/Utils/index.d.ts +17 -0
  133. package/lib/Utils/index.js +33 -0
  134. package/lib/Utils/link-preview.d.ts +21 -0
  135. package/lib/Utils/link-preview.js +93 -0
  136. package/lib/Utils/logger.d.ts +4 -0
  137. package/lib/Utils/logger.js +7 -0
  138. package/lib/Utils/lt-hash.d.ts +12 -0
  139. package/lib/Utils/lt-hash.js +51 -0
  140. package/lib/Utils/make-mutex.d.ts +7 -0
  141. package/lib/Utils/make-mutex.js +43 -0
  142. package/lib/Utils/messages-media.d.ts +116 -0
  143. package/lib/Utils/messages-media.js +819 -0
  144. package/lib/Utils/messages.d.ts +77 -0
  145. package/lib/Utils/messages.js +827 -0
  146. package/lib/Utils/noise-handler.d.ts +21 -0
  147. package/lib/Utils/noise-handler.js +155 -0
  148. package/lib/Utils/process-message.d.ts +41 -0
  149. package/lib/Utils/process-message.js +321 -0
  150. package/lib/Utils/signal.d.ts +32 -0
  151. package/lib/Utils/signal.js +153 -0
  152. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  153. package/lib/Utils/use-multi-file-auth-state.js +119 -0
  154. package/lib/Utils/validate-connection.d.ts +11 -0
  155. package/lib/Utils/validate-connection.js +229 -0
  156. package/lib/WABinary/constants.d.ts +30 -0
  157. package/lib/WABinary/constants.js +40 -0
  158. package/lib/WABinary/decode.d.ts +7 -0
  159. package/lib/WABinary/decode.js +252 -0
  160. package/lib/WABinary/encode.d.ts +3 -0
  161. package/lib/WABinary/encode.js +265 -0
  162. package/lib/WABinary/generic-utils.d.ts +17 -0
  163. package/lib/WABinary/generic-utils.js +198 -0
  164. package/lib/WABinary/index.d.ts +5 -0
  165. package/lib/WABinary/index.js +21 -0
  166. package/lib/WABinary/jid-utils.d.ts +31 -0
  167. package/lib/WABinary/jid-utils.js +62 -0
  168. package/lib/WABinary/types.d.ts +18 -0
  169. package/lib/WABinary/types.js +2 -0
  170. package/lib/WAM/BinaryInfo.d.ts +17 -0
  171. package/lib/WAM/BinaryInfo.js +13 -0
  172. package/lib/WAM/constants.d.ts +38 -0
  173. package/lib/WAM/constants.js +15350 -0
  174. package/lib/WAM/encode.d.ts +3 -0
  175. package/lib/WAM/encode.js +155 -0
  176. package/lib/WAM/index.d.ts +3 -0
  177. package/lib/WAM/index.js +19 -0
  178. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +9 -0
  179. package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
  180. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
  181. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
  182. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
  183. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
  184. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
  185. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
  186. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +25 -0
  187. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
  188. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +8 -0
  189. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
  190. package/lib/WAUSync/Protocols/index.d.ts +4 -0
  191. package/lib/WAUSync/Protocols/index.js +20 -0
  192. package/lib/WAUSync/USyncQuery.d.ts +28 -0
  193. package/lib/WAUSync/USyncQuery.js +89 -0
  194. package/lib/WAUSync/USyncUser.d.ts +12 -0
  195. package/lib/WAUSync/USyncUser.js +26 -0
  196. package/lib/WAUSync/index.d.ts +3 -0
  197. package/lib/WAUSync/index.js +19 -0
  198. package/lib/index.d.ts +12 -0
  199. package/lib/index.js +39 -0
  200. package/package.json +104 -0
@@ -0,0 +1,180 @@
1
+ /// <reference types="node" />
2
+ import { proto } from '../../WAProto'
3
+ import { GroupMetadata, ParticipantAction, SocketConfig, NewsletterFetchedUpdate, NewsletterMetadata, NewsletterReactionMode, NewsletterViewRole, SocketConfig, WAMediaUpload } from '../Types'
4
+ import { BinaryNode } from '../WABinary'
5
+
6
+ export interface CommunityMetadata extends GroupMetadata {
7
+ isCommunity: boolean
8
+ isCommunityAnnounce?: boolean
9
+ linkedParent?: string
10
+ joinApprovalMode?: boolean
11
+ }
12
+
13
+ export interface LinkedGroup {
14
+ id?: string
15
+ subject: string
16
+ creation?: number
17
+ owner?: string
18
+ size?: number
19
+ }
20
+
21
+ export interface CommunityLinkedGroups {
22
+ communityJid: string
23
+ isCommunity: boolean
24
+ linkedGroups: LinkedGroup[]
25
+ }
26
+
27
+ export declare const makeCommunitiesSocket: (config: SocketConfig) => {
28
+ communityMetadata: (jid: string) => Promise<CommunityMetadata>
29
+ communityCreate: (subject: string, body?: string) => Promise<GroupMetadata | null>
30
+ communityCreateGroup: (subject: string, participants: string[], parentCommunityJid: string) => Promise<GroupMetadata | null>
31
+ communityLeave: (id: string) => Promise<void>
32
+ communityUpdateSubject: (jid: string, subject: string) => Promise<void>
33
+ communityLinkGroup: (groupJid: string, parentCommunityJid: string) => Promise<void>
34
+ communityUnlinkGroup: (groupJid: string, parentCommunityJid: string) => Promise<void>
35
+ communityFetchLinkedGroups: (jid: string) => Promise<CommunityLinkedGroups>
36
+ communityRequestParticipantsList: (jid: string) => Promise<{ [key: string]: string }[]>
37
+ communityRequestParticipantsUpdate: (jid: string, participants: string[], action: 'approve' | 'reject') => Promise<{ status: string, jid: string }[]>
38
+ communityParticipantsUpdate: (jid: string, participants: string[], action: ParticipantAction) => Promise<{ status: string, jid: string, content: BinaryNode }[]>
39
+ communityUpdateDescription: (jid: string, description?: string) => Promise<void>
40
+ communityInviteCode: (jid: string) => Promise<string | undefined>
41
+ communityRevokeInvite: (jid: string) => Promise<string | undefined>
42
+ communityAcceptInvite: (code: string) => Promise<string | undefined>
43
+ /**
44
+ * revoke a v4 invite for someone
45
+ * @param communityJid community jid
46
+ * @param invitedJid jid of person you invited
47
+ * @returns true if successful
48
+ */
49
+ communityRevokeInviteV4: (communityJid: string, invitedJid: string) => Promise<boolean>
50
+ /**
51
+ * accept a CommunityInviteMessage
52
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
53
+ * @param inviteMessage the message to accept
54
+ */
55
+ communityAcceptInviteV4: (key: string | proto.IMessageKey, inviteMessage: proto.Message.IGroupInviteMessage) => Promise<string>
56
+ communityGetInviteInfo: (code: string) => Promise<CommunityMetadata>
57
+ communityToggleEphemeral: (jid: string, ephemeralExpiration?: number) => Promise<void>
58
+ communitySettingUpdate: (jid: string, setting: 'locked' | 'announcement' | 'not_announcement' | 'unlocked') => Promise<void>
59
+ communityMemberAddMode: (jid: string, mode: 'admin_add' | 'all_member_add') => Promise<void>
60
+ communityJoinApprovalMode: (jid: string, mode: 'on' | 'off') => Promise<void>
61
+ communityFetchAllParticipating: () => Promise<{ [_: string]: CommunityMetadata }>
62
+ processingMutex: {
63
+ mutex<T>(code: () => T | Promise<T>): Promise<T>
64
+ }
65
+ subscribeNewsletterUpdates: (jid: string) => Promise<{
66
+ duration: string;
67
+ }>;
68
+ newsletterReactionMode: (jid: string, mode: NewsletterReactionMode) => Promise<void>;
69
+ newsletterUpdateDescription: (jid: string, description?: string) => Promise<void>;
70
+ newsletterUpdateName: (jid: string, name: string) => Promise<void>;
71
+ newsletterUpdatePicture: (jid: string, content: WAMediaUpload) => Promise<void>;
72
+ newsletterRemovePicture: (jid: string) => Promise<void>;
73
+ newsletterUnfollow: (jid: string) => Promise<void>;
74
+ newsletterFollow: (jid: string) => Promise<void>;
75
+ newsletterUnmute: (jid: string) => Promise<void>;
76
+ newsletterMute: (jid: string) => Promise<void>;
77
+ newsletterAction: (jid: string, type: 'follow' | 'unfollow' | 'mute' | 'unmute') => Promise<void>;
78
+ newsletterCreate: (name: string, description: string, reaction_codes: string) => Promise<NewsletterMetadata>;
79
+ newsletterMetadata: (type: 'invite' | 'jid', key: string, role?: NewsletterViewRole) => Promise<NewsletterMetadata>;
80
+ newsletterAdminCount: (jid: string) => Promise<number>;
81
+ /**user is Lid, not Jid */
82
+ newsletterChangeOwner: (jid: string, user: string) => Promise<void>;
83
+ /**user is Lid, not Jid */
84
+ newsletterDemote: (jid: string, user: string) => Promise<void>;
85
+ newsletterDelete: (jid: string) => Promise<void>;
86
+ /**if code wasn't passed, the reaction will be removed (if is reacted) */
87
+ newsletterReactMessage: (jid: string, serverId: string, code?: string) => Promise<void>;
88
+ newsletterFetchMessages: (type: 'invite' | 'jid', key: string, count: number, after?: number) => Promise<NewsletterFetchedUpdate[]>;
89
+ newsletterFetchUpdates: (jid: string, count: number, after?: number, since?: number) => Promise<NewsletterFetchedUpdate[]>;
90
+ groupMetadata: (jid: string) => Promise<import("../Types").GroupMetadata>;
91
+ groupCreate: (subject: string, participants: string[]) => Promise<import("../Types").GroupMetadata>;
92
+ groupLeave: (id: string) => Promise<void>;
93
+ groupUpdateSubject: (jid: string, subject: string) => Promise<void>;
94
+ groupRequestParticipantsList: (jid: string) => Promise<{
95
+ [key: string]: string;
96
+ }[]>;
97
+ groupRequestParticipantsUpdate: (jid: string, participants: string[], action: "reject" | "approve") => Promise<{
98
+ status: string;
99
+ jid: string;
100
+ }[]>;
101
+ groupParticipantsUpdate: (jid: string, participants: string[], action: import("../Types").ParticipantAction) => Promise<{
102
+ status: string;
103
+ jid: string;
104
+ content: BinaryNode;
105
+ }[]>;
106
+ groupUpdateDescription: (jid: string, description?: string | undefined) => Promise<void>;
107
+ groupInviteCode: (jid: string) => Promise<string | undefined>;
108
+ groupRevokeInvite: (jid: string) => Promise<string | undefined>;
109
+ groupAcceptInvite: (code: string) => Promise<string | undefined>;
110
+ groupAcceptInviteV4: (key: string | import("../Types").WAProto.IMessageKey, inviteMessage: import("../Types").WAProto.Message.IGroupInviteMessage) => Promise<string>;
111
+ groupGetInviteInfo: (code: string) => Promise<import("../Types").GroupMetadata>;
112
+ groupToggleEphemeral: (jid: string, ephemeralExpiration: number) => Promise<void>;
113
+ groupSettingUpdate: (jid: string, setting: "announcement" | "locked" | "not_announcement" | "unlocked") => Promise<void>;
114
+ groupMemberAddMode: (jid: string, mode: "all_member_add" | "admin_add") => Promise<void>;
115
+ groupJoinApprovalMode: (jid: string, mode: "on" | "off") => Promise<void>;
116
+ groupFetchAllParticipating: () => Promise<{
117
+ [_: string]: import("../Types").GroupMetadata;
118
+ }>;
119
+ fetchPrivacySettings: (force?: boolean) => Promise<{ [_: string]: string }>
120
+ upsertMessage: (msg: proto.IWebMessageInfo, type: import('../Types').MessageUpsertType) => Promise<void>
121
+ appPatch: (patchCreate: import('../Types').WAPatchCreate) => Promise<void>
122
+ sendPresenceUpdate: (type: import('../Types').WAPresence, toJid?: string) => Promise<void>
123
+ presenceSubscribe: (toJid: string, tcToken?: Buffer) => Promise<void>
124
+ profilePictureUrl: (jid: string, type?: 'image' | 'preview', timeoutMs?: number) => Promise<string | undefined>
125
+ onWhatsApp: (...jids: string[]) => Promise<{ jid: string, exists: unknown, lid: unknown }[] | undefined>
126
+ fetchBlocklist: () => Promise<string[]>
127
+ fetchStatus: (jid: string) => Promise<{ status: string | undefined, setAt: Date } | undefined>
128
+ updateProfilePicture: (jid: string, content: import('../Types').WAMediaUpload) => Promise<void>
129
+ removeProfilePicture: (jid: string) => Promise<void>
130
+ updateProfileStatus: (status: string) => Promise<void>
131
+ updateProfileName: (name: string) => Promise<void>
132
+ updateBlockStatus: (jid: string, action: 'block' | 'unblock') => Promise<void>
133
+ updateLastSeenPrivacy: (value: import('../Types').WAPrivacyValue) => Promise<void>
134
+ updateOnlinePrivacy: (value: import('../Types').WAPrivacyOnlineValue) => Promise<void>
135
+ updateProfilePicturePrivacy: (value: import('../Types').WAPrivacyValue) => Promise<void>
136
+ updateStatusPrivacy: (value: import('../Types').WAPrivacyValue) => Promise<void>
137
+ updateReadReceiptsPrivacy: (value: import('../Types').WAReadReceiptsValue) => Promise<void>
138
+ updateGroupsAddPrivacy: (value: import('../Types').WAPrivacyValue) => Promise<void>
139
+ updateDefaultDisappearingMode: (duration: number) => Promise<void>
140
+ getBusinessProfile: (jid: string) => Promise<void | import('../Types').WABusinessProfile>
141
+ resyncAppState: (collections: readonly ('critical_block' | 'critical_unblock_low' | 'regular_high' | 'regular_low' | 'regular')[], isInitialSync: boolean) => Promise<void>
142
+ chatModify: (mod: import('../Types').ChatModification, jid: string) => Promise<void>
143
+ cleanDirtyBits: (type: 'account_sync' | 'groups', fromTimestamp?: string | number) => Promise<void>
144
+ addChatLabel: (jid: string, labelId: string) => Promise<void>
145
+ removeChatLabel: (jid: string, labelId: string) => Promise<void>
146
+ addMessageLabel: (jid: string, messageId: string, labelId: string) => Promise<void>
147
+ removeMessageLabel: (jid: string, messageId: string, labelId: string) => Promise<void>
148
+ star: (jid: string, messages: { id: string, fromMe?: boolean }[], star: boolean) => Promise<void>
149
+ type: 'md'
150
+ ws: any
151
+ ev: import('../Types').BaileysEventEmitter & {
152
+ process(handler: (events: Partial<import('../Types').BaileysEventMap>) => void | Promise<void>): () => void
153
+ buffer(): void
154
+ createBufferedFunction<A extends any[], T>(work: (...args: A) => Promise<T>): (...args: A) => Promise<T>
155
+ flush(force?: boolean): boolean
156
+ isBuffering(): boolean
157
+ }
158
+ authState: {
159
+ creds: import('../Types').AuthenticationCreds
160
+ keys: import('../Types').SignalKeyStoreWithTransaction
161
+ }
162
+ signalRepository: import('../Types').SignalRepository
163
+ user: import('../Types').Contact | undefined
164
+ generateMessageTag: () => string
165
+ query: (node: BinaryNode, timeoutMs?: number) => Promise<BinaryNode>
166
+ waitForMessage: <T>(msgId: string, timeoutMs?: number) => Promise<T>
167
+ waitForSocketOpen: () => Promise<void>
168
+ sendRawMessage: (data: Uint8Array | Buffer) => Promise<void>
169
+ sendNode: (frame: BinaryNode) => Promise<void>
170
+ logout: (msg?: string) => Promise<void>
171
+ end: (error: Error | undefined) => void
172
+ onUnexpectedError: (err: Error | import('@hapi/boom').Boom, msg: string) => void
173
+ uploadPreKeys: (count?: number) => Promise<void>
174
+ uploadPreKeysToServerIfRequired: () => Promise<void>
175
+ requestPairingCode: (phoneNumber: string) => Promise<string>
176
+ waitForConnectionUpdate: (check: (u: Partial<import('../Types').ConnectionState>) => boolean | undefined, timeoutMs?: number) => Promise<void>
177
+ sendWAMBuffer: (wamBuffer: Buffer) => Promise<BinaryNode>
178
+ }
179
+
180
+ export declare const extractCommunityMetadata: (result: BinaryNode) => CommunityMetadata
@@ -0,0 +1,421 @@
1
+ const WAProto_1 = require('../../WAProto');
2
+ const Types_1 = require('../Types');
3
+ const Utils_1 = require('../Utils');
4
+ const WABinary_1 = require('../WABinary');
5
+ const newsletter_1 = require('./newsletter');
6
+
7
+ const makeCommunitiesSocket = (config) => {
8
+ const sock = (0, newsletter_1.makeNewsletterSocket)(config);
9
+ const { authState, ev, query, upsertMessage } = sock;
10
+
11
+ const communityQuery = async (jid, type, content) => query({
12
+ tag: 'iq',
13
+ attrs: {
14
+ type,
15
+ xmlns: 'w:g2',
16
+ to: jid
17
+ },
18
+ content
19
+ });
20
+
21
+ const communityMetadata = async (jid) => {
22
+ const result = await communityQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
23
+ return extractCommunityMetadata(result);
24
+ };
25
+
26
+ const communityFetchAllParticipating = async () => {
27
+ const result = await query({
28
+ tag: 'iq',
29
+ attrs: {
30
+ to: '@g.us',
31
+ xmlns: 'w:g2',
32
+ type: 'get'
33
+ },
34
+ content: [
35
+ {
36
+ tag: 'participating',
37
+ attrs: {},
38
+ content: [
39
+ { tag: 'participants', attrs: {} },
40
+ { tag: 'description', attrs: {} }
41
+ ]
42
+ }
43
+ ]
44
+ });
45
+ const data = {};
46
+ const communitiesChild = WABinary_1.getBinaryNodeChild(result, 'communities');
47
+ if (communitiesChild) {
48
+ const communities = WABinary_1.getBinaryNodeChildren(communitiesChild, 'community');
49
+ for (const communityNode of communities) {
50
+ const meta = extractCommunityMetadata({
51
+ tag: 'result',
52
+ attrs: {},
53
+ content: [communityNode]
54
+ });
55
+ data[meta.id] = meta;
56
+ }
57
+ }
58
+ sock.ev.emit('groups.update', Object.values(data));
59
+ return data;
60
+ };
61
+
62
+ async function parseGroupResult(node) {
63
+ Utils_1.logger.info({ node }, 'parseGroupResult');
64
+ const groupNode = WABinary_1.getBinaryNodeChild(node, 'group');
65
+ if (groupNode) {
66
+ try {
67
+ Utils_1.logger.info({ groupNode }, 'groupNode');
68
+ const metadata = await sock.groupMetadata(`${groupNode.attrs.id}@g.us`);
69
+ return metadata? metadata : Optional.empty();
70
+ }
71
+ catch (error) {
72
+ console.error('Error parsing group metadata:', error);
73
+ return Optional.empty();
74
+ }
75
+ }
76
+ return Optional.empty();
77
+ }
78
+
79
+ const Optional = {
80
+ empty: () => null,
81
+ of: (value) => (value!== null? { value } : null)
82
+ };
83
+
84
+ sock.ws.on('CB:ib,,dirty', async (node) => {
85
+ const { attrs } = WABinary_1.getBinaryNodeChild(node, 'dirty');
86
+ if (attrs.type!== 'communities') {
87
+ return;
88
+ }
89
+ await communityFetchAllParticipating();
90
+ await sock.cleanDirtyBits('groups');
91
+ });
92
+
93
+ return {
94
+ ...sock,
95
+ communityMetadata,
96
+ communityCreate: async (subject, body) => {
97
+ const descriptionId = Utils_1.generateMessageID().substring(0, 12);
98
+ const result = await communityQuery('@g.us', 'set', [
99
+ {
100
+ tag: 'create',
101
+ attrs: { subject },
102
+ content: [
103
+ {
104
+ tag: 'description',
105
+ attrs: { id: descriptionId },
106
+ content: [
107
+ {
108
+ tag: 'body',
109
+ attrs: {},
110
+ content: Buffer.from(body || '', 'utf-8')
111
+ }
112
+ ]
113
+ },
114
+ {
115
+ tag: 'parent',
116
+ attrs: { default_membership_approval_mode: 'request_required' }
117
+ },
118
+ {
119
+ tag: 'allow_non_admin_sub_group_creation',
120
+ attrs: {}
121
+ },
122
+ {
123
+ tag: 'create_general_chat',
124
+ attrs: {}
125
+ }
126
+ ]
127
+ }
128
+ ]);
129
+ return await parseGroupResult(result);
130
+ },
131
+ communityCreateGroup: async (subject, participants, parentCommunityJid) => {
132
+ const key = Utils_1.generateMessageIDV2();
133
+ const result = await communityQuery('@g.us', 'set', [
134
+ {
135
+ tag: 'create',
136
+ attrs: {
137
+ subject,
138
+ key
139
+ },
140
+ content: [
141
+ ...participants.map(jid => ({
142
+ tag: 'participant',
143
+ attrs: { jid }
144
+ })),
145
+ { tag: 'linked_parent', attrs: { jid: parentCommunityJid } }
146
+ ]
147
+ }
148
+ ]);
149
+ return await parseGroupResult(result);
150
+ },
151
+ communityLeave: async (id) => {
152
+ await communityQuery('@g.us', 'set', [
153
+ {
154
+ tag: 'leave',
155
+ attrs: {},
156
+ content: [{ tag: 'community', attrs: { id } }]
157
+ }
158
+ ]);
159
+ },
160
+ communityUpdateSubject: async (jid, subject) => {
161
+ await communityQuery(jid, 'set', [
162
+ {
163
+ tag: 'subject',
164
+ attrs: {},
165
+ content: Buffer.from(subject, 'utf-8')
166
+ }
167
+ ]);
168
+ },
169
+ communityLinkGroup: async (groupJid, parentCommunityJid) => {
170
+ await communityQuery(parentCommunityJid, 'set', [
171
+ {
172
+ tag: 'links',
173
+ attrs: {},
174
+ content: [
175
+ {
176
+ tag: 'link',
177
+ attrs: { link_type: 'sub_group' },
178
+ content: [{ tag: 'group', attrs: { jid: groupJid } }]
179
+ }
180
+ ]
181
+ }
182
+ ]);
183
+ },
184
+ communityUnlinkGroup: async (groupJid, parentCommunityJid) => {
185
+ await communityQuery(parentCommunityJid, 'set', [
186
+ {
187
+ tag: 'unlink',
188
+ attrs: { unlink_type: 'sub_group' },
189
+ content: [{ tag: 'group', attrs: { jid: groupJid } }]
190
+ }
191
+ ]);
192
+ },
193
+ communityFetchLinkedGroups: async (jid) => {
194
+ let communityJid = jid;
195
+ let isCommunity = false;
196
+ const metadata = await sock.groupMetadata(jid);
197
+ if (metadata.linkedParent) {
198
+ communityJid = metadata.linkedParent;
199
+ }
200
+ else {
201
+ isCommunity = true;
202
+ }
203
+ const result = await communityQuery(communityJid, 'get', [{ tag: 'sub_groups', attrs: {} }]);
204
+ const linkedGroupsData = [];
205
+ const subGroupsNode = WABinary_1.getBinaryNodeChild(result, 'sub_groups');
206
+ if (subGroupsNode) {
207
+ const groupNodes = WABinary_1.getBinaryNodeChildren(subGroupsNode, 'group');
208
+ for (const groupNode of groupNodes) {
209
+ linkedGroupsData.push({
210
+ id: groupNode.attrs.id? WABinary_1.jidEncode(groupNode.attrs.id, 'g.us') : undefined,
211
+ subject: groupNode.attrs.subject || '',
212
+ creation: groupNode.attrs.creation? Number(groupNode.attrs.creation) : undefined,
213
+ owner: groupNode.attrs.creator? WABinary_1.jidNormalizedUser(groupNode.attrs.creator) : undefined,
214
+ size: groupNode.attrs.size? Number(groupNode.attrs.size) : undefined
215
+ });
216
+ }
217
+ }
218
+ return {
219
+ communityJid,
220
+ isCommunity,
221
+ linkedGroups: linkedGroupsData
222
+ };
223
+ },
224
+ communityRequestParticipantsList: async (jid) => {
225
+ const result = await communityQuery(jid, 'get', [
226
+ {
227
+ tag: 'membership_approval_requests',
228
+ attrs: {}
229
+ }
230
+ ]);
231
+ const node = WABinary_1.getBinaryNodeChild(result, 'membership_approval_requests');
232
+ const participants = WABinary_1.getBinaryNodeChildren(node, 'membership_approval_request');
233
+ return participants.map(v => v.attrs);
234
+ },
235
+ communityRequestParticipantsUpdate: async (jid, participants, action) => {
236
+ const result = await communityQuery(jid, 'set', [
237
+ {
238
+ tag: 'membership_requests_action',
239
+ attrs: {},
240
+ content: [
241
+ {
242
+ tag: action,
243
+ attrs: {},
244
+ content: participants.map(jid => ({
245
+ tag: 'participant',
246
+ attrs: { jid }
247
+ }))
248
+ }
249
+ ]
250
+ }
251
+ ]);
252
+ const node = WABinary_1.getBinaryNodeChild(result, 'membership_requests_action');
253
+ const nodeAction = WABinary_1.getBinaryNodeChild(node, action);
254
+ const participantsAffected = WABinary_1.getBinaryNodeChildren(nodeAction, 'participant');
255
+ return participantsAffected.map(p => {
256
+ return { status: p.attrs.error || '200', jid: p.attrs.jid };
257
+ });
258
+ },
259
+ communityParticipantsUpdate: async (jid, participants, action) => {
260
+ const result = await communityQuery(jid, 'set', [
261
+ {
262
+ tag: action,
263
+ attrs: action === 'remove'? { linked_groups: 'true' } : {},
264
+ content: participants.map(jid => ({
265
+ tag: 'participant',
266
+ attrs: { jid }
267
+ }))
268
+ }
269
+ ]);
270
+ const node = WABinary_1.getBinaryNodeChild(result, action);
271
+ const participantsAffected = WABinary_1.getBinaryNodeChildren(node, 'participant');
272
+ return participantsAffected.map(p => {
273
+ return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
274
+ });
275
+ },
276
+ communityUpdateDescription: async (jid, description) => {
277
+ const metadata = await communityMetadata(jid);
278
+ const prev = metadata.descId?? null;
279
+ await communityQuery(jid, 'set', [
280
+ {
281
+ tag: 'description',
282
+ attrs: {
283
+ ...(description? { id: Utils_1.generateMessageID() } : { delete: 'true' }),
284
+ ...(prev? { prev } : {})
285
+ },
286
+ content: description? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
287
+ }
288
+ ]);
289
+ },
290
+ communityInviteCode: async (jid) => {
291
+ const result = await communityQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
292
+ const inviteNode = WABinary_1.getBinaryNodeChild(result, 'invite');
293
+ return inviteNode?.attrs.code;
294
+ },
295
+ communityRevokeInvite: async (jid) => {
296
+ const result = await communityQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
297
+ const inviteNode = WABinary_1.getBinaryNodeChild(result, 'invite');
298
+ return inviteNode?.attrs.code;
299
+ },
300
+ communityAcceptInvite: async (code) => {
301
+ const results = await communityQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
302
+ const result = WABinary_1.getBinaryNodeChild(results, 'community');
303
+ return result?.attrs.jid;
304
+ },
305
+ communityRevokeInviteV4: async (communityJid, invitedJid) => {
306
+ const result = await communityQuery(communityJid, 'set', [
307
+ { tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }
308
+ ]);
309
+ return!!result;
310
+ },
311
+ communityAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
312
+ key = typeof key === 'string'? { remoteJid: key } : key;
313
+ const results = await communityQuery(inviteMessage.groupJid, 'set', [
314
+ {
315
+ tag: 'accept',
316
+ attrs: {
317
+ code: inviteMessage.inviteCode,
318
+ expiration: inviteMessage.inviteExpiration.toString(),
319
+ admin: key.remoteJid
320
+ }
321
+ }
322
+ ]);
323
+ if (key.id) {
324
+ inviteMessage = WAProto_1.proto.Message.GroupInviteMessage.fromObject(inviteMessage);
325
+ inviteMessage.inviteExpiration = 0;
326
+ inviteMessage.inviteCode = '';
327
+ ev.emit('messages.update', [
328
+ {
329
+ key,
330
+ update: {
331
+ message: {
332
+ groupInviteMessage: inviteMessage
333
+ }
334
+ }
335
+ }
336
+ ]);
337
+ }
338
+ await upsertMessage({
339
+ key: {
340
+ remoteJid: inviteMessage.groupJid,
341
+ id: Utils_1.generateMessageIDV2(sock.user?.id),
342
+ fromMe: false,
343
+ participant: key.remoteJid
344
+ },
345
+ messageStubType: Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD,
346
+ messageStubParameters: [JSON.stringify(authState.creds.me)],
347
+ participant: key.remoteJid,
348
+ messageTimestamp: Utils_1.unixTimestampSeconds()
349
+ }, 'notify');
350
+ return results.attrs.from;
351
+ }),
352
+ communityGetInviteInfo: async (code) => {
353
+ const results = await communityQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
354
+ return extractCommunityMetadata(results);
355
+ },
356
+ communityToggleEphemeral: async (jid, ephemeralExpiration) => {
357
+ const content = ephemeralExpiration
358
+ ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
359
+ : { tag: 'not_ephemeral', attrs: {} };
360
+ await communityQuery(jid, 'set', [content]);
361
+ },
362
+ communitySettingUpdate: async (jid, setting) => {
363
+ await communityQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
364
+ },
365
+ communityMemberAddMode: async (jid, mode) => {
366
+ await communityQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
367
+ },
368
+ communityJoinApprovalMode: async (jid, mode) => {
369
+ await communityQuery(jid, 'set', [
370
+ { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'community_join', attrs: { state: mode } }] }
371
+ ]);
372
+ },
373
+ communityFetchAllParticipating
374
+ };
375
+ };
376
+
377
+ const extractCommunityMetadata = (result) => {
378
+ const community = WABinary_1.getBinaryNodeChild(result, 'community');
379
+ const descChild = WABinary_1.getBinaryNodeChild(community, 'description');
380
+ let desc;
381
+ let descId;
382
+ if (descChild) {
383
+ desc = WABinary_1.getBinaryNodeChildString(descChild, 'body');
384
+ descId = descChild.attrs.id;
385
+ }
386
+ const communityId = community.attrs.id?.includes('@')
387
+ ? community.attrs.id
388
+ : WABinary_1.jidEncode(community.attrs.id || '', 'g.us');
389
+ const eph = WABinary_1.getBinaryNodeChild(community, 'ephemeral')?.attrs.expiration;
390
+ const memberAddMode = WABinary_1.getBinaryNodeChildString(community, 'member_add_mode') === 'all_member_add';
391
+ const metadata = {
392
+ id: communityId,
393
+ subject: community.attrs.subject || '',
394
+ subjectOwner: community.attrs.s_o,
395
+ subjectTime: Number(community.attrs.s_t || 0),
396
+ size: WABinary_1.getBinaryNodeChildren(community, 'participant').length,
397
+ creation: Number(community.attrs.creation || 0),
398
+ owner: community.attrs.creator? WABinary_1.jidNormalizedUser(community.attrs.creator) : undefined,
399
+ desc,
400
+ descId,
401
+ linkedParent: WABinary_1.getBinaryNodeChild(community, 'linked_parent')?.attrs.jid || undefined,
402
+ restrict:!!WABinary_1.getBinaryNodeChild(community, 'locked'),
403
+ announce:!!WABinary_1.getBinaryNodeChild(community, 'announcement'),
404
+ isCommunity:!!WABinary_1.getBinaryNodeChild(community, 'parent'),
405
+ isCommunityAnnounce:!!WABinary_1.getBinaryNodeChild(community, 'default_sub_community'),
406
+ joinApprovalMode:!!WABinary_1.getBinaryNodeChild(community, 'membership_approval_mode'),
407
+ memberAddMode,
408
+ participants: WABinary_1.getBinaryNodeChildren(community, 'participant').map(({ attrs }) => {
409
+ return {
410
+ id: attrs.jid,
411
+ admin: (attrs.type || null)
412
+ };
413
+ }),
414
+ ephemeralDuration: eph? +eph : undefined,
415
+ addressingMode: WABinary_1.getBinaryNodeChildString(community, 'addressing_mode')
416
+ };
417
+ return metadata;
418
+ };
419
+
420
+ exports.makeCommunitiesSocket = makeCommunitiesSocket;
421
+ exports.extractCommunityMetadata = extractCommunityMetadata;