Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jajbshjahavahh
GitHub Repository: jajbshjahavahh/Gojo-Satoru
Path: blob/master/node_modules/@adiwajshing/baileys/lib/LegacySocket/groups.js
2593 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 = 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 ? WABinary_1.jidNormalizedUser(metadata.owner) : undefined,
46
desc: metadata.desc,
47
descOwner: metadata.descOwner,
48
participants: metadata.participants.map(p => ({
49
id: WABinary_1.jidNormalizedUser(p.id),
50
admin: p.isSuperAdmin ? 'super-admin' : p.isAdmin ? 'admin' : undefined
51
}))
52
};
53
return meta;
54
};
55
/** Get the metadata (works after you've left the group also) */
56
const groupMetadataMinimal = async (jid) => {
57
const { attrs, content } = await query({
58
json: {
59
tag: 'query',
60
attrs: { type: 'group', jid: jid, epoch: currentEpoch().toString() }
61
},
62
binaryTag: [Types_1.WAMetric.group, Types_1.WAFlag.ignore],
63
expect200: true
64
});
65
const participants = [];
66
let desc;
67
if (Array.isArray(content) && Array.isArray(content[0].content)) {
68
const nodes = content[0].content;
69
for (const item of nodes) {
70
if (item.tag === 'participant') {
71
participants.push({
72
id: item.attrs.jid,
73
isAdmin: item.attrs.type === 'admin',
74
isSuperAdmin: false
75
});
76
}
77
else if (item.tag === 'description') {
78
desc = item.content.toString('utf-8');
79
}
80
}
81
}
82
const meta = {
83
id: jid,
84
owner: attrs === null || attrs === void 0 ? void 0 : attrs.creator,
85
creation: +(attrs === null || attrs === void 0 ? void 0 : attrs.create),
86
subject: null,
87
desc,
88
participants
89
};
90
return meta;
91
};
92
socketEvents.on('CB:Chat,cmd:action', (json) => {
93
/*const data = json[1].data
94
if (data) {
95
const emitGroupParticipantsUpdate = (action: WAParticipantAction) => this.emitParticipantsUpdate
96
(json[1].id, data[2].participants.map(whatsappID), action)
97
const emitGroupUpdate = (data: Partial<WAGroupMetadata>) => this.emitGroupUpdate(json[1].id, data)
98
99
switch (data[0]) {
100
case "promote":
101
emitGroupParticipantsUpdate('promote')
102
break
103
case "demote":
104
emitGroupParticipantsUpdate('demote')
105
break
106
case "desc_add":
107
emitGroupUpdate({ ...data[2], descOwner: data[1] })
108
break
109
default:
110
this.logger.debug({ unhandled: true }, json)
111
break
112
}
113
}*/
114
});
115
return {
116
...sock,
117
groupMetadata: async (jid, minimal) => {
118
let result;
119
if (minimal) {
120
result = await groupMetadataMinimal(jid);
121
}
122
else {
123
result = await groupMetadataFull(jid);
124
}
125
return result;
126
},
127
/**
128
* Create a group
129
* @param title like, the title of the group
130
* @param participants people to include in the group
131
*/
132
groupCreate: async (title, participants) => {
133
const response = await groupQuery('create', null, title, participants);
134
const gid = response.gid;
135
let metadata;
136
try {
137
metadata = await groupMetadataFull(gid);
138
}
139
catch (error) {
140
logger.warn(`error in group creation: ${error}, switching gid & checking`);
141
// if metadata is not available
142
const comps = gid.replace('@g.us', '').split('-');
143
response.gid = `${comps[0]}-${+comps[1] + 1}@g.us`;
144
metadata = await groupMetadataFull(gid);
145
logger.warn(`group ID switched from ${gid} to ${response.gid}`);
146
}
147
ev.emit('chats.upsert', [
148
{
149
id: response.gid,
150
name: title,
151
conversationTimestamp: generics_1.unixTimestampSeconds(),
152
unreadCount: 0
153
}
154
]);
155
return metadata;
156
},
157
/**
158
* Leave a group
159
* @param jid the ID of the group
160
*/
161
groupLeave: async (id) => {
162
await groupQuery('leave', id);
163
ev.emit('chats.update', [{ id, readOnly: true }]);
164
},
165
/**
166
* Update the subject of the group
167
* @param {string} jid the ID of the group
168
* @param {string} title the new title of the group
169
*/
170
groupUpdateSubject: async (id, title) => {
171
await groupQuery('subject', id, title);
172
ev.emit('chats.update', [{ id, name: title }]);
173
ev.emit('contacts.update', [{ id, name: title }]);
174
ev.emit('groups.update', [{ id: id, subject: title }]);
175
},
176
/**
177
* Update the group description
178
* @param {string} jid the ID of the group
179
* @param {string} title the new title of the group
180
*/
181
groupUpdateDescription: async (jid, description) => {
182
const metadata = await groupMetadataFull(jid);
183
const node = {
184
tag: 'description',
185
attrs: { id: generics_1.generateMessageID(), prev: metadata === null || metadata === void 0 ? void 0 : metadata.descId },
186
content: Buffer.from(description, 'utf-8')
187
};
188
const response = await groupQuery('description', jid, null, null, [node]);
189
ev.emit('groups.update', [{ id: jid, desc: description }]);
190
return response;
191
},
192
/**
193
* Update participants in the group
194
* @param jid the ID of the group
195
* @param participants the people to add
196
*/
197
groupParticipantsUpdate: async (id, participants, action) => {
198
const result = await groupQuery(action, id, null, participants);
199
const jids = Object.keys(result.participants || {});
200
ev.emit('group-participants.update', { id, participants: jids, action });
201
return jids;
202
},
203
/** Query broadcast list info */
204
getBroadcastListInfo: async (jid) => {
205
var _a, _b;
206
const result = await query({
207
json: ['query', 'contact', jid],
208
expect200: true,
209
requiresPhoneConnection: true
210
});
211
const metadata = {
212
subject: result.name,
213
id: jid,
214
creation: undefined,
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: 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