aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc/lib/attachments.ts
blob: 3ad79a5ab248fae952287e698313ebbdf4b9e1d9 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { z } from "zod";

import { AssetTypes } from "@hoarder/db/schema";
import { ZAssetType, zAssetTypesSchema } from "@hoarder/shared/types/bookmarks";

export function mapDBAssetTypeToUserType(assetType: AssetTypes): ZAssetType {
  const map: Record<AssetTypes, z.infer<typeof zAssetTypesSchema>> = {
    [AssetTypes.LINK_SCREENSHOT]: "screenshot",
    [AssetTypes.ASSET_SCREENSHOT]: "assetScreenshot",
    [AssetTypes.LINK_FULL_PAGE_ARCHIVE]: "fullPageArchive",
    [AssetTypes.LINK_PRECRAWLED_ARCHIVE]: "precrawledArchive",
    [AssetTypes.LINK_BANNER_IMAGE]: "bannerImage",
    [AssetTypes.LINK_VIDEO]: "video",
    [AssetTypes.BOOKMARK_ASSET]: "bookmarkAsset",
    [AssetTypes.UNKNOWN]: "bannerImage",
  };
  return map[assetType];
}

export function mapSchemaAssetTypeToDB(
  assetType: z.infer<typeof zAssetTypesSchema>,
): AssetTypes {
  const map: Record<ZAssetType, AssetTypes> = {
    screenshot: AssetTypes.LINK_SCREENSHOT,
    assetScreenshot: AssetTypes.ASSET_SCREENSHOT,
    fullPageArchive: AssetTypes.LINK_FULL_PAGE_ARCHIVE,
    precrawledArchive: AssetTypes.LINK_PRECRAWLED_ARCHIVE,
    bannerImage: AssetTypes.LINK_BANNER_IMAGE,
    video: AssetTypes.LINK_VIDEO,
    bookmarkAsset: AssetTypes.BOOKMARK_ASSET,
    unknown: AssetTypes.UNKNOWN,
  };
  return map[assetType];
}

export function humanFriendlyNameForAssertType(type: ZAssetType) {
  const map: Record<ZAssetType, string> = {
    screenshot: "Screenshot",
    assetScreenshot: "Asset Screenshot",
    fullPageArchive: "Full Page Archive",
    precrawledArchive: "Precrawled Archive",
    bannerImage: "Banner Image",
    video: "Video",
    bookmarkAsset: "Bookmark Asset",
    unknown: "Unknown",
  };
  return map[type];
}

export function isAllowedToAttachAsset(type: ZAssetType) {
  const map: Record<ZAssetType, boolean> = {
    screenshot: true,
    assetScreenshot: true,
    fullPageArchive: false,
    precrawledArchive: false,
    bannerImage: true,
    video: false,
    bookmarkAsset: false,
    unknown: false,
  };
  return map[type];
}

export function isAllowedToDetachAsset(type: ZAssetType) {
  const map: Record<ZAssetType, boolean> = {
    screenshot: true,
    assetScreenshot: true,
    fullPageArchive: true,
    precrawledArchive: false,
    bannerImage: true,
    video: true,
    bookmarkAsset: false,
    unknown: false,
  };
  return map[type];
}