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