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

217 lines
5.7 KiB
TypeScript
Raw Normal View History

2025-05-25 15:40:16 +01:00
import type { Bookmark, BookmarkId, Tag, TaggedBookmark } from "@markone/core"
2025-05-07 15:47:08 +01:00
import type { User } from "@markone/core/user"
2025-05-13 11:55:50 +01:00
import { Readability } from "@mozilla/readability"
import { JSDOM } from "jsdom"
2025-05-07 15:47:08 +01:00
import { db } from "~/database.ts"
2025-05-25 15:40:16 +01:00
import { findTagsByNames, insertTags } from "~/tag/tag.js"
2025-05-07 15:47:08 +01:00
import { DEMO_BOOKMARKS } from "./demo-bookmarks.ts"
2025-05-13 11:55:50 +01:00
class LinkUnreachable {}
class UnsupportedLink {}
2025-05-21 13:18:16 +01:00
interface CachedContent {
2025-05-13 11:55:50 +01:00
title: string
2025-05-21 13:18:16 +01:00
mimeType: string
data: Buffer | null
2025-05-13 11:55:50 +01:00
}
2025-05-07 15:47:08 +01:00
function insertDemoBookmarks(user: User) {
const query = db.query(`
2025-05-21 13:18:16 +01:00
INSERT OR IGNORE INTO bookmarks (id, user_id, title, url, mime_type, content)
VALUES ${Array(DEMO_BOOKMARKS.length).fill("(?,?,?,?,NULL,NULL)").join(",")}
2025-05-07 15:47:08 +01:00
`)
2025-05-21 13:18:16 +01:00
const args: Parameters<typeof query.run> = []
for (const bookmark of DEMO_BOOKMARKS) {
args.push(bookmark.id, user.id, bookmark.title, bookmark.url)
}
query.run(...args)
}
function insertBookmark(bookmark: Bookmark, cachedContent: CachedContent | null, user: User) {
const query = db.query(`
INSERT INTO bookmarks (id, user_id, title, url, mime_type, content)
VALUES ($id, $userId, $title, $url, $mimeType, $content)
`)
query.run({
id: bookmark.id,
userId: user.id,
title: bookmark.title,
url: bookmark.url,
mimeType: cachedContent?.mimeType || null,
content: cachedContent?.data ?? null,
2025-05-07 15:47:08 +01:00
})
}
2025-05-21 13:18:16 +01:00
function findBookmarkCachedContent(id: string, user: User): Buffer | null {
const query = db.query<{ content: Buffer | null }, { id: string; userId: string }>(
"SELECT content FROM bookmarks WHERE id = $id AND user_id = $userId",
)
2025-05-08 15:51:17 +01:00
const row = query.get({ id, userId: user.id })
2025-05-21 13:18:16 +01:00
return row?.content ?? null
2025-05-07 23:09:14 +01:00
}
function findBookmark(id: string, user: User): Bookmark | null {
2025-05-25 15:40:16 +01:00
const bookmarkQuery = db.query<TaggedBookmark, { id: string; userId: string }>(
2025-05-21 13:18:16 +01:00
"SELECT id, title, url FROM bookmarks WHERE id = $id AND user_id = $userId",
)
const bookmark = bookmarkQuery.get({ id, userId: user.id })
if (!bookmark) {
return null
}
2025-05-25 15:40:16 +01:00
const tagsQuery = db.query<Tag, { bookmarkId: string }>(`
2025-05-21 13:18:16 +01:00
SELECT tags.id, tags.name FROM tags
INNER JOIN bookmark_tags
ON bookmark_tags.tag_id = tags.id AND bookmark_tags.bookmark_id = $bookmarkId
`)
const tags = tagsQuery.all({ bookmarkId: id })
bookmark.tags = tags
return bookmark
}
2025-05-25 15:40:16 +01:00
function updateBookmarkTitle(bookmark: Bookmark, newTitle: string, user: User) {
const query = db.query("UPDATE bookmarks SET title = $title WHERE id = $id AND user_id = $userId")
query.run({
title: newTitle,
id: bookmark.id,
userId: user.id,
})
}
2025-05-21 23:27:17 +01:00
function deleteBookmark(id: BookmarkId, user: User) {
db.query("DELETE FROM bookmarks WHERE user_id = $userId AND id = $id").run({
id,
userId: user.id,
})
db.query("DELETE FROM bookmark_tags WHERE bookmark_id = ?").run(id)
}
2025-05-21 13:18:16 +01:00
async function cacheContent(url: string): Promise<CachedContent | null> {
const res = await fetch(url).catch(() => {
throw new LinkUnreachable()
})
let contentType = res.headers.get("Content-Type")
if (!contentType) {
contentType = "application/octet-stream"
}
if (contentType.startsWith("text/html")) {
const matches = contentType.match(/charset=([^; ]+)/gi)
let charset: string
if (!matches || matches.length <= 2) {
charset = "utf-8"
} else {
charset = matches[1] || "utf-8"
}
const websiteText = await res.text().catch(() => {
2025-05-13 11:55:50 +01:00
throw new UnsupportedLink()
})
2025-05-21 13:18:16 +01:00
const dom = new JSDOM(websiteText, { url })
const reader = new Readability(dom.window.document)
const article = reader.parse()
2025-05-13 11:55:50 +01:00
2025-05-21 13:18:16 +01:00
if (!article) {
return null
}
2025-05-13 11:55:50 +01:00
2025-05-21 13:18:16 +01:00
if (article.content) {
const newDom = new JSDOM(article.content, { url })
const doc = newDom.window.document
2025-05-13 11:55:50 +01:00
2025-05-21 13:18:16 +01:00
const lightStyleLink = doc.createElement("link")
lightStyleLink.rel = "stylesheet"
lightStyleLink.href = "/reader-styles/sakura.css"
lightStyleLink.media = "screen"
2025-05-13 11:55:50 +01:00
2025-05-21 13:18:16 +01:00
const darkStyleLink = doc.createElement("link")
darkStyleLink.rel = "stylesheet"
darkStyleLink.href = "/reader-styles/sakura-dark.css"
darkStyleLink.media = "screen and (prefers-color-scheme: dark)"
2025-05-13 11:55:50 +01:00
2025-05-21 13:18:16 +01:00
doc.head.appendChild(lightStyleLink)
doc.head.appendChild(darkStyleLink)
article.content = newDom.serialize()
}
2025-05-13 11:55:50 +01:00
2025-05-21 13:18:16 +01:00
return {
title: article.title || "Untitled",
mimeType: "text/html",
data: article.content ? Buffer.from(article.content, charset as BufferEncoding) : null,
}
2025-05-13 11:55:50 +01:00
}
2025-05-21 13:18:16 +01:00
throw new UnsupportedLink()
}
2025-05-25 15:40:16 +01:00
function assignTagsToBookmark(tags: Tag[], bookmark: Bookmark) {
2025-05-21 13:18:16 +01:00
const query = db.query(`
2025-05-25 15:40:16 +01:00
INSERT OR IGNORE INTO bookmark_tags (tag_id, bookmark_id)
2025-05-21 13:18:16 +01:00
VALUES ${Array(tags.length).fill("(?,?)").join(",")}
`)
const args: Parameters<typeof query.run> = []
for (const tag of tags) {
args.push(tag.id)
args.push(bookmark.id)
2025-05-13 11:55:50 +01:00
}
2025-05-21 13:18:16 +01:00
query.run(...args)
}
2025-05-25 15:40:16 +01:00
function findBookmarkTags(bookmark: Bookmark): Tag[] {
const query = db.query<Tag, { bookmarkId: string }>(`
2025-05-21 13:18:16 +01:00
SELECT tags.name as name, tags.id as id FROM bookmark_tags
INNER JOIN tags
ON tags.id = bookmark_tags.tag_id
WHERE bookmark_tags.bookmark_id = $bookmarkId
`)
const tags = query.all({ bookmarkId: bookmark.id })
return tags
2025-05-13 11:55:50 +01:00
}
2025-05-25 15:40:16 +01:00
function updateBookmarkTags(bookmark: Bookmark, tagNames: string[], user: User) {
const tags = findTagsByNames(tagNames, user)
const existingTagNames = new Set<string>()
for (const tag of tags) {
existingTagNames.add(tag.name)
}
const newTagNames: string[] = []
for (const name of tagNames) {
if (!existingTagNames.has(name)) {
newTagNames.push(name)
}
}
if (newTagNames.length > 0) {
const newTags = insertTags(newTagNames, user)
tags.push(...newTags)
}
assignTagsToBookmark(tags, bookmark)
return tags
}
2025-05-21 13:18:16 +01:00
export {
insertDemoBookmarks,
insertBookmark,
2025-05-25 15:40:16 +01:00
updateBookmarkTitle,
2025-05-21 23:27:17 +01:00
deleteBookmark,
2025-05-21 13:18:16 +01:00
findBookmark,
findBookmarkCachedContent,
cacheContent,
assignTagsToBookmark,
findBookmarkTags,
2025-05-25 15:40:16 +01:00
updateBookmarkTags,
2025-05-21 13:18:16 +01:00
}
2025-05-13 11:55:50 +01:00
export { LinkUnreachable, UnsupportedLink }