sanka-baileyss 2.2.8 → 2.2.9

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.
@@ -1,17 +1,17 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", { value: true })
3
3
 
4
- const WAProto_1 = require("../../WAProto");
5
- const Types_1 = require("../Types");
6
- const Utils_1 = require("../Utils");
7
- const WABinary_1 = require("../WABinary");
8
- const chats_1 = require("./chats");
4
+ const WAProto_1 = require("../../WAProto")
5
+ const Types_1 = require("../Types")
6
+ const Utils_1 = require("../Utils")
7
+ const WABinary_1 = require("../WABinary")
8
+ const chats_1 = require("./chats")
9
9
 
10
10
  const makeGroupsSocket = (config) => {
11
- const sock = chats_1.makeChatsSocket(config);
12
- const { authState, ev, query, upsertMessage } = sock;
13
-
14
- const groupQuery = async (jid, type, content) => query({
11
+ const suki = chats_1.makeChatsSocket(config)
12
+ const { authState, ev, query, upsertMessage } = suki
13
+
14
+ const groupQuery = async (jid, type, content) => (query({
15
15
  tag: 'iq',
16
16
  attrs: {
17
17
  type,
@@ -19,13 +19,13 @@ const makeGroupsSocket = (config) => {
19
19
  to: jid,
20
20
  },
21
21
  content
22
- });
23
-
22
+ }))
23
+
24
24
  const groupMetadata = async (jid) => {
25
- const result = await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
26
- return extractGroupMetadata(result);
27
- };
28
-
25
+ const result = await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }])
26
+ return extractGroupMetadata(result)
27
+ }
28
+
29
29
  const groupFetchAllParticipating = async () => {
30
30
  const result = await query({
31
31
  tag: 'iq',
@@ -44,44 +44,34 @@ const makeGroupsSocket = (config) => {
44
44
  ]
45
45
  }
46
46
  ]
47
- });
48
-
49
- const data = {};
50
- const groupsChild = WABinary_1.getBinaryNodeChild(result, 'groups');
51
-
47
+ })
48
+
49
+ const data = {}
50
+ const groupsChild = WABinary_1.getBinaryNodeChild(result, 'groups')
51
+
52
52
  if (groupsChild) {
53
- const groups = WABinary_1.getBinaryNodeChildren(groupsChild, 'group');
53
+ const groups = WABinary_1.getBinaryNodeChildren(groupsChild, 'group')
54
54
  for (const groupNode of groups) {
55
55
  const meta = extractGroupMetadata({
56
56
  tag: 'result',
57
57
  attrs: {},
58
58
  content: [groupNode]
59
- });
60
- data[meta.id] = meta;
59
+ })
60
+ data[meta.id] = meta
61
61
  }
62
62
  }
63
-
64
- sock.ev.emit('groups.update', Object.values(data));
65
- return data;
66
- };
67
-
68
- sock.ws.on('CB:ib,,dirty', async (node) => {
69
- const { attrs } = WABinary_1.getBinaryNodeChild(node, 'dirty') || {};
70
- if (!attrs || attrs.type !== 'groups') {
71
- return;
72
- }
73
-
74
- await groupFetchAllParticipating();
75
- await sock.cleanDirtyBits('groups');
76
- });
77
-
63
+
64
+ suki.ev.emit('groups.update', Object.values(data))
65
+ return data
66
+ }
67
+
78
68
  return {
79
- ...sock,
80
- groupQuery,
69
+ ...suki,
70
+ groupQuery,
81
71
  groupMetadata,
82
72
  groupCreate: async (subject, participants) => {
83
- const key = Utils_1.generateMessageID();
84
-
73
+ const key = Utils_1.generateMessageID()
74
+
85
75
  const result = await groupQuery('@g.us', 'set', [
86
76
  {
87
77
  tag: 'create',
@@ -94,9 +84,9 @@ const makeGroupsSocket = (config) => {
94
84
  attrs: { jid }
95
85
  }))
96
86
  }
97
- ]);
98
-
99
- return extractGroupMetadata(result);
87
+ ])
88
+
89
+ return extractGroupMetadata(result)
100
90
  },
