wip: implement add bookmark
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { User } from "@markone/core/user"
|
||||
import type { Bookmark } from "@markone/core/bookmark"
|
||||
import { db } from "~/database.ts"
|
||||
import { DEMO_BOOKMARKS } from "./demo-bookmarks.ts"
|
||||
|
||||
@@ -21,4 +22,18 @@ VALUES ($id, $userId, $kind, $title, $url)
|
||||
insert(DEMO_BOOKMARKS)
|
||||
}
|
||||
|
||||
function insertBookmark(bookmark: Bookmark, user: User) {
|
||||
const query = db.query(`
|
||||
INSERT INTO bookmarks (id, user_id, kind, title, url)
|
||||
VALUES ($id, $userId, $kind, $title, $url)
|
||||
`)
|
||||
query.run({
|
||||
id: bookmark.id,
|
||||
userId: user.id,
|
||||
kind: bookmark.kind,
|
||||
title: bookmark.title,
|
||||
url: bookmark.url,
|
||||
})
|
||||
}
|
||||
|
||||
export { insertDemoBookmarks }
|
||||
|
@@ -1,8 +1,12 @@
|
||||
import { DEMO_USER } from "@markone/core/user"
|
||||
import type { LinkBookmark, BookmarkTag } from "@markone/core/bookmark"
|
||||
import { type } from "arktype"
|
||||
import { ulid } from "ulid"
|
||||
import { db } from "~/database.ts"
|
||||
import { HttpError } from "~/error.ts"
|
||||
import type { User } from "~/user/user.ts"
|
||||
import { JSDOM } from "jsdom"
|
||||
import { Readability } from "@mozilla/readability"
|
||||
|
||||
const BOOKMARK_PAGINATION_LIMIT = 100
|
||||
|
||||
@@ -11,10 +15,18 @@ const ListUserBookmarksParams = type({
|
||||
skip: ["number", "=", 5],
|
||||
})
|
||||
|
||||
const AddBookmarkRequestBody = type({
|
||||
"title?": "string",
|
||||
kind: "string",
|
||||
url: "string",
|
||||
tags: "string[]",
|
||||
"force?": "boolean",
|
||||
})
|
||||
|
||||
async function listUserBookmarks(request: Bun.BunRequest<"/api/bookmarks">, user: User) {
|
||||
const queryParams = ListUserBookmarksParams(request.params)
|
||||
if (queryParams instanceof type.errors) {
|
||||
throw new HttpError(400, queryParams.summary)
|
||||
throw new HttpError(400, "", queryParams.summary)
|
||||
}
|
||||
|
||||
const listBookmarksQuery = db.query(
|
||||
@@ -37,7 +49,6 @@ ORDER BY bookmarks.id LIMIT $limit OFFSET $skip
|
||||
}
|
||||
|
||||
async function deleteUserBookmark(request: Bun.BunRequest<"/api/bookmark/:id">, user: User) {
|
||||
console.log("askldjlskajdkl")
|
||||
if (user.id !== DEMO_USER.id) {
|
||||
const deleteBookmarkQuery = db.query("DELETE FROM bookmarks WHERE user_id = $userId AND id = $id")
|
||||
const tx = db.transaction(() => {
|
||||
@@ -48,4 +59,67 @@ async function deleteUserBookmark(request: Bun.BunRequest<"/api/bookmark/:id">,
|
||||
return Response.json(undefined, { status: 204 })
|
||||
}
|
||||
|
||||
export { listUserBookmarks, deleteUserBookmark }
|
||||
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 }
|
||||
|
Reference in New Issue
Block a user