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