From 36f64a3258ad91e0a16313457f18b29637c5cca6 Mon Sep 17 00:00:00 2001 From: quickwick <2566133+quickwick@users.noreply.github.com> Date: Sun, 4 Sep 2022 20:31:36 -0700 Subject: [PATCH] feat: add preview for external urls in twitterv2 (#3006) Fix #2430 --- bridges/TwitterV2Bridge.php | 48 +++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/bridges/TwitterV2Bridge.php b/bridges/TwitterV2Bridge.php index fe1c43aa..3ba0e251 100644 --- a/bridges/TwitterV2Bridge.php +++ b/bridges/TwitterV2Bridge.php @@ -470,6 +470,24 @@ EOD $this->item['title'] = $titleText; + // Get external link info + $extURL = null; + if (isset($tweet->entities->urls) && strpos($tweet->entities->urls[0]->expanded_url, 'twitter.com') === false) { + Debug::log('Found an external link!'); + $extURL = $tweet->entities->urls[0]->expanded_url; + Debug::log($extURL); + $extDisplayURL = $tweet->entities->urls[0]->display_url; + $extTitle = $tweet->entities->urls[0]->title; + $extDesc = $tweet->entities->urls[0]->description; + if (isset($tweet->entities->urls[0]->images)) { + $extMediaOrig = $tweet->entities->urls[0]->images[0]->url; + $extMediaScaled = $tweet->entities->urls[0]->images[1]->url; + } else { + $extMediaOrig = ''; + $extMediaScaled = ''; + } + } + // Generate Avatar HTML block $picture_html = ''; if (!$hideProfilePic && isset($this->item['avatar'])) { @@ -487,6 +505,7 @@ EOD; // Generate media HTML block $media_html = ''; $quoted_media_html = ''; + $ext_media_html = ''; if (!$hideImages) { if (isset($tweet->attachments->media_keys)) { Debug::log('Generating HTML for tweet media'); @@ -496,6 +515,17 @@ EOD; Debug::log('Generating HTML for quoted tweet media'); $quoted_media_html = $this->createTweetMediaHTML($quotedTweet, $includesMedia, $retweetedMedia); } + if (isset($extURL)) { + Debug::log('Generating HTML for external link media'); + if ($this->getInput('noimgscaling')) { + $extMediaURL = $extMediaOrig; + } else { + $extMediaURL = $extMediaScaled; + } + $ext_media_html = << +EOD; + } } // Generate the HTML for Item content @@ -514,8 +544,8 @@ EOD; if (isset($quotedTweet)) { $quotedTweetURI = self::URI . $quotedUser->username . '/status/' . $quotedTweet->id; $quote_html = << +
+

$quotedUser->name @$quotedUser->username ยท $quotedTweet->created_at

$cleanedQuotedTweet @@ -525,6 +555,20 @@ QUOTE; $this->item['content'] .= $quote_html; } + // Add External Link HTML, if relevant + if (isset($extURL)) { + Debug::log('Adding HTML for external link'); + $ext_html = << + $ext_media_html
+ $extDisplayURL
+ $extTitle
+ $extDesc +
+EXTERNAL; + $this->item['content'] .= $ext_html; + } + $this->item['content'] = htmlspecialchars_decode($this->item['content'], ENT_QUOTES); // Add current Item to Items array