Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/history.js
2593 views
"use strict";1Object.defineProperty(exports, "__esModule", { value: true });2exports.downloadAndProcessHistorySyncNotification = exports.processHistoryMessage = exports.downloadHistory = void 0;3const util_1 = require("util");4const zlib_1 = require("zlib");5const WAProto_1 = require("../../WAProto");6const messages_media_1 = require("./messages-media");7const inflatePromise = util_1.promisify(zlib_1.inflate);8const downloadHistory = async (msg) => {9const stream = await messages_media_1.downloadContentFromMessage(msg, 'history');10let buffer = Buffer.from([]);11for await (const chunk of stream) {12buffer = Buffer.concat([buffer, chunk]);13}14// decompress buffer15buffer = await inflatePromise(buffer);16const syncData = WAProto_1.proto.HistorySync.decode(buffer);17return syncData;18};19exports.downloadHistory = downloadHistory;20const processHistoryMessage = (item, historyCache) => {21const isLatest = historyCache.size === 0;22const messages = [];23const contacts = [];24const chats = [];25switch (item.syncType) {26case WAProto_1.proto.HistorySync.HistorySyncHistorySyncType.INITIAL_BOOTSTRAP:27case WAProto_1.proto.HistorySync.HistorySyncHistorySyncType.RECENT:28for (const chat of item.conversations) {29const contactId = `c:${chat.id}`;30if (chat.name && !historyCache.has(contactId)) {31contacts.push({32id: chat.id,33name: chat.name34});35historyCache.add(contactId);36}37for (const { message } of chat.messages || []) {38const uqId = `${message === null || message === void 0 ? void 0 : message.key.remoteJid}:${message.key.id}`;39if (message && !historyCache.has(uqId)) {40messages.push(message);41historyCache.add(uqId);42}43}44delete chat.messages;45if (!historyCache.has(chat.id)) {46chats.push(chat);47historyCache.add(chat.id);48}49}50break;51case WAProto_1.proto.HistorySync.HistorySyncHistorySyncType.PUSH_NAME:52for (const c of item.pushnames) {53const contactId = `c:${c.id}`;54if (historyCache.has(contactId)) {55contacts.push({ notify: c.pushname, id: c.id });56historyCache.add(contactId);57}58}59break;60case WAProto_1.proto.HistorySync.HistorySyncHistorySyncType.INITIAL_STATUS_V3:61// TODO62break;63}64return {65chats,66contacts,67messages,68isLatest,69};70};71exports.processHistoryMessage = processHistoryMessage;72const downloadAndProcessHistorySyncNotification = async (msg, historyCache) => {73const historyMsg = await exports.downloadHistory(msg);74return exports.processHistoryMessage(historyMsg, historyCache);75};76exports.downloadAndProcessHistorySyncNotification = downloadAndProcessHistorySyncNotification;777879