(input: z.output<typeof createReadLaterItemSchema>)
| 30 | |
| 31 | const 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({}) |
nothing calls this directly
no test coverage detected