From fc42bebb475cdd7177b862f902609c1d007d2e9f Mon Sep 17 00:00:00 2001 From: Rongrong Date: Sat, 27 Jan 2024 16:11:32 +0000 Subject: [PATCH] fix(route/twitter): reply URL; threads when excludeReplies=1 (#14330) Without `legacy.id_str`, reply URLs would be constructed with conversion IDs, resulting in: - wrong URLs: pointing to the first tweet in the conversion (thread) - multiple items with the same guid: each tweet in a conversion (thread) had the same URL and thus guid The bug was caused by the widely used temporary fix (legacy.id_str || legacy.conversation_id_str) after Twitter had upgraded their API. Thus, it is quite easy to fix it: Refill legacy.id_str using the rest_id that resides in the container of legacy. With it fixed, take a step further to include threads when excludeReplies=1 (simulating the behavior of Twitter Web/App). Signed-off-by: Rongrong --- lib/v2/twitter/web-api/twitter-api.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/v2/twitter/web-api/twitter-api.js b/lib/v2/twitter/web-api/twitter-api.js index 9c912adef0..c0b86bca53 100644 --- a/lib/v2/twitter/web-api/twitter-api.js +++ b/lib/v2/twitter/web-api/twitter-api.js @@ -151,6 +151,7 @@ function gatherLegacyFromData(entries, filterNested, userId) { continue; } t.legacy.user = t.core?.user_result?.result?.legacy || t.core?.user_results?.result?.legacy; + t.legacy.id_str = t.rest_id; // avoid falling back to conversation_id_str elsewhere const quote = t.quoted_status_result?.result; if (quote) { t.legacy.quoted_status = quote.legacy; @@ -257,7 +258,11 @@ const getUserTweets = async (cache, id, params = {}) => { } const idSet = new Set(); tweets = tweets - .filter((tweet) => !tweet.in_reply_to_screen_name) // exclude replies + .filter( + (tweet) => + !tweet.in_reply_to_user_id_str || // exclude replies + tweet.in_reply_to_user_id_str === rest_id // but include replies to self (threads) + ) .map((tweet) => { const id_str = tweet.id_str || tweet.conversation_id_str; return !idSet.has(id_str) && idSet.add(id_str) && tweet;