Tumblr: wrong "page" values are corrected automatically #1369

Merged
MrPetovan merged 2 commits from heluecht/friendica-addons:tumblr-page into 2023.03-rc 2023-04-10 13:37:35 +02:00

View file

@ -180,10 +180,7 @@ function tumblr_callback()
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'tumblr', 'oauth_token', $access_token['oauth_token']); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'tumblr', 'oauth_token', $access_token['oauth_token']);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'tumblr', 'oauth_token_secret', $access_token['oauth_token_secret']); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'tumblr', 'oauth_token_secret', $access_token['oauth_token_secret']);
$o = DI::l10n()->t('You are now authenticated to tumblr.'); DI::baseUrl()->redirect('settings/connectors/tumblr');
$o .= '<br /><a href="' . DI::baseUrl() . '/settings/connectors">' . DI::l10n()->t("return to the connector page") . '</a>';
return $o;
} }
function tumblr_jot_nets(array &$jotnets_fields) function tumblr_jot_nets(array &$jotnets_fields)
@ -213,30 +210,9 @@ function tumblr_settings(array &$data)
$enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'post', false); $enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'post', false);
$def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'post_by_default', false); $def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'post_by_default', false);
$oauth_token = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'oauth_token'); $blogs = tumblr_get_blogs(DI::userSession()->getLocalUserId());
$oauth_token_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'oauth_token_secret'); if (!empty($blogs)) {
$consumer_key = DI::config()->get('tumblr', 'consumer_key'); $page = tumblr_get_page(DI::userSession()->getLocalUserId(), $blogs);
$consumer_secret = DI::config()->get('tumblr', 'consumer_secret');
if ($consumer_key && $consumer_secret && $oauth_token && $oauth_token_secret) {
$page = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'tumblr', 'page');
$blogs = [];
$connection = tumblr_client($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
if ($connection) {
$userinfo = tumblr_get($connection, 'user/info');
if (!empty($userinfo['success'])) {
foreach ($userinfo['data']->response->user->blogs as $blog) {
$blogs[$blog->uuid] = $blog->name;
}
}
if (empty($page) && !empty($blogs)) {
$page = reset($blogs);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'tumblr', 'page', $page);
}
}
$page_select = ['tumblr_page', DI::l10n()->t('Post to page:'), $page, '', $blogs]; $page_select = ['tumblr_page', DI::l10n()->t('Post to page:'), $page, '', $blogs];
} }
@ -417,7 +393,7 @@ function tumblr_send(array &$b)
'<p>' . $params['caption'] . '</p>'; '<p>' . $params['caption'] . '</p>';
} }
$page = DI::pConfig()->get($b['uid'], 'tumblr', 'page'); $page = tumblr_get_page($b['uid']);
$result = tumblr_post($connection, 'blog/' . $page . '/post', $params); $result = tumblr_post($connection, 'blog/' . $page . '/post', $params);
@ -432,11 +408,11 @@ function tumblr_send(array &$b)
function tumblr_send_npf(array $post): bool function tumblr_send_npf(array $post): bool
{ {
$page = DI::pConfig()->get($post['uid'], 'tumblr', 'page'); $page = tumblr_get_page($post['uid']);
$connection = tumblr_connection($post['uid']); $connection = tumblr_connection($post['uid']);
if (empty($connection)) { if (empty($page)) {
Logger::notice('Missing data, post will not be send to Tumblr.', ['uid' => $post['uid'], 'page' => $page, 'id' => $post['id']]); Logger::notice('Missing page, post will not be send to Tumblr.', ['uid' => $post['uid'], 'page' => $page, 'id' => $post['id']]);
// "true" is returned, since the legacy function will fail as well. // "true" is returned, since the legacy function will fail as well.
return true; return true;
} }
@ -471,13 +447,11 @@ function tumblr_connection(int $uid): GuzzleHttp\Client|null
$oauth_token = DI::pConfig()->get($uid, 'tumblr', 'oauth_token'); $oauth_token = DI::pConfig()->get($uid, 'tumblr', 'oauth_token');
$oauth_token_secret = DI::pConfig()->get($uid, 'tumblr', 'oauth_token_secret'); $oauth_token_secret = DI::pConfig()->get($uid, 'tumblr', 'oauth_token_secret');
$page = DI::pConfig()->get($uid, 'tumblr', 'page');
$consumer_key = DI::config()->get('tumblr', 'consumer_key'); $consumer_key = DI::config()->get('tumblr', 'consumer_key');
$consumer_secret = DI::config()->get('tumblr', 'consumer_secret'); $consumer_secret = DI::config()->get('tumblr', 'consumer_secret');
if (!$consumer_key || !$consumer_secret || !$oauth_token || !$oauth_token_secret || !$page) { if (!$consumer_key || !$consumer_secret || !$oauth_token || !$oauth_token_secret) {
Logger::notice('Missing data, connection is not established', ['uid' => $uid, 'page' => $page]); Logger::notice('Missing data, connection is not established', ['uid' => $uid]);
return null; return null;
} }
return tumblr_client($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret); return tumblr_client($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
@ -501,6 +475,46 @@ function tumblr_client(string $consumer_key, string $consumer_secret, string $oa
]); ]);
} }
function tumblr_get_page(int $uid, array $blogs = [])
{
$page = DI::pConfig()->get($uid, 'tumblr', 'page');
if (!empty($page) && (strpos($page, '/') === false)) {
return $page;
}
if (empty($blogs)) {
$blogs = tumblr_get_blogs($uid);
}
if (!empty($blogs)) {
$page = array_key_first($blogs);
DI::pConfig()->set($uid, 'tumblr', 'page', $page);
return $page;
}
return '';
}
function tumblr_get_blogs(int $uid)
{
$connection = tumblr_connection($uid);
if (empty($connection)) {
return [];
}
$userinfo = tumblr_get($connection, 'user/info');
if (empty($userinfo['success'])) {
return [];
}
$blogs = [];
foreach ($userinfo['data']->response->user->blogs as $blog) {
$blogs[$blog->uuid] = $blog->name;
}
return $blogs;
}
function tumblr_get($connection, string $url) function tumblr_get($connection, string $url)
{ {
try { try {
@ -510,8 +524,8 @@ function tumblr_get($connection, string $url)
$data = json_decode($res->getBody()->getContents()); $data = json_decode($res->getBody()->getContents());
} catch (RequestException $exception) { } catch (RequestException $exception) {
$success = false; $success = false;
$data = [];
Logger::notice('Request failed', ['code' => $exception->getCode(), 'message' => $exception->getMessage()]); Logger::notice('Request failed', ['code' => $exception->getCode(), 'message' => $exception->getMessage()]);
$data = json_decode($exception->getResponse()->getBody()->getContents());
} }
return ['success' => $success, 'data' => $data]; return ['success' => $success, 'data' => $data];
} }
@ -525,8 +539,8 @@ function tumblr_post($connection, string $url, array $parameter)
$data = json_decode($res->getBody()->getContents()); $data = json_decode($res->getBody()->getContents());
} catch (RequestException $exception) { } catch (RequestException $exception) {
$success = false; $success = false;
Logger::notice('Post failed', ['code' => $exception->getCode(), 'message' => $exception->getMessage()]);
$data = json_decode($exception->getResponse()->getBody()->getContents()); $data = json_decode($exception->getResponse()->getBody()->getContents());
Logger::notice('Post failed', ['code' => $exception->getCode(), 'message' => $exception->getMessage()]);
} }
return ['success' => $success, 'data' => $data]; return ['success' => $success, 'data' => $data];
} }