101
91
  groupLeave: async (id) => {
102
92
  await groupQuery('@g.us', 'set', [
@@ -107,7 +97,7 @@ const makeGroupsSocket = (config) => {
107
97
  { tag: 'group', attrs: { id } }
108
98
  ]
109
99
  }
110
- ]);
100
+ ])
111
101
  },
112
102
  groupUpdateSubject: async (jid, subject) => {
113
103
  await groupQuery(jid, 'set', [
@@ -116,7 +106,7 @@ const makeGroupsSocket = (config) => {
116
106
  attrs: {},
117
107
  content: Buffer.from(subject, 'utf-8')
118
108
  }
119
- ]);
109
+ ])
120
110
  },
121
111
  groupRequestParticipantsList: async (jid) => {
122
112
  const result = await groupQuery(jid, 'get', [
@@ -124,12 +114,12 @@ const makeGroupsSocket = (config) => {
124
114
  tag: 'membership_approval_requests',
125
115
  attrs: {}
126
116
  }
127
- ]);
128
-
129
- const node = WABinary_1.getBinaryNodeChild(result, 'membership_approval_requests');
130
- const participants = WABinary_1.getBinaryNodeChildren(node, 'membership_approval_request');
131
-
132
- return participants.map(v => v.attrs);
117
+ ])
118
+
119
+ const node = WABinary_1.getBinaryNodeChild(result, 'membership_approval_requests')
120
+ const participants = WABinary_1.getBinaryNodeChildren(node, 'membership_approval_request')
121
+
122
+ return participants.map(v => v.attrs)
133
123
  },
134
124
  groupRequestParticipantsUpdate: async (jid, participants, action) => {
135
125
  const result = await groupQuery(jid, 'set', [{
@@ -145,15 +135,15 @@ const makeGroupsSocket = (config) => {
145
135
  }))
146
136
  }
147
137
  ]
148
- }]);
149
-
150
- const node = WABinary_1.getBinaryNodeChild(result, 'membership_requests_action');
151
- const nodeAction = WABinary_1.getBinaryNodeChild(node, action);
152
- const participantsAffected = WABinary_1.getBinaryNodeChildren(nodeAction, 'participant');
153
-
138
+ }])
139
+
140
+ const node = WABinary_1.getBinaryNodeChild(result, 'membership_requests_action')
141
+ const nodeAction = WABinary_1.getBinaryNodeChild(node, action)
142
+ const participantsAffected = WABinary_1.getBinaryNodeChildren(nodeAction, 'participant')
143
+
154
144
  return participantsAffected.map(p => {
155
- return { status: p.attrs.error || '200', jid: p.attrs.jid };
156
- });
145
+ return { status: p.attrs.error || '200', jid: p.attrs.jid }
146
+ })
157
147
  },
158
148
  groupParticipantsUpdate: async (jid, participants, action) => {
159
149
  const result = await groupQuery(jid, 'set', [
@@ -165,18 +155,18 @@ const makeGroupsSocket = (config) => {
165
155
  attrs: { jid }
166
156
  }))
167
157
  }
168
- ]);
169
- const node = WABinary_1.getBinaryNodeChild(result, action);
170
- const participantsAffected = WABinary_1.getBinaryNodeChildren(node, 'participant');
171
-
158
+ ])
159
+ const node = WABinary_1.getBinaryNodeChild(result, action)
160
+ const participantsAffected = WABinary_1.getBinaryNodeChildren(node, 'participant')
161
+
172
162
  return participantsAffected.map(p => {
173
- return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p };
174
- });
163
+ return { status: p.attrs.error || '200', jid: p.attrs.jid, content: p }
164
+ })
175
165
  },
176
166
  groupUpdateDescription: async (jid, description) => {
177
- const metadata = await groupMetadata(jid);
178
- const prev = metadata.descId ? metadata.descId : null;
179
-
167
+ const metadata = await groupMetadata(jid)
168
+ const prev = metadata.descId ? metadata.descId : null
169
+
180
170
  await groupQuery(jid, 'set', [
181
171
  {
182
172
  tag: 'description',
@@ -188,39 +178,44 @@ const makeGroupsSocket = (config) => {
188
178
  { tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }
189
179
  ] : undefined
190
180
  }
191
- ]);
181
+ ])
192
182
  },
