diff --git a/lib/routes/twitter/api/web-api/utils.ts b/lib/routes/twitter/api/web-api/utils.ts index 560211aab..522cdb147 100644 --- a/lib/routes/twitter/api/web-api/utils.ts +++ b/lib/routes/twitter/api/web-api/utils.ts @@ -298,6 +298,26 @@ export const paginationTweets = async (endpoint: string, userId: number | undefi return gridEntries || moduleItems || entries || []; }; +const getLegacyUser = (tweet: any) => tweet.core?.user_result?.result?.legacy || tweet.core?.user_results?.result?.legacy; + +const getCoreUser = (tweet: any) => tweet.core?.user_result?.result?.core || tweet.core?.user_results?.result?.core; + +const hydrateLegacyUser = (legacy: any, tweet: any) => { + legacy.user = getLegacyUser(tweet); + + const coreUser = getCoreUser(tweet); + if (!legacy.user || !coreUser) { + return; + } + + if (coreUser.name) { + legacy.user.name = coreUser.name; + } + if (coreUser.screen_name) { + legacy.user.screen_name = coreUser.screen_name; + } +}; + export function gatherLegacyFromData(entries: any[], filterNested?: string[], userId?: number | string) { const tweets: any[] = []; const filteredEntries: any[] = []; @@ -327,32 +347,12 @@ export function gatherLegacyFromData(entries: any[], filterNested?: string[], us if (!t?.legacy) { continue; } - t.legacy.user = t.core?.user_result?.result?.legacy || t.core?.user_results?.result?.legacy; - // Add name and screen_name from core to maintain compatibility - if (t.legacy.user && t.core?.user_results?.result?.core) { - const coreUser = t.core.user_results.result.core; - if (coreUser.name) { - t.legacy.user.name = coreUser.name; - } - if (coreUser.screen_name) { - t.legacy.user.screen_name = coreUser.screen_name; - } - } + hydrateLegacyUser(t.legacy, t); t.legacy.id_str = t.rest_id; // avoid falling back to conversation_id_str elsewhere const quote = t.quoted_status_result?.result?.tweet || t.quoted_status_result?.result; - if (quote) { + if (quote?.legacy) { t.legacy.quoted_status = quote.legacy; - t.legacy.quoted_status.user = quote.core.user_result?.result?.legacy || quote.core.user_results?.result?.legacy; - // Add name and screen_name from core for quoted status user - if (t.legacy.quoted_status.user && quote.core?.user_results?.result?.core) { - const quoteCoreUser = quote.core.user_results.result.core; - if (quoteCoreUser.name) { - t.legacy.quoted_status.user.name = quoteCoreUser.name; - } - if (quoteCoreUser.screen_name) { - t.legacy.quoted_status.user.screen_name = quoteCoreUser.screen_name; - } - } + hydrateLegacyUser(t.legacy.quoted_status, quote); } if (t.note_tweet) { const tmp = t.note_tweet.note_tweet_results.result; diff --git a/lib/routes/twitter/utils.ts b/lib/routes/twitter/utils.ts index e1de97426..9eee3dfcc 100644 --- a/lib/routes/twitter/utils.ts +++ b/lib/routes/twitter/utils.ts @@ -210,7 +210,7 @@ const ProcessFeed = (ctx, { data = [] }, params = {}) => { if (item.is_quote_status) { const quoteData = item.quoted_status; - if (quoteData) { + if (quoteData?.user) { quoteData.full_text = quoteData.full_text || quoteData.text; const author = quoteData.user; quote += '