store: remove built-in store

This commit is contained in:
Rajeh Taher
2025-03-10 17:35:46 +02:00
parent 1c2af09b03
commit 64d09c70b4
7 changed files with 11 additions and 726 deletions

View File

@@ -1,7 +1,7 @@
import { Boom } from '@hapi/boom'
import NodeCache from '@cacheable/node-cache'
import readline from 'readline'
import makeWASocket, { AnyMessageContent, BinaryInfo, delay, DisconnectReason, downloadAndProcessHistorySyncNotification, encodeWAM, fetchLatestBaileysVersion, getAggregateVotesInPollMessage, getHistoryMsg, isJidNewsletter, makeCacheableSignalKeyStore, makeInMemoryStore, proto, useMultiFileAuthState, WAMessageContent, WAMessageKey } from '../src'
import makeWASocket, { AnyMessageContent, BinaryInfo, delay, DisconnectReason, downloadAndProcessHistorySyncNotification, encodeWAM, fetchLatestBaileysVersion, getAggregateVotesInPollMessage, getHistoryMsg, isJidNewsletter, makeCacheableSignalKeyStore, proto, useMultiFileAuthState, WAMessageContent, WAMessageKey } from '../src'
//import MAIN_LOGGER from '../src/Utils/logger'
import open from 'open'
import fs from 'fs'
@@ -10,7 +10,6 @@ import P from 'pino'
const logger = P({ timestamp: () => `,"time":"${new Date().toJSON()}"` }, P.destination('./wa-logs.txt'))
logger.level = 'trace'
const useStore = !process.argv.includes('--no-store')
const doReplies = process.argv.includes('--do-reply')
const usePairingCode = process.argv.includes('--use-pairing-code')
@@ -24,15 +23,6 @@ const onDemandMap = new Map<string, string>()
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
const question = (text: string) => new Promise<string>((resolve) => rl.question(text, resolve))
// the store maintains the data of the WA connection in memory
// can be written out to a file & read from it
const store = useStore ? makeInMemoryStore({ logger }) : undefined
store?.readFromFile('./baileys_store_multi.json')
// save every 10s
setInterval(() => {
store?.writeToFile('./baileys_store_multi.json')
}, 10_000)
// start a connection
const startSock = async() => {
const { state, saveCreds } = await useMultiFileAuthState('baileys_auth_info')
@@ -58,8 +48,6 @@ const startSock = async() => {
getMessage,
})
store?.bind(sock.ev)
// Pairing code for Web clients
if (usePairingCode && !sock.authState.creds.registered) {
// todo move to QR event
@@ -98,7 +86,7 @@ const startSock = async() => {
console.log('Connection closed. You are logged out.')
}
}
// WARNING: THIS WILL SEND A WAM EXAMPLE AND THIS IS A ****CAPTURED MESSAGE.****
// DO NOT ACTUALLY ENABLE THIS UNLESS YOU MODIFIED THE FILE.JSON!!!!!
// THE ANALYTICS IN THE FILE ARE OLD. DO NOT USE THEM.
@@ -124,7 +112,7 @@ const startSock = async() => {
})
const buffer = encodeWAM(binaryInfo);
const result = await sock.sendWAMBuffer(buffer)
console.log(result)
}
@@ -182,11 +170,11 @@ const startSock = async() => {
{}
)
const chatId = onDemandMap.get(
historySyncNotification!.peerDataRequestSessionId!
)
console.log(messages)
onDemandMap.delete(
@@ -209,7 +197,7 @@ const startSock = async() => {
if (msg.message?.conversation || msg.message?.extendedTextMessage?.text) {
const text = msg.message?.conversation || msg.message?.extendedTextMessage?.text
if (text == "requestPlaceholder" && !upsert.requestId) {
const messageId = await sock.requestPlaceholderResend(msg.key)
const messageId = await sock.requestPlaceholderResend(msg.key)
console.log('requested placeholder resync, id=', messageId)
} else if (upsert.requestId) {
console.log('Message received from phone, id=', upsert.requestId, msg)
@@ -217,7 +205,7 @@ const startSock = async() => {
// go to an old chat and send this
if (text == "onDemandHistSync") {
const messageId = await sock.fetchMessageHistory(50, msg.key, msg.messageTimestamp!)
const messageId = await sock.fetchMessageHistory(50, msg.key, msg.messageTimestamp!)
console.log('requested on-demand sync, id=', messageId)
}
}
@@ -240,7 +228,7 @@ const startSock = async() => {
for(const { key, update } of events['messages.update']) {
if(update.pollUpdates) {
const pollCreation = await getMessage(key)
const pollCreation: proto.IMessage = {} // get the poll creation message somehow
if(pollCreation) {
console.log(
'got poll update, aggregation: ',
@@ -292,14 +280,12 @@ const startSock = async() => {
return sock
async function getMessage(key: WAMessageKey): Promise<WAMessageContent | undefined> {
if(store) {
const msg = await store.loadMessage(key.remoteJid!, key.id!)
return msg?.message || undefined
}
// Implement a way to retreive messages that were upserted from messages.upsert
// up to you
// only if store is present
return proto.Message.fromObject({})
}
}
startSock()
startSock()