Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@adiwajshing/baileys/lib/LegacySocket/groups.js
1129 views
1
"use strict";
2
var __importDefault = (this && this.__importDefault) || function (mod) {
3
return (mod && mod.__esModule) ? mod : { "default": mod };
4
};
5
Object.defineProperty(exports, "__esModule", { value: true });
6
const Types_1 = require("../Types");
7
const generics_1 = require("../Utils/generics");
8
const WABinary_1 = require("../WABinary");
9
const messages_1 = __importDefault(require("./messages"));
10
const makeGroupsSocket = (config) => {
11
const { logger } = config;
12
const sock = (0, messages_1.default)(config);
13
const { ev, ws: socketEvents, query, generateMessageTag, currentEpoch, setQuery, state } = sock;
14
/** Generic function for group queries */
15
const groupQuery = async (type, jid, subject, participants, additionalNodes) => {
16
var _a, _b;
17
const tag = generateMessageTag();
18
const result = await setQuery([
19
{
20
tag: 'group',
21
attrs: {
22
author: (_b = (_a = state.legacy) === null || _a === void 0 ? void 0 : _a.user) === null || _b === void 0 ? void 0 : _b.id,
23
id: tag,
24
type: type,
25
jid: jid,
26
subject: subject,
27
},
28
content: participants ?
29
participants.map(jid => ({ tag: 'participant', attrs: { jid } })) :
30
additionalNodes
31
}
32
], [Types_1.WAMetric.group, 136], tag);
33
return result;
34
};
35
/** Get the metadata of the group from WA */
36
const groupMetadataFull = async (jid) => {
37
const metadata = await query({
38
json: ['query', 'GroupMetadata', jid],
39
expect200: true
40
});
41
const meta = {
42
id: metadata.id,
43
subject: metadata.subject,
44
creation: +metadata.creation,
45
owner: metadata.owner ? (0, WABinary_1.jidNormalizedUser)(metadata.owner) : undefined,
46
desc: metadata.desc,
47
descOwner: metadata.descOwner,
48
participants: metadata.participants.map(p => ({
49
id: (0, WABinary_1.jidNormalizedUser)(p.id),
50
admin: p.isSuperAdmin ? 'super-admin' : p.isAdmin ? 'admin' : undefined
51
})),
52
ephemeralDuration: metadata.ephemeralDuration
53
};
54
return meta;
55
};
56
/** Get the metadata (works after you've left the group also) */
57
const groupMetadataMinimal = async (jid) => {
58
const { attrs, content } = await query({
59
json: {
60
tag: 'query',
61
attrs: { type: 'group', jid: jid, epoch: currentEpoch().toString() }
62
},
63
binaryTag: [Types_1.WAMetric.group, Types_1.WAFlag.ignore],
64
expect200: true
65
});
66
const participants = [];
67
let desc;
68
if (Array.isArray(content) && Array.isArray(content[0].content)) {
69
const nodes = content[0].content;
70
for (const item of nodes) {
71
if (item.tag === 'participant') {
72
participants.push({
73
id: item.attrs.jid,
74
isAdmin: item.attrs.type === 'admin',
75
isSuperAdmin: false
76
});
77
}
78
else if (item.tag === 'description') {
79
desc = item.content.toString('utf-8');
80
}
81
}
82
}
83
const meta = {
84
id: jid,
85
owner: attrs === null || attrs === void 0 ? void 0 : attrs.creator,
86
creation: +(attrs === null || attrs === void 0 ? void 0 : attrs.create),
87
subject: '',
88
desc,
89
participants
90
};
91
return meta;
92
};
93
socketEvents.on('CB:Chat,cmd:action', () => {
94
/*const data = json[1].data
95
if (data) {
96
const emitGroupParticipantsUpdate = (action: WAParticipantAction) => this.emitParticipantsUpdate
97
(json[1].id, data[2].participants.map(whatsappID), action)
98
const emitGroupUpdate = (data: Partial<WAGroupMetadata>) => this.emitGroupUpdate(json[1].id, data)
99
100
switch (data[0]) {
101
case "promote":
102
emitGroupParticipantsUpdate('promote')
103
break
104
case "demote":
105
emitGroupParticipantsUpdate('demote')
106
break
107
case "desc_add":
108
emitGroupUpdate({ ...data[2], descOwner: data[1] })
109
break
110
default:
111
this.logger.debug({ unhandled: true }, json)
112
break
113
}
114
}*/
115
});
116
return {
117
...sock,
118
groupMetadata: async (jid, minimal) => {
119
let result;
120
if (minimal) {
121
result = await groupMetadataMinimal(jid);
122
}
123
else {
124
result = await groupMetadataFull(jid);
125
}
126
return result;
127
},
128
/**
129
* Create a group
130
* @param title like, the title of the group
131
* @param participants people to include in the group
132
*/
133
groupCreate: async (title, participants) => {
134
const response = await groupQuery('create', undefined, title, participants);
135
const gid = response.gid;
136
let metadata;
137
try {
138
metadata = await groupMetadataFull(gid);
139
}
140
catch (error) {
141
logger.warn(`error in group creation: ${error}, switching gid & checking`);
142
// if metadata is not available
143
const comps = gid.replace('@g.us', '').split('-');
144
response.gid = `${comps[0]}-${+comps[1] + 1}@g.us`;
145
metadata = await groupMetadataFull(gid);
146
logger.warn(`group ID switched from ${gid} to ${response.gid}`);
147
}
148
ev.emit('chats.upsert', [
149
{
150
id: response.gid,
151
name: title,
152
conversationTimestamp: (0, generics_1.unixTimestampSeconds)(),
153
unreadCount: 0
154
}
155
]);
156
return metadata;
157
},
158
/**
159
* Leave a group
160
* @param jid the ID of the group
161
*/
162
groupLeave: async (id) => {
163
await groupQuery('leave', id);
164
ev.emit('chats.update', [{ id, readOnly: true }]);
165
},
166
/**
167
* Update the subject of the group
168
* @param {string} jid the ID of the group
169
* @param {string} title the new title of the group
170
*/
171
groupUpdateSubject: async (id, title) => {
172
await groupQuery('subject', id, title);
173
ev.emit('chats.update', [{ id, name: title }]);
174
ev.emit('contacts.update', [{ id, name: title }]);
175
ev.emit('groups.update', [{ id: id, subject: title }]);
176
},
177
/**
178
* Update the group description
179
* @param {string} jid the ID of the group
180
* @param {string} title the new title of the group
181
*/
182
groupUpdateDescription: async (jid, description) => {
183
const metadata = await groupMetadataFull(jid);
184
const node = {
185
tag: 'description',
186
attrs: { id: (0, generics_1.generateMessageID)(), prev: metadata === null || metadata === void 0 ? void 0 : metadata.descId },
187
content: Buffer.from(description, 'utf-8')
188
};
189
const response = await groupQuery('description', jid, undefined, undefined, [node]);
190
ev.emit('groups.update', [{ id: jid, desc: description }]);
191
return response;
192
},
193
/**
194
* Update participants in the group
195
* @param jid the ID of the group
196
* @param participants the people to add
197
*/
198
groupParticipantsUpdate: async (id, participants, action) => {
199
const result = await groupQuery(action, id, undefined, participants);
200
const jids = Object.keys(result.participants || {});
201
ev.emit('group-participants.update', { id, participants: jids, action });
202
return Object.keys(result.participants || {}).map(jid => { var _a; return ({ jid, status: (_a = result.participants) === null || _a === void 0 ? void 0 : _a[jid] }); });
203
},
204
/** Query broadcast list info */
205
getBroadcastListInfo: async (jid) => {
206
var _a, _b;
207
const result = await query({
208
json: ['query', 'contact', jid],
209
expect200: true,
210
requiresPhoneConnection: true
211
});
212
const metadata = {
213
subject: result.name,
214
id: jid,
215
owner: (_b = (_a = state.legacy) === null || _a === void 0 ? void 0 : _a.user) === null || _b === void 0 ? void 0 : _b.id,
216
participants: result.recipients.map(({ id }) => ({ id: (0, WABinary_1.jidNormalizedUser)(id), isAdmin: false, isSuperAdmin: false }))
217
};
218
return metadata;
219
},
220
groupInviteCode: async (jid) => {
221
const response = await sock.query({
222
json: ['query', 'inviteCode', jid],
223
expect200: true,
224
requiresPhoneConnection: false
225
});
226
return response.code;
227
}
228
};
229
};
230
exports.default = makeGroupsSocket;
231
232