@mrlegendbot/baileys 1.2.4 → 1.2.6

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 (214) hide show
  1. package/WAProto/GenerateStatics.sh +3 -0
  2. package/WAProto/fix-imports.js +85 -0
  3. package/WAProto/index.d.ts +14017 -0
  4. package/WAProto/index.js +97691 -2
  5. package/lib/Defaults/index.d.ts +75 -0
  6. package/lib/Defaults/index.js +92 -89
  7. package/lib/Menu/index.d.ts +42 -0
  8. package/lib/Menu/index.js +283 -0
  9. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  10. package/lib/Signal/Group/ciphertext-message.js +10 -9
  11. package/lib/Signal/Group/group-session-builder.d.ts +15 -0
  12. package/lib/Signal/Group/group-session-builder.js +22 -21
  13. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  14. package/lib/Signal/Group/group_cipher.js +69 -75
  15. package/lib/Signal/Group/index.d.ts +12 -0
  16. package/lib/Signal/Group/index.js +1 -0
  17. package/lib/Signal/Group/keyhelper.d.ts +11 -0
  18. package/lib/Signal/Group/keyhelper.js +10 -9
  19. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  20. package/lib/Signal/Group/sender-chain-key.js +22 -21
  21. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  22. package/lib/Signal/Group/sender-key-distribution-message.js +60 -51
  23. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  24. package/lib/Signal/Group/sender-key-message.js +62 -56
  25. package/lib/Signal/Group/sender-key-name.d.ts +18 -0
  26. package/lib/Signal/Group/sender-key-name.js +40 -38
  27. package/lib/Signal/Group/sender-key-record.d.ts +31 -0
  28. package/lib/Signal/Group/sender-key-record.js +34 -33
  29. package/lib/Signal/Group/sender-key-state.d.ts +39 -0
  30. package/lib/Signal/Group/sender-key-state.js +76 -72
  31. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  32. package/lib/Signal/Group/sender-message-key.js +23 -22
  33. package/lib/Signal/libsignal.d.ts +5 -0
  34. package/lib/Signal/libsignal.js +405 -506
  35. package/lib/Signal/lid-mapping.d.ts +23 -0
  36. package/lib/Signal/lid-mapping.js +258 -276
  37. package/lib/Socket/Client/index.d.ts +3 -0
  38. package/lib/Socket/Client/index.js +1 -0
  39. package/lib/Socket/Client/types.d.ts +16 -0
  40. package/lib/Socket/Client/types.js +7 -6
  41. package/lib/Socket/Client/websocket.d.ts +13 -0
  42. package/lib/Socket/Client/websocket.js +49 -48
  43. package/lib/Socket/business.d.ts +217 -0
  44. package/lib/Socket/business.js +369 -349
  45. package/lib/Socket/chats.d.ts +125 -0
  46. package/lib/Socket/chats.js +1148 -1172
  47. package/lib/Socket/communities.d.ts +273 -0
  48. package/lib/Socket/communities.js +413 -473
  49. package/lib/Socket/groups.d.ts +161 -0
  50. package/lib/Socket/groups.js +331 -387
  51. package/lib/Socket/index.d.ts +267 -0
  52. package/lib/Socket/index.js +19 -6
  53. package/lib/Socket/messages-recv.d.ts +203 -0
  54. package/lib/Socket/messages-recv.js +1719 -1782
  55. package/lib/Socket/messages-send.d.ts +199 -0
  56. package/lib/Socket/messages-send.js +2451 -1504
  57. package/lib/Socket/mex.d.ts +3 -0
  58. package/lib/Socket/mex.js +35 -40
  59. package/lib/Socket/newsletter.d.ts +170 -0
  60. package/lib/Socket/newsletter.js +191 -239
  61. package/lib/Socket/socket.d.ts +59 -0
  62. package/lib/Socket/socket.js +923 -992
  63. package/lib/Types/Auth.d.ts +117 -0
  64. package/lib/Types/Auth.js +1 -0
  65. package/lib/Types/Bussines.d.ts +25 -0
  66. package/lib/Types/Bussines.js +1 -0
  67. package/lib/Types/Call.d.ts +15 -0
  68. package/lib/Types/Call.js +1 -0
  69. package/lib/Types/Chat.d.ts +124 -0
  70. package/lib/Types/Chat.js +8 -1
  71. package/lib/Types/Contact.d.ts +26 -0
  72. package/lib/Types/Contact.js +1 -0
  73. package/lib/Types/Events.d.ts +256 -0
  74. package/lib/Types/Events.js +2 -1
  75. package/lib/Types/GroupMetadata.d.ts +71 -0
  76. package/lib/Types/GroupMetadata.js +1 -0
  77. package/lib/Types/Label.d.ts +47 -0
  78. package/lib/Types/Label.js +22 -20
  79. package/lib/Types/LabelAssociation.d.ts +30 -0
  80. package/lib/Types/LabelAssociation.js +4 -2
  81. package/lib/Types/Message.d.ts +320 -0
  82. package/lib/Types/Message.js +5 -3
  83. package/lib/Types/Mex.d.ts +141 -0
  84. package/lib/Types/Mex.js +31 -26
  85. package/lib/Types/Product.d.ts +79 -0
  86. package/lib/Types/Product.js +1 -0
  87. package/lib/Types/Signal.d.ts +87 -0
  88. package/lib/Types/Signal.js +2 -1
  89. package/lib/Types/Socket.d.ts +136 -0
  90. package/lib/Types/Socket.js +3 -2
  91. package/lib/Types/State.d.ts +97 -0
  92. package/lib/Types/State.js +52 -4
  93. package/lib/Types/USync.d.ts +26 -0
  94. package/lib/Types/USync.js +1 -0
  95. package/lib/Types/index.d.ts +66 -0
  96. package/lib/Types/index.js +11 -10
  97. package/lib/Utils/auth-utils.d.ts +24 -0
  98. package/lib/Utils/auth-utils.js +277 -240
  99. package/lib/Utils/browser-utils.d.ts +4 -0
  100. package/lib/Utils/browser-utils.js +23 -20
  101. package/lib/Utils/business.d.ts +23 -0
  102. package/lib/Utils/business.js +204 -200
  103. package/lib/Utils/chat-utils.d.ts +100 -0
  104. package/lib/Utils/chat-utils.js +810 -836
  105. package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
  106. package/lib/Utils/companion-reg-client-utils.js +25 -23
  107. package/lib/Utils/crypto.d.ts +37 -0
  108. package/lib/Utils/crypto.js +86 -70
  109. package/lib/Utils/decode-wa-message.d.ts +66 -0
  110. package/lib/Utils/decode-wa-message.js +284 -277
  111. package/lib/Utils/event-buffer.d.ts +36 -0
  112. package/lib/Utils/event-buffer.js +595 -592
  113. package/lib/Utils/generics.d.ts +92 -0
  114. package/lib/Utils/generics.js +335 -312
  115. package/lib/Utils/history.d.ts +24 -0
  116. package/lib/Utils/history.js +115 -123
  117. package/lib/Utils/identity-change-handler.d.ts +44 -0
  118. package/lib/Utils/identity-change-handler.js +50 -0
  119. package/lib/Utils/index.d.ts +22 -0
  120. package/lib/Utils/index.js +2 -1
  121. package/lib/Utils/link-preview.d.ts +21 -0
  122. package/lib/Utils/link-preview.js +73 -74
  123. package/lib/Utils/logger.d.ts +12 -0
  124. package/lib/Utils/logger.js +2 -3
  125. package/lib/Utils/lt-hash.d.ts +8 -0
  126. package/lib/Utils/lt-hash.js +6 -0
  127. package/lib/Utils/make-mutex.d.ts +9 -0
  128. package/lib/Utils/make-mutex.js +27 -27
  129. package/lib/Utils/message-retry-manager.d.ts +115 -0
  130. package/lib/Utils/message-retry-manager.js +254 -213
  131. package/lib/Utils/messages-media.d.ts +133 -0
  132. package/lib/Utils/messages-media.js +715 -741
  133. package/lib/Utils/messages.d.ts +91 -0
  134. package/lib/Utils/messages.js +827 -791
  135. package/lib/Utils/noise-handler.d.ts +20 -0
  136. package/lib/Utils/noise-handler.js +187 -215
  137. package/lib/Utils/offline-node-processor.d.ts +17 -0
  138. package/lib/Utils/offline-node-processor.js +40 -0
  139. package/lib/Utils/pre-key-manager.d.ts +28 -0
  140. package/lib/Utils/pre-key-manager.js +98 -69
  141. package/lib/Utils/process-message.d.ts +60 -0
  142. package/lib/Utils/process-message.js +586 -617
  143. package/lib/Utils/reporting-utils.d.ts +11 -0
  144. package/lib/Utils/reporting-utils.js +234 -553
  145. package/lib/Utils/signal.d.ts +47 -0
  146. package/lib/Utils/signal.js +170 -217
  147. package/lib/Utils/stanza-ack.d.ts +11 -0
  148. package/lib/Utils/stanza-ack.js +35 -27
  149. package/lib/Utils/sync-action-utils.d.ts +19 -0
  150. package/lib/Utils/sync-action-utils.js +41 -37
  151. package/lib/Utils/tc-token-utils.d.ts +37 -0
  152. package/lib/Utils/tc-token-utils.js +141 -120
  153. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  154. package/lib/Utils/use-multi-file-auth-state.js +105 -87
  155. package/lib/Utils/validate-connection.d.ts +11 -0
  156. package/lib/Utils/validate-connection.js +188 -190
  157. package/lib/WABinary/constants.d.ts +28 -0
  158. package/lib/WABinary/constants.js +1294 -28
  159. package/lib/WABinary/decode.d.ts +7 -0
  160. package/lib/WABinary/decode.js +249 -245
  161. package/lib/WABinary/encode.d.ts +3 -0
  162. package/lib/WABinary/encode.js +206 -201
  163. package/lib/WABinary/generic-utils.d.ts +15 -0
  164. package/lib/WABinary/generic-utils.js +89 -86
  165. package/lib/WABinary/index.d.ts +6 -0
  166. package/lib/WABinary/index.js +1 -0
  167. package/lib/WABinary/jid-utils.d.ts +48 -0
  168. package/lib/WABinary/jid-utils.js +77 -68
  169. package/lib/WABinary/types.d.ts +19 -0
  170. package/lib/WABinary/types.js +1 -0
  171. package/lib/WAM/BinaryInfo.d.ts +9 -0
  172. package/lib/WAM/BinaryInfo.js +8 -7
  173. package/lib/WAM/constants.d.ts +40 -0
  174. package/lib/WAM/constants.js +22853 -19892
  175. package/lib/WAM/encode.d.ts +3 -0
  176. package/lib/WAM/encode.js +132 -121
  177. package/lib/WAM/index.d.ts +4 -0
  178. package/lib/WAM/index.js +1 -0
  179. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  180. package/lib/WAUSync/Protocols/USyncContactProtocol.js +43 -46
  181. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  182. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +48 -47
  183. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  184. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +23 -22
  185. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  186. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +33 -30
  187. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  188. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +20 -19
  189. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  190. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +44 -53
  191. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  192. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -24
  193. package/lib/WAUSync/Protocols/index.d.ts +6 -0
  194. package/lib/WAUSync/Protocols/index.js +1 -0
  195. package/lib/WAUSync/USyncQuery.d.ts +30 -0
  196. package/lib/WAUSync/USyncQuery.js +88 -79
  197. package/lib/WAUSync/USyncUser.d.ts +17 -0
  198. package/lib/WAUSync/USyncUser.js +29 -28
  199. package/lib/WAUSync/index.d.ts +4 -0
  200. package/lib/WAUSync/index.js +1 -0
  201. package/lib/index.d.ts +12 -0
  202. package/lib/index.js +2 -2
  203. package/package.json +86 -22
  204. package/README.upstream.md +0 -1312
  205. package/WAProto/WAProto.json +0 -1
  206. package/WAProto/WAProto.proto +0 -6275
  207. package/WAProto/compiler.js +0 -550
  208. package/lib/Store/index.js +0 -8
  209. package/lib/Store/keyed-db.js +0 -108
  210. package/lib/Store/make-cache-manager-store.js +0 -70
  211. package/lib/Store/make-in-memory-store.js +0 -417
  212. package/lib/Store/make-ordered-dictionary.js +0 -69
  213. package/lib/Store/object-repository.js +0 -27
  214. package/lib/Utils/use-sqlite-auth-state.js +0 -162
