From 4402e6f04170cbb0613d35fe94471162253e91b2 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Fri, 19 Apr 2024 20:01:51 +0100 Subject: feature: Download images and screenshots --- packages/trpc/routers/bookmarks.ts | 41 ++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) (limited to 'packages/trpc') 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 >; +async function cleanupAssetForBookmark( + bookmark: Pick, +) { + 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 -- cgit v1.2.3-70-g09d2