1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-18 09:35:20 +02:00
joplin/packages/lib/services/trash/permanentlyDeleteOldItems.ts
2024-03-02 14:25:27 +00:00

47 lines
1.3 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);
// 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;