aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc/routers/highlights.ts
blob: 7338077af9e95b1e85fc2af56400dfc1129a8afd (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
83
84
85
86
87
88
89
90
91
import { z } from "zod";

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

import { authedProcedure, router } from "../index";
import { Highlight } from "../models/highlights";
import { ensureBookmarkAccess, 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(ensureBookmarkAccess)
    .query(async ({ ctx }) => {
      const highlights = await Highlight.getForBookmark(ctx, ctx.bookmark);
      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,
      };
    }),
  search: authedProcedure
    .input(
      z.object({
        text: z.string(),
        cursor: zCursorV2.nullish(),
        limit: z.number().optional().default(DEFAULT_NUM_HIGHLIGHTS_PER_PAGE),
      }),
    )
    .output(zGetAllHighlightsResponseSchema)
    .query(async ({ input, ctx }) => {
      const result = await Highlight.search(
        ctx,
        input.text,
        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();
    }),
});