aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc/routers/bookmarks.ts
diff options
context:
space:
mode:
authorkamtschatka <simon.schatka@gmx.at>2024-07-01 13:03:53 +0200
committerGitHub <noreply@github.com>2024-07-01 12:03:53 +0100
commite6486465decd612f7e437abe904960a47ff359ce (patch)
treef36fd7efbcf2a083905061d8c5f1310f36350ced /packages/trpc/routers/bookmarks.ts
parentccbff18a9763e458c07f46cb3a331062df14a9b9 (diff)
downloadkarakeep-e6486465decd612f7e437abe904960a47ff359ce.tar.zst
refactor: added the bookmark type to the database (#256)
* refactoring asset types Extracted out functions to silently delete assets and to update them after crawling Generalized the mapping of assets to bookmark fields to make extending them easier * Added the bookmark type to the database Introduced an enum to have better type safety cleaned up the code and based some code on the type directly * add BookmarkType.UNKNWON * lint and remove unused function --------- Co-authored-by: MohamedBassem <me@mbassem.com>
Diffstat (limited to 'packages/trpc/routers/bookmarks.ts')
-rw-r--r--packages/trpc/routers/bookmarks.ts105
1 files changed, 60 insertions, 45 deletions
diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts
index 8644bbcf..edf196bb 100644
--- a/packages/trpc/routers/bookmarks.ts
+++ b/packages/trpc/routers/bookmarks.ts
@@ -29,6 +29,7 @@ import {
} from "@hoarder/shared/queues";
import { getSearchIdxClient } from "@hoarder/shared/search";
import {
+ BookmarkTypes,
DEFAULT_NUM_BOOKMARKS_PER_PAGE,
zBareBookmarkSchema,
zBookmarkSchema,
@@ -176,25 +177,29 @@ async function cleanupAssetForBookmark(
function toZodSchema(bookmark: BookmarkQueryReturnType): ZBookmark {
const { tagsOnBookmarks, link, text, asset, assets, ...rest } = bookmark;
- let content: ZBookmarkContent;
- if (link) {
- content = {
- type: "link",
- ...mapAssetsToBookmarkFields(assets),
- ...link,
- };
- } else if (text) {
- content = { type: "text", text: text.text ?? "" };
- } else if (asset) {
- content = {
- type: "asset",
- assetType: asset.assetType,
- assetId: asset.assetId,
- fileName: asset.fileName,
- sourceUrl: asset.sourceUrl,
- };
- } else {
- content = { type: "unknown" };
+ let content: ZBookmarkContent = {
+ type: BookmarkTypes.UNKNWON,
+ };
+ switch (bookmark.type) {
+ case BookmarkTypes.LINK:
+ content = {
+ type: bookmark.type,
+ ...mapAssetsToBookmarkFields(assets),
+ ...link,
+ };
+ break;
+ case BookmarkTypes.TEXT:
+ content = { type: bookmark.type, text: text.text ?? "" };
+ break;
+ case BookmarkTypes.ASSET:
+ content = {
+ type: bookmark.type,
+ assetType: asset.assetType,
+ assetId: asset.assetId,
+ fileName: asset.fileName,
+ sourceUrl: asset.sourceUrl,
+ };
+ break;
}
return {
@@ -218,19 +223,23 @@ export const bookmarksAppRouter = router({
),
)
.mutation(async ({ input, ctx }) => {
- if (input.type == "link") {
+ if (input.type == BookmarkTypes.LINK) {
// This doesn't 100% protect from duplicates because of races, but it's more than enough for this usecase.
const alreadyExists = await attemptToDedupLink(ctx, input.url);
if (alreadyExists) {
return { ...alreadyExists, alreadyExists: true };
}
}
+ if (input.type == BookmarkTypes.UNKNWON) {
+ throw new TRPCError({ code: "BAD_REQUEST" });
+ }
const bookmark = await ctx.db.transaction(async (tx) => {
const bookmark = (
await tx
.insert(bookmarks)
.values({
userId: ctx.user.id,
+ type: input.type,
})
.returning()
)[0];
@@ -238,7 +247,7 @@ export const bookmarksAppRouter = router({
let content: ZBookmarkContent;
switch (input.type) {
- case "link": {
+ case BookmarkTypes.LINK: {
const link = (
await tx
.insert(bookmarkLinks)
@@ -249,12 +258,12 @@ export const bookmarksAppRouter = router({
.returning()
)[0];
content = {
- type: "link",
+ type: BookmarkTypes.LINK,
...link,
};
break;
}
- case "text": {
+ case BookmarkTypes.TEXT: {
const text = (
await tx
.insert(bookmarkTexts)
@@ -262,12 +271,12 @@ export const bookmarksAppRouter = router({
.returning()
)[0];
content = {
- type: "text",
+ type: BookmarkTypes.TEXT,
text: text.text ?? "",
};
break;
}
- case "asset": {
+ case BookmarkTypes.ASSET: {
const [asset] = await tx
.insert(bookmarkAssets)
.values({
@@ -281,15 +290,12 @@ export const bookmarksAppRouter = router({
})
.returning();
content = {
- type: "asset",
+ type: BookmarkTypes.ASSET,
assetType: asset.assetType,
assetId: asset.assetId,
};
break;
}
- case "unknown": {
- throw new TRPCError({ code: "BAD_REQUEST" });
- }
}
return {
@@ -302,15 +308,15 @@ export const bookmarksAppRouter = router({
// Enqueue crawling request
switch (bookmark.content.type) {
- case "link": {
+ case BookmarkTypes.LINK: {
// The crawling job triggers openai when it's done
await LinkCrawlerQueue.add("crawl", {
bookmarkId: bookmark.id,
});
break;
}
- case "text":
- case "asset": {
+ case BookmarkTypes.TEXT:
+ case BookmarkTypes.ASSET: {
await OpenAIQueue.add("openai", {
bookmarkId: bookmark.id,
});
@@ -565,19 +571,28 @@ export const bookmarksAppRouter = router({
const bookmarkId = row.bookmarksSq.id;
if (!acc[bookmarkId]) {
let content: ZBookmarkContent;
- if (row.bookmarkLinks) {
- content = { type: "link", ...row.bookmarkLinks };
- } else if (row.bookmarkTexts) {
- content = { type: "text", text: row.bookmarkTexts.text ?? "" };
- } else if (row.bookmarkAssets) {
- content = {
- type: "asset",
- assetId: row.bookmarkAssets.assetId,
- assetType: row.bookmarkAssets.assetType,
- fileName: row.bookmarkAssets.fileName,
- };
- } else {
- content = { type: "unknown" };
+ switch (row.bookmarksSq.type) {
+ case BookmarkTypes.LINK: {
+ content = { type: row.bookmarksSq.type, ...row.bookmarkLinks! };
+ break;
+ }
+ case BookmarkTypes.TEXT: {
+ content = {
+ type: row.bookmarksSq.type,
+ text: row.bookmarkTexts?.text ?? "",
+ };
+ break;
+ }
+ case BookmarkTypes.ASSET: {
+ const bookmarkAssets = row.bookmarkAssets!;
+ content = {
+ type: row.bookmarksSq.type,
+ assetId: bookmarkAssets.assetId,
+ assetType: bookmarkAssets.assetType,
+ fileName: bookmarkAssets.fileName,
+ };
+ break;
+ }
}
acc[bookmarkId] = {
...row.bookmarksSq,