From f00a7afab88b70903fa8083657d7f51464ab9a2e Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Sun, 17 Oct 2021 20:12:42 +0300 Subject: [PATCH] feat(mutes): clear old expired mutes for servers that have disabled the mutes plugin --- backend/src/data/Mutes.ts | 14 +++++++++++++- backend/src/data/loops/expiringMutesLoop.ts | 3 +++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/backend/src/data/Mutes.ts b/backend/src/data/Mutes.ts index 06674717..5734f334 100644 --- a/backend/src/data/Mutes.ts +++ b/backend/src/data/Mutes.ts @@ -1,9 +1,11 @@ import moment from "moment-timezone"; import { Brackets, getRepository, Repository } from "typeorm"; import { Mute } from "./entities/Mute"; -import { DBDateFormat } from "../utils"; +import { DAYS, DBDateFormat } from "../utils"; import { BaseRepository } from "./BaseRepository"; +const OLD_EXPIRED_MUTE_THRESHOLD = 7 * DAYS; + export class Mutes extends BaseRepository { private mutes: Repository; @@ -20,4 +22,14 @@ export class Mutes extends BaseRepository { .andWhere("expires_at <= :date", { date: thresholdDateStr }) .getMany(); } + + async clearOldExpiredMutes(): Promise { + const thresholdDateStr = moment.utc().subtract(OLD_EXPIRED_MUTE_THRESHOLD, "ms").format(DBDateFormat); + await this.mutes + .createQueryBuilder("mutes") + .andWhere("expires_at IS NOT NULL") + .andWhere("expires_at <= :date", { date: thresholdDateStr }) + .delete() + .execute(); + } } diff --git a/backend/src/data/loops/expiringMutesLoop.ts b/backend/src/data/loops/expiringMutesLoop.ts index 9efc5708..ba62ad19 100644 --- a/backend/src/data/loops/expiringMutesLoop.ts +++ b/backend/src/data/loops/expiringMutesLoop.ts @@ -35,6 +35,9 @@ export async function runExpiringMutesLoop() { clearTimeout(timeout); } + console.log("[EXPIRING MUTES LOOP] Clearing old expired mutes"); + await getMutesRepository().clearOldExpiredMutes(); + console.log("[EXPIRING MUTES LOOP] Setting timeouts for expiring mutes"); const expiringMutes = await getMutesRepository().getSoonExpiringMutes(LOOP_INTERVAL); for (const mute of expiringMutes) {