aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc/routers/importSessions.ts
blob: 62263bdd8351c48e85c8c3153ff3d46253338cdb (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { experimental_trpcMiddleware } from "@trpc/server";
import { and, eq, gt } from "drizzle-orm";
import { z } from "zod";

import { importStagingBookmarks } from "@karakeep/db/schema";
import {
  zCreateImportSessionRequestSchema,
  zDeleteImportSessionRequestSchema,
  zGetImportSessionStatsRequestSchema,
  zImportSessionWithStatsSchema,
  zListImportSessionsRequestSchema,
  zListImportSessionsResponseSchema,
} from "@karakeep/shared/types/importSessions";

import type { AuthedContext } from "../index";
import { authedProcedure, router } from "../index";
import { ImportSession } from "../models/importSessions";

const ensureImportSessionAccess = experimental_trpcMiddleware<{
  ctx: AuthedContext;
  input: { importSessionId: string };
}>().create(async (opts) => {
  const importSession = await ImportSession.fromId(
    opts.ctx,
    opts.input.importSessionId,
  );
  return opts.next({
    ctx: {
      ...opts.ctx,
      importSession,
    },
  });
});

export const importSessionsRouter = router({
  createImportSession: authedProcedure
    .input(zCreateImportSessionRequestSchema)
    .output(z.object({ id: z.string() }))
    .mutation(async ({ input, ctx }) => {
      const session = await ImportSession.create(ctx, input);
      return { id: session.session.id };
    }),

  getImportSessionStats: authedProcedure
    .input(zGetImportSessionStatsRequestSchema)
    .output(zImportSessionWithStatsSchema)
    .query(async ({ input, ctx }) => {
      const session = await ImportSession.fromId(ctx, input.importSessionId);
      return await session.getWithStats();
    }),

  listImportSessions: authedProcedure
    .input(zListImportSessionsRequestSchema)
    .output(zListImportSessionsResponseSchema)
    .query(async ({ ctx }) => {
      const sessions = await ImportSession.getAllWithStats(ctx);
      return { sessions };
    }),

  deleteImportSession: authedProcedure
    .input(zDeleteImportSessionRequestSchema)
    .output(z.object({ success: z.boolean() }))
    .mutation(async ({ input, ctx }) => {
      const session = await ImportSession.fromId(ctx, input.importSessionId);
      await session.delete();
      return { success: true };
    }),

  stageImportedBookmarks: authedProcedure
    .input(
      z.object({
        importSessionId: z.string(),
        bookmarks: z
          .array(
            z.object({
              type: z.enum(["link", "text", "asset"]),
              url: z.string().optional(),
              title: z.string().optional(),
              content: z.string().optional(),
              note: z.string().optional(),
              tags: z.array(z.string()).default([]),
              listIds: z.array(z.string()).default([]),
              sourceAddedAt: z.date().optional(),
            }),
          )
          .max(50),
      }),
    )
    .use(ensureImportSessionAccess)
    .mutation(async ({ input, ctx }) => {
      await ctx.importSession.stageBookmarks(input.bookmarks);
    }),

  finalizeImportStaging: authedProcedure
    .input(z.object({ importSessionId: z.string() }))
    .use(ensureImportSessionAccess)
    .mutation(async ({ ctx }) => {
      await ctx.importSession.finalize();
    }),

  pauseImportSession: authedProcedure
    .input(z.object({ importSessionId: z.string() }))
    .use(ensureImportSessionAccess)
    .mutation(async ({ ctx }) => {
      await ctx.importSession.pause();
    }),

  resumeImportSession: authedProcedure
    .input(z.object({ importSessionId: z.string() }))
    .use(ensureImportSessionAccess)
    .mutation(async ({ ctx }) => {
      await ctx.importSession.resume();
    }),

  getImportSessionResults: authedProcedure
    .input(
      z.object({
        importSessionId: z.string(),
        filter: z
          .enum(["all", "accepted", "rejected", "skipped_duplicate", "pending"])
          .optional(),
        cursor: z.string().optional(),
        limit: z.number().default(50),
      }),
    )
    .use(ensureImportSessionAccess)
    .query(async ({ ctx, input }) => {
      const results = await ctx.db
        .select()
        .from(importStagingBookmarks)
        .where(
          and(
            eq(
              importStagingBookmarks.importSessionId,
              ctx.importSession.session.id,
            ),
            input.filter && input.filter !== "all"
              ? input.filter === "pending"
                ? eq(importStagingBookmarks.status, "pending")
                : eq(importStagingBookmarks.result, input.filter)
              : undefined,
            input.cursor
              ? gt(importStagingBookmarks.id, input.cursor)
              : undefined,
          ),
        )
        .orderBy(importStagingBookmarks.id)
        .limit(input.limit + 1);

      // Return with pagination info
      const hasMore = results.length > input.limit;
      return {
        items: results.slice(0, input.limit),
        nextCursor: hasMore ? results[input.limit - 1].id : null,
      };
    }),
});