implement nav chrome for bookmark previewer
This commit is contained in:
@@ -3,6 +3,7 @@ import { Readability } from "@mozilla/readability"
|
||||
import { JSDOM } from "jsdom"
|
||||
import { db } from "~/database.ts"
|
||||
import { DEMO_BOOKMARKS } from "./demo-bookmarks.ts"
|
||||
import type { Bookmark, BookmarkTag } from "@markone/core/bookmark"
|
||||
|
||||
class LinkUnreachable {}
|
||||
class UnsupportedLink {}
|
||||
@@ -41,6 +42,23 @@ function findBookmarkHtml(id: string, user: User): string | null {
|
||||
return content_html
|
||||
}
|
||||
|
||||
function findBookmark(id: string, user: User): Bookmark | null {
|
||||
const bookmarkQuery = db.query<Bookmark, { id: string; userId: string }>(
|
||||
"SELECT id, kind, title, url FROM bookmarks WHERE id = $id AND user_id = $userId",
|
||||
)
|
||||
const bookmark = bookmarkQuery.get({ id, userId: user.id })
|
||||
if (!bookmark) {
|
||||
return null
|
||||
}
|
||||
|
||||
const tagsQuery = db.query<BookmarkTag, { id: string }>("SELECT id, name FROM tags WHERE bookmark_id = $id")
|
||||
const tags = tagsQuery.all({ id })
|
||||
|
||||
bookmark.tags = tags
|
||||
|
||||
return bookmark
|
||||
}
|
||||
|
||||
async function cacheWebsite(url: string): Promise<CachedPage | null> {
|
||||
const websiteText = await fetch(url)
|
||||
.catch(() => {
|
||||
@@ -85,5 +103,5 @@ async function cacheWebsite(url: string): Promise<CachedPage | null> {
|
||||
}
|
||||
}
|
||||
|
||||
export { insertDemoBookmarks, findBookmarkHtml, cacheWebsite }
|
||||
export { insertDemoBookmarks, findBookmark, findBookmarkHtml, cacheWebsite }
|
||||
export { LinkUnreachable, UnsupportedLink }
|
||||
|
@@ -5,7 +5,7 @@ import { ulid } from "ulid"
|
||||
import { db } from "~/database.ts"
|
||||
import { HttpError } from "~/error.ts"
|
||||
import type { User } from "~/user/user.ts"
|
||||
import { LinkUnreachable, UnsupportedLink, findBookmarkHtml, cacheWebsite } from "./bookmark.ts"
|
||||
import { LinkUnreachable, UnsupportedLink, findBookmarkHtml, cacheWebsite, findBookmark } from "./bookmark.ts"
|
||||
|
||||
const BOOKMARK_PAGINATION_LIMIT = 100
|
||||
|
||||
@@ -46,7 +46,7 @@ LIMIT $limit OFFSET $skip
|
||||
return Response.json(results, { status: 200 })
|
||||
}
|
||||
|
||||
async function deleteUserBookmark(request: Bun.BunRequest<"/api/bookmark/:id">, user: User) {
|
||||
async function deleteUserBookmark(request: Bun.BunRequest<"/api/bookmarks/: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(() => {
|
||||
@@ -127,7 +127,7 @@ VALUES ($id, $bookmarkId, $name)
|
||||
return Response.json(undefined, { status: 204 })
|
||||
}
|
||||
|
||||
async function fetchBookmark(request: Bun.BunRequest<"/api/bookmark/:id">, user: User) {
|
||||
async function fetchBookmark(request: Bun.BunRequest<"/api/bookmarks/:id">, user: User) {
|
||||
switch (request.headers.get("Accept")) {
|
||||
case "text/html": {
|
||||
const html = findBookmarkHtml(request.params.id, user)
|
||||
@@ -141,6 +141,15 @@ async function fetchBookmark(request: Bun.BunRequest<"/api/bookmark/:id">, user:
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
case "application/json": {
|
||||
const bookmark = findBookmark(request.params.id, user)
|
||||
if (bookmark === null) {
|
||||
throw new HttpError(404)
|
||||
}
|
||||
return Response.json(bookmark, { status: 200 })
|
||||
}
|
||||
|
||||
default:
|
||||
throw new HttpError(400, "UnsupportedAcceptHeader")
|
||||
}
|
||||
|
Reference in New Issue
Block a user