UNPKG

@baileys-md/baileys

Version:

Baileys WhatsApp API

86 lines (83 loc) 2.86 kB
//========================================// import PQueue from "p-queue"; // esta lib achei num jogo feito aqui Moçambique* kkkkkk mais que mundo pequeno há... "o qui ela faz eu também não sei kkkkk estuda pará saber fui. export class PreKeyManager { constructor(store, logger) { this.store = store; this.logger = logger; this.queues = new Map(); } getQueue(keyType) { if (!this.queues.has(keyType)) { this.queues.set(keyType, new PQueue({ concurrency: 1 })); } return this.queues.get(keyType); } async processOperations(data, keyType, transactionCache, mutations, isInTransaction) { const keyData = data[keyType]; if (!keyData) return; return this.getQueue(keyType).add(async () => { transactionCache[keyType] = transactionCache[keyType] || {}; mutations[keyType] = mutations[keyType] || {}; const deletions = []; const updates = {}; for (const keyId in keyData) { if (keyData[keyId] === null) { deletions.push(keyId); } else { updates[keyId] = keyData[keyId]; } } if (Object.keys(updates).length > 0) { Object.assign(transactionCache[keyType], updates); Object.assign(mutations[keyType], updates); } if (deletions.length > 0) { await this.processDeletions(keyType, deletions, transactionCache, mutations, isInTransaction); } }); } async processDeletions(keyType, ids, transactionCache, mutations, isInTransaction) { if (isInTransaction) { for (const keyId of ids) { if (transactionCache[keyType]?.[keyId]) { transactionCache[keyType][keyId] = null; mutations[keyType][keyId] = null; } else { this.logger.warn(`Skipping deletion of non-existent ${keyType} in transaction: ${keyId}`); } } } else { const existingKeys = await this.store.get(keyType, ids); for (const keyId of ids) { if (existingKeys[keyId]) { transactionCache[keyType][keyId] = null; mutations[keyType][keyId] = null; } else { this.logger.warn(`Skipping deletion of non-existent ${keyType}: ${keyId}`); } } } } async validateDeletions(data, keyType) { const keyData = data[keyType]; if (!keyData) return; return this.getQueue(keyType).add(async () => { const deletionIds = Object.keys(keyData).filter(id => keyData[id] === null); if (deletionIds.length === 0) return; const existingKeys = await this.store.get(keyType, deletionIds); for (const keyId of deletionIds) { if (!existingKeys[keyId]) { this.logger.warn(`Skipping deletion of non-existent ${keyType}: ${keyId}`); delete data[keyType][keyId]; } } }); } }