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 ""
"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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\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 <strong>public</strong> 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 ""
"<strong>Note</strong>: Due to your privacy settings (<em>Hide your profile "
"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."
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 ""

View file

@ -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) {