Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/history.js
1129 views
"use strict";1Object.defineProperty(exports, "__esModule", { value: true });2exports.isHistoryMsg = exports.downloadAndProcessHistorySyncNotification = exports.processHistoryMessage = exports.downloadHistory = void 0;3const util_1 = require("util");4const zlib_1 = require("zlib");5const WAProto_1 = require("../../WAProto");6const WABinary_1 = require("../WABinary");7const generics_1 = require("./generics");8const messages_1 = require("./messages");9const messages_media_1 = require("./messages-media");10const inflatePromise = (0, util_1.promisify)(zlib_1.inflate);11const downloadHistory = async (msg) => {12const stream = await (0, messages_media_1.downloadContentFromMessage)(msg, 'history');13let buffer = Buffer.from([]);14for await (const chunk of stream) {15buffer = Buffer.concat([buffer, chunk]);16}17// decompress buffer18buffer = await inflatePromise(buffer);19const syncData = WAProto_1.proto.HistorySync.decode(buffer);20return syncData;21};22exports.downloadHistory = downloadHistory;23const processHistoryMessage = (item, historyCache, recvChats) => {24const messages = [];25const contacts = [];26const chats = [];27switch (item.syncType) {28case WAProto_1.proto.HistorySync.HistorySyncType.INITIAL_BOOTSTRAP:29case WAProto_1.proto.HistorySync.HistorySyncType.RECENT:30for (const chat of item.conversations) {31const contactId = `c:${chat.id}`;32if (chat.name && !historyCache.has(contactId)) {33contacts.push({ id: chat.id, name: chat.name });34historyCache.add(contactId);35}36const msgs = chat.messages || [];37for (const item of msgs) {38const message = item.message;39const uqId = `${message.key.remoteJid}:${message.key.id}`;40if (!historyCache.has(uqId)) {41messages.push(message);42const curItem = recvChats[message.key.remoteJid];43const timestamp = (0, generics_1.toNumber)(message.messageTimestamp);44if (!message.key.fromMe && (!curItem || timestamp > curItem.lastMsgRecvTimestamp)) {45recvChats[chat.id] = { lastMsgRecvTimestamp: timestamp };46// keep only the most recent message in the chat array47chat.messages = [{ message }];48}49historyCache.add(uqId);50}51}52if (!historyCache.has(chat.id)) {53if ((0, WABinary_1.isJidUser)(chat.id) && chat.readOnly && chat.archived) {54chat.readOnly = false;55}56chats.push(chat);57historyCache.add(chat.id);58}59}60break;61case WAProto_1.proto.HistorySync.HistorySyncType.PUSH_NAME:62for (const c of item.pushnames) {63const contactId = `c:${c.id}`;64if (!historyCache.has(contactId)) {65contacts.push({ notify: c.pushname, id: c.id });66historyCache.add(contactId);67}68}69break;70case WAProto_1.proto.HistorySync.HistorySyncType.INITIAL_STATUS_V3:71// TODO72break;73}74const didProcess = !!(chats.length || messages.length || contacts.length);75return {76chats,77contacts,78messages,79didProcess,80};81};82exports.processHistoryMessage = processHistoryMessage;83const downloadAndProcessHistorySyncNotification = async (msg, historyCache, recvChats) => {84const historyMsg = await (0, exports.downloadHistory)(msg);85return (0, exports.processHistoryMessage)(historyMsg, historyCache, recvChats);86};87exports.downloadAndProcessHistorySyncNotification = downloadAndProcessHistorySyncNotification;88const isHistoryMsg = (message) => {89var _a;90const normalizedContent = !!message ? (0, messages_1.normalizeMessageContent)(message) : undefined;91const isAnyHistoryMsg = !!((_a = normalizedContent === null || normalizedContent === void 0 ? void 0 : normalizedContent.protocolMessage) === null || _a === void 0 ? void 0 : _a.historySyncNotification);92return isAnyHistoryMsg;93};94exports.isHistoryMsg = isHistoryMsg;959697