Files
markone/packages/server/src/bookmark/handlers.ts

150 lines
3.9 KiB
TypeScript
Raw Normal View History

2025-05-07 15:47:08 +01:00
import { DEMO_USER } from "@markone/core/user"
2025-05-07 23:09:14 +01:00
import type { LinkBookmark, BookmarkTag } from "@markone/core/bookmark"
2025-05-06 11:00:35 +01:00
import { type } from "arktype"
2025-05-07 23:09:14 +01:00
import { ulid } from "ulid"
2025-05-06 11:00:35 +01:00
import { db } from "~/database.ts"
import { HttpError } from "~/error.ts"
import type { User } from "~/user/user.ts"
2025-05-13 11:55:50 +01:00
import { LinkUnreachable, UnsupportedLink, findBookmarkHtml, cacheWebsite } from "./bookmark.ts"
2025-05-06 11:00:35 +01:00
const BOOKMARK_PAGINATION_LIMIT = 100
const ListUserBookmarksParams = type({
limit: ["number", "=", BOOKMARK_PAGINATION_LIMIT],
2025-05-08 15:51:17 +01:00
skip: ["number", "=", 0],
2025-05-06 11:00:35 +01:00
})
2025-05-07 23:09:14 +01:00
const AddBookmarkRequestBody = type({
"title?": "string",
kind: "string",
url: "string",
tags: "string[]",
"force?": "boolean",
})
2025-05-06 11:00:35 +01:00
async function listUserBookmarks(request: Bun.BunRequest<"/api/bookmarks">, user: User) {
const queryParams = ListUserBookmarksParams(request.params)
if (queryParams instanceof type.errors) {
2025-05-07 23:09:14 +01:00
throw new HttpError(400, "", queryParams.summary)
2025-05-06 11:00:35 +01:00
}
2025-05-07 15:47:08 +01:00
const listBookmarksQuery = db.query(
2025-05-07 16:57:09 +01:00
`
2025-05-08 15:51:17 +01:00
SELECT bookmarks.id, bookmarks.kind, bookmarks.title, bookmarks.url FROM bookmarks
2025-05-07 16:57:09 +01:00
WHERE bookmarks.user_id = $userId
2025-05-08 15:51:17 +01:00
ORDER BY bookmarks.id DESC
LIMIT $limit OFFSET $skip
2025-05-07 16:57:09 +01:00
`,
2025-05-07 15:47:08 +01:00
)
2025-05-06 11:00:35 +01:00
const results = listBookmarksQuery.all({
userId: user.id,
limit: queryParams.limit,
skip: queryParams.skip,
})
return Response.json(results, { status: 200 })
}
2025-05-07 15:47:08 +01:00
async function deleteUserBookmark(request: Bun.BunRequest<"/api/bookmark/:id">, user: User) {
if (user.id !== DEMO_USER.id) {
const deleteBookmarkQuery = db.query("DELETE FROM bookmarks WHERE user_id = $userId AND id = $id")
const tx = db.transaction(() => {
deleteBookmarkQuery.run({ userId: user.id, id: request.params.id })
})
tx()
}
return Response.json(undefined, { status: 204 })
}
2025-05-07 23:09:14 +01:00
async function addBookmark(request: Bun.BunRequest<"/api/bookmarks">, user: User) {
if (user.id !== DEMO_USER.id) {
const body = AddBookmarkRequestBody(await request.json())
if (body instanceof type.errors) {
throw new HttpError(400)
}
2025-05-13 11:55:50 +01:00
const cachedWebsite = await cacheWebsite(body.url).catch((error) => {
if (error instanceof LinkUnreachable) {
if (body.force) {
return null
}
throw new HttpError(400, "LinkUnreachable")
2025-05-08 15:51:17 +01:00
}
2025-05-13 11:55:50 +01:00
if (error instanceof UnsupportedLink) {
throw new HttpError(400, "UnsupportedLink")
}
console.error(error)
throw new HttpError(500)
2025-05-07 23:09:14 +01:00
})
const bookmark: LinkBookmark = {
kind: "link",
id: ulid(),
2025-05-08 15:51:17 +01:00
title: "",
2025-05-07 23:09:14 +01:00
url: body.url,
tags: body.tags.map((tag) => ({ id: ulid(), name: tag })),
}
2025-05-08 15:51:17 +01:00
if (body.title) {
bookmark.title = body.title
2025-05-13 11:55:50 +01:00
} else if (cachedWebsite?.title) {
bookmark.title = cachedWebsite.title
2025-05-08 15:51:17 +01:00
}
2025-05-07 23:09:14 +01:00
const query = db.query(`
INSERT INTO bookmarks (id, user_id, kind, title, url, content_html)
VALUES ($id, $userId, $kind, $title, $url, $html)
`)
query.run({
id: bookmark.id,
userId: user.id,
kind: bookmark.kind,
title: bookmark.title,
url: bookmark.url,
2025-05-13 11:55:50 +01:00
html: cachedWebsite?.readableHtml ?? "",
2025-05-07 23:09:14 +01:00
})
const insertTagQuery = db.query(`
INSERT INTO tags(id, bookmark_id, name)
VALUES ($id, $bookmarkId, $name)
`)
const insertTags = db.transaction((tags: BookmarkTag[]) => {
for (const tag of tags) {
insertTagQuery.run({
bookmarkId: bookmark.id,
id: tag.id,
name: tag.name,
})
}
})
insertTags(bookmark.tags)
return Response.json(bookmark, { status: 200 })
}
return Response.json(undefined, { status: 204 })
}
2025-05-08 15:51:17 +01:00
async function fetchBookmark(request: Bun.BunRequest<"/api/bookmark/:id">, user: User) {
switch (request.headers.get("Accept")) {
case "text/html": {
const html = findBookmarkHtml(request.params.id, user)
if (html === null) {
throw new HttpError(404)
}
return new Response(html, {
status: 200,
headers: {
"Content-Type": "text/html",
},
})
}
default:
throw new HttpError(400, "UnsupportedAcceptHeader")
}
}
export { addBookmark, fetchBookmark, listUserBookmarks, deleteUserBookmark }