This commit is contained in:
dangered wolf 2023-10-17 23:44:49 -04:00
parent 98d3e54ff0
commit 56b26d2121
No known key found for this signature in database
GPG key ID: 41E4D37680ED8B58
3 changed files with 18 additions and 13 deletions

View file

@ -16,7 +16,7 @@ const populateTweetProperties = async (
conversation: TweetResultsByRestIdResult, // TimelineBlobPartial,
language: string | undefined
// eslint-disable-next-line sonarjs/cognitive-complexity
): Promise<APITweet> => {
): Promise<APITweet | null> => {
const apiTweet = {} as APITweet;
/* Sometimes, Twitter returns a different kind of Tweet type called 'TweetWithVisibilityResults'.
@ -38,6 +38,11 @@ const populateTweetProperties = async (
tweet.views = tweet?.tweet?.views;
}
if (typeof tweet.core === 'undefined') {
console.log('Tweet still not valid', tweet);
return null;
}
/* With v2 conversation API we re-add the user object ot the tweet because
Twitter stores it separately in the conversation API. This is to consolidate
it in case a user appears multiple times in a thread. */
@ -292,7 +297,7 @@ export const statusAPI = async (
if (quoteTweet) {
apiTweet.quote = (await populateTweetProperties(quoteTweet, res, language)) as APITweet;
/* Only override the twitter_card if it's a basic tweet, since media always takes precedence */
if (apiTweet.twitter_card === 'tweet') {
if (apiTweet.twitter_card === 'tweet' && apiTweet.quote !== null) {
apiTweet.twitter_card = apiTweet.quote.twitter_card;
}
}

View file

@ -36,6 +36,16 @@ export const handleStatus = async (
const api = await statusAPI(status, language, event as FetchEvent, flags);
const tweet = api?.tweet as APITweet;
/* Catch this request if it's an API response */
if (flags?.api) {
return {
response: new Response(JSON.stringify(api), {
headers: { ...Constants.RESPONSE_HEADERS, ...Constants.API_RESPONSE_HEADERS },
status: api.code
})
};
}
/* If there was any errors fetching the Tweet, we'll return it */
switch (api.code) {
case 401:
@ -66,16 +76,6 @@ export const handleStatus = async (
let ivbody = '';
/* Catch this request if it's an API response */
if (flags?.api) {
return {
response: new Response(JSON.stringify(api), {
headers: { ...Constants.RESPONSE_HEADERS, ...Constants.API_RESPONSE_HEADERS },
status: api.code
})
};
}
let overrideMedia: APIMedia | undefined;
// Check if mediaNumber exists, and if that media exists in tweet.media.all. If it does, we'll store overrideMedia variable

View file

@ -152,7 +152,7 @@ const statusRequest = async (request: IRequest, event: FetchEvent, flags: InputF
Since we obviously have no media to give the user, we'll just redirect to the Tweet.
Embeds will return as usual to bots as if direct media was never specified. */
if (!isBotUA) {
if (!isBotUA && !flags.api) {
const baseUrl = getBaseRedirectUrl(request);
/* Do not cache if using a custom redirect */
const cacheControl = baseUrl !== Constants.TWITTER_ROOT ? 'max-age=0' : undefined;