-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(import): implement WIP bookmark import execution with metadata s…
…upport Signed-off-by: Robert Goniszewski <[email protected]>
- Loading branch information
1 parent
4bb9cba
commit ecb7bad
Showing
8 changed files
with
336 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import type { BookmarkEdit } from '$lib/types/Bookmark.type'; | ||
import { db } from '$lib/database/db'; | ||
import { | ||
bookmarkSchema, bookmarksToTagsSchema, categorySchema, tagSchema | ||
} from '$lib/database/schema'; | ||
import { eq } from 'drizzle-orm'; | ||
|
||
import { createSlug } from '../create-slug'; | ||
|
||
import type { ImportExecutionResult } from '$lib/types/BookmarkImport.type'; | ||
|
||
export async function executeImport( | ||
bookmarks: BookmarkEdit[], | ||
userId: number | ||
): Promise<ImportExecutionResult> { | ||
const categoryCache = new Map<string, number>(); | ||
const tagCache = new Map<string, number>(); | ||
|
||
async function getOrCreateCategory(categoryPath: string, userId: number) { | ||
if (categoryCache.has(categoryPath)) { | ||
return categoryCache.get(categoryPath); | ||
} | ||
|
||
const parts = categoryPath.split('/').filter(Boolean); | ||
let parentId: number | null = null; | ||
let currentPath = ''; | ||
|
||
for (const part of parts) { | ||
currentPath += `/${part}`; | ||
const slug = createSlug(part); | ||
|
||
let category = await db.query.categorySchema.findFirst({ | ||
where: eq(categorySchema.slug, slug) | ||
}); | ||
|
||
if (!category) { | ||
const [newCategory] = await db | ||
.insert(categorySchema) | ||
.values({ | ||
name: part, | ||
slug, | ||
ownerId: userId, | ||
parentId, | ||
created: new Date(), | ||
updated: new Date() | ||
}) | ||
.returning(); | ||
category = newCategory as typeof categorySchema.$inferSelect; | ||
} | ||
|
||
categoryCache.set(currentPath, category.id); | ||
parentId = category.id; | ||
} | ||
|
||
return parentId; | ||
} | ||
async function getOrCreateTag(tagName: string, userId: number) { | ||
if (tagCache.has(tagName)) { | ||
return tagCache.get(tagName); | ||
} | ||
|
||
const slug = createSlug(tagName); | ||
let tag = await db.query.tagSchema.findFirst({ | ||
where: eq(tagSchema.slug, slug) | ||
}); | ||
|
||
if (!tag) { | ||
const [newTag] = await db | ||
.insert(tagSchema) | ||
.values({ | ||
name: tagName, | ||
slug, | ||
ownerId: userId, | ||
created: new Date(), | ||
updated: new Date() | ||
}) | ||
.returning(); | ||
tag = newTag; | ||
} | ||
|
||
tagCache.set(tagName, tag.id); | ||
return tag.id; | ||
} | ||
|
||
const results = []; | ||
for (const bookmark of bookmarks) { | ||
try { | ||
const categoryId = await getOrCreateCategory(bookmark.category, userId); | ||
|
||
const [newBookmark] = await db | ||
.insert(bookmarkSchema) | ||
.values({ | ||
url: bookmark.url, | ||
title: bookmark.title, | ||
slug: createSlug(bookmark.title), | ||
domain: new URL(bookmark.url).hostname, | ||
description: bookmark.description || null, | ||
ownerId: userId, | ||
categoryId: categoryId ?? null, | ||
created: new Date(), | ||
updated: new Date() | ||
} as typeof bookmarkSchema.$inferInsert) | ||
.returning(); | ||
|
||
if (bookmark.bookmarkTags?.length) { | ||
const tagIds = await Promise.all( | ||
bookmark.bookmarkTags.map((tag) => getOrCreateTag(tag.value, userId)) | ||
); | ||
|
||
await db.insert(bookmarksToTagsSchema).values( | ||
tagIds | ||
.filter((tagId): tagId is number => tagId !== undefined) | ||
.map((tagId) => ({ | ||
bookmarkId: newBookmark.id, | ||
tagId | ||
})) | ||
); | ||
} | ||
|
||
results.push({ | ||
success: true, | ||
bookmark: newBookmark | ||
}); | ||
} catch (error) { | ||
results.push({ | ||
success: false, | ||
error: error instanceof Error ? error.message : 'Unknown error', | ||
bookmark | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
total: bookmarks.length, | ||
successful: results.filter((r) => r.success).length, | ||
failed: results.filter((r) => !r.success).length, | ||
results | ||
} as unknown as ImportExecutionResult; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { executeImport } from '$lib/utils/bookmark-import/execute-import'; | ||
import joi from 'joi'; | ||
|
||
import { json } from '@sveltejs/kit'; | ||
|
||
import type { RequestHandler } from './$types'; | ||
export const POST: RequestHandler = async ({ locals, request }) => { | ||
const ownerId = locals.user?.id; | ||
|
||
if (!ownerId) { | ||
return json({ success: false, error: 'Unauthorized' }, { status: 401 }); | ||
} | ||
|
||
const requestBody = await request.json(); | ||
|
||
const validationSchema = joi.object({ | ||
bookmarks: joi | ||
.array() | ||
.items( | ||
joi.object({ | ||
url: joi.string().uri().required(), | ||
title: joi.string().required(), | ||
description: joi.string().allow('').optional(), | ||
category: joi.string().required(), | ||
bookmarkTags: joi | ||
.array() | ||
.items( | ||
joi.object({ | ||
label: joi.string().required(), | ||
value: joi.string().required() | ||
}) | ||
) | ||
.optional() | ||
}) | ||
) | ||
.required() | ||
}); | ||
|
||
const { error } = validationSchema.validate(requestBody); | ||
|
||
if (error) { | ||
return json({ success: false, error: error.message }, { status: 400 }); | ||
} | ||
|
||
try { | ||
const result = await executeImport(requestBody.bookmarks, ownerId); | ||
|
||
return json(result, { status: 201 }); | ||
} catch (error: any) { | ||
console.error('Error importing bookmarks:', error?.message); | ||
|
||
return json({ success: false, error: error?.message }, { status: 500 }); | ||
} | ||
}; |
Oops, something went wrong.