wip: implement add bookmark
This commit is contained in:
@@ -8,13 +8,16 @@
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"@types/bun": "^1.2.12",
|
||||
"@types/jsdom": "^21.1.7",
|
||||
"@types/uid-safe": "^2.1.5"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5"
|
||||
},
|
||||
"dependencies": {
|
||||
"@mozilla/readability": "^0.6.0",
|
||||
"arktype": "^2.1.20",
|
||||
"jsdom": "^26.1.0",
|
||||
"uid-safe": "^2.1.5",
|
||||
"ulid": "^3.0.0"
|
||||
}
|
||||
|
@@ -90,7 +90,7 @@ async function signUp(request: Bun.BunRequest<"/api/sign-up">) {
|
||||
|
||||
const signUpRequest = SignUpRequest(body)
|
||||
if (signUpRequest instanceof type.errors) {
|
||||
throw new HttpError(400, signUpRequest.summary)
|
||||
throw new HttpError(400, "BadRequestBody", signUpRequest.summary)
|
||||
}
|
||||
|
||||
const { username, password } = signUpRequest
|
||||
@@ -110,7 +110,7 @@ async function login(request: Bun.BunRequest<"/api/login">) {
|
||||
|
||||
const loginRequest = LoginRequest(body)
|
||||
if (loginRequest instanceof type.errors) {
|
||||
throw new HttpError(400, loginRequest.summary)
|
||||
throw new HttpError(400, "BadRequestBody")
|
||||
}
|
||||
|
||||
const foundUser = findUserByUsername(loginRequest.username, {
|
||||
@@ -145,7 +145,7 @@ async function logout(request: Bun.BunRequest<"/api/logout">, user: User): Promi
|
||||
|
||||
forgetAllSessions(user)
|
||||
deleteAllAuthTokensQuery.run({ userId: user.id })
|
||||
return new Response(undefined, { status: 200 })
|
||||
return new Response(undefined, { status: 204 })
|
||||
}
|
||||
|
||||
export { authenticated, signUp, login, logout }
|
||||
|
@@ -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 }
|
||||
|
@@ -25,7 +25,8 @@ CREATE TABLE IF NOT EXISTS bookmarks(
|
||||
user_id TEXT NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
url TEXT NOT NULL
|
||||
url TEXT NOT NULL,
|
||||
content_html TEXT NOT NULL,
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tags(
|
||||
|
@@ -1,6 +1,7 @@
|
||||
class HttpError {
|
||||
constructor(
|
||||
public readonly status: number,
|
||||
public readonly code?: string,
|
||||
public readonly message?: string,
|
||||
) {}
|
||||
}
|
||||
|
@@ -13,8 +13,8 @@ function httpHandler<Route extends string>(
|
||||
response = await handler(request)
|
||||
} catch (error) {
|
||||
if (error instanceof HttpError) {
|
||||
if (error.message) {
|
||||
response = Response.json({ message: error.message }, { status: error.status })
|
||||
if (error.message || error.code) {
|
||||
response = Response.json({ code: error.code, message: error.message }, { status: error.status })
|
||||
} else {
|
||||
response = new Response(undefined, { status: error.status })
|
||||
}
|
||||
@@ -36,7 +36,7 @@ function preflightHandler<Route extends string>({ allowedMethods }: { allowedMet
|
||||
new Response(undefined, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Access-Control-Allow-Origin": ALLOWED_ORIGINS,
|
||||
"Access-Control-Allow-Origin": ALLOWED_ORIGINS.join(", "),
|
||||
"Access-Control-Allow-Methods": allowedMethods.join(", "),
|
||||
"Access-Control-Allow-Credentials": "true",
|
||||
},
|
||||
|
@@ -1,7 +1,7 @@
|
||||
import { authenticated, login, logout, signUp } from "./auth/auth.ts"
|
||||
import { startBackgroundSessionCleanup } from "./auth/session.ts"
|
||||
import { insertDemoBookmarks } from "./bookmark/bookmark.ts"
|
||||
import { listUserBookmarks, deleteUserBookmark } from "./bookmark/handlers.ts"
|
||||
import { addBookmark, listUserBookmarks, deleteUserBookmark } from "./bookmark/handlers.ts"
|
||||
import { migrateDatabase } from "./database.ts"
|
||||
import { httpHandler, preflightHandler } from "./http-handler.ts"
|
||||
import { createDemoUser } from "./user/user.ts"
|
||||
@@ -25,6 +25,7 @@ async function main() {
|
||||
},
|
||||
"/api/bookmarks": {
|
||||
GET: authenticated(listUserBookmarks),
|
||||
POST: authenticated(addBookmark),
|
||||
},
|
||||
"/api/bookmark/:id": {
|
||||
DELETE: authenticated(deleteUserBookmark),
|
||||
|
Reference in New Issue
Block a user