aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc/routers/assets.ts
blob: c75f1e2eda52005b84cad5ab59cb6d7363fd7f72 (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
77
78
79
80
81
82
import { z } from "zod";

import {
  zAssetSchema,
  zAssetTypesSchema,
} from "@karakeep/shared/types/bookmarks";

import { authedProcedure, router } from "../index";
import { Asset } from "../models/assets";
import { ensureBookmarkOwnership } from "./bookmarks";

export const assetsAppRouter = router({
  list: authedProcedure
    .input(
      z.object({
        limit: z.number().min(1).max(100).default(20),
        cursor: z.number().nullish(),
      }),
    )
    .output(
      z.object({
        assets: z.array(
          z.object({
            id: z.string(),
            assetType: zAssetTypesSchema,
            size: z.number(),
            contentType: z.string().nullable(),
            fileName: z.string().nullable(),
            bookmarkId: z.string().nullable(),
          }),
        ),
        nextCursor: z.number().nullish(),
        totalCount: z.number(),
      }),
    )
    .query(async ({ input, ctx }) => {
      return await Asset.list(ctx, {
        limit: input.limit,
        cursor: input.cursor ?? null,
      });
    }),
  attachAsset: authedProcedure
    .input(
      z.object({
        bookmarkId: z.string(),
        asset: z.object({
          id: z.string(),
          assetType: zAssetTypesSchema,
        }),
      }),
    )
    .output(zAssetSchema)
    .use(ensureBookmarkOwnership)
    .mutation(async ({ input, ctx }) => {
      return await Asset.attachAsset(ctx, input);
    }),
  replaceAsset: authedProcedure
    .input(
      z.object({
        bookmarkId: z.string(),
        oldAssetId: z.string(),
        newAssetId: z.string(),
      }),
    )
    .output(z.void())
    .use(ensureBookmarkOwnership)
    .mutation(async ({ input, ctx }) => {
      await Asset.replaceAsset(ctx, input);
    }),
  detachAsset: authedProcedure
    .input(
      z.object({
        bookmarkId: z.string(),
        assetId: z.string(),
      }),
    )
    .output(z.void())
    .use(ensureBookmarkOwnership)
    .mutation(async ({ input, ctx }) => {
      await Asset.detachAsset(ctx, input);
    }),
});