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