mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-27 10:32:58 +02:00
47 lines
1.4 KiB
TypeScript
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;
|