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) {