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