noteshare.space/server/src/tasks/deleteExpiredNotes.ts
2022-08-11 14:59:48 +02:00

40 lines
1.3 KiB
TypeScript

import { deleteNotes, getExpiredNotes } from "../controllers/note/note.dao";
import { getExpiredNoteFilter } from "../lib/expiredNoteFilter";
import EventLogger from "../logging/EventLogger";
import logger from "../logging/logger";
export async function deleteExpiredNotes(): Promise<number> {
logger.info("[Cleanup] Cleaning up expired notes...");
const toDelete = await getExpiredNotes();
return deleteNotes(toDelete.map((n) => n.id))
.then(async (deleteCount) => {
const logs = toDelete.map(async (note) => {
logger.info(
`[Cleanup] Deleted note ${note.id} with size ${
note.ciphertext.length + note.hmac.length
} bytes`
);
return EventLogger.purgeEvent({
success: true,
note_id: note.id,
size_bytes: note.ciphertext.length + note.hmac.length,
});
});
await Promise.all(logs);
const filter = await getExpiredNoteFilter();
await filter.addNoteIds(toDelete.map((n) => n.id));
logger.info(`[Cleanup] Deleted ${deleteCount} expired notes.`);
return deleteCount;
})
.catch((err) => {
logger.error(`[Cleanup] Error cleaning expired notes:`);
logger.error(err);
return -1;
});
}
export const deleteInterval =
Math.max(parseInt(<string>process.env.CLEANUP_INTERVAL_SECONDS) || 1, 1) *
1000;