diff --git a/src/api/status.ts b/src/api/status.ts index 3c69107..e5e0ef6 100644 --- a/src/api/status.ts +++ b/src/api/status.ts @@ -20,10 +20,23 @@ const populateTweetProperties = async ( ): Promise => { const apiTweet = {} as APITweet; + /* Sometimes, Twitter returns a different kind of Tweet type called 'TweetWithVisibilityResults'. + It has slightly different attributes from the regular 'Tweet' type. We fix that up here. */ + if (typeof tweet.core === 'undefined' && typeof tweet.result !== 'undefined') { tweet = tweet.result; - } else { - console.log('tweet core exists'); + } + + if (typeof tweet.core === 'undefined' && typeof tweet.tweet?.core !== 'undefined') { + tweet.core = tweet.tweet.core; + } + + if (typeof tweet.legacy === 'undefined' && typeof tweet.tweet?.legacy !== 'undefined') { + tweet.legacy = tweet.tweet?.legacy; + } + + if (typeof tweet.views === 'undefined' && typeof tweet?.tweet?.views !== 'undefined') { + tweet.views = tweet?.tweet?.views; } /* With v2 conversation API we re-add the user object ot the tweet because diff --git a/src/types/twitterTypes.d.ts b/src/types/twitterTypes.d.ts index 9aaf1d6..5f45302 100644 --- a/src/types/twitterTypes.d.ts +++ b/src/types/twitterTypes.d.ts @@ -305,6 +305,48 @@ type GraphQLUser = { }; }; +type GraphQLTweetLegacy = { + created_at: string; // "Tue Sep 14 20:00:00 +0000 2021" + conversation_id_str: string; // "1674824189176590336" + bookmark_count: number; // 0 + bookmarked: boolean; // false + favorite_count: number; // 28 + full_text: string; // "This is a test tweet" + in_reply_to_screen_name: string; // "username" + in_reply_to_status_id_str: string; // "1674824189176590336" + in_reply_to_user_id_str: string; // "783214" + is_quote_status: boolean; // false + quote_count: number; // 39 + quoted_status_id_str: string; // "1674824189176590336" + quoted_status_permalink: { + url: string; // "https://t.co/aBcDeFgHiJ" + expanded: string; // "https://twitter.com/username/status/1674824189176590336" + display: string; // "twitter.com/username/statu…" + }; + reply_count: number; // 1 + retweet_count: number; // 4 + lang: string; // "en" + possibly_sensitive: boolean; // false + possibly_sensitive_editable: boolean; // false + entities: { + media: { + display_url: string; // "pic.twitter.com/1X2X3X4X5X" + expanded_url: string; // "https://twitter.com/username/status/1674824189176590336/photo/1" "https://twitter.com/username/status/1674824189176590336/video/1" + id_str: string; // "1674824189176590336" + indices: [number, number]; // [number, number] + media_url_https: string; // "https://pbs.twimg.com/media/FAKESCREENSHOT.jpg" With videos appears to be the thumbnail + type: string; // "photo" Seems to be photo even with videos + }[]; + user_mentions: unknown[]; + urls: TcoExpansion[]; + hashtags: unknown[]; + symbols: unknown[]; + }; + extended_entities: { + media: TweetMedia[]; + }; +}; + type GraphQLTweet = { // Workaround result: GraphQLTweet; @@ -316,6 +358,18 @@ type GraphQLTweet = { result: GraphQLUser; }; }; + tweet?: { + legacy: GraphQLTweetLegacy; + views: { + count: string; // "562" + state: string; // "EnabledWithCount" + }; + core: { + user_results: { + result: GraphQLUser; + }; + } + }; edit_control: unknown; edit_perspective: unknown; is_translatable: false; @@ -325,47 +379,7 @@ type GraphQLTweet = { }; source: string; // "Twitter Web App" quoted_status_result?: GraphQLTweet; - legacy: { - created_at: string; // "Tue Sep 14 20:00:00 +0000 2021" - conversation_id_str: string; // "1674824189176590336" - bookmark_count: number; // 0 - bookmarked: boolean; // false - favorite_count: number; // 28 - full_text: string; // "This is a test tweet" - in_reply_to_screen_name: string; // "username" - in_reply_to_status_id_str: string; // "1674824189176590336" - in_reply_to_user_id_str: string; // "783214" - is_quote_status: boolean; // false - quote_count: number; // 39 - quoted_status_id_str: string; // "1674824189176590336" - quoted_status_permalink: { - url: string; // "https://t.co/aBcDeFgHiJ" - expanded: string; // "https://twitter.com/username/status/1674824189176590336" - display: string; // "twitter.com/username/statu…" - }; - reply_count: number; // 1 - retweet_count: number; // 4 - lang: string; // "en" - possibly_sensitive: boolean; // false - possibly_sensitive_editable: boolean; // false - entities: { - media: { - display_url: string; // "pic.twitter.com/1X2X3X4X5X" - expanded_url: string; // "https://twitter.com/username/status/1674824189176590336/photo/1" "https://twitter.com/username/status/1674824189176590336/video/1" - id_str: string; // "1674824189176590336" - indices: [number, number]; // [number, number] - media_url_https: string; // "https://pbs.twimg.com/media/FAKESCREENSHOT.jpg" With videos appears to be the thumbnail - type: string; // "photo" Seems to be photo even with videos - }[]; - user_mentions: unknown[]; - urls: TcoExpansion[]; - hashtags: unknown[]; - symbols: unknown[]; - }; - extended_entities: { - media: TweetMedia[]; - }; - }; + legacy: GraphQLTweetLegacy; note_tweet: { is_expandable: boolean; entity_set: { diff --git a/src/types/types.d.ts b/src/types/types.d.ts index bf62310..4afb63c 100644 --- a/src/types/types.d.ts +++ b/src/types/types.d.ts @@ -150,7 +150,7 @@ interface APITweet { replies: number; views?: number | null; - color: string; + color: string | null; quote?: APITweet; poll?: APIPoll; diff --git a/wrangler.example.toml b/wrangler.example.toml index 3f0e89c..388f146 100644 --- a/wrangler.example.toml +++ b/wrangler.example.toml @@ -14,4 +14,7 @@ analytics_engine_datasets = [ command = "npm run build" [miniflare.globals] -TEST = "true" # Will have unicode character errors in headers if not set to true \ No newline at end of file +TEST = "true" # Will have unicode character errors in headers if not set to true + +[placement] +mode = "smart" \ No newline at end of file