mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-26 18:38:39 -06:00
Added option to delete messages from the database
This commit is contained in:
parent
b73e260e35
commit
aacda0fffa
@ -61,6 +61,7 @@ export type Database = {
|
||||
CONNECTION: DBConnection;
|
||||
ENABLED: boolean;
|
||||
SAVE_DATA: SaveData;
|
||||
CLEANING_DB_INTERVAL: number;
|
||||
};
|
||||
|
||||
export type Redis = {
|
||||
@ -226,6 +227,7 @@ export class ConfigService {
|
||||
URI: process.env.DATABASE_CONNECTION_URI || '',
|
||||
DB_PREFIX_NAME: process.env.DATABASE_CONNECTION_DB_PREFIX_NAME || 'evolution',
|
||||
},
|
||||
CLEANING_DB_INTERVAL: Number(process.env?.DATABASE_CLEANING_INTERVAL),
|
||||
ENABLED: process.env?.DATABASE_ENABLED === 'true',
|
||||
SAVE_DATA: {
|
||||
INSTANCE: process.env?.DATABASE_SAVE_DATA_INSTANCE === 'true',
|
||||
|
@ -197,4 +197,18 @@ export class MessageRepository extends Repository {
|
||||
this.logger.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
public async delete(query: any) {
|
||||
try {
|
||||
this.logger.verbose('deleting messages');
|
||||
if (this.dbSettings.ENABLED) {
|
||||
this.logger.verbose('deleting messages in db');
|
||||
return await this.messageModel.deleteMany(query);
|
||||
}
|
||||
|
||||
return { deleted: { chatId: query.where.messageTimestamp } };
|
||||
} catch (error) {
|
||||
return { error: error?.toString() };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -145,6 +145,7 @@ export class WAStartupService {
|
||||
) {
|
||||
this.logger.verbose('WAStartupService initialized');
|
||||
this.cleanStore();
|
||||
this.cleanDB();
|
||||
this.instance.qrcode = { count: 0 };
|
||||
}
|
||||
|
||||
@ -1323,6 +1324,23 @@ export class WAStartupService {
|
||||
}
|
||||
}
|
||||
|
||||
private cleanDB() {
|
||||
this.logger.verbose('Cronjob to clean db initialized');
|
||||
const database = this.configService.get<Database>('DATABASE');
|
||||
if (database?.CLEANING_DB_INTERVAL && database.ENABLED) {
|
||||
this.logger.verbose('Cronjob to clean db enabled');
|
||||
let data = new Date()
|
||||
data.setDate(data.getDate() - database.CLEANING_DB_INTERVAL);
|
||||
let timestamp = Math.floor(data.getTime() / 1000) as number | Long.Long;
|
||||
this.repository.message.delete({
|
||||
where: {
|
||||
owner: this.instance.name,
|
||||
messageTimestamp: { $lte: timestamp }
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private async defineAuthState() {
|
||||
this.logger.verbose('Defining auth state');
|
||||
const db = this.configService.get<Database>('DATABASE');
|
||||
|
Loading…
Reference in New Issue
Block a user