Merge pull request #1037 from MrPetovan/bug/9447-restore-twitter-link-preview

[twitter] Add new parse_link hook function
This commit is contained in:
Michael Vogel 2020-10-23 08:22:43 +02:00 committed by GitHub
commit 1c7b312442
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 107 additions and 33 deletions

View file

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -17,31 +17,27 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
#: twitter.php:195 #: twitter.php:189
msgid "Post to Twitter" msgid "Post to Twitter"
msgstr "" msgstr ""
#: twitter.php:236 #: twitter.php:234
msgid "" msgid ""
"You submitted an empty PIN, please Sign In with Twitter again to get a new " "You submitted an empty PIN, please Sign In with Twitter again to get a new "
"one." "one."
msgstr "" msgstr ""
#: twitter.php:263 #: twitter.php:291 twitter.php:295
msgid "Twitter settings updated."
msgstr ""
#: twitter.php:293 twitter.php:297
msgid "Twitter Import/Export/Mirror" msgid "Twitter Import/Export/Mirror"
msgstr "" msgstr ""
#: twitter.php:304 #: twitter.php:302
msgid "" msgid ""
"No consumer key pair for Twitter found. Please contact your site " "No consumer key pair for Twitter found. Please contact your site "
"administrator." "administrator."
msgstr "" msgstr ""
#: twitter.php:316 #: twitter.php:314
msgid "" msgid ""
"At this Friendica instance the Twitter addon was enabled but you have not " "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 " "yet connected your account to your Twitter account. To do so click the "
@ -50,38 +46,42 @@ msgid ""
"be posted to Twitter." "be posted to Twitter."
msgstr "" msgstr ""
#: twitter.php:317 #: twitter.php:315
msgid "Log in with Twitter" msgid "Log in with Twitter"
msgstr "" msgstr ""
#: twitter.php:319 #: twitter.php:317
msgid "Copy the PIN from Twitter here" msgid "Copy the PIN from Twitter here"
msgstr "" msgstr ""
#: twitter.php:324 twitter.php:366 twitter.php:636 #: twitter.php:322 twitter.php:377 twitter.php:757
msgid "Save Settings" msgid "Save Settings"
msgstr "" msgstr ""
#: twitter.php:336 #: twitter.php:324 twitter.php:379
msgid "An error occured: "
msgstr ""
#: twitter.php:341
msgid "Currently connected to: " msgid "Currently connected to: "
msgstr "" msgstr ""
#: twitter.php:337 #: twitter.php:342 twitter.php:352
msgid "Disconnect" msgid "Disconnect"
msgstr "" msgstr ""
#: twitter.php:347 #: twitter.php:359
msgid "Allow posting to Twitter" msgid "Allow posting to Twitter"
msgstr "" msgstr ""
#: twitter.php:347 #: twitter.php:359
msgid "" msgid ""
"If enabled all your <strong>public</strong> postings can be posted to the " "If enabled all your <strong>public</strong> postings can be posted to the "
"associated Twitter account. You can choose to do so by default (here) or for " "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." "every posting separately in the posting options when writing the entry."
msgstr "" msgstr ""
#: twitter.php:350 #: twitter.php:362
msgid "" msgid ""
"<strong>Note</strong>: Due to your privacy settings (<em>Hide your profile " "<strong>Note</strong>: Due to your privacy settings (<em>Hide your profile "
"details from unknown viewers?</em>) the link potentially included in public " "details from unknown viewers?</em>) the link potentially included in public "
@ -89,23 +89,23 @@ msgid ""
"the visitor that the access to your profile has been restricted." "the visitor that the access to your profile has been restricted."
msgstr "" msgstr ""
#: twitter.php:353 #: twitter.php:365
msgid "Send public postings to Twitter by default" msgid "Send public postings to Twitter by default"
msgstr "" msgstr ""
#: twitter.php:356 #: twitter.php:368
msgid "Mirror all posts from twitter that are no replies" msgid "Mirror all posts from twitter that are no replies"
msgstr "" msgstr ""
#: twitter.php:359 #: twitter.php:371
msgid "Import the remote timeline" msgid "Import the remote timeline"
msgstr "" msgstr ""
#: twitter.php:362 #: twitter.php:374
msgid "Automatically create contacts" msgid "Automatically create contacts"
msgstr "" msgstr ""
#: twitter.php:362 #: twitter.php:374
msgid "" msgid ""
"This will automatically create a contact in Friendica as soon as you receive " "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 " "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." "recreate this contact when they post again."
msgstr "" msgstr ""
#: twitter.php:614 #: twitter.php:759
msgid "Twitter post failed. Queued for retry."
msgstr ""
#: twitter.php:628
msgid "Settings updated."
msgstr ""
#: twitter.php:638
msgid "Consumer key" msgid "Consumer key"
msgstr "" msgstr ""
#: twitter.php:639 #: twitter.php:760
msgid "Consumer secret" msgid "Consumer secret"
msgstr "" msgstr ""
#: twitter.php:945
#, php-format
msgid "%s on Twitter"
msgstr ""

View file

@ -111,6 +111,7 @@ function twitter_install()
Hook::register('prepare_body' , __FILE__, 'twitter_prepare_body'); Hook::register('prepare_body' , __FILE__, 'twitter_prepare_body');
Hook::register('check_item_notification', __FILE__, 'twitter_check_item_notification'); Hook::register('check_item_notification', __FILE__, 'twitter_check_item_notification');
Hook::register('probe_detect' , __FILE__, 'twitter_probe_detect'); Hook::register('probe_detect' , __FILE__, 'twitter_probe_detect');
Hook::register('parse_link' , __FILE__, 'twitter_parse_link');
Logger::info("installed twitter"); 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 * @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 // This is a pure media post, first search for all media urls
$media = []; $media = [];
foreach ($post->extended_entities->media AS $medium) { foreach ($post->extended_entities->media AS $medium) {