Skip to content

Commit

Permalink
[TwitterBridge] Improve timeline processing for username mode (#1946)
Browse files Browse the repository at this point in the history
  • Loading branch information
fivefilters authored Apr 12, 2021
1 parent 65be209 commit 76c3833
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions bridges/TwitterBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,22 @@ private function getApiURI() {
. urlencode($this->getInput('q'))
. '&tweet_mode=extended&tweet_search_mode=live';
case 'By username':
return self::API_URI
. '/2/timeline/profile/'
. $this->getRestId($this->getInput('u'))
. '.json?tweet_mode=extended';
// use search endpoint if without replies or without retweets enabled
if ($this->getInput('noretweet') || $this->getInput('norep')) {
$query = 'from:' . $this->getInput('u');
// Twitter's from: search excludes retweets by default
if (!$this->getInput('noretweet')) $query .= ' include:nativeretweets';
if ($this->getInput('norep')) $query .= ' exclude:replies';
return self::API_URI
. '/2/search/adaptive.json?q='
. urlencode($query)
. '&tweet_mode=extended&tweet_search_mode=live';
} else {
return self::API_URI
. '/2/timeline/profile/'
. $this->getRestId($this->getInput('u'))
. '.json?tweet_mode=extended';
}
case 'By list':
return self::API_URI
. '/2/timeline/list.json?list_id='
Expand Down Expand Up @@ -260,7 +272,35 @@ public function collectData(){
}
}

foreach($data->globalObjects->tweets as $tweet) {
$tweets = array();

// Extract tweets from timeline property when in username mode
// This fixes number of issues:
// * If there's a retweet of a quote tweet, the quoted tweet will not appear in results (since it wasn't retweeted directly)
// * Pinned tweets do not get stuck at the bottom
if ($this->queriedContext === 'By username') {
foreach($data->timeline->instructions[0]->addEntries->entries as $tweet) {
if (!isset($tweet->content->item)) continue;
$tweetId = $tweet->content->item->content->tweet->id;
$selectedTweet = $this->getTweet($tweetId, $data->globalObjects);
if (!$selectedTweet) continue;
// If this is a retweet, it will contain shorter text and will point to the original full tweet (retweeted_status_id_str).
// Let's use the original tweet text.
if (isset($selectedTweet->retweeted_status_id_str)) {
$tweetId = $selectedTweet->retweeted_status_id_str;
$selectedTweet = $this->getTweet($tweetId, $data->globalObjects);
if (!$selectedTweet) continue;
}
// use $tweetId as key to avoid duplicates (e.g. user retweeting their own tweet)
$tweets[$tweetId] = $selectedTweet;
}
} else {
foreach($data->globalObjects->tweets as $tweet) {
$tweets[] = $tweet;
}
}

foreach($tweets as $tweet) {

/* Debug::log('>>> ' . json_encode($tweet)); */
// Skip spurious retweets
Expand Down Expand Up @@ -567,4 +607,12 @@ private function getUserInformation($userId, $apiData) {
}
}
}

private function getTweet($tweetId, $apiData) {
if (property_exists($apiData->tweets, $tweetId)) {
return $apiData->tweets->$tweetId;
} else {
return null;
}
}
}

0 comments on commit 76c3833

Please sign in to comment.