Merge pull request 'Simplified status display' (#1412) from heluecht/friendica-addons:resize into develop

Reviewed-on: friendica/friendica-addons#1412
This commit is contained in:
Hypolite Petovan 2023-08-16 12:27:28 +02:00
commit 886638f8a5
3 changed files with 78 additions and 17 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: 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" "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,42 +17,46 @@ 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:80 #: twitter.php:84
msgid "Post to Twitter" msgid "Post to Twitter"
msgstr "" msgstr ""
#: twitter.php:117 #: twitter.php:123
msgid "No status."
msgstr ""
#: twitter.php:129
msgid "Allow posting to Twitter" msgid "Allow posting to Twitter"
msgstr "" msgstr ""
#: twitter.php:117 #: twitter.php:129
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:118 #: twitter.php:130
msgid "Send public postings to Twitter by default" msgid "Send public postings to Twitter by default"
msgstr "" msgstr ""
#: twitter.php:119 #: twitter.php:131
msgid "API Key" msgid "API Key"
msgstr "" msgstr ""
#: twitter.php:120 #: twitter.php:132
msgid "API Secret" msgid "API Secret"
msgstr "" msgstr ""
#: twitter.php:121 #: twitter.php:133
msgid "Access Token" msgid "Access Token"
msgstr "" msgstr ""
#: twitter.php:122 #: twitter.php:134
msgid "Access Secret" msgid "Access Secret"
msgstr "" msgstr ""
#: twitter.php:123 #: twitter.php:135
msgid "" msgid ""
"Each user needs to register their own app to be able to post to Twitter. " "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 " "Please visit https://developer.twitter.com/en/portal/projects-and-apps to "
@ -61,6 +65,14 @@ msgid ""
"in the app settings." "in the app settings."
msgstr "" 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" msgid "Twitter Export"
msgstr "" msgstr ""

View file

@ -5,4 +5,5 @@
{{include file="field_input.tpl" field=$api_secret}} {{include file="field_input.tpl" field=$api_secret}}
{{include file="field_input.tpl" field=$access_token}} {{include file="field_input.tpl" field=$access_token}}
{{include file="field_input.tpl" field=$access_secret}} {{include file="field_input.tpl" field=$access_secret}}
{{include file="field_input.tpl" field=$status_title}}
{{include file="field_textarea.tpl" field=$status}} {{include file="field_textarea.tpl" field=$status}}

View file

@ -94,12 +94,23 @@ function twitter_settings_post()
return; 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', (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', '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_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', '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_token', $_POST['twitter-access-token']);
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'twitter', 'access_secret', $_POST['twitter-access-secret']); 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) function twitter_settings(array &$data)
@ -117,13 +128,12 @@ function twitter_settings(array &$data)
$access_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'access_secret'); $access_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'access_secret');
$last_status = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'last_status'); $last_status = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'twitter', 'last_status');
if (empty($last_status)) { if (!empty($last_status['code']) && !empty($last_status['reason'])) {
$status = DI::l10n()->t('No status.'); $status_title = sprintf('%d - %s', $last_status['code'], $last_status['reason']);
} elseif (!empty($last_status['code'])) {
$status = print_r($last_status, true);
} else { } else {
$status = print_r($last_status, true); $status_title = DI::l10n()->t('No status.');
} }
$status_content = $last_status['content'] ?? '';
$t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/twitter/'); $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/twitter/');
$html = Renderer::replaceMacros($t, [ $html = Renderer::replaceMacros($t, [
@ -134,7 +144,8 @@ function twitter_settings(array &$data)
'$access_token' => ['twitter-access-token', DI::l10n()->t('Access Token'), $access_token], '$access_token' => ['twitter-access-token', DI::l10n()->t('Access Token'), $access_token],
'$access_secret' => ['twitter-access-secret', DI::l10n()->t('Access Secret'), $access_secret], '$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.'), '$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 = [ $data = [
@ -362,3 +373,40 @@ function twitter_post(int $uid, string $url, string $type, array $data): stdClas
Logger::debug('Success', ['content' => $content]); Logger::debug('Success', ['content' => $content]);
return $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]);
}
}