@@ -1,491 +1,431 @@
1
- import { proto } from '../../WAProto/compiler.js';
1
+ import { proto } from '../../WAProto/index.js';
2
2
  import { WAMessageStubType } from '../Types/index.js';
3
3
  import { generateMessageID, generateMessageIDV2, unixTimestampSeconds } from '../Utils/index.js';
4
4
  import logger from '../Utils/logger.js';
5
5
  import { getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString, jidEncode, jidNormalizedUser } from '../WABinary/index.js';
6
6
  import { makeBusinessSocket } from './business.js';
7
- export const makeCommunitiesSocket = config => {
8
- const sock = makeBusinessSocket(config);
9
- const {
10
- authState,
11
- ev,
12
- query,
13
- upsertMessage
14
- } = sock;
15
- const communityQuery = async (jid, type, content) => query({
16
- tag: 'iq',
17
- attrs: {
18
- type,
19
- xmlns: 'w:g2',
20
- to: jid
21
- },
22
- content
23
- });
24
- const communityMetadata = async jid => {
25
- const result = await communityQuery(jid, 'get', [{
26
- tag: 'query',
27
- attrs: {
28
- request: 'interactive'
29
- }
30
- }]);
31
- return extractCommunityMetadata(result);
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
- tag: 'participating',
43
- attrs: {},
44
- content: [{
45
- tag: 'participants',
46
- attrs: {}
47
- }, {
48
- tag: 'description',
49
- attrs: {}
50
- }]
51
- }]
7
+ export const makeCommunitiesSocket = (config) => {
8
+ const sock = makeBusinessSocket(config);
9
+ const { authState, ev, query, upsertMessage } = sock;
10
+ const communityQuery = async (jid, type, content) => query({
11
+ tag: 'iq',
12
+ attrs: {
13
+ type,
14
+ xmlns: 'w:g2',
15
+ to: jid
16
+ },
17
+ content
52
18
  });
53
- const data = {};
54
- const communitiesChild = getBinaryNodeChild(result, 'communities');
55
- if (communitiesChild) {
56
- const communities = getBinaryNodeChildren(communitiesChild, 'community');
57
- for (const communityNode of communities) {
58
- const meta = extractCommunityMetadata({
59
- tag: 'result',
60
- attrs: {},
61
- content: [communityNode]
19
+ const communityMetadata = async (jid) => {
20
+ const result = await communityQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
21
+ return extractCommunityMetadata(result);
22
+ };
23
+ const communityFetchAllParticipating = async () => {
24
+ const result = await query({
25
+ tag: 'iq',
26
+ attrs: {
27
+ to: '@g.us',
28
+ xmlns: 'w:g2',
29
+ type: 'get'
30
+ },
31
+ content: [
32
+ {
33
+ tag: 'participating',
34
+ attrs: {},
35
+ content: [
36
+ { tag: 'participants', attrs: {} },
37
+ { tag: 'description', attrs: {} }
38
+ ]
39
+ }
40
+ ]
62
41
  });
63
- data[meta.id] = meta;
64
- }
65
- }
66
- sock.ev.emit('groups.update', Object.values(data));
67
- return data;
68
- };
69
- async function parseGroupResult(node) {
70
- logger.info({
71
- node
72
- }, 'parseGroupResult');
73
- const groupNode = getBinaryNodeChild(node, 'group');
74
- if (groupNode) {
75
- try {
76
- logger.info({
77
- groupNode
78
- }, 'groupNode');
79
- const metadata = await sock.groupMetadata(`${groupNode.attrs.id}@g.us`);
80
- return metadata ? metadata : Optional.empty();
81
- } catch (error) {
42
+ const data = {};
43
+ const communitiesChild = getBinaryNodeChild(result, 'communities');
44
+ if (communitiesChild) {
45
+ const communities = getBinaryNodeChildren(communitiesChild, 'community');
46
+ for (const communityNode of communities) {
47
+ const meta = extractCommunityMetadata({
48
+ tag: 'result',
49
+ attrs: {},
50
+ content: [communityNode]
51
+ });
52
+ data[meta.id] = meta;
53
+ }
54
+ }
55
+ sock.ev.emit('groups.update', Object.values(data));
56
+ return data;
57
+ };
58
+ async function parseGroupResult(node) {
59
+ logger.info({ node }, 'parseGroupResult');
60
+ const groupNode = getBinaryNodeChild(node, 'group');
61
+ if (groupNode) {
62
+ try {
63
+ logger.info({ groupNode }, 'groupNode');
64
+ const metadata = await sock.groupMetadata(`${groupNode.attrs.id}@g.us`);
65
+ return metadata ? metadata : Optional.empty();
66
+ }
67
+ catch (error) {
68
+ console.error('Error parsing group metadata:', error);
69
+ return Optional.empty();
70
+ }
71
+ }
82
72
  return Optional.empty();
83
- }
84
73
  }
85
- return Optional.empty();
86
- }
87
- const Optional = {
88
- empty: () => null,
89
- of: value => value !== null ? {
90
- value
91
- } : null
92
- };
93
- sock.ws.on('CB:ib,,dirty', async node => {
94
- const {
95
- attrs
96
- } = getBinaryNodeChild(node, 'dirty');
97
- if (attrs.type !== 'communities') {
98
- return;
99
- }
100
- await communityFetchAllParticipating();
101
- await sock.cleanDirtyBits('groups');
102
- });
103
- return {
104
- ...sock,
105
- communityMetadata,
106
- communityCreate: async (subject, body) => {
107
- const descriptionId = generateMessageID().substring(0, 12);
108
- const result = await communityQuery('@g.us', 'set', [{
109
- tag: 'create',
110
- attrs: {
111
- subject
74
+ const Optional = {
75
+ empty: () => null,
76
+ of: (value) => (value !== null ? { value } : null)
77
+ };
78
+ sock.ws.on('CB:ib,,dirty', async (node) => {
79
+ const { attrs } = getBinaryNodeChild(node, 'dirty');
80
+ if (attrs.type !== 'communities') {
81
+ return;
82
+ }
83
+ await communityFetchAllParticipating();
84
+ await sock.cleanDirtyBits('groups');
85
+ });
86
+ return {
87
+ ...sock,
88
+ communityMetadata,
89
+ communityCreate: async (subject, body) => {
90
+ const descriptionId = generateMessageID().substring(0, 12);
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: { default_membership_approval_mode: 'request_required' }
110
+ },
111
+ {
112
+ tag: 'allow_non_admin_sub_group_creation',
113
+ attrs: {}
114
+ },
115
+ {
116
+ tag: 'create_general_chat',
117
+ attrs: {}
118
+ }
119
+ ]
120
+ }
121
+ ]);
122
+ return await parseGroupResult(result);
112
123
  },
113
- content: [{
114
- tag: 'description',
115
- attrs: {
116
- id: descriptionId
117
- },
118
- content: [{
119
- tag: 'body',
120
- attrs: {},
121
- content: Buffer.from(body || '', 'utf-8')
122
- }]
123
- }, {
124
- tag: 'parent',
125
- attrs: {
126
- default_membership_approval_mode: 'request_required'
127
- }
128
- }, {
129
- tag: 'allow_non_admin_sub_group_creation',
130
- attrs: {}
131
- }, {
132
- tag: 'create_general_chat',
133
- attrs: {}
134
- }]
135
- }]);
136
- return await parseGroupResult(result);
137
- },
138
- communityCreateGroup: async (subject, participants, parentCommunityJid) => {
139
- const key = generateMessageIDV2();
140
- const result = await communityQuery('@g.us', 'set', [{
141
- tag: 'create',
142
- attrs: {
143
- subject,
144
- key
124
+ communityCreateGroup: async (subject, participants, parentCommunityJid) => {
125
+ const key = generateMessageIDV2();
126
+ const result = await communityQuery('@g.us', 'set', [
127
+ {
128
+ tag: 'create',
129
+ attrs: {
130
+ subject,
131
+ key
132
+ },
133
+ content: [
134
+ ...participants.map(jid => ({
135
+ tag: 'participant',
136
+ attrs: { jid }
137
+ })),
138
+ { tag: 'linked_parent', attrs: { jid: parentCommunityJid } }
139
+ ]
140
+ }
141
+ ]);
142
+ return await parseGroupResult(result);
145
143
  },
146
- content: [...participants.map(jid => ({
147
- tag: 'participant',
148
- attrs: {
149
- jid
150
- }
151
- })), {
152
- tag: 'linked_parent',
153
- attrs: {
154
- jid: parentCommunityJid
155
- }
156
- }]
157
- }]);
158
- return await parseGroupResult(result);
159
- },
160
- communityLeave: async id => {
161
- await communityQuery('@g.us', 'set', [{
162
- tag: 'leave',
163
- attrs: {},
164
- content: [{
165
- tag: 'community',
166
- attrs: {
167
- id
168
- }
169
- }]
170
- }]);
171
- },
172
- communityUpdateSubject: async (jid, subject) => {
173
- await communityQuery(jid, 'set', [{
174
- tag: 'subject',
175
- attrs: {},
176
- content: Buffer.from(subject, 'utf-8')
177
- }]);
178
- },
179
- communityLinkGroup: async (groupJid, parentCommunityJid) => {
180
- await communityQuery(parentCommunityJid, 'set', [{
181
- tag: 'links',
182
- attrs: {},
183
- content: [{
184
- tag: 'link',
185
- attrs: {
186
- link_type: 'sub_group'
187
- },
188
- content: [{
189
- tag: 'group',
190
- attrs: {
191
- jid: groupJid
192
- }
193
- }]
194
- }]
195
- }]);
196
- },
197
- communityUnlinkGroup: async (groupJid, parentCommunityJid) => {
198
- await communityQuery(parentCommunityJid, 'set', [{
199
- tag: 'unlink',
200
- attrs: {
201
- unlink_type: 'sub_group'
144
+ communityLeave: async (id) => {
145
+ await communityQuery('@g.us', 'set', [
146
+ {
147
+ tag: 'leave',
148
+ attrs: {},
149
+ content: [{ tag: 'community', attrs: { id } }]
150
+ }
151
+ ]);
202
152
  },
203
- content: [{
204
- tag: 'group',
205
- attrs: {
206
- jid: groupJid
207
- }
208
- }]
209
- }]);
210
- },
211
- communityFetchLinkedGroups: async jid => {
212
- let communityJid = jid;
213
- let isCommunity = false;
214
- const metadata = await sock.groupMetadata(jid);
215
- if (metadata.linkedParent) {
216
- communityJid = metadata.linkedParent;
217
- } else {
218
- isCommunity = true;
219
- }
220
- const result = await communityQuery(communityJid, 'get', [{
221
- tag: 'sub_groups',
222
- attrs: {}
223
- }]);
224
- const linkedGroupsData = [];
225
- const subGroupsNode = getBinaryNodeChild(result, 'sub_groups');
226
- if (subGroupsNode) {
227
- const groupNodes = getBinaryNodeChildren(subGroupsNode, 'group');
228
- for (const groupNode of groupNodes) {
229
- linkedGroupsData.push({
230
- id: groupNode.attrs.id ? jidEncode(groupNode.attrs.id, 'g.us') : undefined,
231
- subject: groupNode.attrs.subject || '',
232
- creation: groupNode.attrs.creation ? Number(groupNode.attrs.creation) : undefined,
233
- owner: groupNode.attrs.creator ? jidNormalizedUser(groupNode.attrs.creator) : undefined,
234
- size: groupNode.attrs.size ? Number(groupNode.attrs.size) : undefined
235
- });
236
- }
237
- }
238
- return {
239
- communityJid,
240
- isCommunity,
241
- linkedGroups: linkedGroupsData
242
- };
243
- },
244
- communityRequestParticipantsList: async jid => {
245
- const result = await communityQuery(jid, 'get', [{
246
- tag: 'membership_approval_requests',
247
- attrs: {}
248
- }]);
249
- const node = getBinaryNodeChild(result, 'membership_approval_requests');
250
- const participants = getBinaryNodeChildren(node, 'membership_approval_request');
251
- return participants.map(v => v.attrs);
252
- },
253
- communityRequestParticipantsUpdate: async (jid, participants, action) => {
254
- const result = await communityQuery(jid, 'set', [{
255
- tag: 'membership_requests_action',
256
- attrs: {},
257
- content: [{
258
- tag: action,
259
- attrs: {},
260
- content: participants.map(jid => ({
261
- tag: 'participant',
262
- attrs: {
263
- jid
153
+ communityUpdateSubject: async (jid, subject) => {
154
+ await communityQuery(jid, 'set', [
155
+ {
156
+ tag: 'subject',
157
+ attrs: {},
158
+ content: Buffer.from(subject, 'utf-8')
159
+ }
160
+ ]);
161
+ },
162
+ communityLinkGroup: async (groupJid, parentCommunityJid) => {
163
+ await communityQuery(parentCommunityJid, 'set', [
164
+ {
165
+ tag: 'links',
166
+ attrs: {},
167
+ content: [
168
+ {
169
+ tag: 'link',
170
+ attrs: { link_type: 'sub_group' },
171
+ content: [{ tag: 'group', attrs: { jid: groupJid } }]
172
+ }
173
+ ]
174
+ }
175
+ ]);
176
+ },
177
+ communityUnlinkGroup: async (groupJid, parentCommunityJid) => {
178
+ await communityQuery(parentCommunityJid, 'set', [
179
+ {
180
+ tag: 'unlink',
181
+ attrs: { unlink_type: 'sub_group' },
182
+ content: [{ tag: 'group', attrs: { jid: groupJid } }]
183
+ }
184
+ ]);
185
+ },
186
+ communityFetchLinkedGroups: async (jid) => {
187
+ let communityJid = jid;
188
+ let isCommunity = false;
189
+ // Try to determine if it is a subgroup or a community
190
+ const metadata = await sock.groupMetadata(jid);
191
+ if (metadata.linkedParent) {
192
+ // It is a subgroup, get the community jid
193
+ communityJid = metadata.linkedParent;
264
194
  }
265
- }))
266
- }]
267
- }]);
268
- const node = getBinaryNodeChild(result, 'membership_requests_action');
269
- const nodeAction = getBinaryNodeChild(node, action);
270
- const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant');
271
- return participantsAffected.map(p => {
272
- return {
273
- status: p.attrs.error || '200',
274
- jid: p.attrs.jid
275
- };
276
- });
277
- },
278
- communityParticipantsUpdate: async (jid, participants, action) => {
279
- const result = await communityQuery(jid, 'set', [{
280
- tag: action,
281
- attrs: action === 'remove' ? {
282
- linked_groups: 'true'
283
- } : {},
284
- content: participants.map(jid => ({
285
- tag: 'participant',
286
- attrs: {
287
- jid
288
- }
289
- }))
290
- }]);
291
- const node = getBinaryNodeChild(result, action);
292
- const participantsAffected = getBinaryNodeChildren(node, 'participant');
293
- return participantsAffected.map(p => {
294
- return {
295
- status: p.attrs.error || '200',
296
- jid: p.attrs.jid,
297
- content: p
298
- };
299
- });
300
- },
301
- communityUpdateDescription: async (jid, description) => {
302
- const metadata = await communityMetadata(jid);
303
- const prev = metadata.descId ?? null;
304
- await communityQuery(jid, 'set', [{
305
- tag: 'description',
306
- attrs: {
307
- ...(description ? {
308
- id: generateMessageID()
309
- } : {
310
- delete: 'true'
311
- }),
312
- ...(prev ? {
313
- prev
314
- } : {})
195
+ else {
196
+ // It is a community
197
+ isCommunity = true;
198
+ }
199
+ // Fetch all subgroups of the community
200
+ const result = await communityQuery(communityJid, 'get', [{ tag: 'sub_groups', attrs: {} }]);
201
+ const linkedGroupsData = [];
202
+ const subGroupsNode = getBinaryNodeChild(result, 'sub_groups');
203
+ if (subGroupsNode) {
204
+ const groupNodes = getBinaryNodeChildren(subGroupsNode, 'group');
205
+ for (const groupNode of groupNodes) {
206
+ linkedGroupsData.push({
207
+ id: groupNode.attrs.id ? jidEncode(groupNode.attrs.id, 'g.us') : undefined,
208
+ subject: groupNode.attrs.subject || '',
209
+ creation: groupNode.attrs.creation ? Number(groupNode.attrs.creation) : undefined,
210
+ owner: groupNode.attrs.creator ? jidNormalizedUser(groupNode.attrs.creator) : undefined,
211
+ size: groupNode.attrs.size ? Number(groupNode.attrs.size) : undefined
212
+ });
213
+ }
214
+ }
215
+ return {
216
+ communityJid,
217
+ isCommunity,
218
+ linkedGroups: linkedGroupsData
219
+ };
315
220
  },
316
- content: description ? [{
317
- tag: 'body',
318
- attrs: {},
319
- content: Buffer.from(description, 'utf-8')
320
- }] : undefined
321
- }]);
322
- },
323
- communityInviteCode: async jid => {
324
- const result = await communityQuery(jid, 'get', [{
325
- tag: 'invite',
326
- attrs: {}
327
- }]);
328
- const inviteNode = getBinaryNodeChild(result, 'invite');
329
- return inviteNode?.attrs.code;
330
- },
331
- communityRevokeInvite: async jid => {
332
- const result = await communityQuery(jid, 'set', [{
333
- tag: 'invite',
334
- attrs: {}
335
- }]);
336
- const inviteNode = getBinaryNodeChild(result, 'invite');
337
- return inviteNode?.attrs.code;
338
- },
339
- communityAcceptInvite: async code => {
340
- const results = await communityQuery('@g.us', 'set', [{
341
- tag: 'invite',
342
- attrs: {
343
- code
344
- }
345
- }]);
346
- const result = getBinaryNodeChild(results, 'community');
347
- return result?.attrs.jid;
348
- },
349
- communityRevokeInviteV4: async (communityJid, invitedJid) => {
350
- const result = await communityQuery(communityJid, 'set', [{
351
- tag: 'revoke',
352
- attrs: {},
353
- content: [{
354
- tag: 'participant',
355
- attrs: {
356
- jid: invitedJid
357
- }
358
- }]
359
- }]);
360
- return !!result;
361
- },
362
- communityAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
363
- key = typeof key === 'string' ? {
364
- remoteJid: key
365
- } : key;
366
- const results = await communityQuery(inviteMessage.groupJid, 'set', [{
367
- tag: 'accept',
368
- attrs: {
369
- code: inviteMessage.inviteCode,
370
- expiration: inviteMessage.inviteExpiration.toString(),
371
- admin: key.remoteJid
372
- }
373
- }]);
374
- if (key.id) {
375
- inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
376
- inviteMessage.inviteExpiration = 0;
377
- inviteMessage.inviteCode = '';
378
- ev.emit('messages.update', [{
379
- key,
380
- update: {
381
- message: {
382
- groupInviteMessage: inviteMessage
221
+ communityRequestParticipantsList: async (jid) => {
222
+ const result = await communityQuery(jid, 'get', [
223
+ {
224
+ tag: 'membership_approval_requests',
225
+ attrs: {}
226
+ }
227
+ ]);
228
+ const node = getBinaryNodeChild(result, 'membership_approval_requests');
229
+ const participants = getBinaryNodeChildren(node, 'membership_approval_request');
230
+ return participants.map(v => v.attrs);
231
+ },
232
+ communityRequestParticipantsUpdate: async (jid, participants, action) => {
233
+ const result = await communityQuery(jid, 'set', [
234
+ {
235
+ tag: 'membership_requests_action',
236
+ attrs: {},
237
+ content: [
238
+ {
239
+ tag: action,
240
+ attrs: {},
241
+ content: participants.map(jid => ({
242
+ tag: 'participant',
243
+ attrs: { jid }
244
+ }))
245
+ }
246
+ ]
247
+ }
248
+ ]);
249
+ const node = getBinaryNodeChild(result, 'membership_requests_action');
250
+ const nodeAction = getBinaryNodeChild(node, action);
251
+ const participantsAffected = getBinaryNodeChildren(nodeAction, 'participant');
252
+ return participantsAffected.map(p => {
253
+ return { status: p.attrs.error || '200', jid: p.attrs.jid };
254
+ });
255
+ },
256
+ communityParticipantsUpdate: async (jid, participants, action) => {
257
+ const result = await communityQuery(jid, 'set', [
258
+ {
259
+ tag: action,
260
+ attrs: action === 'remove' ? { linked_groups: 'true' } : {},
261
+ content: participants.map(jid => ({
262
+ tag: 'participant',
263
+ attrs: { jid }
264
+ }))
265
+ }
266
+ ]);
267
+ const node = getBinaryNodeChild(result, action);
268
+ const participantsAffected = getBinaryNodeChildren(node, 'participant');
269
+ return participantsAffected.map(p => {
270
+ return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
271
+ });
272
+ },
273
+ communityUpdateDescription: async (jid, description) => {
274
+ const metadata = await communityMetadata(jid);
275
+ const prev = metadata.descId ?? null;
276
+ await communityQuery(jid, 'set', [
277
+ {
278
+ tag: 'description',
279
+ attrs: {
280
+ ...(description ? { id: generateMessageID() } : { delete: 'true' }),
281
+ ...(prev ? { prev } : {})
282
+ },
283
+ content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : undefined
284
+ }
285
+ ]);
286
+ },
287
+ communityInviteCode: async (jid) => {
288
+ const result = await communityQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
289
+ const inviteNode = getBinaryNodeChild(result, 'invite');
290
+ return inviteNode?.attrs.code;
291
+ },
292
+ communityRevokeInvite: async (jid) => {
293
+ const result = await communityQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
294
+ const inviteNode = getBinaryNodeChild(result, 'invite');
295
+ return inviteNode?.attrs.code;
296
+ },
297
+ communityAcceptInvite: async (code) => {
298
+ const results = await communityQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
299
+ const result = getBinaryNodeChild(results, 'community');
300
+ return result?.attrs.jid;
301
+ },
302
+ /**
303
+ * revoke a v4 invite for someone
304
+ * @param communityJid community jid
305
+ * @param invitedJid jid of person you invited
306
+ * @returns true if successful
307
+ */
308
+ communityRevokeInviteV4: async (communityJid, invitedJid) => {
309
+ const result = await communityQuery(communityJid, 'set', [
310
+ { tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }
311
+ ]);
312
+ return !!result;
313
+ },
314
+ /**
315
+ * accept a CommunityInviteMessage
316
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
317
+ * @param inviteMessage the message to accept
318
+ */
319
+ communityAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
320
+ key = typeof key === 'string' ? { remoteJid: key } : key;
321
+ const results = await communityQuery(inviteMessage.groupJid, 'set', [
322
+ {
323
+ tag: 'accept',
324
+ attrs: {
325
+ code: inviteMessage.inviteCode,
326
+ expiration: inviteMessage.inviteExpiration.toString(),
327
+ admin: key.remoteJid
328
+ }
329
+ }
330
+ ]);
331
+ // if we have the full message key
332
+ // update the invite message to be expired
333
+ if (key.id) {
334
+ // create new invite message that is expired
335
+ inviteMessage = proto.Message.GroupInviteMessage.fromObject(inviteMessage);
336
+ inviteMessage.inviteExpiration = 0;
337
+ inviteMessage.inviteCode = '';
338
+ ev.emit('messages.update', [
339
+ {
340
+ key,
341
+ update: {
342
+ message: {
343
+ groupInviteMessage: inviteMessage
344
+ }
345
+ }
346
+ }
347
+ ]);
383
348
  }
384
- }
385
- }]);
386
- }
387
- await upsertMessage({
388
- key: {
389
- remoteJid: inviteMessage.groupJid,
390
- id: generateMessageIDV2(sock.user?.id),
391
- fromMe: false,
392
- participant: key.remoteJid
349
+ // generate the community add message
350
+ await upsertMessage({
351
+ key: {
352
+ remoteJid: inviteMessage.groupJid,
353
+ id: generateMessageIDV2(sock.user?.id),
354
+ fromMe: false,
355
+ participant: key.remoteJid // TODO: investigate if this makes any sense at all
356
+ },
357
+ messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
358
+ messageStubParameters: [JSON.stringify(authState.creds.me)],
359
+ participant: key.remoteJid,
360
+ messageTimestamp: unixTimestampSeconds()
361
+ }, 'notify');
362
+ return results.attrs.from;
363
+ }),
364
+ communityGetInviteInfo: async (code) => {
365
+ const results = await communityQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
366
+ return extractCommunityMetadata(results);
393
367
  },
394
- messageStubType: WAMessageStubType.GROUP_PARTICIPANT_ADD,
395
- messageStubParameters: [JSON.stringify(authState.creds.me)],
396
- participant: key.remoteJid,
397
- messageTimestamp: unixTimestampSeconds()
398
- }, 'notify');
399
- return results.attrs.from;
400
- }),
401
- communityGetInviteInfo: async code => {
402
- const results = await communityQuery('@g.us', 'get', [{
403
- tag: 'invite',
404
- attrs: {
405
- code
406
- }
407
- }]);
408
- return extractCommunityMetadata(results);
409
- },
410
- communityToggleEphemeral: async (jid, ephemeralExpiration) => {
411
- const content = ephemeralExpiration ? {
412
- tag: 'ephemeral',
413
- attrs: {
414
- expiration: ephemeralExpiration.toString()
415
- }
416
- } : {
417
- tag: 'not_ephemeral',
418
- attrs: {}
419
- };
420
- await communityQuery(jid, 'set', [content]);
421
- },
422
- communitySettingUpdate: async (jid, setting) => {
423
- await communityQuery(jid, 'set', [{
424
- tag: setting,
425
- attrs: {}
426
- }]);
427
- },
428
- communityMemberAddMode: async (jid, mode) => {
429
- await communityQuery(jid, 'set', [{
430
- tag: 'member_add_mode',
431
- attrs: {},
432
- content: mode
433
- }]);
434
- },
435
- communityJoinApprovalMode: async (jid, mode) => {
436
- await communityQuery(jid, 'set', [{
437
- tag: 'membership_approval_mode',
438
- attrs: {},
439
- content: [{
440
- tag: 'community_join',
441
- attrs: {
442
- state: mode
443
- }
444
- }]
445
- }]);
446
- },
447
- communityFetchAllParticipating
448
- };
368
+ communityToggleEphemeral: async (jid, ephemeralExpiration) => {
369
+ const content = ephemeralExpiration
370
+ ? { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } }
371
+ : { tag: 'not_ephemeral', attrs: {} };
372
+ await communityQuery(jid, 'set', [content]);
373
+ },
374
+ communitySettingUpdate: async (jid, setting) => {
375
+ await communityQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
376
+ },
377
+ communityMemberAddMode: async (jid, mode) => {
378
+ await communityQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
379
+ },
380
+ communityJoinApprovalMode: async (jid, mode) => {
381
+ await communityQuery(jid, 'set', [
382
+ { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'community_join', attrs: { state: mode } }] }
383
+ ]);
384
+ },
385
+ communityFetchAllParticipating
386
+ };
449
387
  };
