@celestial-community/baileys 0.0.1-security → 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.

Potentially problematic release.


This version of @celestial-community/baileys might be problematic. Click here for more details.

Files changed (175) hide show
  1. package/WAProto/index.js +138933 -0
  2. package/engine-requirements.js +10 -0
  3. package/lib/Defaults/baileys-version.json +7 -0
  4. package/lib/Defaults/index.js +157 -0
  5. package/lib/Defaults/phonenumber-mcc.json +223 -0
  6. package/lib/Signal/Group/ciphertext-message.js +15 -0
  7. package/lib/Signal/Group/group-session-builder.js +94 -0
  8. package/lib/Signal/Group/group_cipher.js +118 -0
  9. package/lib/Signal/Group/index.js +138 -0
  10. package/lib/Signal/Group/keyhelper.js +75 -0
  11. package/lib/Signal/Group/queue-job.js +54 -0
  12. package/lib/Signal/Group/sender-chain-key.js +39 -0
  13. package/lib/Signal/Group/sender-key-distribution-message.js +71 -0
  14. package/lib/Signal/Group/sender-key-message.js +76 -0
  15. package/lib/Signal/Group/sender-key-name.js +50 -0
  16. package/lib/Signal/Group/sender-key-record.js +65 -0
  17. package/lib/Signal/Group/sender-key-state.js +114 -0
  18. package/lib/Signal/Group/sender-message-key.js +33 -0
  19. package/lib/Signal/libsignal.js +204 -0
  20. package/lib/Signal/lid-mapping.js +267 -0
  21. package/lib/Socket/Client/abstract-socket-client.js +13 -0
  22. package/lib/Socket/Client/index.js +32 -0
  23. package/lib/Socket/Client/mobile-socket-client.js +82 -0
  24. package/lib/Socket/Client/web-socket-client.js +89 -0
  25. package/lib/Socket/Denzy-ZeroDay.js +805 -0
  26. package/lib/Socket/business.js +279 -0
  27. package/lib/Socket/chats.js +1169 -0
  28. package/lib/Socket/community.d.ts +120 -0
  29. package/lib/Socket/community.js +388 -0
  30. package/lib/Socket/graphql.js +1122 -0
  31. package/lib/Socket/groups.js +345 -0
  32. package/lib/Socket/index.js +22 -0
  33. package/lib/Socket/interop.js +265 -0
  34. package/lib/Socket/lid-mapping.js +111 -0
  35. package/lib/Socket/messages-recv.js +1385 -0
  36. package/lib/Socket/messages-send.js +956 -0
  37. package/lib/Socket/mex.js +69 -0
  38. package/lib/Socket/newsletter.js +424 -0
  39. package/lib/Socket/privacy.js +229 -0
  40. package/lib/Socket/registration.js +190 -0
  41. package/lib/Socket/socket.js +783 -0
  42. package/lib/Socket/username.d.ts +185 -0
  43. package/lib/Socket/username.js +168 -0
  44. package/lib/Socket/usync.js +80 -0
  45. package/lib/Store/index.js +14 -0
  46. package/lib/Store/keyed-db.js +158 -0
  47. package/lib/Store/make-cache-manager-store.js +88 -0
  48. package/lib/Store/make-in-memory-store.js +484 -0
  49. package/lib/Store/make-ordered-dictionary.js +78 -0
  50. package/lib/Store/object-repository.js +27 -0
  51. package/lib/Types/Auth.js +2 -0
  52. package/lib/Types/Call.js +2 -0
  53. package/lib/Types/Chat.js +10 -0
  54. package/lib/Types/Contact.js +2 -0
  55. package/lib/Types/Events.js +2 -0
  56. package/lib/Types/GroupMetadata.js +2 -0
  57. package/lib/Types/Label.js +27 -0
  58. package/lib/Types/LabelAssociation.js +9 -0
  59. package/lib/Types/Message.js +14 -0
  60. package/lib/Types/MexUpdates.js +19 -0
  61. package/lib/Types/Newsletter.js +38 -0
  62. package/lib/Types/Product.js +2 -0
  63. package/lib/Types/RichType.js +30 -0
  64. package/lib/Types/Signal.js +2 -0
  65. package/lib/Types/Socket.js +2 -0
  66. package/lib/Types/State.js +2 -0
  67. package/lib/Types/USync.js +2 -0
  68. package/lib/Types/index.js +58 -0
  69. package/lib/Utils/anti-ban.js +339 -0
  70. package/lib/Utils/anti-delete.d.ts +158 -0
  71. package/lib/Utils/anti-delete.js +293 -0
  72. package/lib/Utils/antiban-presets.js +91 -0
  73. package/lib/Utils/antiban-profiles.js +43 -0
  74. package/lib/Utils/auth-utils.js +210 -0
  75. package/lib/Utils/baileys-event-stream.js +66 -0
  76. package/lib/Utils/browser-utils.js +52 -0
  77. package/lib/Utils/business.js +261 -0
  78. package/lib/Utils/chat-control.d.ts +113 -0
  79. package/lib/Utils/chat-control.js +230 -0
  80. package/lib/Utils/chat-utils.js +922 -0
  81. package/lib/Utils/companion-reg-client-utils.js +37 -0
  82. package/lib/Utils/contact-graph-warmer.js +208 -0
  83. package/lib/Utils/crypto.js +196 -0
  84. package/lib/Utils/decode-wa-message.js +208 -0
  85. package/lib/Utils/delivery-tracker.js +110 -0
  86. package/lib/Utils/device-fingerprint.js +135 -0
  87. package/lib/Utils/event-buffer.js +531 -0
  88. package/lib/Utils/generics.js +571 -0
  89. package/lib/Utils/group-operation-guard.js +147 -0
  90. package/lib/Utils/health-monitor.js +207 -0
  91. package/lib/Utils/history.js +122 -0
  92. package/lib/Utils/human-entropy.js +226 -0
  93. package/lib/Utils/identity-change-handler.js +72 -0
  94. package/lib/Utils/index.js +83 -0
  95. package/lib/Utils/interactive-message.d.ts +139 -0
  96. package/lib/Utils/interactive-message.js +273 -0
  97. package/lib/Utils/legitimacy-signal-injector.js +180 -0
  98. package/lib/Utils/link-preview.js +106 -0
  99. package/lib/Utils/logger.d.ts +13 -0
  100. package/lib/Utils/logger.js +9 -0
  101. package/lib/Utils/lt-hash.js +54 -0
  102. package/lib/Utils/make-mutex.js +41 -0
  103. package/lib/Utils/message-composer.d.ts +106 -0
  104. package/lib/Utils/message-composer.js +721 -0
  105. package/lib/Utils/message-retry-manager.d.ts +88 -0
  106. package/lib/Utils/message-retry-manager.js +160 -0
  107. package/lib/Utils/message-search.d.ts +52 -0
  108. package/lib/Utils/message-search.js +226 -0
  109. package/lib/Utils/messages-media.js +899 -0
  110. package/lib/Utils/messages.js +2698 -0
  111. package/lib/Utils/noise-handler.js +175 -0
  112. package/lib/Utils/offline-node-processor.js +45 -0
  113. package/lib/Utils/pre-key-manager.js +131 -0
  114. package/lib/Utils/presence-choreographer.js +438 -0
  115. package/lib/Utils/process-message.js +423 -0
  116. package/lib/Utils/rate-limiter.js +232 -0
  117. package/lib/Utils/reconnect-throttle.js +157 -0
  118. package/lib/Utils/reply-ratio-guard.js +137 -0
  119. package/lib/Utils/reporting-utils.js +307 -0
  120. package/lib/Utils/retry-reason-tracker.js +140 -0
  121. package/lib/Utils/rich-message-utils.d.ts +48 -0
  122. package/lib/Utils/rich-message-utils.js +400 -0
  123. package/lib/Utils/rich-response-utils.js +461 -0
  124. package/lib/Utils/scheduling.d.ts +135 -0
  125. package/lib/Utils/scheduling.js +246 -0
  126. package/lib/Utils/session-fingerprint.js +226 -0
  127. package/lib/Utils/session-stability.js +224 -0
  128. package/lib/Utils/signal.js +188 -0
  129. package/lib/Utils/stanza-ack.js +38 -0
  130. package/lib/Utils/state-manager.js +91 -0
  131. package/lib/Utils/status-posting.d.ts +81 -0
  132. package/lib/Utils/status-posting.js +187 -0
  133. package/lib/Utils/stealth-connect.js +124 -0
  134. package/lib/Utils/sync-action-utils.js +55 -0
  135. package/lib/Utils/tc-token-utils.js +171 -0
  136. package/lib/Utils/templates.d.ts +54 -0
  137. package/lib/Utils/templates.js +244 -0
  138. package/lib/Utils/timelock-guard.js +162 -0
  139. package/lib/Utils/use-mongo-file-auth-state.d.ts +6 -0
  140. package/lib/Utils/use-mongo-file-auth-state.js +81 -0
  141. package/lib/Utils/use-multi-file-auth-state.js +125 -0
  142. package/lib/Utils/use-single-file-auth-state.d.ts +13 -0
  143. package/lib/Utils/use-single-file-auth-state.js +86 -0
  144. package/lib/Utils/use-sqlite-auth-state.js +187 -0
  145. package/lib/Utils/vCard.d.ts +71 -0
  146. package/lib/Utils/vCard.js +196 -0
  147. package/lib/Utils/validate-connection.js +253 -0
  148. package/lib/Utils/warmup.js +154 -0
  149. package/lib/WABinary/constants.js +1303 -0
  150. package/lib/WABinary/decode.js +270 -0
  151. package/lib/WABinary/encode.js +279 -0
  152. package/lib/WABinary/generic-utils.js +223 -0
  153. package/lib/WABinary/index.js +34 -0
  154. package/lib/WABinary/jid-utils.js +84 -0
  155. package/lib/WABinary/types.js +2 -0
  156. package/lib/WAM/BinaryInfo.js +13 -0
  157. package/lib/WAM/constants.js +15254 -0
  158. package/lib/WAM/encode.js +149 -0
  159. package/lib/WAM/index.js +32 -0
  160. package/lib/WAUSync/Protocols/USyncContactProtocol.js +37 -0
  161. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +71 -0
  162. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +32 -0
  163. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +49 -0
  164. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +23 -0
  165. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +58 -0
  166. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
  167. package/lib/WAUSync/Protocols/index.js +34 -0
  168. package/lib/WAUSync/USyncQuery.js +99 -0
  169. package/lib/WAUSync/USyncUser.js +26 -0
  170. package/lib/WAUSync/USyncUsernameProtocol.d.ts +9 -0
  171. package/lib/WAUSync/USyncUsernameProtocol.js +37 -0
  172. package/lib/WAUSync/index.js +32 -0
  173. package/lib/index.js +59 -0
  174. package/package.json +105 -5
  175. package/README.md +0 -5
