aboutsummaryrefslogtreecommitdiffstats
path: root/apps/workers/workerUtils.ts
blob: d41df578d56f23f7e0b40a1e80989b4491fdce9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { eq } from "drizzle-orm";

import { db, KarakeepDBTransaction } from "@karakeep/db";
import { assets, AssetTypes, bookmarks } from "@karakeep/db/schema";

type DBAssetType = typeof assets.$inferInsert;
export async function updateAsset(
  oldAssetId: string | undefined,
  newAsset: DBAssetType,
  txn: KarakeepDBTransaction,
) {
  if (oldAssetId) {
    await txn.delete(assets).where(eq(assets.id, oldAssetId));
  }

  await txn.insert(assets).values(newAsset);
}

export async function getBookmarkDetails(bookmarkId: string) {
  const bookmark = await db.query.bookmarks.findFirst({
    where: eq(bookmarks.id, bookmarkId),
    with: {
      link: true,
      assets: true,
    },
  });

  if (!bookmark || !bookmark.link) {
    throw new Error("The bookmark either doesn't exist or is not a link");
  }
  return {
    url: bookmark.link.url,
    userId: bookmark.userId,
    screenshotAssetId: bookmark.assets.find(
      (a) => a.assetType == AssetTypes.LINK_SCREENSHOT,
    )?.id,
    imageAssetId: bookmark.assets.find(
      (a) => a.assetType == AssetTypes.LINK_BANNER_IMAGE,
    )?.id,
    fullPageArchiveAssetId: bookmark.assets.find(
      (a) => a.assetType == AssetTypes.LINK_FULL_PAGE_ARCHIVE,
    )?.id,
    videoAssetId: bookmark.assets.find(
      (a) => a.assetType == AssetTypes.LINK_VIDEO,
    )?.id,
    precrawledArchiveAssetId: bookmark.assets
      .filter((a) => a.assetType == AssetTypes.LINK_PRECRAWLED_ARCHIVE)
      .at(-1)?.id,
  };
}