193
183
  groupInviteCode: async (jid) => {
194
- const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
195
- const inviteNode = WABinary_1.getBinaryNodeChild(result, 'invite');
196
-
197
- return inviteNode?.attrs?.code;
184
+ const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }])
185
+ const inviteNode = WABinary_1.getBinaryNodeChild(result, 'invite')
186
+
187
+ return inviteNode?.attrs?.code
198
188
  },
199
189
  groupRevokeInvite: async (jid) => {
200
- const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
201
- const inviteNode = WABinary_1.getBinaryNodeChild(result, 'invite');
202
-
203
- return inviteNode?.attrs?.code;
190
+ const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }])
191
+ const inviteNode = WABinary_1.getBinaryNodeChild(result, 'invite')
192
+
193
+ return inviteNode?.attrs?.code
204
194
  },
205
195
  groupAcceptInvite: async (code) => {
206
- const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
207
- const result = WABinary_1.getBinaryNodeChild(results, 'group');
208
-
209
- return result?.attrs?.jid;
196
+ const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }])
197
+ const result = WABinary_1.getBinaryNodeChild(results, 'group')
198
+
199
+ return result?.attrs?.jid
210
200
  },
211
- groupRevokeInviteV4: async (groupJid, invitedJid) => {
212
- const result = await groupQuery(groupJid, 'set', [
213
- {
214
- tag: 'revoke',
215
- attrs: {},
216
- content: [{ tag: 'participant', attrs: { jid: invitedJid } }]
217
- }
218
- ]);
219
-
220
- return !!result;
221
- },
201
+ /**
202
+ * revoke a v4 invite for someone
203
+ * @param groupJid group jid
204
+ * @param invitedJid jid of person you invited
205
+ * @returns true if successful
206
+ */
207
+ groupRevokeInviteV4: async (groupJid, invitedJid) => {
208
+ const result = await groupQuery(groupJid, 'set', [{ tag: 'revoke', attrs: {}, content: [{ tag: 'participant', attrs: { jid: invitedJid } }] }])
209
+
210
+ return !!result
211
+ },
212
+ /**
213
+ * accept a GroupInviteMessage
214
+ * @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
215
+ * @param inviteMessage the message to accept
216
+ */
222
217
  groupAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
223
- key = typeof key === 'string' ? { remoteJid: key } : key;
218
+ key = typeof key === 'string' ? { remoteJid: key } : key
224
219
  const results = await groupQuery(inviteMessage.groupJid, 'set', [{
225
220
  tag: 'accept',
226
221
  attrs: {
@@ -228,12 +223,15 @@ groupRevokeInviteV4: async (groupJid, invitedJid) => {
228
223
  expiration: inviteMessage.inviteExpiration.toString(),
229
224
  admin: key.remoteJid
230
225
  }
231
- }]);
232
-
226
+ }])
227
+
228
+ // if we have the full message key
229
+ // update the invite message to be expired
233
230
  if (key.id) {
234
- inviteMessage = WAProto_1.proto.Message.GroupInviteMessage.fromObject(inviteMessage);
235
- inviteMessage.inviteExpiration = 0;
236
- inviteMessage.inviteCode = '';
231
+ // create new invite message that is expired
232
+ inviteMessage = WAProto_1.proto.Message.GroupInviteMessage.fromObject(inviteMessage)
233
+ inviteMessage.inviteExpiration = 0
234
+ inviteMessage.inviteCode = ''
237
235
  ev.emit('messages.update', [
238
236
  {
239
237
  key,
@@ -243,13 +241,14 @@ groupRevokeInviteV4: async (groupJid, invitedJid) => {
243
241
  }
244
242
  }
245
243
  }
246
- ]);
244
+ ])
247
245
  }
