Skip to content

Commit

Permalink
✨ Support bulk importing of existing links via API
Browse files Browse the repository at this point in the history
  • Loading branch information
BetaHuhn committed Sep 1, 2021
1 parent 2097b14 commit 2645bd5
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 60 deletions.
114 changes: 54 additions & 60 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions server/router/api/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { Stat } from '../../models/stats'

import log from '../../utils/log'
import { parseUrl } from '../../utils/url'
import emojis from '../../utils/emojis'

import { parsePaginate } from '../../middleware'

export const router = express.Router()
Expand Down Expand Up @@ -54,6 +56,54 @@ router.post('/', async (req: express.Request, res: express.Response, next: expre
}
})

router.post('/bulk', async (req: express.Request, res: express.Response, next: express.NextFunction) => {
try {
type Bookmark = {
url: string,
title?: string,
crate?: string
}

const bookmarks: Bookmark[] | undefined = req.body
if (!bookmarks) return res.fail(400, 'No bookmarks provided')

const links: Link[] = []
for await (const bookmark of bookmarks) {

if (!bookmark.url) continue

let crate
if (bookmark.crate !== undefined) {
crate = await Crate.findOne({ name: bookmark.crate })
if (!crate) {
const emojiKeys = Object.keys(emojis)

// Try to find a matching emoji based on the bookmark folder name (not very accurate but better than random)
const icon =
// See if any icon includes the name
emojiKeys.find((icon) => icon.includes(bookmark.crate?.toLowerCase() as string)) ||
// See if the name includes any icon
emojiKeys.find((icon) => (bookmark.crate?.toLowerCase() as string).includes(icon)) ||
undefined

log.debug(icon)
crate = await Crate.create(bookmark.crate, undefined, icon || undefined)
}
}

const parsedUrl = parseUrl(bookmark.url)
const favicon = new URL(parsedUrl).origin + '/favicon.ico'

const link = await Link.create(parsedUrl, { title: bookmark.title, icon: favicon }, crate && crate.id)
links.push(link)
}

res.ok(links)
} catch (err) {
return next(err)
}
})

router.get('/', parsePaginate, async (req: express.Request, res: express.Response, next: express.NextFunction) => {
try {
const limit = req?.paginate?.limit
Expand Down

0 comments on commit 2645bd5

Please sign in to comment.