Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jajbshjahavahh
GitHub Repository: jajbshjahavahh/Gojo-Satoru
Path: blob/master/node_modules/@adiwajshing/baileys/lib/Socket/groups.js
2593 views
1
"use strict";
2
Object.defineProperty(exports, "__esModule", { value: true });
3
exports.extractGroupMetadata = exports.makeGroupsSocket = void 0;
4
const Utils_1 = require("../Utils");
5
const WABinary_1 = require("../WABinary");
6
const socket_1 = require("./socket");
7
const makeGroupsSocket = (config) => {
8
const sock = socket_1.makeSocket(config);
9
const { query } = sock;
10
const groupQuery = async (jid, type, content) => (query({
11
tag: 'iq',
12
attrs: {
13
type,
14
xmlns: 'w:g2',
15
to: jid,
16
},
17
content
18
}));
19
const groupMetadata = async (jid) => {
20
const result = await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);
21
return exports.extractGroupMetadata(result);
22
};
23
return {
24
...sock,
25
groupMetadata,
26
groupCreate: async (subject, participants) => {
27
const key = Utils_1.generateMessageID();
28
const result = await groupQuery('@g.us', 'set', [
29
{
30
tag: 'create',
31
attrs: {
32
subject,
33
key
34
},
35
content: participants.map(jid => ({
36
tag: 'participant',
37
attrs: { jid }
38
}))
39
}
40
]);
41
return exports.extractGroupMetadata(result);
42
},
43
groupLeave: async (id) => {
44
await groupQuery('@g.us', 'set', [
45
{
46
tag: 'leave',
47
attrs: {},
48
content: [
49
{ tag: 'group', attrs: { id } }
50
]
51
}
52
]);
53
},
54
groupUpdateSubject: async (jid, subject) => {
55
await groupQuery(jid, 'set', [
56
{
57
tag: 'subject',
58
attrs: {},
59
content: Buffer.from(subject, 'utf-8')
60
}
61
]);
62
},
63
groupParticipantsUpdate: async (jid, participants, action) => {
64
const result = await groupQuery(jid, 'set', participants.map(jid => ({
65
tag: action,
66
attrs: {},
67
content: [{ tag: 'participant', attrs: { jid } }]
68
})));
69
const node = WABinary_1.getBinaryNodeChild(result, action);
70
const participantsAffected = WABinary_1.getBinaryNodeChildren(node, 'participant');
71
return participantsAffected.map(p => p.attrs.jid);
72
},
73
groupUpdateDescription: async (jid, description) => {
74
var _a;
75
const metadata = await groupMetadata(jid);
76
const prev = (_a = metadata.descId) !== null && _a !== void 0 ? _a : null;
77
await groupQuery(jid, 'set', [
78
{
79
tag: 'description',
80
attrs: {
81
...(description ? { id: Utils_1.generateMessageID() } : { delete: 'true' }),
82
...(prev ? { prev } : {})
83
},
84
content: description ? [{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }] : null
85
}
86
]);
87
},
88
groupInviteCode: async (jid) => {
89
const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);
90
const inviteNode = WABinary_1.getBinaryNodeChild(result, 'invite');
91
return inviteNode.attrs.code;
92
},
93
groupRevokeInvite: async (jid) => {
94
const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);
95
const inviteNode = WABinary_1.getBinaryNodeChild(result, 'invite');
96
return inviteNode.attrs.code;
97
},
98
groupAcceptInvite: async (code) => {
99
const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);
100
const result = WABinary_1.getBinaryNodeChild(results, 'group');
101
return result.attrs.jid;
102
},
103
groupToggleEphemeral: async (jid, ephemeralExpiration) => {
104
const content = ephemeralExpiration ?
105
{ tag: 'ephemeral', attrs: { ephemeral: ephemeralExpiration.toString() } } :
106
{ tag: 'not_ephemeral', attrs: {} };
107
await groupQuery(jid, 'set', [content]);
108
},
109
groupSettingUpdate: async (jid, setting) => {
110
await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }]);
111
},
112
groupFetchAllParticipating: async () => {
113
const result = await query({
114
tag: 'iq',
115
attrs: {
116
to: '@g.us',
117
xmlns: 'w:g2',
118
type: 'get',
119
},
120
content: [
121
{
122
tag: 'participating',
123
attrs: {},
124
content: [
125
{ tag: 'participants', attrs: {} },
126
{ tag: 'description', attrs: {} }
127
]
128
}
129
]
130
});
131
const data = {};
132
const groupsChild = WABinary_1.getBinaryNodeChild(result, 'groups');
133
if (groupsChild) {
134
const groups = WABinary_1.getBinaryNodeChildren(groupsChild, 'group');
135
for (const groupNode of groups) {
136
const meta = exports.extractGroupMetadata({
137
tag: 'result',
138
attrs: {},
139
content: [groupNode]
140
});
141
data[meta.id] = meta;
142
}
143
}
144
return data;
145
}
146
};
147
};
148
exports.makeGroupsSocket = makeGroupsSocket;
149
const extractGroupMetadata = (result) => {
150
var _a, _b;
151
const group = WABinary_1.getBinaryNodeChild(result, 'group');
152
const descChild = WABinary_1.getBinaryNodeChild(group, 'description');
153
let desc;
154
let descId;
155
if (descChild) {
156
desc = (_a = WABinary_1.getBinaryNodeChild(descChild, 'body')) === null || _a === void 0 ? void 0 : _a.content;
157
descId = descChild.attrs.id;
158
}
159
const groupId = group.attrs.id.includes('@') ? group.attrs.id : WABinary_1.jidEncode(group.attrs.id, 'g.us');
160
const eph = (_b = WABinary_1.getBinaryNodeChild(group, 'ephemeral')) === null || _b === void 0 ? void 0 : _b.attrs.expiration;
161
const metadata = {
162
id: groupId,
163
subject: group.attrs.subject,
164
creation: +group.attrs.creation,
165
owner: group.attrs.creator ? WABinary_1.jidNormalizedUser(group.attrs.creator) : undefined,
166
desc,
167
descId,
168
restrict: !!WABinary_1.getBinaryNodeChild(group, 'locked'),
169
announce: !!WABinary_1.getBinaryNodeChild(group, 'announcement'),
170
participants: WABinary_1.getBinaryNodeChildren(group, 'participant').map(({ attrs }) => {
171
return {
172
id: attrs.jid,
173
admin: attrs.type || null,
174
};
175
}),
176
ephemeralDuration: eph ? +eph : undefined
177
};
178
return metadata;
179
};
180
exports.extractGroupMetadata = extractGroupMetadata;
181
182