This commit is contained in:
abhinav7x94 2026-08-26 04:01:27 +05:30 committed by GitHub
commit 10f886851c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 66 additions and 2 deletions

View file

@ -46,6 +46,11 @@ interface TwitterAPITweet {
}
}
interface TwitterAPIVisibilityResult {
__typename: "TweetWithVisibilityResults"
tweet?: TwitterAPITweet
}
interface MediaEntity {
type: string
media_url_https: string
@ -270,9 +275,13 @@ export function transformTweetData(
return null
}
const tweet = tweetData as TwitterAPITweet
const visibilityResult = tweetData as TwitterAPIVisibilityResult
const tweet =
visibilityResult.__typename === "TweetWithVisibilityResults"
? visibilityResult.tweet
: (tweetData as TwitterAPITweet)
if (!tweet.legacy) {
if (!tweet?.legacy) {
return null
}

View file

@ -0,0 +1,55 @@
import { describe, expect, it } from "bun:test"
import { getAllTweets, type TwitterAPIResponse } from "./twitter-utils"
const apiTweet = (id: string) => ({
__typename: "Tweet",
legacy: {
favorite_count: 0,
created_at: "Wed Oct 10 20:19:24 +0000 2018",
id_str: id,
full_text: `Tweet ${id}`,
},
})
const timelineEntry = (id: string, result: unknown) => ({
entryId: `tweet-${id}`,
sortIndex: id,
content: { itemContent: { tweet_results: { result } } },
})
describe("getAllTweets", () => {
it("extracts visibility-wrapped results and skips unavailable tweets", () => {
const response: TwitterAPIResponse = {
data: {
bookmark_timeline_v2: {
timeline: {
instructions: [
{
type: "TimelineAddEntries",
entries: [
timelineEntry("100", apiTweet("100")),
timelineEntry("200", {
__typename: "TweetWithVisibilityResults",
limitedActionResults: {},
tweet: apiTweet("200"),
}),
timelineEntry("300", {
__typename: "TweetTombstone",
}),
timelineEntry("400", {
__typename: "TweetWithVisibilityResults",
}),
],
},
],
},
},
},
}
expect(getAllTweets(response).map((tweet) => tweet.id_str)).toEqual([
"100",
"200",
])
})
})