MCPcopy Create free account
hub / github.com/Y80/bmm / createFromUrl

Function createFromUrl

src/controllers/UserReadLater.controller.ts:32–73  ·  view source on GitHub ↗
(input: z.output<typeof createReadLaterItemSchema>)

Source from the content-addressed store, hash-verified

30
31const UserReadLaterController = {
32 async createFromUrl(input: z.output<typeof createReadLaterItemSchema>) {
33 const userId = await getAuthedUserId()
34 let url = input.url
35 let html = ''
36 try {
37 const res = await fetchHtml(input.url)
38 html = res.html
39 url = res.finalUrl
40 } catch {}
41
42 const count = await db.$count(
43 userReadLaterItems,
44 and(eq(userReadLaterItems.userId, userId), eq(userReadLaterItems.url, url))
45 )
46 if (count > 0) throw new Error('该链接已在稍后阅读中')
47
48 let article = extractArticleFromHtml(html, url)
49 let mode: CreateReadLaterItemResult['mode'] = html ? 'html' : 'url'
50 if (html) {
51 try {
52 article = await analyzeReadLaterArticle(html, url)
53 mode = 'ai'
54 } catch {}
55 }
56
57 const [row] = await db
58 .insert(userReadLaterItems)
59 .values({
60 url,
61 icon: null,
62 title: article.title,
63 summary: article.summary,
64 estimatedReadingMinutes: article.estimatedReadingMinutes,
65 status: 'unread',
66 userId,
67 })
68 .returning()
69 return {
70 item: row,
71 mode,
72 }
73 },
74
75 async findMany(query?: z.output<typeof findManyReadLaterItemsSchema>) {
76 query ||= findManyReadLaterItemsSchema.parse({})

Callers

nothing calls this directly

Calls 4

getAuthedUserIdFunction · 0.90
extractArticleFromHtmlFunction · 0.90
analyzeReadLaterArticleFunction · 0.90
fetchHtmlFunction · 0.85

Tested by

no test coverage detected