1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/lib/services/trash/permanentlyDeleteOldItems.ts

47 lines
1.4 KiB
TypeScript

import Logger from '@joplin/utils/Logger';
import Folder from '../../models/Folder';
import Setting from '../../models/Setting';
import Note from '../../models/Note';
import { Day, Hour } from '@joplin/utils/time';
import shim from '../../shim';
const logger = Logger.create('permanentlyDeleteOldData');
const permanentlyDeleteOldItems = async (ttl: number = null) => {
ttl = ttl === null ? Setting.value('trash.ttlDays') * Day : ttl;
logger.info(`Processing items older than ${ttl}ms...`);
if (!Setting.value('trash.autoDeletionEnabled')) {
logger.info('Auto-deletion is not enabled - skipping');
return;
}
const result = await Folder.trashItemsOlderThan(ttl);
logger.info('Items to permanently delete:', result);
await Note.batchDelete(result.noteIds, { sourceDescription: 'permanentlyDeleteOldItems' });
// We only auto-delete folders if they are empty.
for (const folderId of result.folderIds) {
const noteIds = await Folder.noteIds(folderId, { includeDeleted: true });
if (!noteIds.length) {
logger.info(`Deleting empty folder: ${folderId}`);
await Folder.delete(folderId);
} else {
logger.info(`Skipping non-empty folder: ${folderId}`);
}
}
};
export const setupAutoDeletion = async () => {
await permanentlyDeleteOldItems();
shim.setInterval(async () => {
await permanentlyDeleteOldItems();
}, 18 * Hour);
};
export default permanentlyDeleteOldItems;