feat: add tweet fallback

This commit is contained in:
DIYgod 2024-01-14 11:54:42 +08:00
parent 3edf43af7c
commit 4327e85a5b
No known key found for this signature in database
4 changed files with 15 additions and 5 deletions

View File

@ -1,7 +1,7 @@
{
"latest": [
151, 53994, 54570, 54481, 54537, 54302, 53944, 55768, 55855, 56592, 54986,
56875, 56873, 56666, 55996, 57407, 56249, 57858, 56973, 58600
56875, 56873, 56666, 55996, 57407, 56249, 57858, 56973, 58600, 57399
],
"comment": [54986],
"comment_content": ["snowcherryblossom"]

View File

@ -15,7 +15,7 @@ export async function GET(req: Request): Promise<Response> {
const tweet = (await cacheGet({
key: `tweet/${tweetId}`,
noUpdate: true,
expireTime: 60 * 60 * 24 * 3,
noExpire: true,
getValueFun: async () => await getTweet(tweetId),
})) as Tweet | undefined
return res.status(tweet ? 200 : 404).json({ data: tweet ?? null })

View File

@ -11,13 +11,20 @@ const components: TwitterComponents = {
MediaImg: (props) => <Image {...props} fill unoptimized alt="tweet-media" />,
}
export default function Tweet({ id }: { id: string }) {
export default function Tweet({
id,
fullUrl,
}: {
id: string
fullUrl: string
}) {
return (
<div className="flex justify-center">
<ReactTweet
id={id}
components={components}
apiUrl={`${SITE_URL}/api/tweet?id=${id}`}
fallback={<>{fullUrl}</>}
/>
</div>
)

View File

@ -8,12 +8,15 @@ export const TweetTransformer: Transformer = {
shouldTransform(url) {
const { host, pathname } = url
return isHostIncludes("twitter.com", host) && pathname.includes("/status/")
return (
(isHostIncludes("twitter.com", host) || isHostIncludes("x.com", host)) &&
pathname.includes("/status/")
)
},
getHTML(url) {
const { pathname } = url
const matched = match<{ id: string }>("/:user/status/:id")(pathname)
if (!matched) return
return `<tweet id="${matched.params.id}" />`
return `<tweet id="${matched.params.id}" fullUrl="${url}" />`
},
}