aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc/routers/highlights.ts
blob: cb9b5e794f26e4b03ba7cd7d35e8e8489171917d (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
import { z } from "zod";

import {
  DEFAULT_NUM_HIGHLIGHTS_PER_PAGE,
  zGetAllHighlightsResponseSchema,
  zHighlightSchema,
  zNewHighlightSchema,
  zUpdateHighlightSchema,
} from "@karakeep/shared/types/highlights";

import { authedProcedure, router } from "../index";
import { Highlight } from "../models/highlights";
import { ensureBookmarkOwnership } from "./bookmarks";

export const highlightsAppRouter = router({
  create: authedProcedure
    .input(zNewHighlightSchema)
    .output(zHighlightSchema)
    .use(ensureBookmarkOwnership)
    .mutation(async ({ input, ctx }) => {
      const highlight = await Highlight.create(ctx, input);
      return highlight.asPublicHighlight();
    }),
  getForBookmark: authedProcedure
    .input(z.object({ bookmarkId: z.string() }))
    .output(z.object({ highlights: z.array(zHighlightSchema) }))
    .use(ensureBookmarkOwnership)
    .query(async ({ input, ctx }) => {
      const highlights = await Highlight.getForBookmark(ctx, input.bookmarkId);
      return { highlights: highlights.map((h) => h.asPublicHighlight()) };
    }),
  get: authedProcedure
    .input(z.object({ highlightId: z.string() }))
    .output(zHighlightSchema)
    .query(async ({ input, ctx }) => {
      const highlight = await Highlight.fromId(ctx, input.highlightId);
      return highlight.asPublicHighlight();
    }),
  getAll: authedProcedure
    .input(
      z.object({
        cursor: z.any().nullish(),
        limit: z.number().optional().default(DEFAULT_NUM_HIGHLIGHTS_PER_PAGE),
      }),
    )
    .output(zGetAllHighlightsResponseSchema)
    .query(async ({ input, ctx }) => {
      const result = await Highlight.getAll(ctx, input.cursor, input.limit);
      return {
        highlights: result.highlights.map((h) => h.asPublicHighlight()),
        nextCursor: result.nextCursor,
      };
    }),
  delete: authedProcedure
    .input(z.object({ highlightId: z.string() }))
    .output(zHighlightSchema)
    .mutation(async ({ input, ctx }) => {
      const highlight = await Highlight.fromId(ctx, input.highlightId);
      return await highlight.delete();
    }),
  update: authedProcedure
    .input(zUpdateHighlightSchema)
    .output(zHighlightSchema)
    .mutation(async ({ input, ctx }) => {
      const highlight = await Highlight.fromId(ctx, input.highlightId);
      await highlight.update(input);
      return highlight.asPublicHighlight();
    }),
});