From 3d6748eaf7e6a42e2e993a19bd8c8f37f7fc9967 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 15 Aug 2023 20:43:32 +0000 Subject: [PATCH 1/3] Simplified status display --- twitter/twitter.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/twitter/twitter.php b/twitter/twitter.php index 7b2e424c..c033fa93 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -117,12 +117,10 @@ function twitter_settings(array &$data) $access_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'access_secret'); $last_status = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'last_status'); - if (empty($last_status)) { - $status = DI::l10n()->t('No status.'); - } elseif (!empty($last_status['code'])) { + if (!empty($last_status['code'])) { $status = print_r($last_status, true); } else { - $status = print_r($last_status, true); + $status = DI::l10n()->t('No status.'); } $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/twitter/'); From a3e24a55ec5ac3b25e3a442026e78ef4dacca452 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 16 Aug 2023 03:28:54 +0000 Subject: [PATCH 2/3] Last status is split for better readability --- twitter/lang/C/messages.po | 34 ++++++++++++++++-------- twitter/templates/connector_settings.tpl | 1 + twitter/twitter.php | 10 ++++--- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/twitter/lang/C/messages.po b/twitter/lang/C/messages.po index 4bcc4efa..366d6c8f 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: 2023-08-09 19:59+0000\n" +"POT-Creation-Date: 2023-08-16 03:27+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -17,42 +17,46 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -#: twitter.php:80 +#: twitter.php:84 msgid "Post to Twitter" msgstr "" -#: twitter.php:117 +#: twitter.php:123 +msgid "No status." +msgstr "" + +#: twitter.php:129 msgid "Allow posting to Twitter" msgstr "" -#: twitter.php:117 +#: twitter.php:129 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:118 +#: twitter.php:130 msgid "Send public postings to Twitter by default" msgstr "" -#: twitter.php:119 +#: twitter.php:131 msgid "API Key" msgstr "" -#: twitter.php:120 +#: twitter.php:132 msgid "API Secret" msgstr "" -#: twitter.php:121 +#: twitter.php:133 msgid "Access Token" msgstr "" -#: twitter.php:122 +#: twitter.php:134 msgid "Access Secret" msgstr "" -#: twitter.php:123 +#: twitter.php:135 msgid "" "Each user needs to register their own app to be able to post to Twitter. " "Please visit https://developer.twitter.com/en/portal/projects-and-apps to " @@ -61,6 +65,14 @@ msgid "" "in the app settings." msgstr "" -#: twitter.php:128 +#: twitter.php:136 +msgid "Last Status Summary" +msgstr "" + +#: twitter.php:137 +msgid "Last Status Content" +msgstr "" + +#: twitter.php:142 msgid "Twitter Export" msgstr "" diff --git a/twitter/templates/connector_settings.tpl b/twitter/templates/connector_settings.tpl index 534f39dc..251093de 100644 --- a/twitter/templates/connector_settings.tpl +++ b/twitter/templates/connector_settings.tpl @@ -5,4 +5,5 @@ {{include file="field_input.tpl" field=$api_secret}} {{include file="field_input.tpl" field=$access_token}} {{include file="field_input.tpl" field=$access_secret}} +{{include file="field_input.tpl" field=$status_title}} {{include file="field_textarea.tpl" field=$status}} \ No newline at end of file diff --git a/twitter/twitter.php b/twitter/twitter.php index c033fa93..d52da4f7 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -117,11 +117,12 @@ function twitter_settings(array &$data) $access_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'access_secret'); $last_status = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'last_status'); - if (!empty($last_status['code'])) { - $status = print_r($last_status, true); + if (!empty($last_status['code']) && !empty($last_status['reason'])) { + $status_title = sprintf('%d - %s', $last_status['code'], $last_status['reason']); } else { - $status = DI::l10n()->t('No status.'); + $status_title = DI::l10n()->t('No status.'); } + $status_content = $last_status['content'] ?? ''; $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/twitter/'); $html = Renderer::replaceMacros($t, [ @@ -132,7 +133,8 @@ function twitter_settings(array &$data) '$access_token' => ['twitter-access-token', DI::l10n()->t('Access Token'), $access_token], '$access_secret' => ['twitter-access-secret', DI::l10n()->t('Access Secret'), $access_secret], '$help' => DI::l10n()->t('Each user needs to register their own app to be able to post to Twitter. Please visit https://developer.twitter.com/en/portal/projects-and-apps to register a project. Inside the project you then have to register an app. You will find the needed data for the connector on the page "Keys and token" in the app settings.'), - '$status' => ['twitter-status', DI::l10n()->t('Last Status'), $status, '', '', 'readonly'], + '$status_title' => ['twitter-status-title', DI::l10n()->t('Last Status Summary'), $status_title, '', '', 'readonly'], + '$status' => ['twitter-status', DI::l10n()->t('Last Status Content'), $status_content, '', '', 'readonly'], ]); $data = [ From 339c88353b4d152218e56f91bf9879658980aa93 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 16 Aug 2023 06:17:18 +0000 Subject: [PATCH 3/3] Test the connection when API credential changed --- twitter/twitter.php | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/twitter/twitter.php b/twitter/twitter.php index d52da4f7..7b46508e 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -94,12 +94,23 @@ function twitter_settings_post() return; } + $api_key = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'api_key'); + $api_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'api_secret'); + $access_token = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'access_token'); + $access_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'access_secret'); + DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'post', (bool)$_POST['twitter-enable']); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'post_by_default', (bool)$_POST['twitter-default']); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'api_key', $_POST['twitter-api-key']); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'api_secret', $_POST['twitter-api-secret']); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'access_token', $_POST['twitter-access-token']); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'access_secret', $_POST['twitter-access-secret']); + + if (empty(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'last_status')) || + ($api_key != $_POST['twitter-api-key']) || ($api_secret != $_POST['twitter-api-secret']) || + ($access_token != $_POST['twitter-access-token']) || ($access_secret != $_POST['twitter-access-secret'])) { + twitter_test_connection(DI::userSession()->getLocalUserId()); + } } function twitter_settings(array &$data) @@ -362,3 +373,40 @@ function twitter_post(int $uid, string $url, string $type, array $data): stdClas Logger::debug('Success', ['content' => $content]); return $content; } + +function twitter_test_connection(int $uid) +{ + $stack = HandlerStack::create(); + + $middleware = new Oauth1([ + 'consumer_key' => DI::pConfig()->get($uid, 'twitter', 'api_key'), + 'consumer_secret' => DI::pConfig()->get($uid, 'twitter', 'api_secret'), + 'token' => DI::pConfig()->get($uid, 'twitter', 'access_token'), + 'token_secret' => DI::pConfig()->get($uid, 'twitter', 'access_secret'), + ]); + + $stack->push($middleware); + + $client = new Client([ + 'handler' => $stack + ]); + + try { + $response = $client->get('https://api.twitter.com/2/users/me', ['auth' => 'oauth']); + $status = [ + 'code' => $response->getStatusCode(), + 'reason' => $response->getReasonPhrase(), + 'content' => $response->getBody()->getContents() + ]; + DI::pConfig()->set(1, 'twitter', 'last_status', $status); + Logger::info('Test successful', ['uid' => $uid]); + } catch (RequestException $exception) { + $status = [ + 'code' => $exception->getCode(), + 'reason' => $exception->getResponse()->getReasonPhrase(), + 'content' => $exception->getMessage() + ]; + DI::pConfig()->set(1, 'twitter', 'last_status', $status); + Logger::info('Test failed', ['uid' => $uid]); + } +} \ No newline at end of file