diff options
| author | Mohamed Bassem <me@mbassem.com> | 2025-08-31 13:19:02 +0000 |
|---|---|---|
| committer | Mohamed Bassem <me@mbassem.com> | 2025-08-31 16:52:21 +0000 |
| commit | ac4e4fabffd750bd257432eadf36fb1b95e882fa (patch) | |
| tree | 709f958c8d324db315f7411ffeb6f339b8413b06 /packages/trpc/models/highlights.ts | |
| parent | 15efda6dfc71f4a5e593fba93d349236baee3ea4 (diff) | |
| download | karakeep-ac4e4fabffd750bd257432eadf36fb1b95e882fa.tar.zst | |
refactor: Move highlights object into models
Diffstat (limited to 'packages/trpc/models/highlights.ts')
| -rw-r--r-- | packages/trpc/models/highlights.ts | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/packages/trpc/models/highlights.ts b/packages/trpc/models/highlights.ts new file mode 100644 index 00000000..326f97f3 --- /dev/null +++ b/packages/trpc/models/highlights.ts @@ -0,0 +1,171 @@ +import { TRPCError } from "@trpc/server"; +import { and, desc, eq, 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 { PrivacyAware } from "./privacy"; + +export class Highlight implements PrivacyAware { + 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, + bookmarkId: string, + ): Promise<Highlight[]> { + const results = await ctx.db.query.highlights.findMany({ + where: and( + eq(highlights.bookmarkId, bookmarkId), + eq(highlights.userId, ctx.user.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, + }; + } + + ensureCanAccess(ctx: AuthedContext): void { + if (this.highlight.userId !== ctx.user.id) { + throw new TRPCError({ + code: "FORBIDDEN", + message: "User is not allowed to access resource", + }); + } + } + + 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; + } +} |