248
-
246
+
247
+ // generate the group add message
249
248
  await upsertMessage({
250
249
  key: {
251
250
  remoteJid: inviteMessage.groupJid,
252
- id: Utils_1.generateMessageID(authState.creds.me?.id),
251
+ id: Utils_1.generateMessageID(authState.creds.me?.id),
253
252
  fromMe: false,
254
253
  participant: key.remoteJid,
255
254
  },
@@ -259,102 +258,87 @@ groupRevokeInviteV4: async (groupJid, invitedJid) => {
259
258
  ],
260
259
  participant: key.remoteJid,
261
260
  messageTimestamp: Utils_1.unixTimestampSeconds()
262
- }, 'notify');
263
-
264
- return results.attrs.from;
261
+ }, 'notify')
262
+
263
+ return results.attrs.from
265
264
  }),
266
265
  groupGetInviteInfo: async (code) => {
267
- const results = await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
268
-
269
- return extractGroupMetadata(results);
266
+ const results = await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }])
267
+
268
+ return extractGroupMetadata(results)
270
269
  },
271
270
  groupToggleEphemeral: async (jid, ephemeralExpiration) => {
272
271
  const content = ephemeralExpiration ?
273
272
  { tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } } :
274
- { tag: 'not_ephemeral', attrs: {} };
275
- await groupQuery(jid, 'set', [content]);
273
+ { tag: 'not_ephemeral', attrs: {} }
274
+ await groupQuery(jid, 'set', [content])
276
275
  },
277
276
  groupSettingUpdate: async (jid, setting) => {
278
- await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
277
+ await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }])
279
278
  },
280
279
  groupMemberAddMode: async (jid, mode) => {
281
- await groupQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }]);
280
+ await groupQuery(jid, 'set', [{ tag: 'member_add_mode', attrs: {}, content: mode }])
282
281
  },
283
282
  groupJoinApprovalMode: async (jid, mode) => {
284
- await groupQuery(jid, 'set', [
285
- { tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }
286
- ]);
283
+ await groupQuery(jid, 'set', [{ tag: 'membership_approval_mode', attrs: {}, content: [{ tag: 'group_join', attrs: { state: mode } }] }])
287
284
  },
288
285
  groupFetchAllParticipating
289
- };
290
- };
286
+ }
287
+ }
291
288
 
