Path: blob/master/node_modules/@adiwajshing/baileys/lib/Socket/messages-recv.js
2593 views
"use strict";1Object.defineProperty(exports, "__esModule", { value: true });2exports.makeMessagesRecvSocket = void 0;3const WAProto_1 = require("../../WAProto");4const Defaults_1 = require("../Defaults");5const Types_1 = require("../Types");6const Utils_1 = require("../Utils");7const make_mutex_1 = require("../Utils/make-mutex");8const WABinary_1 = require("../WABinary");9const chats_1 = require("./chats");10const groups_1 = require("./groups");11const STATUS_MAP = {12'played': WAProto_1.proto.WebMessageInfo.WebMessageInfoStatus.PLAYED,13'read': WAProto_1.proto.WebMessageInfo.WebMessageInfoStatus.READ,14'read-self': WAProto_1.proto.WebMessageInfo.WebMessageInfoStatus.READ15};16const getStatusFromReceiptType = (type) => {17const status = STATUS_MAP[type];18if (typeof type === 'undefined') {19return WAProto_1.proto.WebMessageInfo.WebMessageInfoStatus.DELIVERY_ACK;20}21return status;22};23const makeMessagesRecvSocket = (config) => {24const { logger } = config;25const sock = chats_1.makeChatsSocket(config);26const { ev, authState, ws, assertSessions, assertingPreKeys, sendNode, relayMessage, sendReceipt, resyncMainAppState, } = sock;27/** this mutex ensures that the notifications (receipts, messages etc.) are processed in order */28const processingMutex = make_mutex_1.makeKeyedMutex();29/** this mutex ensures that each retryRequest will wait for the previous one to finish */30const retryMutex = make_mutex_1.makeMutex();31const msgRetryMap = config.msgRetryCounterMap || {};32const historyCache = new Set();33const sendMessageAck = async ({ tag, attrs }, extraAttrs) => {34const stanza = {35tag: 'ack',36attrs: {37id: attrs.id,38to: attrs.from,39...extraAttrs,40}41};42if (!!attrs.participant) {43stanza.attrs.participant = attrs.participant;44}45logger.debug({ recv: attrs, sent: stanza.attrs }, `sent "${tag}" ack`);46await sendNode(stanza);47};48const sendRetryRequest = async (node) => {49const msgId = node.attrs.id;50const retryCount = msgRetryMap[msgId] || 1;51if (retryCount >= 5) {52logger.debug({ retryCount, msgId }, 'reached retry limit, clearing');53delete msgRetryMap[msgId];54return;55}56msgRetryMap[msgId] = retryCount + 1;57const isGroup = !!node.attrs.participant;58const { account, signedPreKey, signedIdentityKey: identityKey } = authState.creds;59const deviceIdentity = WAProto_1.proto.ADVSignedDeviceIdentity.encode(account).finish();60await assertingPreKeys(1, async (preKeys) => {61const [keyId] = Object.keys(preKeys);62const key = preKeys[+keyId];63const decFrom = node.attrs.from ? WABinary_1.jidDecode(node.attrs.from) : undefined;64const receipt = {65tag: 'receipt',66attrs: {67id: msgId,68type: 'retry',69to: isGroup ? node.attrs.from : WABinary_1.jidEncode(decFrom.user, 's.whatsapp.net', decFrom.device, 0)70},71content: [72{73tag: 'retry',74attrs: {75count: retryCount.toString(),76id: node.attrs.id,77t: node.attrs.t,78v: '1'79}80},81{82tag: 'registration',83attrs: {},84content: Utils_1.encodeBigEndian(authState.creds.registrationId)85}86]87};88if (node.attrs.recipient) {89receipt.attrs.recipient = node.attrs.recipient;90}91if (node.attrs.participant) {92receipt.attrs.participant = node.attrs.participant;93}94if (retryCount > 1) {95const exec = Utils_1.generateSignalPubKey(Buffer.from(Defaults_1.KEY_BUNDLE_TYPE)).slice(0, 1);96receipt.content.push({97tag: 'keys',98attrs: {},99content: [100{ tag: 'type', attrs: {}, content: exec },101{ tag: 'identity', attrs: {}, content: identityKey.public },102Utils_1.xmppPreKey(key, +keyId),103Utils_1.xmppSignedPreKey(signedPreKey),104{ tag: 'device-identity', attrs: {}, content: deviceIdentity }105]106});107}108await sendNode(receipt);109logger.info({ msgAttrs: node.attrs, retryCount }, 'sent retry receipt');110});111};112const processMessage = async (message, chatUpdate) => {113var _a;114const protocolMsg = (_a = message.message) === null || _a === void 0 ? void 0 : _a.protocolMessage;115if (protocolMsg) {116switch (protocolMsg.type) {117case WAProto_1.proto.ProtocolMessage.ProtocolMessageType.HISTORY_SYNC_NOTIFICATION:118const histNotification = protocolMsg.historySyncNotification;119logger.info({ histNotification, id: message.key.id }, 'got history notification');120const { chats, contacts, messages, isLatest } = await Utils_1.downloadAndProcessHistorySyncNotification(histNotification, historyCache);121const meJid = authState.creds.me.id;122await sendNode({123tag: 'receipt',124attrs: {125id: message.key.id,126type: 'hist_sync',127to: WABinary_1.jidEncode(WABinary_1.jidDecode(meJid).user, 'c.us')128}129});130if (chats.length) {131ev.emit('chats.set', { chats, isLatest });132}133if (messages.length) {134ev.emit('messages.set', { messages, isLatest });135}136if (contacts.length) {137ev.emit('contacts.set', { contacts });138}139break;140case WAProto_1.proto.ProtocolMessage.ProtocolMessageType.APP_STATE_SYNC_KEY_SHARE:141const keys = protocolMsg.appStateSyncKeyShare.keys;142if (keys === null || keys === void 0 ? void 0 : keys.length) {143let newAppStateSyncKeyId = '';144for (const { keyData, keyId } of keys) {145const strKeyId = Buffer.from(keyId.keyId).toString('base64');146logger.info({ strKeyId }, 'injecting new app state sync key');147await authState.keys.set({ 'app-state-sync-key': { [strKeyId]: keyData } });148newAppStateSyncKeyId = strKeyId;149}150ev.emit('creds.update', { myAppStateKeyId: newAppStateSyncKeyId });151resyncMainAppState();152}153else {154[155logger.info({ protocolMsg }, 'recv app state sync with 0 keys')156];157}158break;159case WAProto_1.proto.ProtocolMessage.ProtocolMessageType.REVOKE:160ev.emit('messages.update', [161{162key: {163...message.key,164id: protocolMsg.key.id165},166update: { message: null, messageStubType: Types_1.WAMessageStubType.REVOKE, key: message.key }167}168]);169break;170case WAProto_1.proto.ProtocolMessage.ProtocolMessageType.EPHEMERAL_SETTING:171chatUpdate.ephemeralSettingTimestamp = Utils_1.toNumber(message.messageTimestamp);172chatUpdate.ephemeralExpiration = protocolMsg.ephemeralExpiration || null;173break;174}175}176else if (message.messageStubType) {177const meJid = authState.creds.me.id;178const jid = message.key.remoteJid;179//let actor = whatsappID (message.participant)180let participants;181const emitParticipantsUpdate = (action) => (ev.emit('group-participants.update', { id: jid, participants, action }));182const emitGroupUpdate = (update) => {183ev.emit('groups.update', [{ id: jid, ...update }]);184};185switch (message.messageStubType) {186case Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE:187case Types_1.WAMessageStubType.GROUP_PARTICIPANT_REMOVE:188participants = message.messageStubParameters;189emitParticipantsUpdate('remove');190// mark the chat read only if you left the group191if (participants.includes(meJid)) {192chatUpdate.readOnly = true;193}194break;195case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD:196case Types_1.WAMessageStubType.GROUP_PARTICIPANT_INVITE:197case Types_1.WAMessageStubType.GROUP_PARTICIPANT_ADD_REQUEST_JOIN:198participants = message.messageStubParameters;199if (participants.includes(meJid)) {200chatUpdate.readOnly = false;201}202emitParticipantsUpdate('add');203break;204case Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE:205const announceValue = message.messageStubParameters[0];206emitGroupUpdate({ announce: announceValue === 'true' || announceValue === 'on' });207break;208case Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT:209const restrictValue = message.messageStubParameters[0];210emitGroupUpdate({ restrict: restrictValue === 'true' || restrictValue === 'on' });211break;212case Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT:213chatUpdate.name = message.messageStubParameters[0];214emitGroupUpdate({ subject: chatUpdate.name });215break;216}217}218};219const processNotification = (node) => {220const result = {};221const [child] = WABinary_1.getAllBinaryNodeChildren(node);222if (node.attrs.type === 'w:gp2') {223switch (child === null || child === void 0 ? void 0 : child.tag) {224case 'create':225const metadata = groups_1.extractGroupMetadata(child);226result.messageStubType = Types_1.WAMessageStubType.GROUP_CREATE;227result.messageStubParameters = [metadata.subject];228result.key = { participant: metadata.owner };229ev.emit('chats.upsert', [{230id: metadata.id,231name: metadata.subject,232conversationTimestamp: metadata.creation,233}]);234ev.emit('groups.upsert', [metadata]);235break;236case 'ephemeral':237case 'not_ephemeral':238result.message = {239protocolMessage: {240type: WAProto_1.proto.ProtocolMessage.ProtocolMessageType.EPHEMERAL_SETTING,241ephemeralExpiration: +(child.attrs.expiration || 0)242}243};244break;245case 'promote':246case 'demote':247case 'remove':248case 'add':249case 'leave':250const stubType = `GROUP_PARTICIPANT_${child.tag.toUpperCase()}`;251result.messageStubType = Types_1.WAMessageStubType[stubType];252const participants = WABinary_1.getBinaryNodeChildren(child, 'participant').map(p => p.attrs.jid);253if (participants.length === 1 &&254// if recv. "remove" message and sender removed themselves255// mark as left256WABinary_1.areJidsSameUser(participants[0], node.attrs.participant) &&257child.tag === 'remove') {258result.messageStubType = Types_1.WAMessageStubType.GROUP_PARTICIPANT_LEAVE;259}260result.messageStubParameters = participants;261break;262case 'subject':263result.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_SUBJECT;264result.messageStubParameters = [child.attrs.subject];265break;266case 'announcement':267case 'not_announcement':268result.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_ANNOUNCE;269result.messageStubParameters = [(child.tag === 'announcement') ? 'on' : 'off'];270break;271case 'locked':272case 'unlocked':273result.messageStubType = Types_1.WAMessageStubType.GROUP_CHANGE_RESTRICT;274result.messageStubParameters = [(child.tag === 'locked') ? 'on' : 'off'];275break;276}277}278else {279switch (child.tag) {280case 'devices':281const devices = WABinary_1.getBinaryNodeChildren(child, 'device');282if (WABinary_1.areJidsSameUser(child.attrs.jid, authState.creds.me.id)) {283const deviceJids = devices.map(d => d.attrs.jid);284logger.info({ deviceJids }, 'got my own devices');285}286break;287}288}289if (Object.keys(result).length) {290return result;291}292};293// recv a message294ws.on('CB:message', (stanza) => {295const { fullMessage: msg, decryptionTask } = Utils_1.decodeMessageStanza(stanza, authState);296processingMutex.mutex(msg.key.remoteJid, async () => {297await decryptionTask;298// message failed to decrypt299if (msg.messageStubType === WAProto_1.proto.WebMessageInfo.WebMessageInfoStubType.CIPHERTEXT) {300logger.error({ msgId: msg.key.id, params: msg.messageStubParameters }, 'failure in decrypting message');301retryMutex.mutex(async () => await sendRetryRequest(stanza));302}303else {304await sendMessageAck(stanza, { class: 'receipt' });305// no type in the receipt => message delivered306await sendReceipt(msg.key.remoteJid, msg.key.participant, [msg.key.id], undefined);307logger.debug({ msg: msg.key }, 'sent delivery receipt');308}309msg.key.remoteJid = WABinary_1.jidNormalizedUser(msg.key.remoteJid);310ev.emit('messages.upsert', { messages: [msg], type: stanza.attrs.offline ? 'append' : 'notify' });311});312});313ws.on('CB:ack,class:message', async (node) => {314await sendNode({315tag: 'ack',316attrs: {317class: 'receipt',318id: node.attrs.id,319from: node.attrs.from320}321});322logger.debug({ attrs: node.attrs }, 'sending receipt for ack');323});324ws.on('CB:call', async (node) => {325logger.info({ node }, 'recv call');326const [child] = WABinary_1.getAllBinaryNodeChildren(node);327if (!!(child === null || child === void 0 ? void 0 : child.tag)) {328await sendMessageAck(node, { class: 'call', type: child.tag });329}330});331const sendMessagesAgain = async (key, ids) => {332const msgs = await Promise.all(ids.map(id => (config.getMessage({ ...key, id }))));333const participant = key.participant || key.remoteJid;334await assertSessions([participant], true);335if (WABinary_1.isJidGroup(key.remoteJid)) {336await authState.keys.set({ 'sender-key-memory': { [key.remoteJid]: null } });337}338logger.debug({ participant }, 'forced new session for retry recp');339for (let i = 0; i < msgs.length; i++) {340if (msgs[i]) {341await relayMessage(key.remoteJid, msgs[i], {342messageId: ids[i],343participant344});345}346else {347logger.debug({ jid: key.remoteJid, id: ids[i] }, 'recv retry request, but message not available');348}349}350};351const handleReceipt = async (node) => {352var _a;353let shouldAck = true;354const { attrs, content } = node;355const isNodeFromMe = WABinary_1.areJidsSameUser(attrs.participant || attrs.from, (_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.id);356const remoteJid = !isNodeFromMe || WABinary_1.isJidGroup(attrs.from) ? attrs.from : attrs.recipient;357const fromMe = !attrs.recipient;358const ids = [attrs.id];359if (Array.isArray(content)) {360const items = WABinary_1.getBinaryNodeChildren(content[0], 'item');361ids.push(...items.map(i => i.attrs.id));362}363const key = {364remoteJid,365id: '',366fromMe,367participant: attrs.participant368};369await processingMutex.mutex(remoteJid, async () => {370const status = getStatusFromReceiptType(attrs.type);371if (typeof status !== 'undefined' &&372(373// basically, we only want to know when a message from us has been delivered to/read by the other person374// or another device of ours has read some messages375status > WAProto_1.proto.WebMessageInfo.WebMessageInfoStatus.DELIVERY_ACK ||376!isNodeFromMe)) {377if (WABinary_1.isJidGroup(remoteJid)) {378const updateKey = status === WAProto_1.proto.WebMessageInfo.WebMessageInfoStatus.DELIVERY_ACK ? 'receiptTimestamp' : 'readTimestamp';379ev.emit('message-receipt.update', ids.map(id => ({380key: { ...key, id },381receipt: {382userJid: WABinary_1.jidNormalizedUser(attrs.participant),383[updateKey]: +attrs.t384}385})));386}387else {388ev.emit('messages.update', ids.map(id => ({389key: { ...key, id },390update: { status }391})));392}393}394if (attrs.type === 'retry') {395// correctly set who is asking for the retry396key.participant = key.participant || attrs.from;397if (key.fromMe) {398try {399logger.debug({ attrs }, 'recv retry request');400await sendMessagesAgain(key, ids);401}402catch (error) {403logger.error({ key, ids, trace: error.stack }, 'error in sending message again');404shouldAck = false;405}406}407else {408logger.info({ attrs, key }, 'recv retry for not fromMe message');409}410}411if (shouldAck) {412await sendMessageAck(node, { class: 'receipt', type: attrs.type });413}414});415};416ws.on('CB:receipt', handleReceipt);417ws.on('CB:notification', async (node) => {418const remoteJid = node.attrs.from;419processingMutex.mutex(remoteJid, () => {420const msg = processNotification(node);421if (msg) {422const fromMe = WABinary_1.areJidsSameUser(node.attrs.participant || node.attrs.from, authState.creds.me.id);423msg.key = {424remoteJid: node.attrs.from,425fromMe,426participant: node.attrs.participant,427id: node.attrs.id,428...(msg.key || {})429};430msg.messageTimestamp = +node.attrs.t;431const fullMsg = WAProto_1.proto.WebMessageInfo.fromObject(msg);432ev.emit('messages.upsert', { messages: [fullMsg], type: 'append' });433}434});435await sendMessageAck(node, { class: 'notification', type: node.attrs.type });436});437ev.on('messages.upsert', async ({ messages, type }) => {438var _a;439if (type === 'notify' || type === 'append') {440const chat = { id: messages[0].key.remoteJid };441const contactNameUpdates = {};442for (const msg of messages) {443if (!!msg.pushName) {444const jid = msg.key.fromMe ? WABinary_1.jidNormalizedUser(authState.creds.me.id) : (msg.key.participant || msg.key.remoteJid);445contactNameUpdates[jid] = msg.pushName;446// update our pushname too447if (msg.key.fromMe && ((_a = authState.creds.me) === null || _a === void 0 ? void 0 : _a.name) !== msg.pushName) {448ev.emit('creds.update', { me: { ...authState.creds.me, name: msg.pushName } });449}450}451await processingMutex.mutex('p-' + chat.id, () => processMessage(msg, chat));452if (!!msg.message && !msg.message.protocolMessage) {453chat.conversationTimestamp = Utils_1.toNumber(msg.messageTimestamp);454if (!msg.key.fromMe) {455chat.unreadCount = (chat.unreadCount || 0) + 1;456}457}458}459if (Object.keys(chat).length > 1) {460ev.emit('chats.update', [chat]);461}462if (Object.keys(contactNameUpdates).length) {463ev.emit('contacts.update', Object.keys(contactNameUpdates).map(id => ({ id, notify: contactNameUpdates[id] })));464}465}466});467return { ...sock, processMessage, sendMessageAck, sendRetryRequest };468};469exports.makeMessagesRecvSocket = makeMessagesRecvSocket;470471472