2021-09-25 21:33:59 +03:00
|
|
|
import moment from "moment-timezone";
|
2023-04-01 12:58:17 +01:00
|
|
|
import { getRepository, Repository } from "typeorm";
|
2021-10-17 20:12:42 +03:00
|
|
|
import { DAYS, DBDateFormat } from "../utils";
|
2021-09-25 21:33:59 +03:00
|
|
|
import { BaseRepository } from "./BaseRepository";
|
2023-04-01 12:58:17 +01:00
|
|
|
import { Mute } from "./entities/Mute";
|
2023-04-01 18:33:09 +03:00
|
|
|
import { MuteTypes } from "./MuteTypes";
|
2021-09-25 21:33:59 +03:00
|
|
|
|
2021-10-17 20:12:42 +03:00
|
|
|
const OLD_EXPIRED_MUTE_THRESHOLD = 7 * DAYS;
|
|
|
|
|
2023-04-01 18:33:09 +03:00
|
|
|
export const MAX_TIMEOUT_DURATION = 28 * DAYS;
|
|
|
|
// When a timeout is under this duration but the mute expires later, the timeout will be reset to max duration
|
|
|
|
export const TIMEOUT_RENEWAL_THRESHOLD = 21 * DAYS;
|
|
|
|
|
2021-09-25 21:33:59 +03:00
|
|
|
export class Mutes extends BaseRepository {
|
|
|
|
private mutes: Repository<Mute>;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.mutes = getRepository(Mute);
|
|
|
|
}
|
|
|
|
|
2023-04-01 18:33:09 +03:00
|
|
|
findMute(guildId: string, userId: string): Promise<Mute | undefined> {
|
|
|
|
return this.mutes.findOne({
|
|
|
|
where: {
|
|
|
|
guild_id: guildId,
|
|
|
|
user_id: userId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getSoonExpiringMutes(threshold: number): Promise<Mute[]> {
|
2021-09-25 21:33:59 +03:00
|
|
|
const thresholdDateStr = moment.utc().add(threshold, "ms").format(DBDateFormat);
|
|
|
|
return this.mutes
|
|
|
|
.createQueryBuilder("mutes")
|
|
|
|
.andWhere("expires_at IS NOT NULL")
|
|
|
|
.andWhere("expires_at <= :date", { date: thresholdDateStr })
|
|
|
|
.getMany();
|
|
|
|
}
|
2021-10-17 20:12:42 +03:00
|
|
|
|
2023-04-01 18:33:09 +03:00
|
|
|
getTimeoutMutesToRenew(threshold: number): Promise<Mute[]> {
|
|
|
|
const thresholdDateStr = moment.utc().add(threshold, "ms").format(DBDateFormat);
|
|
|
|
return this.mutes
|
|
|
|
.createQueryBuilder("mutes")
|
|
|
|
.andWhere("type = :type", { type: MuteTypes.Timeout })
|
|
|
|
.andWhere("(expires_at IS NULL OR timeout_expires_at < expires_at)")
|
|
|
|
.andWhere("timeout_expires_at <= :date", { date: thresholdDateStr })
|
|
|
|
.getMany();
|
|
|
|
}
|
|
|
|
|
2021-10-17 20:12:42 +03:00
|
|
|
async clearOldExpiredMutes(): Promise<void> {
|
|
|
|
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();
|
|
|
|
}
|
2021-09-25 21:33:59 +03:00
|
|
|
}
|