450
- export const extractCommunityMetadata = result => {
451
- const community = getBinaryNodeChild(result, 'community');
452
- const descChild = getBinaryNodeChild(community, 'description');
453
- let desc;
454
- let descId;
455
- if (descChild) {
456
- desc = getBinaryNodeChildString(descChild, 'body');
457
- descId = descChild.attrs.id;
458
- }
459
- const communityId = community.attrs.id?.includes('@') ? community.attrs.id : jidEncode(community.attrs.id || '', 'g.us');
460
- const eph = getBinaryNodeChild(community, 'ephemeral')?.attrs.expiration;
461
- const memberAddMode = getBinaryNodeChildString(community, 'member_add_mode') === 'all_member_add';
462
- const metadata = {
463
- id: communityId,
464
- subject: community.attrs.subject || '',
465
- subjectOwner: community.attrs.s_o,
466
- subjectTime: Number(community.attrs.s_t || 0),
467
- size: getBinaryNodeChildren(community, 'participant').length,
468
- creation: Number(community.attrs.creation || 0),
469
- owner: community.attrs.creator ? jidNormalizedUser(community.attrs.creator) : undefined,
470
- desc,
471
- descId,
472
- linkedParent: getBinaryNodeChild(community, 'linked_parent')?.attrs.jid || undefined,
473
- restrict: !!getBinaryNodeChild(community, 'locked'),
474
- announce: !!getBinaryNodeChild(community, 'announcement'),
475
- isCommunity: !!getBinaryNodeChild(community, 'parent'),
476
- isCommunityAnnounce: !!getBinaryNodeChild(community, 'default_sub_community'),
477
- joinApprovalMode: !!getBinaryNodeChild(community, 'membership_approval_mode'),
478
- memberAddMode,
479
- participants: getBinaryNodeChildren(community, 'participant').map(({
480
- attrs
481
- }) => {
482
- return {
483
- id: attrs.jid,
484
- admin: attrs.type || null
485
- };
486
- }),
487
- ephemeralDuration: eph ? +eph : undefined,
488
- addressingMode: getBinaryNodeChildString(community, 'addressing_mode')
489
- };
490
- return metadata;
388
+ export const extractCommunityMetadata = (result) => {
389
+ const community = getBinaryNodeChild(result, 'community');
390
+ const descChild = getBinaryNodeChild(community, 'description');
391
+ let desc;
392
+ let descId;
393
+ if (descChild) {
394
+ desc = getBinaryNodeChildString(descChild, 'body');
395
+ descId = descChild.attrs.id;
396
+ }
397
+ const communityId = community.attrs.id?.includes('@')
398
+ ? community.attrs.id
399
+ : jidEncode(community.attrs.id || '', 'g.us');
400
+ const eph = getBinaryNodeChild(community, 'ephemeral')?.attrs.expiration;
401
+ const memberAddMode = getBinaryNodeChildString(community, 'member_add_mode') === 'all_member_add';
402
+ const metadata = {
403
+ id: communityId,
404
+ subject: community.attrs.subject || '',
405
+ subjectOwner: community.attrs.s_o,
406
+ subjectTime: Number(community.attrs.s_t || 0),
407
+ size: getBinaryNodeChildren(community, 'participant').length,
408
+ creation: Number(community.attrs.creation || 0),
409
+ owner: community.attrs.creator ? jidNormalizedUser(community.attrs.creator) : undefined,
410
+ desc,
411
+ descId,
412
+ linkedParent: getBinaryNodeChild(community, 'linked_parent')?.attrs.jid || undefined,
413
+ restrict: !!getBinaryNodeChild(community, 'locked'),
414
+ announce: !!getBinaryNodeChild(community, 'announcement'),
415
+ isCommunity: !!getBinaryNodeChild(community, 'parent'),
416
+ isCommunityAnnounce: !!getBinaryNodeChild(community, 'default_sub_community'),
417
+ joinApprovalMode: !!getBinaryNodeChild(community, 'membership_approval_mode'),
418
+ memberAddMode,
419
+ participants: getBinaryNodeChildren(community, 'participant').map(({ attrs }) => {
420
+ return {
421
+ // TODO: IMPLEMENT THE PN/LID FIELDS HERE!!
422
+ id: attrs.jid,
423
+ admin: (attrs.type || null)
424
+ };
425
+ }),
426
+ ephemeralDuration: eph ? +eph : undefined,
427
+ addressingMode: getBinaryNodeChildString(community, 'addressing_mode')
428
+ };
429
+ return metadata;
491
430
  };
431
+ //# sourceMappingURL=communities.js.map