Merge pull request 'Pnut: Client Id/Secret can be set by admins' (#1473) from heluecht/friendica-addons:pnut into 2024.03-rc

Reviewed-on: #1473
This commit is contained in:
Hypolite Petovan 2024-03-03 18:29:53 +01:00
commit c49b61be8b
5 changed files with 147 additions and 31 deletions

View file

@ -2,4 +2,9 @@
With this addon to friendica you can give your users the possibility to post their *public* messages to pnut.io.
No setup is needed for the admins to make it work for their users.
No setup is needed for the admins to make it work for their users, however it is possible for the admin to create a client, so that the users don't have to.
To do so, go to https://pnut.io/dev and scroll down to "Create New Client".
Enter a name of your choice and enter your Friendica host name as the website.
Use https://(yourhost.name)/pnut/connect as a redirect url, replace "(yourhost.name)" with the host name of your system.
Limit the scope to "basic,files,follow,polls,presence,stream,update_profile,write_post"

74
pnut/lang/C/messages.po Normal file
View file

@ -0,0 +1,74 @@
# ADDON pnut
# Copyright (C)
# This file is distributed under the same license as the Friendica pnut addon package.
#
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-03-03 11:53+0000\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"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: pnut.php:42
msgid "Permission denied."
msgstr ""
#: pnut.php:79
msgid "You are now authenticated with pnut.io."
msgstr ""
#: pnut.php:82
msgid "Error fetching token. Please try again."
msgstr ""
#: pnut.php:85
msgid "return to the connector page"
msgstr ""
#: pnut.php:103
msgid "Save Settings"
msgstr ""
#: pnut.php:104 pnut.php:150
msgid "Client ID"
msgstr ""
#: pnut.php:105 pnut.php:151
msgid "Client Secret"
msgstr ""
#: pnut.php:139
msgid "Authenticate with pnut.io"
msgstr ""
#: pnut.php:143
msgid "Disconnect"
msgstr ""
#: pnut.php:148
msgid "Enable Pnut Post Addon"
msgstr ""
#: pnut.php:149
msgid "Post to Pnut by default"
msgstr ""
#: pnut.php:152
msgid "Access Token"
msgstr ""
#: pnut.php:161
msgid "Pnut Export"
msgstr ""
#: pnut.php:203
msgid "Post to Pnut"
msgstr ""

View file

@ -2,7 +2,7 @@
/**
* Name: Pnut Connector
* Description: Post to pnut.io
* Version: 0.1.1
* Version: 0.1.2
* Author: Morgan McMillian <https://social.clacks.network/profile/spacenerdmo>
* Status: In Development
*/
@ -18,12 +18,10 @@ use Friendica\Core\Logger;
use Friendica\Core\Renderer;
use Friendica\Core\System;
use Friendica\DI;
use Friendica\Model\Item;
use Friendica\Model\Photo;
use Friendica\Object\Image;
use Friendica\Network\HTTPClient\Client\HttpClientAccept;
use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Util\DateTimeFormat;
use phpnut\phpnutException;
const PNUT_LIMIT = 256;
function pnut_install()
{
@ -63,19 +61,25 @@ function pnut_content()
function pnut_connect()
{
$client_id = DI::config()->get('pnut', 'client_id');
$client_secret = DI::config()->get('pnut', 'client_secret');
if (empty($client_id) || empty($client_secret)) {
$client_id = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'client_id');
$client_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'client_secret');
}
$callback_url = DI::baseUrl() . '/pnut/connect';
$nut = new phpnut\phpnut($client_id, $client_secret);
try {
$token = $nut->getAccessToken($callback_url);
Logger::debug('TOKEN', [$token]);
Logger::debug('Got Token', [$token]);
$o = DI::l10n()->t('You are now authenticated with pnut.io.');
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pnut', 'access_token', $token);
} catch (phpnutException $e) {
$o = DI::l10n()->t('Error fetching token. Please try again.');
$o = DI::l10n()->t('Error fetching token. Please try again.', ['code' => $e->getCode(), 'message' => $e->getMessage()]);
}
$o .= '<br /><a href="' . DI::baseUrl() . '/settings/connectors">' . DI::l10n()->t("return to the connector page").'</a>';
@ -88,6 +92,26 @@ function pnut_load_config(ConfigFileManager $loader)
DI::app()->getConfigCache()->load($loader->loadAddonConfig('pnut'), \Friendica\Core\Config\ValueObject\Cache::SOURCE_STATIC);
}
function pnut_addon_admin(string &$o)
{
$client_id = DI::config()->get('pnut', 'client_id');
$client_secret = DI::config()->get('pnut', 'client_secret');
$t = Renderer::getMarkupTemplate('admin.tpl', 'addon/pnut/');
$o = Renderer::replaceMacros($t, [
'$submit' => DI::l10n()->t('Save Settings'),
'$client_id' => ['pnut_client_id', DI::l10n()->t('Client ID'), $client_id],
'$client_secret' => ['pnut_client_secret', DI::l10n()->t('Client Secret'), $client_secret],
]);
}
function pnut_addon_admin_post()
{
DI::config()->set('pnut', 'client_id', $_POST['pnut_client_id']);
DI::config()->set('pnut', 'client_secret', $_POST['pnut_client_secret']);
}
function pnut_settings(array &$data)
{
if (!DI::userSession()->getLocalUserId()) {
@ -99,12 +123,15 @@ function pnut_settings(array &$data)
$enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'post') ?? false;
$def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'post_by_default') ?? false;
$client_id = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'client_id');
$client_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'client_secret');
$client_id = DI::config()->get('pnut', 'client_id');
$client_secret = DI::config()->get('pnut', 'client_secret');
$token = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'access_token');
Logger::debug('CLIENT_ID', [$client_id]);
Logger::debug('CLIENT_SECRET', [$client_secret]);
$user_client = empty($client_id) || empty($client_secret);
if ($user_client) {
$client_id = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'client_id');
$client_secret = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'pnut', 'client_secret');
}
if (!empty($client_id) && !empty($client_secret) && empty($token)) {
$nut = new phpnut\phpnut($client_id, $client_secret);
@ -126,11 +153,12 @@ function pnut_settings(array &$data)
'$authorize_url' => $authorize_url ?? '',
'$authorize_text' => $authorize_text ?? '',
'$disconn_btn' => $disconn_btn ?? '',
'user_client' => $user_client,
]);
$data = [
'connector' => 'pnut',
'title' => DI::l10n()->t('Pnut Import/Export'),
'title' => DI::l10n()->t('Pnut Export'),
'image' => 'addon/pnut/pnut.svg',
'enabled' => $enabled,
'html' => $html,
@ -152,10 +180,14 @@ function pnut_settings_post(array &$b)
} else {
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pnut', 'post', intval($_POST['pnut']));
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pnut', 'post_by_default', intval($_POST['pnut_bydefault']));
if (!empty($_POST['pnut_client_id'])) {
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pnut', 'client_id', $_POST['pnut_client_id']);
}
if (!empty($_POST['pnut_client_secret'])) {
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'pnut', 'client_secret', $_POST['pnut_client_secret']);
}
}
}
function pnut_jot_nets(array &$jotnets_fields)
{
@ -243,7 +275,7 @@ function pnut_post_hook(array &$b)
$token = DI::pConfig()->get($b['uid'], 'pnut', 'access_token');
$nut = new phpnut\phpnut($token);
$msgarr = Plaintext::getPost($b, 256, true, BBCode::EXTERNAL);
$msgarr = Plaintext::getPost($b, PNUT_LIMIT, true, BBCode::EXTERNAL);
$text = $msgarr['text'];
$raw = [];
@ -275,7 +307,7 @@ function pnut_post_hook(array &$b)
$picturedata = Photo::getImageForPhoto($photo);
Logger::debug('PNUT photo', $photo);
$picurefile = System::getTempPath() . DIRECTORY_SEPARATOR . $photo['filename'];
$picurefile = tempnam(System::getTempPath(), 'pnut');
file_put_contents($picurefile, $picturedata);
Logger::debug('PNUT got file?', ['filename' => $picurefile]);
$imagefile = $nut->createFile($picurefile, $fileraw);

3
pnut/templates/admin.tpl Normal file
View file

@ -0,0 +1,3 @@
{{include file="field_input.tpl" field=$client_id}}
{{include file="field_input.tpl" field=$client_secret}}
<div class="submit"><input type="submit" name="page_site" value="{{$submit}}" /></div>

View file

@ -1,9 +1,11 @@
<p>{{$status}}</p>
{{include file="field_checkbox.tpl" field=$enable}}
{{include file="field_checkbox.tpl" field=$bydefault}}
{{if $user_client}}
{{include file="field_input.tpl" field=$client_id}}
{{include file="field_input.tpl" field=$client_secret}}
{{include file="field_input.tpl" field=$access_token}}
{{/if}}
{{if $authorize_url}}
<a href="{{$authorize_url}}">{{$authorize_text}}</a>
{{/if}}