From 74cfe72750ad73532c5d951b59bf286253186450 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 23 Oct 2020 02:01:42 -0400 Subject: [PATCH 1/2] [twitter] Add new parse_link hook function - This will populate the link preview now that OEmbed isn't supported anymore --- twitter/twitter.php | 77 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/twitter/twitter.php b/twitter/twitter.php index da1ea4c69..99fc91b6d 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -111,6 +111,7 @@ function twitter_install() Hook::register('prepare_body' , __FILE__, 'twitter_prepare_body'); Hook::register('check_item_notification', __FILE__, 'twitter_check_item_notification'); Hook::register('probe_detect' , __FILE__, 'twitter_probe_detect'); + Hook::register('parse_link' , __FILE__, 'twitter_parse_link'); Logger::info("installed twitter"); } @@ -894,6 +895,80 @@ function twitter_prepare_body(App $a, array &$b) } } +/** + * Parse Twitter status URLs since Twitter removed OEmbed + * + * @param App $a + * @param array $b Expected format: + * [ + * 'url' => [URL to parse], + * 'format' => 'json'|'', + * 'text' => Output parameter + * ] + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ +function twitter_parse_link(App $a, array &$b) +{ + // Only handle Twitter status URLs + if (!preg_match('#^https?://(?:mobile\.|www\.)?twitter.com/[^/]+/status/(\d+).*#', $b['url'], $matches)) { + return; + } + + $ckey = DI::config()->get('twitter', 'consumerkey'); + $csecret = DI::config()->get('twitter', 'consumersecret'); + + if (empty($ckey) || empty($csecret)) { + return; + } + + $connection = new TwitterOAuth($ckey, $csecret); + + $parameters = ['trim_user' => false, 'tweet_mode' => 'extended', 'id' => $matches[1], 'include_ext_alt_text' => true]; + + $status = $connection->get('statuses/show', $parameters); + + if (empty($status->id)) { + return; + } + + $item = twitter_createpost($a, 0, $status, [], true, false, true); + + if ($b['format'] == 'json') { + if (!empty($status->extended_entities->media[0]->media_url_https)) { + $images = [['src' => $status->extended_entities->media[0]->media_url_https]]; + } + + $b['text'] = [ + 'data' => [ + 'type' => 'link', + 'url' => $item['plink'], + 'title' => DI::l10n()->t('%s on Twitter', $status->user->name), + 'text' => BBCode::toPlaintext($item['body'], false), + 'images' => $images ?? [], + ], + 'contentType' => 'attachment', + 'success' => true, + ]; + } else { + $b['text'] = BBCode::getShareOpeningTag( + $item['author-name'], + $item['author-link'], + $item['author-avatar'], + $item['plink'], + $item['created'] + ); + $b['text'] .= $item['body'] . '[/share]'; + } +} + + +/********************* + * + * General functions + * + *********************/ + + /** * @brief Build the item array for the mirrored post * @@ -1410,6 +1485,8 @@ function twitter_media_entities($post, array &$postarray) } } + + // This is a pure media post, first search for all media urls $media = []; foreach ($post->extended_entities->media AS $medium) { From 5ddcc829511da2c443d66bd1930ba661d50575be Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 23 Oct 2020 02:02:07 -0400 Subject: [PATCH 2/2] [twitter] Update base messages.po after adding a translation string --- twitter/lang/C/messages.po | 63 ++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/twitter/lang/C/messages.po b/twitter/lang/C/messages.po index d76bb2b8e..ec200a31a 100644 --- a/twitter/lang/C/messages.po +++ b/twitter/lang/C/messages.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-06-02 10:25+0700\n" +"POT-Creation-Date: 2020-10-23 02:00-0400\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -17,31 +17,27 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -#: twitter.php:195 +#: twitter.php:189 msgid "Post to Twitter" msgstr "" -#: twitter.php:236 +#: twitter.php:234 msgid "" "You submitted an empty PIN, please Sign In with Twitter again to get a new " "one." msgstr "" -#: twitter.php:263 -msgid "Twitter settings updated." -msgstr "" - -#: twitter.php:293 twitter.php:297 +#: twitter.php:291 twitter.php:295 msgid "Twitter Import/Export/Mirror" msgstr "" -#: twitter.php:304 +#: twitter.php:302 msgid "" "No consumer key pair for Twitter found. Please contact your site " "administrator." msgstr "" -#: twitter.php:316 +#: twitter.php:314 msgid "" "At this Friendica instance the Twitter addon was enabled but you have not " "yet connected your account to your Twitter account. To do so click the " @@ -50,38 +46,42 @@ msgid "" "be posted to Twitter." msgstr "" -#: twitter.php:317 +#: twitter.php:315 msgid "Log in with Twitter" msgstr "" -#: twitter.php:319 +#: twitter.php:317 msgid "Copy the PIN from Twitter here" msgstr "" -#: twitter.php:324 twitter.php:366 twitter.php:636 +#: twitter.php:322 twitter.php:377 twitter.php:757 msgid "Save Settings" msgstr "" -#: twitter.php:336 +#: twitter.php:324 twitter.php:379 +msgid "An error occured: " +msgstr "" + +#: twitter.php:341 msgid "Currently connected to: " msgstr "" -#: twitter.php:337 +#: twitter.php:342 twitter.php:352 msgid "Disconnect" msgstr "" -#: twitter.php:347 +#: twitter.php:359 msgid "Allow posting to Twitter" msgstr "" -#: twitter.php:347 +#: twitter.php:359 msgid "" "If enabled all your public postings can be posted to the " "associated Twitter account. You can choose to do so by default (here) or for " "every posting separately in the posting options when writing the entry." msgstr "" -#: twitter.php:350 +#: twitter.php:362 msgid "" "Note: Due to your privacy settings (Hide your profile " "details from unknown viewers?) the link potentially included in public " @@ -89,23 +89,23 @@ msgid "" "the visitor that the access to your profile has been restricted." msgstr "" -#: twitter.php:353 +#: twitter.php:365 msgid "Send public postings to Twitter by default" msgstr "" -#: twitter.php:356 +#: twitter.php:368 msgid "Mirror all posts from twitter that are no replies" msgstr "" -#: twitter.php:359 +#: twitter.php:371 msgid "Import the remote timeline" msgstr "" -#: twitter.php:362 +#: twitter.php:374 msgid "Automatically create contacts" msgstr "" -#: twitter.php:362 +#: twitter.php:374 msgid "" "This will automatically create a contact in Friendica as soon as you receive " "a message from an existing contact via the Twitter network. If you do not " @@ -115,18 +115,15 @@ msgid "" "recreate this contact when they post again." msgstr "" -#: twitter.php:614 -msgid "Twitter post failed. Queued for retry." -msgstr "" - -#: twitter.php:628 -msgid "Settings updated." -msgstr "" - -#: twitter.php:638 +#: twitter.php:759 msgid "Consumer key" msgstr "" -#: twitter.php:639 +#: twitter.php:760 msgid "Consumer secret" msgstr "" + +#: twitter.php:945 +#, php-format +msgid "%s on Twitter" +msgstr ""