diff options
| author | MohamedBassem <me@mbassem.com> | 2024-04-19 20:01:51 +0100 |
|---|---|---|
| committer | Mohamed Bassem <me@mbassem.com> | 2024-04-20 00:05:31 +0100 |
| commit | 4402e6f04170cbb0613d35fe94471162253e91b2 (patch) | |
| tree | 696f6511cefa7d1c6bc3a1f8bc2de755870310cc /packages/trpc | |
| parent | b4a13ce3d92ee505124fc98804935c1122978550 (diff) | |
| download | karakeep-4402e6f04170cbb0613d35fe94471162253e91b2.tar.zst | |
feature: Download images and screenshots
Diffstat (limited to 'packages/trpc')
| -rw-r--r-- | packages/trpc/routers/bookmarks.ts | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts index 0383d3f2..1e154e7b 100644 --- a/packages/trpc/routers/bookmarks.ts +++ b/packages/trpc/routers/bookmarks.ts @@ -93,6 +93,28 @@ type BookmarkQueryReturnType = Awaited< ReturnType<typeof dummyDrizzleReturnType> >; +async function cleanupAssetForBookmark( + bookmark: Pick<BookmarkQueryReturnType, "asset" | "link" | "userId">, +) { + const assetIds = []; + if (bookmark.asset) { + assetIds.push(bookmark.asset.assetId); + } + if (bookmark.link) { + if (bookmark.link.screenshotAssetId) { + assetIds.push(bookmark.link.screenshotAssetId); + } + if (bookmark.link.imageAssetId) { + assetIds.push(bookmark.link.imageAssetId); + } + } + await Promise.all( + assetIds.map((assetId) => + deleteAsset({ userId: bookmark.userId, assetId }), + ), + ); +} + function toZodSchema(bookmark: BookmarkQueryReturnType): ZBookmark { const { tagsOnBookmarks, link, text, asset, ...rest } = bookmark; @@ -291,8 +313,15 @@ export const bookmarksAppRouter = router({ .input(z.object({ bookmarkId: z.string() })) .use(ensureBookmarkOwnership) .mutation(async ({ input, ctx }) => { - const asset = await ctx.db.query.bookmarkAssets.findFirst({ - where: and(eq(bookmarkAssets.id, input.bookmarkId)), + const bookmark = await ctx.db.query.bookmarks.findFirst({ + where: and( + eq(bookmarks.id, input.bookmarkId), + eq(bookmarks.userId, ctx.user.id), + ), + with: { + asset: true, + link: true, + }, }); const deleted = await ctx.db .delete(bookmarks) @@ -306,8 +335,12 @@ export const bookmarksAppRouter = router({ bookmarkId: input.bookmarkId, type: "delete", }); - if (deleted.changes > 0 && asset) { - await deleteAsset({ userId: ctx.user.id, assetId: asset.assetId }); + if (deleted.changes > 0 && bookmark) { + await cleanupAssetForBookmark({ + asset: bookmark.asset, + link: bookmark.link, + userId: ctx.user.id, + }); } }), recrawlBookmark: authedProcedure |
