Merge pull request #1080 from MrPetovan/task/babel-twitter-url

[twitter] Extract statuses/show in separate function
pull/1083/head
Michael Vogel 2021-02-15 07:01:14 +01:00 committed by GitHub
commit 6b558ca236
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 16 deletions

View File

@ -910,6 +910,24 @@ function twitter_prepare_body(App $a, array &$b)
} }
} }
function twitter_statuses_show(string $id, TwitterOAuth $twitterOAuth = null)
{
if ($twitterOAuth === null) {
$ckey = DI::config()->get('twitter', 'consumerkey');
$csecret = DI::config()->get('twitter', 'consumersecret');
if (empty($ckey) || empty($csecret)) {
return new stdClass();
}
$twitterOAuth = new TwitterOAuth($ckey, $csecret);
}
$parameters = ['trim_user' => false, 'tweet_mode' => 'extended', 'id' => $id, 'include_ext_alt_text' => true];
return $twitterOAuth->get('statuses/show', $parameters);
}
/** /**
* Parse Twitter status URLs since Twitter removed OEmbed * Parse Twitter status URLs since Twitter removed OEmbed
* *
@ -929,18 +947,7 @@ function twitter_parse_link(App $a, array &$b)
return; return;
} }
$ckey = DI::config()->get('twitter', 'consumerkey'); $status = twitter_statuses_show($matches[1]);
$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)) { if (empty($status->id)) {
return; return;
@ -1779,17 +1786,15 @@ function twitter_fetchparentposts(App $a, $uid, $post, TwitterOAuth $connection,
$posts = []; $posts = [];
while (!empty($post->in_reply_to_status_id_str)) { while (!empty($post->in_reply_to_status_id_str)) {
$parameters = ["trim_user" => false, "tweet_mode" => "extended", "id" => $post->in_reply_to_status_id_str, "include_ext_alt_text" => true];
try { try {
$post = $connection->get('statuses/show', $parameters); $post = twitter_statuses_show($post->in_reply_to_status_id_str, $connection);
} catch (TwitterOAuthException $e) { } catch (TwitterOAuthException $e) {
Logger::warning('Error fetching parent post', ['uid' => $uid, 'post' => $post->id_str, 'message' => $e->getMessage()]); Logger::warning('Error fetching parent post', ['uid' => $uid, 'post' => $post->id_str, 'message' => $e->getMessage()]);
break; break;
} }
if (empty($post)) { if (empty($post)) {
Logger::log("twitter_fetchparentposts: Can't fetch post " . $parameters['id'], Logger::DEBUG); Logger::log("twitter_fetchparentposts: Can't fetch post " . $post->in_reply_to_status_id_str, Logger::DEBUG);
break; break;
} }