mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
42 lines
1 KiB
TypeScript
42 lines
1 KiB
TypeScript
import { getRepository, In } from "typeorm";
|
|
import moment from "moment-timezone";
|
|
import { NicknameHistoryEntry } from "../entities/NicknameHistoryEntry";
|
|
import { DAYS, DBDateFormat } from "../../utils";
|
|
import { connection } from "../db";
|
|
|
|
export const NICKNAME_RETENTION_PERIOD = 30 * DAYS;
|
|
const CLEAN_PER_LOOP = 500;
|
|
|
|
export async function cleanupNicknames(): Promise<number> {
|
|
let cleaned = 0;
|
|
|
|
const nicknameHistoryRepository = getRepository(NicknameHistoryEntry);
|
|
const dateThreshold = moment
|
|
.utc()
|
|
.subtract(NICKNAME_RETENTION_PERIOD, "ms")
|
|
.format(DBDateFormat);
|
|
|
|
// Clean old nicknames (NICKNAME_RETENTION_PERIOD)
|
|
let rows;
|
|
do {
|
|
rows = await connection.query(
|
|
`
|
|
SELECT id
|
|
FROM nickname_history
|
|
WHERE timestamp < ?
|
|
LIMIT ${CLEAN_PER_LOOP}
|
|
`,
|
|
[dateThreshold],
|
|
);
|
|
|
|
if (rows.length > 0) {
|
|
await nicknameHistoryRepository.delete({
|
|
id: In(rows.map(r => r.id)),
|
|
});
|
|
}
|
|
|
|
cleaned += rows.length;
|
|
} while (rows.length === CLEAN_PER_LOOP);
|
|
|
|
return cleaned;
|
|
}
|