Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@adiwajshing/baileys/lib/Socket/groups.js
1129 views
1
"use strict";
2
Object.defineProperty(exports, "__esModule", { value: true });
3
exports.extractGroupMetadata = exports.makeGroupsSocket = void 0;
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
const makeGroupsSocket = (config) => {
10
const sock = (0, chats_1.makeChatsSocket)(config);
11
const { authState, ev, query, upsertMessage } = sock;
12
const groupQuery = async (jid, type, content) => (query({
13
tag: 'iq',
14
attrs: {
15
type,
16
xmlns: 'w:g2',
17
to: jid,
18
},
19
content
20
}));
21
const groupMetadata = async (jid) => {
22
const result = await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
23
return (0, exports.extractGroupMetadata)(result);
24
};
25
return {
26
...sock,
27
groupMetadata,
28
groupCreate: async (subject, participants) => {
29
const key = (0, Utils_1.generateMessageID)();
30
const result = await groupQuery('@g.us', 'set', [
31
{
32
tag: 'create',
33
attrs: {
34
subject,
35
key
36
},
37
content: participants.map(jid => ({
38
tag: 'participant',
39
attrs: { jid }
40
}))
41
}
42
]);
43
return (0, exports.extractGroupMetadata)(result);
44
},
45
groupLeave: async (id) => {
46
await groupQuery('@g.us', 'set', [
47
{
48
tag: 'leave',
49
attrs: {},
50
content: [
51
{ tag: 'group', attrs: { id } }
52
]
53
}
54
]);
55
},
56
groupUpdateSubject: async (jid, subject) => {
57
await groupQuery(jid, 'set', [
58
{
59
tag: 'subject',
60
attrs: {},
61
content: Buffer.from(subject, 'utf-8')
62
}
63
]);
64
},
65
groupParticipantsUpdate: async (jid, participants, action) => {
66
const result = await groupQuery(jid, 'set', [
67
{
68
tag: action,
69
attrs: {},
70
content: participants.map(jid => ({
71
tag: 'participant',
72
attrs: { jid }
73
}))
74
}
75
]);
76
const node = (0, WABinary_1.getBinaryNodeChild)(result, action);
77
const participantsAffected = (0, WABinary_1.getBinaryNodeChildren)(node, 'participant');
78
return participantsAffected.map(p => {
79
return { status: p.attrs.error || '200', jid: p.attrs.jid };
80
});
81
},
82
groupUpdateDescription: async (jid, description) => {
83
var _a;
84
const metadata = await groupMetadata(jid);
85
const prev = (_a = metadata.descId) !== null && _a !== void 0 ? _a : null;
86
await groupQuery(jid, 'set', [
87
{
88
tag: 'description',
89
attrs: {
90
...(description ? { id: (0, Utils_1.generateMessageID)() } : { delete: 'true' }),
91
...(prev ? { prev } : {})
92
},
93
content: description ? [
94
{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }
95
] : undefined
96
}
97
]);
98
},
99
groupInviteCode: async (jid) => {
100
const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
101
const inviteNode = (0, WABinary_1.getBinaryNodeChild)(result, 'invite');
102
return inviteNode === null || inviteNode === void 0 ? void 0 : inviteNode.attrs.code;
103
},
104
groupRevokeInvite: async (jid) => {
105
const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
106
const inviteNode = (0, WABinary_1.getBinaryNodeChild)(result, 'invite');
107
return inviteNode === null || inviteNode === void 0 ? void 0 : inviteNode.attrs.code;
108
},
109
groupAcceptInvite: async (code) => {
110
const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
111
const result = (0, WABinary_1.getBinaryNodeChild)(results, 'group');
112
return result === null || result === void 0 ? void 0 : result.attrs.jid;
113
},
114
/**
115
* accept a GroupInviteMessage
116
* @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite
117
* @param inviteMessage the message to accept
118
*/
119
groupAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {
120
key = typeof key === 'string' ? { remoteJid: key } : key;
121
const results = await groupQuery(inviteMessage.groupJid, 'set', [{
122
tag: 'accept',
123
attrs: {
124
code: inviteMessage.inviteCode,
125
expiration: inviteMessage.inviteExpiration.toString(),
126
admin: key.remoteJid
127
}
128
}]);
129
// if we have the full message key
130
// update the invite message to be expired
131
if (key.id) {
132
// create new invite message that is expired
133
inviteMessage = WAProto_1.proto.Message.GroupInviteMessage.fromObject(inviteMessage);
134
inviteMessage.inviteExpiration = 0;
135
inviteMessage.inviteCode = '';
136
ev.emit('messages.update', [
137
{
138
key,
139
update: {
140
message: {
141
groupInviteMessage: inviteMessage
142
}
143
}
144
}
145
]);
146
}
147
// generate the group add message
148
await upsertMessage({
149
key: {
150
remoteJid: inviteMessage.groupJid,
151
id: (0, Utils_1.generateMessageID)(),
152
fromMe: false,
153
participant: key.remoteJid,
154
},
155
messageStubType: Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD,
156
messageStubParameters: [
157
authState.creds.me.id
158
],
159
participant: key.remoteJid,
160
messageTimestamp: (0, Utils_1.unixTimestampSeconds)()
161
}, 'notify');
162
return results.attrs.from;
163
}),
164
groupGetInviteInfo: async (code) => {
165
const results = await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);
166
return (0, exports.extractGroupMetadata)(results);
167
},
168
groupToggleEphemeral: async (jid, ephemeralExpiration) => {
169
const content = ephemeralExpiration ?
170
{ tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } } :
171
{ tag: 'not_ephemeral', attrs: {} };
172
await groupQuery(jid, 'set', [content]);
173
},
174
groupSettingUpdate: async (jid, setting) => {
175
await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
176
},
177
groupFetchAllParticipating: async () => {
178
const result = await query({
179
tag: 'iq',
180
attrs: {
181
to: '@g.us',
182
xmlns: 'w:g2',
183
type: 'get',
184
},
185
content: [
186
{
187
tag: 'participating',
188
attrs: {},
189
content: [
190
{ tag: 'participants', attrs: {} },
191
{ tag: 'description', attrs: {} }
192
]
193
}
194
]
195
});
196
const data = {};
197
const groupsChild = (0, WABinary_1.getBinaryNodeChild)(result, 'groups');
198
if (groupsChild) {
199
const groups = (0, WABinary_1.getBinaryNodeChildren)(groupsChild, 'group');
200
for (const groupNode of groups) {
201
const meta = (0, exports.extractGroupMetadata)({
202
tag: 'result',
203
attrs: {},
204
content: [groupNode]
205
});
206
data[meta.id] = meta;
207
}
208
}
209
return data;
210
}
211
};
212
};
213
exports.makeGroupsSocket = makeGroupsSocket;
214
const extractGroupMetadata = (result) => {
215
var _a, _b;
216
const group = (0, WABinary_1.getBinaryNodeChild)(result, 'group');
217
const descChild = (0, WABinary_1.getBinaryNodeChild)(group, 'description');
218
let desc;
219
let descId;
220
if (descChild) {
221
desc = (_a = (0, WABinary_1.getBinaryNodeChild)(descChild, 'body')) === null || _a === void 0 ? void 0 : _a.content;
222
descId = descChild.attrs.id;
223
}
224
const groupId = group.attrs.id.includes('@') ? group.attrs.id : (0, WABinary_1.jidEncode)(group.attrs.id, 'g.us');
225
const eph = (_b = (0, WABinary_1.getBinaryNodeChild)(group, 'ephemeral')) === null || _b === void 0 ? void 0 : _b.attrs.expiration;
226
const metadata = {
227
id: groupId,
228
subject: group.attrs.subject,
229
subjectOwner: group.attrs.s_o,
230
subjectTime: +group.attrs.s_t,
231
size: +group.attrs.size,
232
creation: +group.attrs.creation,
233
owner: group.attrs.creator ? (0, WABinary_1.jidNormalizedUser)(group.attrs.creator) : undefined,
234
desc,
235
descId,
236
restrict: !!(0, WABinary_1.getBinaryNodeChild)(group, 'locked'),
237
announce: !!(0, WABinary_1.getBinaryNodeChild)(group, 'announcement'),
238
participants: (0, WABinary_1.getBinaryNodeChildren)(group, 'participant').map(({ attrs }) => {
239
return {
240
id: attrs.jid,
241
admin: attrs.type || null,
242
};
243
}),
244
ephemeralDuration: eph ? +eph : undefined
245
};
246
return metadata;
247
};
248
exports.extractGroupMetadata = extractGroupMetadata;
249
250