Path: blob/master/node_modules/@adiwajshing/baileys/lib/Socket/groups.js
1129 views
"use strict";1Object.defineProperty(exports, "__esModule", { value: true });2exports.extractGroupMetadata = exports.makeGroupsSocket = void 0;3const WAProto_1 = require("../../WAProto");4const Types_1 = require("../Types");5const Utils_1 = require("../Utils");6const WABinary_1 = require("../WABinary");7const chats_1 = require("./chats");8const makeGroupsSocket = (config) => {9const sock = (0, chats_1.makeChatsSocket)(config);10const { authState, ev, query, upsertMessage } = sock;11const groupQuery = async (jid, type, content) => (query({12tag: 'iq',13attrs: {14type,15xmlns: 'w:g2',16to: jid,17},18content19}));20const groupMetadata = async (jid) => {21const result = await groupQuery(jid, 'get', [{ tag: 'query', attrs: { request: 'interactive' } }]);22return (0, exports.extractGroupMetadata)(result);23};24return {25...sock,26groupMetadata,27groupCreate: async (subject, participants) => {28const key = (0, Utils_1.generateMessageID)();29const result = await groupQuery('@g.us', 'set', [30{31tag: 'create',32attrs: {33subject,34key35},36content: participants.map(jid => ({37tag: 'participant',38attrs: { jid }39}))40}41]);42return (0, exports.extractGroupMetadata)(result);43},44groupLeave: async (id) => {45await groupQuery('@g.us', 'set', [46{47tag: 'leave',48attrs: {},49content: [50{ tag: 'group', attrs: { id } }51]52}53]);54},55groupUpdateSubject: async (jid, subject) => {56await groupQuery(jid, 'set', [57{58tag: 'subject',59attrs: {},60content: Buffer.from(subject, 'utf-8')61}62]);63},64groupParticipantsUpdate: async (jid, participants, action) => {65const result = await groupQuery(jid, 'set', [66{67tag: action,68attrs: {},69content: participants.map(jid => ({70tag: 'participant',71attrs: { jid }72}))73}74]);75const node = (0, WABinary_1.getBinaryNodeChild)(result, action);76const participantsAffected = (0, WABinary_1.getBinaryNodeChildren)(node, 'participant');77return participantsAffected.map(p => {78return { status: p.attrs.error || '200', jid: p.attrs.jid };79});80},81groupUpdateDescription: async (jid, description) => {82var _a;83const metadata = await groupMetadata(jid);84const prev = (_a = metadata.descId) !== null && _a !== void 0 ? _a : null;85await groupQuery(jid, 'set', [86{87tag: 'description',88attrs: {89...(description ? { id: (0, Utils_1.generateMessageID)() } : { delete: 'true' }),90...(prev ? { prev } : {})91},92content: description ? [93{ tag: 'body', attrs: {}, content: Buffer.from(description, 'utf-8') }94] : undefined95}96]);97},98groupInviteCode: async (jid) => {99const result = await groupQuery(jid, 'get', [{ tag: 'invite', attrs: {} }]);100const inviteNode = (0, WABinary_1.getBinaryNodeChild)(result, 'invite');101return inviteNode === null || inviteNode === void 0 ? void 0 : inviteNode.attrs.code;102},103groupRevokeInvite: async (jid) => {104const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]);105const inviteNode = (0, WABinary_1.getBinaryNodeChild)(result, 'invite');106return inviteNode === null || inviteNode === void 0 ? void 0 : inviteNode.attrs.code;107},108groupAcceptInvite: async (code) => {109const results = await groupQuery('@g.us', 'set', [{ tag: 'invite', attrs: { code } }]);110const result = (0, WABinary_1.getBinaryNodeChild)(results, 'group');111return result === null || result === void 0 ? void 0 : result.attrs.jid;112},113/**114* accept a GroupInviteMessage115* @param key the key of the invite message, or optionally only provide the jid of the person who sent the invite116* @param inviteMessage the message to accept117*/118groupAcceptInviteV4: ev.createBufferedFunction(async (key, inviteMessage) => {119key = typeof key === 'string' ? { remoteJid: key } : key;120const results = await groupQuery(inviteMessage.groupJid, 'set', [{121tag: 'accept',122attrs: {123code: inviteMessage.inviteCode,124expiration: inviteMessage.inviteExpiration.toString(),125admin: key.remoteJid126}127}]);128// if we have the full message key129// update the invite message to be expired130if (key.id) {131// create new invite message that is expired132inviteMessage = WAProto_1.proto.Message.GroupInviteMessage.fromObject(inviteMessage);133inviteMessage.inviteExpiration = 0;134inviteMessage.inviteCode = '';135ev.emit('messages.update', [136{137key,138update: {139message: {140groupInviteMessage: inviteMessage141}142}143}144]);145}146// generate the group add message147await upsertMessage({148key: {149remoteJid: inviteMessage.groupJid,150id: (0, Utils_1.generateMessageID)(),151fromMe: false,152participant: key.remoteJid,153},154messageStubType: Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD,155messageStubParameters: [156authState.creds.me.id157],158participant: key.remoteJid,159messageTimestamp: (0, Utils_1.unixTimestampSeconds)()160}, 'notify');161return results.attrs.from;162}),163groupGetInviteInfo: async (code) => {164const results = await groupQuery('@g.us', 'get', [{ tag: 'invite', attrs: { code } }]);165return (0, exports.extractGroupMetadata)(results);166},167groupToggleEphemeral: async (jid, ephemeralExpiration) => {168const content = ephemeralExpiration ?169{ tag: 'ephemeral', attrs: { expiration: ephemeralExpiration.toString() } } :170{ tag: 'not_ephemeral', attrs: {} };171await groupQuery(jid, 'set', [content]);172},173groupSettingUpdate: async (jid, setting) => {174await groupQuery(jid, 'set', [{ tag: setting, attrs: {} }]);175},176groupFetchAllParticipating: async () => {177const result = await query({178tag: 'iq',179attrs: {180to: '@g.us',181xmlns: 'w:g2',182type: 'get',183},184content: [185{186tag: 'participating',187attrs: {},188content: [189{ tag: 'participants', attrs: {} },190{ tag: 'description', attrs: {} }191]192}193]194});195const data = {};196const groupsChild = (0, WABinary_1.getBinaryNodeChild)(result, 'groups');197if (groupsChild) {198const groups = (0, WABinary_1.getBinaryNodeChildren)(groupsChild, 'group');199for (const groupNode of groups) {200const meta = (0, exports.extractGroupMetadata)({201tag: 'result',202attrs: {},203content: [groupNode]204});205data[meta.id] = meta;206}207}208return data;209}210};211};212exports.makeGroupsSocket = makeGroupsSocket;213const extractGroupMetadata = (result) => {214var _a, _b;215const group = (0, WABinary_1.getBinaryNodeChild)(result, 'group');216const descChild = (0, WABinary_1.getBinaryNodeChild)(group, 'description');217let desc;218let descId;219if (descChild) {220desc = (_a = (0, WABinary_1.getBinaryNodeChild)(descChild, 'body')) === null || _a === void 0 ? void 0 : _a.content;221descId = descChild.attrs.id;222}223const groupId = group.attrs.id.includes('@') ? group.attrs.id : (0, WABinary_1.jidEncode)(group.attrs.id, 'g.us');224const eph = (_b = (0, WABinary_1.getBinaryNodeChild)(group, 'ephemeral')) === null || _b === void 0 ? void 0 : _b.attrs.expiration;225const metadata = {226id: groupId,227subject: group.attrs.subject,228subjectOwner: group.attrs.s_o,229subjectTime: +group.attrs.s_t,230size: +group.attrs.size,231creation: +group.attrs.creation,232owner: group.attrs.creator ? (0, WABinary_1.jidNormalizedUser)(group.attrs.creator) : undefined,233desc,234descId,235restrict: !!(0, WABinary_1.getBinaryNodeChild)(group, 'locked'),236announce: !!(0, WABinary_1.getBinaryNodeChild)(group, 'announcement'),237participants: (0, WABinary_1.getBinaryNodeChildren)(group, 'participant').map(({ attrs }) => {238return {239id: attrs.jid,240admin: attrs.type || null,241};242}),243ephemeralDuration: eph ? +eph : undefined244};245return metadata;246};247exports.extractGroupMetadata = extractGroupMetadata;248249250