292
289
  const extractGroupMetadata = (result) => {
293
- const group = WABinary_1.getBinaryNodeChild(result, 'group');
294
- if (!group) return null;
295
-
296
- const descChild = WABinary_1.getBinaryNodeChild(group, 'description');
297
-
298
- let desc;
299
- let descId;
300
- let descOwner;
301
- let descOwnerJid;
302
- let descTime;
303
-
290
+ const group = WABinary_1.getBinaryNodeChild(result, 'group')
291
+ const descChild = WABinary_1.getBinaryNodeChild(group, 'description')
292
+
293
+ let desc
294
+ let descId
295
+
304
296
  if (descChild) {
305
- desc = WABinary_1.getBinaryNodeChildString(descChild, 'body');
306
- descOwner = descChild.attrs.participant ? WABinary_1.jidNormalizedUser(descChild.attrs.participant) : undefined;
307
- descOwnerJid = descChild.attrs.participant_pn ? WABinary_1.jidNormalizedUser(descChild.attrs.participant_pn) : undefined;
308
- descTime = +descChild.attrs.t;
309
- descId = descChild.attrs.id;
297
+ desc = WABinary_1.getBinaryNodeChildString(descChild, 'body')
298
+ descId = descChild.attrs.id
310
299
  }
311
-
312
- const groupId = group.attrs.id.includes('@') ? group.attrs.id : WABinary_1.jidEncode(group.attrs.id, 'g.us');
313
- const eph = WABinary_1.getBinaryNodeChild(group, 'ephemeral')?.attrs?.expiration;
314
- const memberAddMode = WABinary_1.getBinaryNodeChildString(group, 'member_add_mode') === 'all_member_add';
315
-
316
- const participants = WABinary_1.getBinaryNodeChildren(group, 'participant').map(({ attrs }) => {
317
- const isUserJid = WABinary_1.isJidUser(attrs.jid);
318
- const isLidJid = WABinary_1.isLidUser && WABinary_1.isLidUser(attrs.jid); // Soporte condicional para LID
319
-
320
- return {
321
- id: attrs.jid,
322
- jid: isUserJid ? attrs.jid : (attrs.phone_number ? WABinary_1.jidNormalizedUser(attrs.phone_number) : undefined),
323
- lid: isLidJid ? attrs.jid : attrs.lid,
324
- admin: (attrs.type || null),
325
- };
326
- });
327
-
328
-
300
+
301
+ const mode = group.attrs.addressing_mode
302
+ const groupId = group.attrs.id.includes('@') ? group.attrs.id : WABinary_1.jidEncode(group.attrs.id, 'g.us')
303
+ const eph = WABinary_1.getBinaryNodeChild(group, 'ephemeral')?.attrs.expiration
304
+ const memberAddMode = WABinary_1.getBinaryNodeChildString(group, 'member_add_mode') === 'all_member_add'
305
+
329
306
  const metadata = {
330
307
  id: groupId,
331
- addressingMode: group.attrs.addressing_mode,
308
+ addressingMode: mode,
332
309
  subject: group.attrs.subject,
333
- subjectOwner: group.attrs.s_o,
334
- subjectOwnerJid: group.attrs.s_o_pn,
310
+ subjectOwner: mode === 'lid' ? group.attrs.s_o_pn : group.attrs.s_o,
311
+ subjectOwnerJid: mode === 'lid' ? group.attrs.s_o_pn : group.attrs.s_o,
335
312
  subjectTime: +group.attrs.s_t,
336
- size: participants.length,
313
+ size: group.attrs?.size ? +group.attrs.size : WABinary_1.getBinaryNodeChildren(group, 'participant').length,
337
314
  creation: +group.attrs.creation,
338
- owner: group.attrs.creator ? WABinary_1.jidNormalizedUser(group.attrs.creator) : undefined,
339
- ownerJid: group.attrs.creator_pn ? WABinary_1.jidNormalizedUser(group.attrs.creator_pn) : undefined,
315
+ owner: group.attrs.creator ? WABinary_1.jidNormalizedUser(mode === 'lid' ? group.attrs.creator_pn : group.attrs.creator) : undefined,
316
+ ownerJid: group.attrs.creator ? WABinary_1.jidNormalizedUser(mode === 'lid' ? group.attrs.creator_pn : group.attrs.creator) : undefined,
317
+ ownerCountry: group.attrs.creator_country_code,
340
318
  desc,
341
319
  descId,
342
- descOwner,
343
- descOwnerJid,
344
- descTime,
345
- linkedParent: WABinary_1.getBinaryNodeChild(group, 'linked_parent')?.attrs?.jid || undefined,
320
+ linkedParent: WABinary_1.getBinaryNodeChild(group, 'linked_parent')?.attrs.jid || undefined,
346
321
  restrict: !!WABinary_1.getBinaryNodeChild(group, 'locked'),
347
322
  announce: !!WABinary_1.getBinaryNodeChild(group, 'announcement'),
348
323
  isCommunity: !!WABinary_1.getBinaryNodeChild(group, 'parent'),
349
324
  isCommunityAnnounce: !!WABinary_1.getBinaryNodeChild(group, 'default_sub_group'),
350
325
  joinApprovalMode: !!WABinary_1.getBinaryNodeChild(group, 'membership_approval_mode'),
351
326
  memberAddMode,
352
- participants,
353
- ephemeralDuration: eph ? +eph : undefined
354
- };
355
-
356
- return metadata;
357
- };
358
-
359
- exports.makeGroupsSocket = makeGroupsSocket;
360
- exports.extractGroupMetadata = extractGroupMetadata;
327
+ participants: WABinary_1.getBinaryNodeChildren(group, 'participant').map(({ attrs }) => {
328
+ return {
329
+ id: mode === 'lid' ? attrs.phone_number : attrs.jid,
330
+ jid: mode === 'lid' ? attrs.phone_number : attrs.jid,
331
+ lid: mode === 'lid' ? attrs.jid : attrs.lid,
332
+ admin: (attrs.type || null),
333
+ }
334
+ }),
335
+ ephemeralDuration: eph ? +eph : undefined,
336
+ }
337
+
338
+ return metadata
339
+ }
340
+
341
+ module.exports = {
342
+ makeGroupsSocket,
343
+ extractGroupMetadata
344
+ }