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-07 23:09:14 +01:00
|
|
|
import { JSDOM } from "jsdom"
|
|
|
|
import { Readability } from "@mozilla/readability"
|
2025-05-06 11:00:35 +01:00
|
|
|
|
|
|
|
const BOOKMARK_PAGINATION_LIMIT = 100
|
|
|
|
|
|
|
|
const ListUserBookmarksParams = type({
|
|
|
|
limit: ["number", "=", BOOKMARK_PAGINATION_LIMIT],
|
|
|
|
skip: ["number", "=", 5],
|
|
|
|
})
|
|
|
|
|
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
|
|
|
`
|
|
|
|
SELECT bookmarks.id, bookmarks.kind, bookmarks.title, bookmarks.url, tags.name as tag FROM bookmarks
|
|
|
|
LEFT JOIN tags
|
|
|
|
ON bookmarks.id = tags.bookmark_id
|
|
|
|
WHERE bookmarks.user_id = $userId
|
|
|
|
ORDER BY bookmarks.id LIMIT $limit OFFSET $skip
|
|
|
|
`,
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
const websiteResponse = await fetch(body.url).catch(() => {
|
|
|
|
throw new HttpError(400, "WebsiteUnreachable")
|
|
|
|
})
|
|
|
|
const websiteText = await websiteResponse.text().catch(() => {
|
|
|
|
throw new HttpError(400, "UnsupportedWebsite")
|
|
|
|
})
|
|
|
|
|
|
|
|
const dom = new JSDOM(websiteText, {
|
|
|
|
url: body.url,
|
|
|
|
})
|
|
|
|
const reader = new Readability(dom.window.document)
|
|
|
|
const article = reader.parse()
|
|
|
|
|
|
|
|
const bookmark: LinkBookmark = {
|
|
|
|
kind: "link",
|
|
|
|
id: ulid(),
|
|
|
|
title: body.title || article?.title || "Untitled",
|
|
|
|
url: body.url,
|
|
|
|
tags: body.tags.map((tag) => ({ id: ulid(), name: tag })),
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
html: article?.content ?? websiteText,
|
|
|
|
})
|
|
|
|
|
|
|
|
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 })
|
|
|
|
}
|
|
|
|
|
|
|
|
export { addBookmark, listUserBookmarks, deleteUserBookmark }
|