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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
import { TRPCError } from "@trpc/server";
import { and, desc, eq, like, lt, lte, or } from "drizzle-orm";
import { z } from "zod";
import { highlights } from "@karakeep/db/schema";
import {
zHighlightSchema,
zNewHighlightSchema,
zUpdateHighlightSchema,
} from "@karakeep/shared/types/highlights";
import { zCursorV2 } from "@karakeep/shared/types/pagination";
import { AuthedContext } from "..";
import { BareBookmark } from "./bookmarks";
export class Highlight {
constructor(
protected ctx: AuthedContext,
private highlight: typeof highlights.$inferSelect,
) {}
static async fromId(ctx: AuthedContext, id: string): Promise<Highlight> {
const highlight = await ctx.db.query.highlights.findFirst({
where: eq(highlights.id, id),
});
if (!highlight) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Highlight not found",
});
}
// If it exists but belongs to another user, throw forbidden error
if (highlight.userId !== ctx.user.id) {
throw new TRPCError({
code: "FORBIDDEN",
message: "User is not allowed to access resource",
});
}
return new Highlight(ctx, highlight);
}
static async create(
ctx: AuthedContext,
input: z.infer<typeof zNewHighlightSchema>,
): Promise<Highlight> {
const [result] = await ctx.db
.insert(highlights)
.values({
bookmarkId: input.bookmarkId,
startOffset: input.startOffset,
endOffset: input.endOffset,
color: input.color,
text: input.text,
note: input.note,
userId: ctx.user.id,
})
.returning();
return new Highlight(ctx, result);
}
static async getForBookmark(
ctx: AuthedContext,
bookmark: BareBookmark,
): Promise<Highlight[]> {
const results = await ctx.db.query.highlights.findMany({
where: eq(highlights.bookmarkId, bookmark.id),
orderBy: [desc(highlights.createdAt), desc(highlights.id)],
});
return results.map((h) => new Highlight(ctx, h));
}
static async getAll(
ctx: AuthedContext,
cursor?: z.infer<typeof zCursorV2> | null,
limit = 50,
): Promise<{
highlights: Highlight[];
nextCursor: z.infer<typeof zCursorV2> | null;
}> {
const results = await ctx.db.query.highlights.findMany({
where: and(
eq(highlights.userId, ctx.user.id),
cursor
? or(
lt(highlights.createdAt, cursor.createdAt),
and(
eq(highlights.createdAt, cursor.createdAt),
lte(highlights.id, cursor.id),
),
)
: undefined,
),
limit: limit + 1,
orderBy: [desc(highlights.createdAt), desc(highlights.id)],
});
let nextCursor: z.infer<typeof zCursorV2> | null = null;
if (results.length > limit) {
const nextItem = results.pop()!;
nextCursor = {
id: nextItem.id,
createdAt: nextItem.createdAt,
};
}
return {
highlights: results.map((h) => new Highlight(ctx, h)),
nextCursor,
};
}
static async search(
ctx: AuthedContext,
searchText: string,
cursor?: z.infer<typeof zCursorV2> | null,
limit = 50,
): Promise<{
highlights: Highlight[];
nextCursor: z.infer<typeof zCursorV2> | null;
}> {
const searchPattern = `%${searchText}%`;
const results = await ctx.db.query.highlights.findMany({
where: and(
eq(highlights.userId, ctx.user.id),
or(
like(highlights.text, searchPattern),
like(highlights.note, searchPattern),
),
cursor
? or(
lt(highlights.createdAt, cursor.createdAt),
and(
eq(highlights.createdAt, cursor.createdAt),
lte(highlights.id, cursor.id),
),
)
: undefined,
),
limit: limit + 1,
orderBy: [desc(highlights.createdAt), desc(highlights.id)],
});
let nextCursor: z.infer<typeof zCursorV2> | null = null;
if (results.length > limit) {
const nextItem = results.pop()!;
nextCursor = {
id: nextItem.id,
createdAt: nextItem.createdAt,
};
}
return {
highlights: results.map((h) => new Highlight(ctx, h)),
nextCursor,
};
}
async delete(): Promise<z.infer<typeof zHighlightSchema>> {
const result = await this.ctx.db
.delete(highlights)
.where(
and(
eq(highlights.id, this.highlight.id),
eq(highlights.userId, this.ctx.user.id),
),
)
.returning();
if (result.length === 0) {
throw new TRPCError({ code: "NOT_FOUND" });
}
return result[0];
}
async update(input: z.infer<typeof zUpdateHighlightSchema>): Promise<void> {
const result = await this.ctx.db
.update(highlights)
.set({
color: input.color,
})
.where(
and(
eq(highlights.id, this.highlight.id),
eq(highlights.userId, this.ctx.user.id),
),
)
.returning();
if (result.length === 0) {
throw new TRPCError({ code: "NOT_FOUND" });
}
this.highlight = result[0];
}
asPublicHighlight(): z.infer<typeof zHighlightSchema> {
return this.highlight;
}
}
|