FoundKey/packages/backend/src/queue/processors/system/check-expired.ts
Johann150 5b7a06675f
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint-backend Pipeline was successful
ci/woodpecker/push/lint-client Pipeline was successful
ci/woodpecker/push/lint-foundkey-js Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/lint-backend Pipeline was successful
ci/woodpecker/pr/lint-client Pipeline failed
ci/woodpecker/pr/lint-foundkey-js Pipeline was successful
ci/woodpecker/pr/test Pipeline failed
refactor expiring data and expire signins after 60 days
closes #176

Changelog: Added
2022-10-02 00:18:07 +02:00

42 lines
1.2 KiB
TypeScript

import Bull from 'bull';
import { In, LessThan } from 'typeorm';
import { AttestationChallenges, Mutings, Signins } from '@/models/index.js';
import { publishUserEvent } from '@/services/stream.js';
import { MINUTE, DAY } from '@/const.js';
import { queueLogger } from '@/queue/logger.js';
const logger = queueLogger.createSubLogger('check-expired');
export async function checkExpired(job: Bull.Job<Record<string, unknown>>, done: any): Promise<void> {
logger.info('Checking expired data...');
const expiredMutings = await Mutings.createQueryBuilder('muting')
.where('muting.expiresAt IS NOT NULL')
.andWhere('muting.expiresAt < :now', { now: new Date() })
.innerJoinAndSelect('muting.mutee', 'mutee')
.getMany();
if (expiredMutings.length > 0) {
await Mutings.delete({
id: In(expiredMutings.map(m => m.id)),
});
for (const m of expiredMutings) {
publishUserEvent(m.muterId, 'unmute', m.mutee!);
}
}
await Signins.delete({
// 60 days, or roughly equal to two months
createdAt: LessThan(new Date(new Date().getTime() - 60 * DAY)),
});
await AttestationChallenges.delete({
createdAt: LessThan(new Date(new Date().getTime() - 5 * MINUTE)),
});
logger.succ('Deleted expired mutes, signins and attestation challenges.');
done();
}