fix: Adjustment in the recording of temporary files and periodic cleaning

This commit is contained in:
Davidson Gomes
2023-06-20 14:57:19 -03:00
parent 30cd8a03eb
commit d359949310
11 changed files with 352 additions and 60 deletions

View File

@@ -1,5 +1,5 @@
import { join } from 'path';
import { ConfigService } from '../../config/env.config';
import { ConfigService, StoreConf } from '../../config/env.config';
import { IInsert, Repository } from '../abstract/abstract.repository';
import { opendirSync, readFileSync, rmSync } from 'fs';
import { ChatRaw, IChatModel } from '../models';
@@ -27,15 +27,21 @@ export class ChatRepository extends Repository {
return { insertCount: insert.length };
}
data.forEach((chat) => {
this.writeStore<ChatRaw>({
path: join(this.storePath, 'chats', chat.owner),
fileName: chat.id,
data: chat,
});
});
const store = this.configService.get<StoreConf>('STORE');
return { insertCount: data.length };
if (store.CHATS) {
data.forEach((chat) => {
this.writeStore<ChatRaw>({
path: join(this.storePath, 'chats', chat.owner),
fileName: chat.id,
data: chat,
});
});
return { insertCount: data.length };
}
return { insertCount: 0 };
} catch (error) {
return error;
} finally {

View File

@@ -1,6 +1,6 @@
import { opendirSync, readFileSync } from 'fs';
import { join } from 'path';
import { ConfigService } from '../../config/env.config';
import { ConfigService, StoreConf } from '../../config/env.config';
import { ContactRaw, IContactModel } from '../models';
import { IInsert, Repository } from '../abstract/abstract.repository';
@@ -27,15 +27,21 @@ export class ContactRepository extends Repository {
return { insertCount: insert.length };
}
data.forEach((contact) => {
this.writeStore({
path: join(this.storePath, 'contacts', contact.owner),
fileName: contact.id,
data: contact,
});
});
const store = this.configService.get<StoreConf>('STORE');
return { insertCount: data.length };
if (store.CONTACTS) {
data.forEach((contact) => {
this.writeStore({
path: join(this.storePath, 'contacts', contact.owner),
fileName: contact.id,
data: contact,
});
});
return { insertCount: data.length };
}
return { insertCount: 0 };
} catch (error) {
return error;
} finally {

View File

@@ -1,4 +1,4 @@
import { ConfigService } from '../../config/env.config';
import { ConfigService, StoreConf } from '../../config/env.config';
import { join } from 'path';
import { IMessageModel, MessageRaw } from '../models';
import { IInsert, Repository } from '../abstract/abstract.repository';
@@ -47,7 +47,9 @@ export class MessageRepository extends Repository {
return { insertCount: insert.length };
}
if (saveDb) {
const store = this.configService.get<StoreConf>('STORE');
if (store.MESSAGES) {
data.forEach((msg) =>
this.writeStore<MessageRaw>({
path: join(this.storePath, 'messages', msg.owner),

View File

@@ -1,4 +1,4 @@
import { ConfigService } from '../../config/env.config';
import { ConfigService, StoreConf } from '../../config/env.config';
import { IMessageUpModel, MessageUpdateRaw } from '../models';
import { IInsert, Repository } from '../abstract/abstract.repository';
import { join } from 'path';
@@ -28,13 +28,21 @@ export class MessageUpRepository extends Repository {
return { insertCount: insert.length };
}
data.forEach((update) => {
this.writeStore<MessageUpdateRaw>({
path: join(this.storePath, 'message-up', update.owner),
fileName: update.id,
data: update,
const store = this.configService.get<StoreConf>('STORE');
if (store.MESSAGE_UP) {
data.forEach((update) => {
this.writeStore<MessageUpdateRaw>({
path: join(this.storePath, 'message-up', update.owner),
fileName: update.id,
data: update,
});
});
});
return { insertCount: data.length };
}
return { insertCount: 0 };
} catch (error) {
return error;
}

View File

@@ -29,9 +29,16 @@ import makeWASocket, {
WAMessage,
WAMessageUpdate,
WASocket,
WAMessageKey,
WAMessageContent,
getAggregateVotesInPollMessage,
jidNormalizedUser,
getKeyAuthor,
decryptPollVote,
} from '@evolution/base';
import {
Auth,
CleanStoreConf,
ConfigService,
ConfigSessionPhone,
Database,
@@ -40,6 +47,7 @@ import {
StoreConf,
Webhook,
} from '../../config/env.config';
import { PollUpdateDecrypt } from '../../utils/poll-update-decrypt-message';
import fs from 'fs';
import { Logger } from '../../config/logger.config';
import { INSTANCE_DIR, ROOT_DIR } from '../../config/path.config';
@@ -231,14 +239,14 @@ export class WAStartupService {
baseURL = this.localWebhook.url;
}
this.logger.log({
local: WAStartupService.name + '.sendDataWebhook-local',
url: baseURL,
event,
instance: this.instance.name,
data,
destination: this.localWebhook.url,
});
// this.logger.log({
// local: WAStartupService.name + '.sendDataWebhook-local',
// url: baseURL,
// event,
// instance: this.instance.name,
// data,
// destination: this.localWebhook.url,
// });
try {
if (this.localWebhook.enabled && isURL(this.localWebhook.url)) {
@@ -286,14 +294,14 @@ export class WAStartupService {
localUrl = this.localWebhook.url;
}
this.logger.log({
local: WAStartupService.name + '.sendDataWebhook-global',
url: globalURL,
event,
instance: this.instance.name,
data,
destination: localUrl,
});
// this.logger.log({
// local: WAStartupService.name + '.sendDataWebhook-global',
// url: globalURL,
// event,
// instance: this.instance.name,
// data,
// destination: localUrl,
// });
try {
if (globalWebhook && globalWebhook?.ENABLED && isURL(globalURL)) {
@@ -437,24 +445,24 @@ export class WAStartupService {
}
private cleanStore() {
const store = this.configService.get<StoreConf>('STORE');
const cleanStore = this.configService.get<CleanStoreConf>('CLEAN_STORE');
const database = this.configService.get<Database>('DATABASE');
if (store?.CLEANING_INTERVAL && !database.ENABLED) {
if (cleanStore?.CLEANING_INTERVAL && !database.ENABLED) {
setInterval(() => {
try {
for (const [key, value] of Object.entries(store)) {
for (const [key, value] of Object.entries(cleanStore)) {
if (value === true) {
execSync(
`rm -rf ${join(
this.storePath,
key.toLowerCase(),
key.toLowerCase().replace('_', '-'),
this.instance.wuid,
)}/*.json`,
);
}
}
} catch (error) {}
}, (store?.CLEANING_INTERVAL ?? 3600) * 1000);
}, (cleanStore?.CLEANING_INTERVAL ?? 3600) * 1000);
}
}
@@ -715,6 +723,49 @@ export class WAStartupService {
received.messageTimestamp = received.messageTimestamp?.toNumber();
}
// if (received.message?.pollUpdateMessage) {
// const creationMsgKey = received.message.pollUpdateMessage.pollCreationMessageKey;
// const pollCreation = (await this.getMessage(
// creationMsgKey,
// true,
// )) as proto.IWebMessageInfo;
// if (pollCreation) {
// const meIdNormalised = jidNormalizedUser(this.instance.wuid);
// const pollCreatorJid = getKeyAuthor(creationMsgKey, meIdNormalised);
// const voterJid = getKeyAuthor(received.key!, meIdNormalised);
// const pollEncKey = pollCreation.message?.messageContextInfo?.messageSecret;
// // const voteMsg = decryptPollVote(received.message.pollUpdateMessage.vote, {
// // pollEncKey,
// // pollCreatorJid,
// // pollMsgId: creationMsgKey.id,
// // voterJid,
// // });
// // console.log('voteMsg: ', voteMsg);
// // console.log(
// // pollEncKey,
// // received.message?.pollUpdateMessage.vote.encPayload,
// // received.message?.pollUpdateMessage.vote.encIv,
// // pollCreatorJid,
// // pollCreation.key.id,
// // voterJid,
// // );
// const hash = await PollUpdateDecrypt.decrypt(
// pollEncKey, // from PollCreationMessage, HAS to be Uint8Array
// received.message?.pollUpdateMessage.vote.encPayload, // from PollUpdateMessage, HAS to be Uint8Array
// received.message?.pollUpdateMessage.vote.encIv, // from PollUpdateMessage, HAS to be Uint8Array
// pollCreatorJid, // PollCreationMessage sender jid (author)
// pollCreation.key.id, // Message ID of the PollCreationMessage (can be gotten via the store & pollCreationMessageKey property on the update)
// voterJid, // PollUpdateMessage sender jid (author) \\ from above
// );
// const opt = pollCreation.message?.pollCreationMessage?.options.map(
// (o) => o.optionName,
// );
// const option = await PollUpdateDecrypt.compare(opt, hash);
// console.log('option: ', option);
// }
// }
const messageRaw: MessageRaw = {
key: received.key,
pushName: received.pushName,
@@ -732,6 +783,7 @@ export class WAStartupService {
},
'messages.update': async (args: WAMessageUpdate[], database: Database) => {
console.log('messages.update args: ', args);
const status: Record<number, wa.StatusMessage> = {
0: 'ERROR',
1: 'PENDING',
@@ -742,6 +794,18 @@ export class WAStartupService {
};
for await (const { key, update } of args) {
if (key.remoteJid !== 'status@broadcast' && !key?.remoteJid?.match(/(:\d+)/)) {
if (update.pollUpdates) {
const pollCreation = await this.getMessage(key);
console.log('pollCreation: ', pollCreation);
if (pollCreation) {
const pollMessage = getAggregateVotesInPollMessage({
message: pollCreation as proto.IMessage,
pollUpdates: update.pollUpdates,
});
console.log('pollMessage: ', pollMessage);
}
}
const message: MessageUpdateRaw = {
...key,
status: status[update.status],