@@ -0,0 +1,120 @@
1
+ import { proto } from '../../WAProto'
2
+ import { GroupMetadata, ParticipantAction, SocketConfig, ContactAction } from '../Types'
3
+ import { BinaryNode } from '../WABinary'
4
+
5
+ export declare const makeCommunitiesSocket: (config: SocketConfig) => {
6
+ communityQuery: (jid: string, type: string, content: BinaryNode) => Promise<BinaryNode>
7
+ communityMetadata: (jid: string) => Promise<GroupMetadata>
8
+ communityCreate: (subject: string, participants: string[]) => Promise<GroupMetadata>
9
+ communityLeave: (id: string) => Promise<void>
10
+ communityUpdateSubject: (jid: string, subject: string) => Promise<void>
11
+ communityRequestParticipantsList: (jid: string) => Promise<{
12
+ [key: string]: string
13
+ }[]>
14
+ communityRequestParticipantsUpdate: (jid: string, participants: string[], action: 'approve' | 'reject') => Promise<{
15
+ status: string
16
+ jid: string
17
+ }[]>
18
+ communityParticipantsUpdate: (jid: string, participants: string[], action: ParticipantAction) => Promise<{
19
+ status: string
20
+ jid: string
21
+ content: BinaryNode
22
+ }[]>
23
+ communityUpdateDescription: (jid: string, description?: string) => Promise<void>
24
+ communityInviteCode: (jid: string) => Promise<string | undefined>
25
+ communityRevokeInvite: (jid: string) => Promise<string | undefined>
26
+ communityAcceptInvite: (code: string) => Promise<string | undefined>
27
+
28
+ communityRevokeInviteV4: (communityJid: string, invitedJid: string) => Promise<boolean>
29
+
30
+ communityAcceptInviteV4: (key: string | proto.IMessageKey, inviteMessage: proto.Message.IGroupInviteMessage) => Promise<string>
31
+ communityGetInviteInfo: (code: string) => Promise<GroupMetadata>
32
+ communityToggleEphemeral: (jid: string, ephemeralExpiration: number) => Promise<void>
33
+ communitySettingUpdate: (jid: string, setting: 'announcement' | 'not_announcement' | 'locked' | 'unlocked') => Promise<void>
34
+ communityMemberAddMode: (jid: string, mode: 'admin_add' | 'all_member_add') => Promise<void>
35
+ communityJoinApprovalMode: (jid: string, mode: 'on' | 'off') => Promise<void>
36
+ communityFetchAllParticipating: () => Promise<{
37
+ [_: string]: GroupMetadata
38
+ }>
39
+ processingMutex: {
40
+ mutex<T>(code: () => T | Promise<T>): Promise<T>
41
+ }
42
+ fetchPrivacySettings: (force?: boolean) => Promise<{
43
+ [_: string]: string
44
+ }>
45
+ upsertMessage: (msg: proto.IWebMessageInfo, type: import("../Types").MessageUpsertType) => Promise<void>
46
+ appPatch: (patchCreate: import("../Types").WAPatchCreate) => Promise<void>
47
+ sendPresenceUpdate: (type: import("../Types").WAPresence, toJid?: string | undefined, options?: { simulateTyping?: boolean; typingDuration?: number }) => Promise<void>
48
+ presenceSubscribe: (toJid: string, tcToken?: Buffer | undefined) => Promise<void>
49
+ getLidUser: (jid: string) => Promise<{
50
+ lid: string
51
+ id: string
52
+ }[] | undefined>
53
+ fetchBlocklist: () => Promise<string[]>
54
+ fetchStatus: (...jids: string[]) => Promise<import("..").USyncQueryResultList[] | undefined>
55
+ fetchDisappearingDuration: (...jids: string[]) => Promise<import("..").USyncQueryResultList[] | undefined>
56
+ updateProfilePicture: (jid: string, content: import("../Types").WAMediaUpload) => Promise<void>
57
+ removeProfilePicture: (jid: string) => Promise<void>
58
+ updateProfileStatus: (status: string) => Promise<void>
59
+ updateProfileName: (name: string) => Promise<void>
60
+ updateBlockStatus: (jid: string, action: "block" | "unblock") => Promise<void>
61
+ updateCallPrivacy: (value: import("../Types").WAPrivacyCallValue) => Promise<void>
62
+ updateLastSeenPrivacy: (value: import("../Types").WAPrivacyValue) => Promise<void>
63
+ updateOnlinePrivacy: (value: import("../Types").WAPrivacyOnlineValue) => Promise<void>
64
+ updateProfilePicturePrivacy: (value: import("../Types").WAPrivacyValue) => Promise<void>
65
+ updateStatusPrivacy: (value: import("../Types").WAPrivacyValue) => Promise<void>
66
+ updateReadReceiptsPrivacy: (value: import("../Types").WAReadReceiptsValue) => Promise<void>
67
+ updatecommunitysAddPrivacy: (value: import("../Types").WAPrivacycommunityAddValue) => Promise<void>
68
+ updateDefaultDisappearingMode: (duration: number) => Promise<void>
69
+ updateDisableLinkPreviewsPrivacy: (isPreviewsDisabled: boolean) => Promise<void>
70
+ getBusinessProfile: (jid: string) => Promise<void | import("../Types").WABusinessProfile>
71
+ resyncAppState: (collections: readonly ("critical_block" | "critical_unblock_low" | "regular_high" | "regular_low" | "regular")[], isInitialSync: boolean) => Promise<void>
72
+ chatModify: (mod: import("../Types").ChatModification, jid: string) => Promise<void>
73
+ cleanDirtyBits: (type: "account_sync" | "communitys", fromTimestamp?: string | number | undefined) => Promise<void>
74
+ addLabel: (jid: string, labels: import("../Types/Label").LabelActionBody) => Promise<void>
75
+ addChatLabel: (jid: string, labelId: string) => Promise<void>
76
+ removeChatLabel: (jid: string, labelId: string) => Promise<void>
77
+ addMessageLabel: (jid: string, messageId: string, labelId: string) => Promise<void>
78
+ removeMessageLabel: (jid: string, messageId: string, labelId: string) => Promise<void>
79
+ clearMessage: (jid: string, key: import("../Types").WAProto.IMessageKey, timeStamp: number | import("long").Long) => Promise<void>
80
+ star: (jid: string, messages: {
81
+ id: string
82
+ fromMe?: boolean | undefined
83
+ }[], star: boolean) => Promise<void>
84
+ addOrEditQuickReply: (quickReply: import("../Types/Bussines").QuickReplyAction) => Promise<void>
85
+ removeQuickReply: (timestamp: string) => Promise<void>
86
+ addOrEditContact: (jid: string, contact: ContactAction) => Promise<void>
87
+ removeContact: (jid: string) => Promise<void>
88
+ executeUSyncQuery: (usyncQuery: import("..").USyncQuery) => Promise<import("..").USyncQueryResult | undefined>
89
+ type: "md"
90
+ ws: import("./Client").WebSocketClient
91
+ ev: import("../Types").BaileysEventEmitter & {
92
+ process(handler: (events: Partial<import("../Types").BaileysEventMap>) => void | Promise<void>): () => void
93
+ buffer(): void
94
+ createBufferedFunction<A extends any[], T_1>(work: (...args: A) => Promise<T_1>): (...args: A) => Promise<T_1>
95
+ flush(force?: boolean | undefined): boolean
96
+ isBuffering(): boolean
97
+ }
98
+ authState: {
99
+ creds: import("../Types").AuthenticationCreds
100
+ keys: import("../Types").SignalKeyStoreWithTransaction
101
+ }
102
+ signalRepository: import("../Types").SignalRepository
103
+ user: import("../Types").Contact | undefined
104
+ generateMessageTag: () => string
105
+ query: (node: BinaryNode, timeoutMs?: number | undefined) => Promise<BinaryNode>
106
+ waitForMessage: <T_2>(msgId: string, timeoutMs?: number | undefined) => Promise<T_2>
107
+ waitForSocketOpen: () => Promise<void>
108
+ sendRawMessage: (data: Uint8Array | Buffer) => Promise<void>
109
+ sendNode: (frame: BinaryNode) => Promise<void>
110
+ logout: (msg?: string | undefined) => Promise<void>
111
+ end: (error: Error | undefined) => void
112
+ onUnexpectedError: (err: Error | import("@hapi/boom").Boom<any>, msg: string) => void
113
+ uploadPreKeys: (count?: number) => Promise<void>
114
+ uploadPreKeysToServerIfRequired: () => Promise<void>
115
+ requestPairingCode: (phoneNumber: string, code?: string) => Promise<string>
116
+ waitForConnectionUpdate: (check: (u: Partial<import("../Types").ConnectionState>) => boolean | undefined, timeoutMs?: number | undefined) => Promise<void>
117
+ sendWAMBuffer: (wamBuffer: Buffer) => Promise<BinaryNode>
118
+ }
119
+
120
+ export declare const extractCommunityMetadata: (result: BinaryNode) => GroupMetadata
@@ -0,0 +1,388 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', { value: true })
3
+ exports.extractCommunityMetadata = exports.makeCommunitiesSocket = void 0
4
+
5
+ const WAProto_1 = require('../../WAProto')
6
+ const Types_1 = require('../Types')
7
+ const Utils_1 = require('../Utils')
8
+ const WABinary_1 = require('../WABinary')
9
+ const business_1 = require('./business')
10
+
11
+ const makeCommunitiesSocket = (config) => {
12
+ const sock = business_1.makeBusinessSocket(config)
13
+ const { authState, ev, query, upsertMessage } = sock
14
+
15
+ const communityQuery = async (jid, type, content) =>
16
+ query({
17
+ tag: 'iq',
18
+ attrs: {
19
+ type,
20
+ xmlns: 'w:g2',
21
+ to: jid
22
+ },
23
+ content
24
+ })
25
+
26
+ const communityMetadata = async (jid) => {
27
+ const result = await communityQuery(jid, 'get', [
28
+ { tag: 'query', attrs: { request: 'interactive' } }
29
+ ])
30
+ return extractCommunityMetadata(result)
31
+ }
32
+
33
+ const communityFetchAllParticipating = async () => {
34
+ const result = await query({
35
+ tag: 'iq',
36
+ attrs: {
37
+ to: '@g.us',
38
+ xmlns: 'w:g2',
39
+ type: 'get'
40
+ },
41
+ content: [
42
+ {
43
+ tag: 'participating',
44
+ attrs: {},
45
+ content: [
46
+ { tag: 'participants', attrs: {} },
47
+ { tag: 'description', attrs: {} }
48
+ ]
49
+ }
50
+ ]
51
+ })
52
+
53
+ const data = {}
54
+ const communitiesChild = WABinary_1.getBinaryNodeChild(result, 'communities')
55
+
56
+ if (communitiesChild) {
57
+ const communities = WABinary_1.getBinaryNodeChildren(communitiesChild, 'community')
58
+ for (const communityNode of communities) {
59
+ const meta = extractCommunityMetadata({
60
+ tag: 'result',
61
+ attrs: {},
62
+ content: [communityNode]
63
+ })
64
+ data[meta.id] = meta
65
+ }
66
+ }
67
+
68
+ sock.ev.emit('groups.update', Object.values(data))
69
+ return data
70
+ }
71
+
72
+ sock.ws.on('CB:ib,,dirty', async (node) => {
73
+ const { attrs } = WABinary_1.getBinaryNodeChild(node, 'dirty')
74
+
75
+ if (attrs.type !== 'communities') {
76
+ return
77
+ }
78
+
79
+ await communityFetchAllParticipating()
80
+ await sock.cleanDirtyBits('groups')
81
+ })
82
+
83
+ return {
84
+ ...sock,
85
+ communityQuery,
86
+ communityMetadata,
87
+ communityFetchAllParticipating,
88
+ communityCreate: async (subject, body) => {
89
+ const descriptionId = Utils_1.generateMessageID().substring(0, 12)
90
+
91
+ const result = await communityQuery('@g.us', 'set', [
92
+ {
93
+ tag: 'create',
94
+ attrs: { subject },
95
+ content: [
96
+ {
97
+ tag: 'description',
98
+ attrs: { id: descriptionId },
99
+ content: [
100
+ {
101
+ tag: 'body',
102
+ attrs: {},
103
+ content: Buffer.from(body || '', 'utf-8')
104
+ }
105
+ ]
106
+ },
107
+ {
108
+ tag: 'parent',
109
+ attrs: {
110
+ default_membership_approval_mode: 'request_required'
111
+ }
112
+ },
113
+ {
114
+ tag: 'allow_non_admin_sub_group_creation',
115
+ attrs: {}
116
+ },
117
+ {
118
+ tag: 'create_general_chat',
119
+ attrs: {}
120
+ }
121
+ ]
122
+ }
123
+ ])
124
+
125
+ return extractCommunityMetadata(result)
126
+ },
127
+ communityLeave: async (id) => {
128
+ await communityQuery('@g.us', 'set', [
129
+ {
130
+ tag: 'leave',
131
+ attrs: {},
132
+ content: [{ tag: 'community', attrs: { id } }]
133
+ }
134
+ ])
135
+ },
136
+ communityUpdateSubject: async (jid, subject) => {
137
+ await communityQuery(jid, 'set', [
138
+ {
139
+ tag: 'subject',
140
+ attrs: {},
141
+ content: Buffer.from(subject, 'utf-8')
142
+ }
143
+ ])
144
+ },
145
+ communityRequestParticipantsList: async (jid) => {
146
+ const result = await communityQuery(jid, 'get', [
147
+ {
148
+ tag: 'membership_approval_requests',
149
+ attrs: {}
150
+ }
151
+ ])
152
+
153
+ const node = WABinary_1.getBinaryNodeChild(result, 'membership_approval_requests')
154
+ const participants = WABinary_1.getBinaryNodeChildren(
155
+ node,
156
+ 'membership_approval_request'
157
+ )
158
+
159
+ return participants.map((v) => v.attrs)
160
+ },
161
+ communityRequestParticipantsUpdate: async (jid, participants, action) => {
162
+ const result = await communityQuery(jid, 'set', [
163
+ {
164
+ tag: 'membership_requests_action',
165
+ attrs: {},
166
+ content: [
167
+ {
168
+ tag: action,
169
+ attrs: {},
170
+ content: participants.map((jid) => ({
171
+ tag: 'participant',
172
+ attrs: { jid }
173
+ }))
174
+ }
175
+ ]
176
+ }
177
+ ])
178
+
179
+ const node = WABinary_1.getBinaryNodeChild(result, 'membership_requests_action')
180
+ const nodeAction = WABinary_1.getBinaryNodeChild(node, action)
181
+ const participantsAffected = WABinary_1.getBinaryNodeChildren(nodeAction, 'participant')
182
+
183
+ return participantsAffected.map((p) => ({
184
+ status: p.attrs.error || '200',
185
+ jid: p.attrs.jid
186
+ }))
187
+ },
188
+ communityParticipantsUpdate: async (jid, participants, action) => {
189
+ const result = await communityQuery(jid, 'set', [
190
+ {
191
+ tag: action,
192
+ attrs: {},
193
+ content: participants.map((jid) => ({
194
+ tag: 'participant',
195
+ attrs: { jid }
196
+ }))
197
+ }
198
+ ])
199
+ const node = WABinary_1.getBinaryNodeChild(result, action)
200
+ const participantsAffected = WABinary_1.getBinaryNodeChildren(node, 'participant')
201
+
202
+ return participantsAffected.map((p) => ({
203
+ status: p.attrs.error || '200',
204
+ jid: p.attrs.jid,
205
+ content: p
206
+ }))
207
+ },
208
+ communityUpdateDescription: async (jid, description) => {
209
+ const metadata = await communityMetadata(jid)
210
+ const prev = metadata.descId ? metadata.descId : null
211
+
212
+ await communityQuery(jid, 'set', [
213
+ {
214
+ tag: 'description',
215
+ attrs: {
216
+ ...(description ? { id: Utils_1.generateMessageID() } : { delete: 'true' }),
217
+ ...(prev ? { prev } : {})
218
+ },
219
+ content: description
220
+ ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }]
221
+ : undefined
222
+ }
223
+ ])
224
+ },
225
+ communityInviteCode: async (jid) => {
226
+ const result = await communityQuery(jid, 'get', [{ tag: 'invite', attrs: {} }])
227
+ const inviteNode = WABinary_1.getBinaryNodeChild(result, 'invite')
228
+ return inviteNode?.attrs?.code
229
+ },
230
+ communityRevokeInvite: async (jid) => {
231
+ const result = await communityQuery(jid, 'set', [{ tag: 'invite', attrs: {} }])
232
+ const inviteNode = WABinary_1.getBinaryNodeChild(result, 'invite')
233
+ return inviteNode?.attrs?.code
234
+ },
235
+ communityAcceptInvite: async (code) => {
236
+ const results = await communityQuery('@g.us', 'set', [
237
+ { tag: 'invite', attrs: { code } }
238
+ ])
239
+ const result = WABinary_1.getBinaryNodeChild(results, 'community')
240
+ return result?.attrs?.jid
241
+ },
242
+ communityRevokeInviteV4: async (communityJid, invitedJid) => {
243
+ const result = await communityQuery(communityJid, 'set', [
244
+ {
245
+ tag: 'revoke',
246
+ attrs: {},
247
+ content: [{ tag: 'participant', attrs: { jid: invitedJid } }]
248
+ }
249
+ ])
250
+ return !!result
251
+ },
252
+ communityAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
253
+ key = typeof key === 'string' ? { remoteJid: key } : key
254
+ const results = await communityQuery(inviteMessage.groupJid, 'set', [
255
+ {
256
+ tag: 'accept',
257
+ attrs: {
258
+ code: inviteMessage.inviteCode,
259
+ expiration: inviteMessage.inviteExpiration.toString(),
260
+ admin: key.remoteJid
261
+ }
262
+ }
263
+ ])
264
+
265
+ if (key.id) {
266
+ inviteMessage = WAProto_1.proto.Message.GroupInviteMessage.fromObject(inviteMessage)
267
+ inviteMessage.inviteExpiration = 0
268
+ inviteMessage.inviteCode = ''
269
+ ev.emit('messages.update', [
270
+ {
271
+ key,
272
+ update: {
273
+ message: {
274
+ groupInviteMessage: inviteMessage
275
+ }
276
+ }
277
+ }
278
+ ])
279
+ }
280
+
281
+ await upsertMessage(
282
+ {
283
+ key: {
284
+ remoteJid: inviteMessage.groupJid,
285
+ id: Utils_1.generateMessageID(authState.creds.me?.id),
286
+ fromMe: false,
287
+ participant: key.remoteJid
288
+ },
289
+ messageStubType: Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD,
290
+ messageStubParameters: [authState.creds.me.id],
291
+ participant: key.remoteJid,
292
+ messageTimestamp: Utils_1.unixTimestampSeconds()
293
+ },
294
+ 'notify'
295
+ )
296
+
297
+ return results.attrs.from
298
+ }),
299
+ communityGetInviteInfo: async (code) => {
300
+ const results = await communityQuery('@g.us', 'get', [
301
+ { tag: 'invite', attrs: { code } }
302
+ ])
303
+ return extractCommunityMetadata(results)
304
+ },
305
+ communityToggleEphemeral: async (jid, ephemeralExpiration) => {
306
+ const content = ephemeralExpiration
307
+ ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
308
+ : { tag: 'not_ephemeral', attrs: {} }
309
+ await communityQuery(jid, 'set', [content])
310
+ },
311
+ communitySettingUpdate: async (jid, setting) => {
312
+ await communityQuery(jid, 'set', [{ tag: setting, attrs: {} }])
313
+ },
314
+ communityMemberAddMode: async (jid, mode) => {
315
+ await communityQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }])
316
+ },
317
+ communityJoinApprovalMode: async (jid, mode) => {
318
+ await communityQuery(jid, 'set', [
319
+ {
320
+ tag: 'membership_approval_mode',
321
+ attrs: {},
322
+ content: [{ tag: 'community_join', attrs: { state: mode } }]
323
+ }
324
+ ])
325
+ }
326
+ }
327
+ }
328
+
329
+ const extractCommunityMetadata = (result) => {
330
+ const community = WABinary_1.getBinaryNodeChild(result, 'group')
331
+ const descChild = WABinary_1.getBinaryNodeChild(community, 'description')
332
+
333
+ let desc
334
+ let descId
335
+
336
+ if (descChild) {
337
+ desc = WABinary_1.getBinaryNodeChildString(descChild, 'body')
338
+ descId = descChild.attrs.id
339
+ }
340
+
341
+ const mode = community.attrs.addressing_mode
342
+ const communityId = community.attrs.id.includes('@')
343
+ ? community.attrs.id
344
+ : WABinary_1.jidEncode(community.attrs.id, 'g.us')
345
+ const eph = WABinary_1.getBinaryNodeChild(community, 'ephemeral')?.attrs.expiration
346
+ const memberAddMode =
347
+ WABinary_1.getBinaryNodeChildString(community, 'member_add_mode') === 'all_member_add'
348
+
349
+ return {
350
+ id: communityId,
351
+ addressingMode: mode,
352
+ subject: community.attrs.subject,
353
+ subjectOwner: mode === 'lid' ? community.attrs.s_o_pn : community.attrs.s_o,
354
+ subjectTime: +community.attrs.s_t,
355
+ size: community.attrs?.size
356
+ ? +community.attrs.size
357
+ : WABinary_1.getBinaryNodeChildren(community, 'participant').length,
358
+ creation: +community.attrs.creation,
359
+ owner: community.attrs.creator
360
+ ? WABinary_1.jidNormalizedUser(
361
+ mode === 'lid' ? community.attrs.creator_pn : community.attrs.creator
362
+ )
363
+ : undefined,
364
+ desc,
365
+ descId,
366
+ linkedParent:
367
+ WABinary_1.getBinaryNodeChild(community, 'linked_parent')?.attrs.jid || undefined,
368
+ restrict: !!WABinary_1.getBinaryNodeChild(community, 'locked'),
369
+ announce: !!WABinary_1.getBinaryNodeChild(community, 'announcement'),
370
+ isCommunity: !!WABinary_1.getBinaryNodeChild(community, 'parent'),
371
+ isCommunityAnnounce: !!WABinary_1.getBinaryNodeChild(community, 'default_sub_group'),
372
+ joinApprovalMode: !!WABinary_1.getBinaryNodeChild(community, 'membership_approval_mode'),
373
+ memberAddMode,
374
+ participants: WABinary_1.getBinaryNodeChildren(community, 'participant').map(
375
+ ({ attrs }) => ({
376
+ id: mode === 'lid' ? attrs.pn || attrs.jid : attrs.jid,
377
+ lid: mode === 'lid' ? attrs.jid : attrs.lid,
378
+ admin: attrs.type || null
379
+ })
380
+ ),
381
+ ephemeralDuration: eph ? +eph : undefined
382
+ }
383
+ }
384
+
385
+ module.exports = {
386
+ makeCommunitiesSocket,
387
+ extractCommunityMetadata
388
+ }