diff --git a/keyserver/src/cron/cron.js b/keyserver/src/cron/cron.js --- a/keyserver/src/cron/cron.js +++ b/keyserver/src/cron/cron.js @@ -13,6 +13,7 @@ import { deleteExpiredCookies } from '../deleters/cookie-deleters.js'; import { deleteOrphanedDays } from '../deleters/day-deleters.js'; import { deleteOrphanedEntries } from '../deleters/entry-deleters.js'; +import { deleteOrphanedInviteLinks } from '../deleters/link-deleters.js'; import { deleteOrphanedMemberships } from '../deleters/membership-deleters.js'; import { deleteOrphanedMessages } from '../deleters/message-deleters.js'; import { deleteOrphanedNotifs } from '../deleters/notif-deleters.js'; @@ -72,6 +73,7 @@ await deleteExpiredUpdates(); await deleteUnassignedUploads(); await deleteStaleSIWENonceEntries(); + await deleteOrphanedInviteLinks(); } catch (e) { console.warn('encountered error while trying to clean database', e); } diff --git a/keyserver/src/deleters/link-deleters.js b/keyserver/src/deleters/link-deleters.js --- a/keyserver/src/deleters/link-deleters.js +++ b/keyserver/src/deleters/link-deleters.js @@ -65,4 +65,20 @@ await deleteInviteLinks([result]); } -export { deleteInviteLink }; +async function deleteOrphanedInviteLinks(): Promise { + const [selectResults] = await dbQuery(SQL` + SELECT i.id, i.name, i.blob_holder AS blobHolder + FROM invite_links i + LEFT JOIN threads tc ON tc.id = i.community + LEFT JOIN threads tt ON tt.id = i.thread + WHERE tc.id IS NULL OR (i.thread IS NOT NULL AND tt.id IS NULL) + `); + const inviteLinksToDelete = selectResults.map(({ id, name, blobHolder }) => ({ + id, + name, + blobHolder, + })); + await deleteInviteLinks(inviteLinksToDelete); +} + +export { deleteInviteLink, deleteOrphanedInviteLinks };