-
Notifications
You must be signed in to change notification settings - Fork 0
Add bookmark agent API (PUT/DELETE /docs/{slug}/bookmark, GET /bookmarks) #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { apiError, json, requireApiKey } from "@/lib/docs/api"; | ||
| import { docUrl, listBookmarks, type BookmarkDocRow } from "@/lib/docs/store"; | ||
| import { resolveAccess } from "@/lib/docs/grants"; | ||
| import { safeEqualStr } from "@/lib/auth/tokens"; | ||
| import { resolveBookmarkView } from "@/lib/docs/bookmarks-view"; | ||
|
|
||
| export const dynamic = "force-dynamic"; | ||
|
|
||
| const DEFAULT_LIST_LIMIT = 100; | ||
| const MAX_LIST_LIMIT = 500; | ||
|
|
||
| // One bookmark's API view: re-resolve the caller's access (no session — the key | ||
| // acts as its authenticated email), then shape the item. A revoked bookmark | ||
| // carries no url and the bookmark-time title snapshot (the caller can no longer | ||
| // see the doc's current title). | ||
| async function itemView(doc: BookmarkDocRow, principalEmail: string, principalUserId: number) { | ||
| const access = await resolveAccess(doc, principalEmail, principalUserId); | ||
| const grantRole = | ||
| access.kind === "email_grant" || access.kind === "domain_grant" ? access.role : null; | ||
| const view = resolveBookmarkView({ | ||
| deleted: doc.deleted_at != null, | ||
| isOwner: access.kind === "owner", | ||
| grantRole, | ||
| isPublic: doc.is_public, | ||
| tokenValid: doc.bookmark_token != null && safeEqualStr(doc.bookmark_token, doc.view_token), | ||
| }); | ||
|
|
||
| const url = view.revoked | ||
| ? null | ||
| : view.usesToken && doc.bookmark_token | ||
| ? `${docUrl(doc.slug)}?viewtoken=${encodeURIComponent(doc.bookmark_token)}` | ||
| : docUrl(doc.slug); | ||
|
|
||
| return { | ||
| slug: doc.slug, | ||
| url, | ||
| title: view.revoked ? doc.bookmark_title : doc.title, | ||
| access: view.access, | ||
| revoked: view.revoked, | ||
| public: doc.is_public, | ||
| bookmarked_at: doc.bookmarked_at, | ||
| }; | ||
| } | ||
|
|
||
| // GET /api/v1/bookmarks — list the caller's bookmarks, newest first. Scope: | ||
| // docs.read. | ||
| // | ||
| // ?scope=all (default) | owned | shared: | ||
| // - owned : bookmarked docs the key's user owns. | ||
| // - shared: bookmarked docs shared with (not owned by) the caller. | ||
| // - all : both. The web equivalent for a signed-in human is /bookmarks. | ||
| export async function GET(req: Request): Promise<Response> { | ||
| const auth = await requireApiKey(req, "docs.read", "read"); | ||
| if ("response" in auth) return auth.response; | ||
| const { principal } = auth; | ||
|
|
||
| const url = new URL(req.url); | ||
| let limit = DEFAULT_LIST_LIMIT; | ||
| const limitParam = url.searchParams.get("limit"); | ||
| if (limitParam != null) { | ||
| const n = Number(limitParam); | ||
| if (!Number.isInteger(n) || n < 1) { | ||
| return apiError(400, "invalid_request", "Query 'limit' must be a positive integer."); | ||
| } | ||
| limit = Math.min(n, MAX_LIST_LIMIT); | ||
| } | ||
|
|
||
| const scope = (url.searchParams.get("scope") ?? "all") as "owned" | "shared" | "all"; | ||
| if (scope !== "owned" && scope !== "shared" && scope !== "all") { | ||
| return apiError(400, "invalid_request", "Query 'scope' must be one of: owned, shared, all."); | ||
| } | ||
|
|
||
| // Filter by scope in SQL so `limit` bounds the returned scope, not the full | ||
| // bookmark set (an owned/shared request must not underfill because out-of-scope | ||
| // rows consumed the limit first). | ||
| const rows = await listBookmarks(principal.email, limit, { | ||
| scope, | ||
| ownerId: principal.userId, | ||
| }); | ||
| const bookmarks = []; | ||
| for (const doc of rows) { | ||
| bookmarks.push(await itemView(doc, principal.email, principal.userId)); | ||
| } | ||
| return json({ bookmarks }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { json, notFoundDoc, requireApiKey } from "@/lib/docs/api"; | ||
| import { findBySlug, removeBookmarkBySlug, saveBookmark } from "@/lib/docs/store"; | ||
| import { resolveAccess } from "@/lib/docs/grants"; | ||
| import { canView } from "@/lib/docs/access"; | ||
| import { safeEqualStr } from "@/lib/auth/tokens"; | ||
|
|
||
| export const dynamic = "force-dynamic"; | ||
|
|
||
| type Ctx = { params: Promise<{ slug: string }> }; | ||
|
|
||
| // PUT /api/v1/docs/:slug/bookmark — idempotently bookmark a doc the caller can | ||
| // view. Keyed by the key's email, so it unifies with the account's signed-in | ||
| // web bookmarks. Requires view access (owner / grant via identity, a public | ||
| // doc, or a matching ?viewtoken=); an inaccessible doc 404s (no existence | ||
| // oracle). Scope: docs.read — bookmarking only needs to read the doc, and it | ||
| // writes the caller's own personal state, not the document. | ||
| export async function PUT(req: Request, ctx: Ctx): Promise<Response> { | ||
| const auth = await requireApiKey(req, "docs.read", "write"); | ||
| if ("response" in auth) return auth.response; | ||
| const { principal } = auth; | ||
|
|
||
| const { slug } = await ctx.params; | ||
| const viewtoken = new URL(req.url).searchParams.get("viewtoken"); | ||
|
|
||
| const doc = await findBySlug(slug); | ||
| if (!doc) return notFoundDoc(); | ||
|
|
||
| // Owner / email / domain grant via the authenticated email, or public / a | ||
| // matching view token (the viewer-route path an API key may still present). | ||
| const access = await resolveAccess(doc, principal.email, principal.userId); | ||
| if (access.kind === "none" && !canView(doc, viewtoken)) return notFoundDoc(); | ||
|
|
||
| // Persist the token only when it is the actual basis for access — a matching | ||
| // token on an otherwise private doc reached with no grant. Access via | ||
| // ownership / a grant / public makes the submitted token irrelevant, and | ||
| // storing a non-matching token would gate the later re-check on a token that | ||
| // was never the basis for access. Mirrors the web bookmark POST. | ||
| const tokenToStore = | ||
| access.kind === "none" && | ||
| !doc.is_public && | ||
| viewtoken && | ||
| safeEqualStr(viewtoken, doc.view_token) | ||
| ? viewtoken | ||
| : null; | ||
| await saveBookmark(principal.email, doc.id, tokenToStore, doc.title); | ||
| return json({ bookmarked: true }); | ||
| } | ||
|
|
||
| // DELETE /api/v1/docs/:slug/bookmark — idempotently remove the caller's | ||
| // bookmark. Keyed by slug through the doc row so it still drops a bookmark for | ||
| // a revoked/deleted doc; a no-op when nothing was bookmarked. Scope: docs.read. | ||
| export async function DELETE(req: Request, ctx: Ctx): Promise<Response> { | ||
| const auth = await requireApiKey(req, "docs.read", "write"); | ||
| if ("response" in auth) return auth.response; | ||
| const { principal } = auth; | ||
|
|
||
| const { slug } = await ctx.params; | ||
| await removeBookmarkBySlug(principal.email, slug); | ||
| return json({ removed: true }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.