Updated URL Replace addon #1483

Merged
MrPetovan merged 18 commits from toddy/friendica-addons:2024.03-rc into 2024.03-rc 2024-03-19 18:00:46 +01:00
6 changed files with 137 additions and 54 deletions

View file

@ -5,7 +5,8 @@
* Version: 0.3 * Version: 0.3
* Author: Matthias Ebers <https://loma.ml/profile/feb> * Author: Matthias Ebers <https://loma.ml/profile/feb>
* Author: Michael Vogel <https://pirati.ca/profile/heluecht> * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
* * Status: Unsupported
* Note: Please use the URL Replace addon instead
*/ */
use Friendica\Core\Hook; use Friendica\Core\Hook;

View file

@ -4,6 +4,8 @@
* Description: Replaces links to twitter.com to a nitter server in all displays of postings on a node. * Description: Replaces links to twitter.com to a nitter server in all displays of postings on a node.
* Version: 2.0 * Version: 2.0
* Author: Tobias Diekershoff <tobias@social.diekershoff.de> * Author: Tobias Diekershoff <tobias@social.diekershoff.de>
* Status: Unsupported
* Note: Please use the URL Replace addon instead
* *
* Copyright (c) 2020 Tobias Diekershoff * Copyright (c) 2020 Tobias Diekershoff
* *

View file

@ -4,7 +4,8 @@ This addon will replace all occurrences of specified URLs with the address of
alternative servers in all displayed postings on a Friendica node. alternative servers in all displayed postings on a Friendica node.
You can use this to switch from Twitter (or X) to a nitter instance, from You can use this to switch from Twitter (or X) to a nitter instance, from
YouTube to an invidious instance, or from some news sites to 12ft.io. YouTube to an invidious instance, from Instagram to a proxigram instance,
or from some news sites to 12ft.io.
Note: If you are using the twitter connector on your server, the links to the Note: If you are using the twitter connector on your server, the links to the
contacts profile pages will not be replaced by this addon. Only links in the contacts profile pages will not be replaced by this addon. Only links in the

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: 2024-01-05 00:06+0100\n" "POT-Creation-Date: 2024-03-14 09:05+0100\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,34 +17,48 @@ 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"
#: url_replace.php:54 #: url_replace.php:68
msgid "Replace links to X."
msgstr ""
#: url_replace.php:73
msgid "Nitter server" msgid "Nitter server"
msgstr "" msgstr ""
#: url_replace.php:56 #: url_replace.php:75 url_replace.php:88 url_replace.php:101
msgid "Specify the URL with protocol. The default is https://nitter.net." #, php-format
msgid "Specify the URL with protocol. The default is %s."
msgstr "" msgstr ""
#: url_replace.php:62 #: url_replace.php:81
msgid "Replace links to YouTube."
msgstr ""
#: url_replace.php:86
msgid "Invidious server" msgid "Invidious server"
msgstr "" msgstr ""
#: url_replace.php:64 #: url_replace.php:94
msgid "Specify the URL with protocol. The default is https://yewtu.be." msgid "Replace links to Instagram."
msgstr "" msgstr ""
#: url_replace.php:70 #: url_replace.php:99
msgid "Proxigram server"
msgstr ""
#: url_replace.php:107
msgid "Sites which are accessed through 12ft.io" msgid "Sites which are accessed through 12ft.io"
msgstr "" msgstr ""
#: url_replace.php:72 #: url_replace.php:109
msgid "Specify the URLs with protocol, one per line." msgid "Specify the URLs with protocol, one per line."
msgstr "" msgstr ""
#: url_replace.php:76 #: url_replace.php:113
msgid "Save settings" msgid "Save settings"
msgstr "" msgstr ""
#: url_replace.php:125 #: url_replace.php:179
msgid "(URL replace addon enabled for X, YouTube and some news sites.)" msgid ""
"(URL replace addon enabled for X, YouTube, Instagram and some news sites.)"
msgstr "" msgstr ""

View file

@ -1,5 +1,9 @@
{{include file="field_checkbox.tpl" field=$nitter_server_enabled}}
{{include file="field_input.tpl" field=$nitter_server}} {{include file="field_input.tpl" field=$nitter_server}}
{{include file="field_checkbox.tpl" field=$invidious_server_enabled}}
{{include file="field_input.tpl" field=$invidious_server}} {{include file="field_input.tpl" field=$invidious_server}}
{{include file="field_checkbox.tpl" field=$proxigram_server_enabled}}
{{include file="field_input.tpl" field=$proxigram_server}}
{{include file="field_textarea.tpl" field=$twelvefeet_sites}} {{include file="field_textarea.tpl" field=$twelvefeet_sites}}
<div class="submit"><input type="submit" name="page_site" value="{{$submit}}" /></div> <div class="submit"><input type="submit" name="page_site" value="{{$submit}}" /></div>

View file

@ -2,7 +2,7 @@
/** /**
* Name: URL Replace * Name: URL Replace
* Description: Replaces occurrences of specified URLs with the address of alternative servers in all displays of postings on a node. * Description: Replaces occurrences of specified URLs with the address of alternative servers in all displays of postings on a node.
* Version: 1.0 * Version: 1.1
* Author: Dr. Tobias Quathamer <https://social.anoxinon.de/@toddy> * Author: Dr. Tobias Quathamer <https://social.anoxinon.de/@toddy>
* Maintainer: Dr. Tobias Quathamer <https://social.anoxinon.de/@toddy> * Maintainer: Dr. Tobias Quathamer <https://social.anoxinon.de/@toddy>
*/ */
@ -11,6 +11,10 @@ use Friendica\Core\Hook;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\DI; use Friendica\DI;
const URL_REPLACE_NITTER_DEFAULT = 'https://nitter.net';
const URL_REPLACE_INVIDIOUS_DEFAULT = 'https://invidio.us';
toddy marked this conversation as resolved Outdated

Out of interest: Why aren't we using https://invidio.us here? Wouldn't it be better to use the main instances as default values?

Out of interest: Why aren't we using https://invidio.us here? Wouldn't it be better to use the main instances as default values?

You cannot use it to watch something directly, they provide a hub to instances you can use to watch.

You cannot use it to watch something directly, they provide a hub to instances you can use to watch.

You will see this page where you have to select a server. But when you follow one of these links, your content will be loaded. So you will also be able to chose your individual server each time. Also: I just tested with one video. I had to try several servers, until I found one, that finally was able to display the video. So using https://invidio.us helps here.

You will see this page where you have to select a server. But when you follow one of these links, your content will be loaded. So you will also be able to chose your individual server each time. Also: I just tested with one video. I had to try several servers, until I found one, that finally was able to display the video. So using https://invidio.us helps here.

I agree with @heluecht , let’s use the official portal by default and let admins pick a specific instance later.

I agree with @heluecht , let’s use the official portal by default and let admins pick a specific instance later.
Outdated
Review

TBH, I'm not a big fan of that change. After all, it requires two clicks of the user instead of just one to watch the video. All other alternative frontends provided by this addon show the content directly after following the link.

Moreover, yewtu.be has been around for a long time already and seems to be rather reliable. So IMHO it makes for a good and sensible default server. And it has by far the coolest name. :-)

TBH, I'm not a big fan of that change. After all, it requires two clicks of the user instead of just one to watch the video. All other alternative frontends provided by this addon show the content directly after following the link. Moreover, yewtu.be has been around for a long time already and seems to be rather reliable. So IMHO it makes for a good and sensible default server. And it has by far the coolest name. :-)

Well, the first time I tried the service at yewtub.de, I received an error. I then had to try two other servers to finally find one, that was able to display it to me. So having the choice is a nice thing. Also this is only a default value, so people can enter their own preferred server afterwards - based on their own experience and their regional location.

Well, the first time I tried the service at `yewtub.de`, I received an error. I then had to try two other servers to finally find one, that was able to display it to me. So having the choice is a nice thing. Also this is only a default value, so people can enter their own preferred server afterwards - based on their own experience and their regional location.
Outdated
Review

If this is the last discussion point before being able to merge this PR, I'll give in. :-)

If this is the last discussion point before being able to merge this PR, I'll give in. :-)
const URL_REPLACE_PROXIGRAM_DEFAULT = 'https://proxigram.lunar.icu';
function url_replace_install() function url_replace_install()
{ {
Hook::register('prepare_body_final', 'addon/url_replace/url_replace.php', 'url_replace_render'); Hook::register('prepare_body_final', 'addon/url_replace/url_replace.php', 'url_replace_render');
@ -21,18 +25,37 @@ function url_replace_install()
*/ */
function url_replace_addon_admin_post() function url_replace_addon_admin_post()
{ {
DI::config()->set('url_replace', 'nitter_server', rtrim(trim($_POST['nitter_server']), '/')); DI::config()->set('url_replace', 'nitter_server_enabled', !empty($_POST['nitter_server_enabled']));
DI::config()->set('url_replace', 'invidious_server', rtrim(trim($_POST['invidious_server']), '/')); $nitter_server = rtrim(trim($_POST['nitter_server']), '/');
if ($nitter_server) {
DI::config()->set('url_replace', 'nitter_server', $nitter_server);
} else {
DI::config()->delete('url_replace', 'nitter_server');
}
DI::config()->set('url_replace', 'invidious_server_enabled', !empty($_POST['invidious_server_enabled']));
$invidious_server = rtrim(trim($_POST['invidious_server']), '/');
if ($invidious_server) {
DI::config()->set('url_replace', 'invidious_server', $invidious_server);
} else {
DI::config()->delete('url_replace', 'invidious_server');
}
DI::config()->set('url_replace', 'proxigram_server_enabled', !empty($_POST['proxigram_server_enabled']));
$proxigram_server = rtrim(trim($_POST['proxigram_server']), '/');
if ($proxigram_server) {
DI::config()->set('url_replace', 'proxigram_server', $proxigram_server);
} else {
DI::config()->delete('url_replace', 'proxigram_server');
}
// Convert twelvefeet_sites into an array before setting the new value // Convert twelvefeet_sites into an array before setting the new value
$twelvefeet_sites = explode(PHP_EOL, $_POST['twelvefeet_sites']); $twelvefeet_sites = explode(PHP_EOL, $_POST['twelvefeet_sites']);
// Normalize URLs by using lower case, removing a trailing slash and whitespace // Normalize URLs by using lower case, removing a trailing slash and whitespace
$twelvefeet_sites = array_map(fn($value): string => rtrim(trim(strtolower($value)), '/'), $twelvefeet_sites); $twelvefeet_sites = array_map(fn ($value): string => rtrim(trim(strtolower($value)), '/'), $twelvefeet_sites);
// Do not store empty lines or duplicates // Do not store empty lines or duplicates
$twelvefeet_sites = array_filter($twelvefeet_sites, fn($value): bool => !empty($value)); $twelvefeet_sites = array_filter($twelvefeet_sites, fn ($value): bool => !empty($value));
$twelvefeet_sites = array_unique($twelvefeet_sites); $twelvefeet_sites = array_unique($twelvefeet_sites);
// Ensure a protocol and default to HTTPS // Ensure a protocol and default to HTTPS
$twelvefeet_sites = array_map( $twelvefeet_sites = array_map(
fn($value): string => substr($value, 0, 4) !== 'http' ? 'https://' . $value : $value, fn ($value): string => substr($value, 0, 4) !== 'http' ? 'https://' . $value : $value,
$twelvefeet_sites $twelvefeet_sites
); );
asort($twelvefeet_sites); asort($twelvefeet_sites);
@ -41,31 +64,58 @@ function url_replace_addon_admin_post()
/** /**
* Hook into admin settings to enable choosing a different server * Hook into admin settings to enable choosing a different server
* for twitter, youtube, and news sites. * for Twitter, YouTube, Instagram, and news sites.
*/ */
function url_replace_addon_admin(string &$o) function url_replace_addon_admin(string &$o)
{ {
$nitter_server = DI::config()->get('url_replace', 'nitter_server'); $nitter_server_enabled = DI::config()->get('url_replace', 'nitter_server_enabled') ?? true;
$invidious_server = DI::config()->get('url_replace', 'invidious_server'); $nitter_server = DI::config()->get('url_replace', 'nitter_server') ?? '';
$twelvefeet_sites = implode(PHP_EOL, DI::config()->get('url_replace', 'twelvefeet_sites') ?? [] ?: []); $invidious_server_enabled = DI::config()->get('url_replace', 'invidious_server_enabled') ?? true;
$invidious_server = DI::config()->get('url_replace', 'invidious_server') ?? '';
$proxigram_server_enabled = DI::config()->get('url_replace', 'proxigram_server_enabled') ?? true;
$proxigram_server = DI::config()->get('url_replace', 'proxigram_server') ?? '';
$twelvefeet_sites = implode(PHP_EOL, DI::config()->get('url_replace', 'twelvefeet_sites') ?? []);
$t = Renderer::getMarkupTemplate('admin.tpl', 'addon/url_replace/'); $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/url_replace/');
$o = Renderer::replaceMacros($t, [ $o = Renderer::replaceMacros($t, [
'$nitter_server_enabled' => [
'nitter_server_enabled',
DI::l10n()->t('Replace links to X.'),
$nitter_server_enabled,
],
'$nitter_server' => [ '$nitter_server' => [
'nitter_server', 'nitter_server',
DI::l10n()->t('Nitter server'), DI::l10n()->t('Nitter server'),
$nitter_server, $nitter_server,
DI::l10n()->t('Specify the URL with protocol. The default is https://nitter.net.'), DI::l10n()->t('Specify the URL with protocol. The default is %s.', URL_REPLACE_NITTER_DEFAULT),
null, null,
'placeholder="https://nitter.net"', 'placeholder="' . URL_REPLACE_NITTER_DEFAULT . '"',
toddy marked this conversation as resolved Outdated

Code standards: Please wrap operators with spaces.

Code standards: Please wrap operators with spaces.
Outdated
Review

Strange, I did run bin/dev/php-cs-fixer/vendor/bin/php-cs-fixer for the file. However, I've seen that you've done some reformatting of my code as well in commit eeb783d71d -- I'm fine with that, but just out of curiosity: Could you please point me to the canonical configuration settings for PHP-CS-Fixer? I thought it's .php_cs.dist in the root directory, but when using that, my code does not get reformatted with spaces between operators.

Strange, I did run `bin/dev/php-cs-fixer/vendor/bin/php-cs-fixer` for the file. However, I've seen that you've done some reformatting of my code as well in commit https://git.friendi.ca/friendica/friendica-addons/commit/eeb783d71d713df23038102048284c6b170634cb -- I'm fine with that, but just out of curiosity: Could you please point me to the canonical configuration settings for PHP-CS-Fixer? I thought it's `.php_cs.dist` in the root directory, but when using that, my code does not get reformatted with spaces between operators.

Sorry about that, we're following the PSR-12 coding standards: https://www.php-fig.org/psr/psr-12/#62-binary-operators

I don't use PHP CS Fixer myself so I'd be hard-pressed to verify the configuration is correct.

Sorry about that, we're following the PSR-12 coding standards: https://www.php-fig.org/psr/psr-12/#62-binary-operators I don't use PHP CS Fixer myself so I'd be hard-pressed to verify the configuration is correct.
],
'$invidious_server_enabled' => [
'invidious_server_enabled',
DI::l10n()->t('Replace links to YouTube.'),
$invidious_server_enabled,
], ],
'$invidious_server' => [ '$invidious_server' => [
'invidious_server', 'invidious_server',
DI::l10n()->t('Invidious server'), DI::l10n()->t('Invidious server'),
$invidious_server, $invidious_server,
DI::l10n()->t('Specify the URL with protocol. The default is https://yewtu.be.'), DI::l10n()->t('Specify the URL with protocol. The default is %s.', URL_REPLACE_INVIDIOUS_DEFAULT),
null, null,
'placeholder="https://yewtu.be"', 'placeholder="' . URL_REPLACE_INVIDIOUS_DEFAULT . '"',
],
'$proxigram_server_enabled' => [
'proxigram_server_enabled',
DI::l10n()->t('Replace links to Instagram.'),
$proxigram_server_enabled,
],
'$proxigram_server' => [
'proxigram_server',
DI::l10n()->t('Proxigram server'),
$proxigram_server,
DI::l10n()->t('Specify the URL with protocol. The default is %s.', URL_REPLACE_PROXIGRAM_DEFAULT),
null,
'placeholder="' . URL_REPLACE_PROXIGRAM_DEFAULT . '"',
], ],
'$twelvefeet_sites' => [ '$twelvefeet_sites' => [
'twelvefeet_sites', 'twelvefeet_sites',
@ -84,29 +134,41 @@ function url_replace_addon_admin(string &$o)
*/ */
toddy marked this conversation as resolved Outdated

Could you use the third parameter of the get function for the default value? Also it would be nice to use constants for the default URL. See for example the inviduous addon for that.

Could you use the third parameter of the `get` function for the default value? Also it would be nice to use constants for the default URL. See for example the inviduous addon for that.
Outdated
Review

Thanks for your review. Unfotunately, the third parameter of get() does not work in this case, I've tried that. When the addon is freshly installed, there is no configuration in the database. In that case, DI::config()->get() gets a NULL value from the database, so the third parameter will be returned as the default value.

However, if the site admin sets another server, then the URL of that server will be stored in the database. If afterwards the site admin deletes that custom server again and leaves the input field blank, then DI::config()->get() will get a result from the database -- an empty string. In that case, the empty string will be returned. This is the reason why I'm checking the value with the empty() function.

An alternative approach would be to specifically check in url_replace_addon_admin_post() if the posted string is empty. If it is, I could then use DI::config->delete() instead of DI::config->set(), that should solve the problem.

Thanks for your review. Unfotunately, the third parameter of get() does not work in this case, I've tried that. When the addon is freshly installed, there is no configuration in the database. In that case, `DI::config()->get()` gets a NULL value from the database, so the third parameter will be returned as the default value. However, if the site admin sets another server, then the URL of that server will be stored in the database. If afterwards the site admin deletes that custom server again and leaves the input field blank, then `DI::config()->get()` will get a result from the database -- an empty string. In that case, the empty string will be returned. This is the reason why I'm checking the value with the `empty()` function. An alternative approach would be to specifically check in `url_replace_addon_admin_post()` if the posted string is empty. If it is, I could then use `DI::config->delete()` instead of `DI::config->set()`, that should solve the problem.
Outdated
Review

The alternative approach could look like this:

	$server = rtrim(trim($_POST['nitter_server']), '/');
	if (empty($server)) {
		DI::config()->delete('url_replace', 'nitter_server');
	} else {
		DI::config()->set('url_replace', 'nitter_server', $server);
	}

Should I rewrite the addon in that style?

The alternative approach could look like this: ```php $server = rtrim(trim($_POST['nitter_server']), '/'); if (empty($server)) { DI::config()->delete('url_replace', 'nitter_server'); } else { DI::config()->set('url_replace', 'nitter_server', $server); } ``` Should I rewrite the addon in that style?

Looks better.

Looks better.

As a general rule, the default value parameter of both Config->get and IConfig->get is now deprecated in favor of using the short ternary operators ?: and ??. This allows for a better control for default values.

As a general rule, the default value parameter of both `Config->get` and `IConfig->get` is now deprecated in favor of using the short ternary operators `?:` and `??`. This allows for a better control for default values.
Outdated
Review

I've just pushed a commit which uses this pattern. Please take a look.

I've just pushed a commit which uses this pattern. Please take a look.
function url_replace_render(array &$b) function url_replace_render(array &$b)
{ {
$replaced = false; $replaced = false;
$replacements = [];
$nitter_server = DI::config()->get('url_replace', 'nitter_server'); $nitter_server = DI::config()->get('url_replace', 'nitter_server') ?? URL_REPLACE_NITTER_DEFAULT;
if (empty($nitter_server)) { $nitter_server_enabled = DI::config()->get('url_replace', 'nitter_server_enabled') ?? true;
$nitter_server = 'https://nitter.net'; if ($nitter_server_enabled) {
$replacements = array_merge($replacements, [
'https://mobile.twitter.com' => $nitter_server,
'https://twitter.com' => $nitter_server,
'https://mobile.x.com' => $nitter_server,
'https://x.com' => $nitter_server,
]);
} }
$invidious_server = DI::config()->get('url_replace', 'invidious_server'); $invidious_server = DI::config()->get('url_replace', 'invidious_server') ?? URL_REPLACE_INVIDIOUS_DEFAULT;
if (empty($invidious_server)) { $invidious_server_enabled = DI::config()->get('url_replace', 'invidious_server_enabled') ?? true;
$invidious_server = 'https://yewtu.be'; if ($invidious_server_enabled) {
$replacements = array_merge($replacements, [
'https://www.youtube.com' => $invidious_server,
'https://youtube.com' => $invidious_server,
'https://m.youtube.com' => $invidious_server,
'https://youtu.be' => $invidious_server,
]);
}
$proxigram_server = DI::config()->get('url_replace', 'proxigram_server') ?? URL_REPLACE_PROXIGRAM_DEFAULT;
$proxigram_server_enabled = DI::config()->get('url_replace', 'proxigram_server_enabled') ?? true;
if ($proxigram_server_enabled) {
$replacements = array_merge($replacements, [
'https://www.instagram.com' => $proxigram_server,
'https://instagram.com' => $proxigram_server,
'https://ig.me' => $proxigram_server,
]);
} }
// Handle some of twitter and youtube
$replacements = [
'https://mobile.twitter.com' => $nitter_server,
'https://twitter.com' => $nitter_server,
'https://mobile.x.com' => $nitter_server,
'https://x.com' => $nitter_server,
'https://www.youtube.com' => $invidious_server,
'https://youtube.com' => $invidious_server,
'https://m.youtube.com' => $invidious_server,
'https://youtu.be' => $invidious_server,
];
foreach ($replacements as $server => $replacement) { foreach ($replacements as $server => $replacement) {
if (strpos($b['html'], $server) !== false) { if (strpos($b['html'], $server) !== false) {
$b['html'] = str_replace($server, $replacement, $b['html']); $b['html'] = str_replace($server, $replacement, $b['html']);
@ -114,7 +176,7 @@ function url_replace_render(array &$b)
} }
} }
$twelvefeet_sites = DI::config()->get('url_replace', 'twelvefeet_sites') ?? [] ?: []; $twelvefeet_sites = DI::config()->get('url_replace', 'twelvefeet_sites') ?? [];
foreach ($twelvefeet_sites as $twelvefeet_site) { foreach ($twelvefeet_sites as $twelvefeet_site) {
if (strpos($b['html'], $twelvefeet_site) !== false) { if (strpos($b['html'], $twelvefeet_site) !== false) {
$b['html'] = str_replace($twelvefeet_site, 'https://12ft.io/' . $twelvefeet_site, $b['html']); $b['html'] = str_replace($twelvefeet_site, 'https://12ft.io/' . $twelvefeet_site, $b['html']);
@ -122,8 +184,7 @@ function url_replace_render(array &$b)
} }
toddy marked this conversation as resolved Outdated

I suggest to compare the original value with the new value to decide, if content had been replaced.

I suggest to compare the original value with the new value to decide, if content had been replaced.
Outdated
Review

You're right, that makes more sense. I'll change that probably tomorrow, I'm short on time right now.

You're right, that makes more sense. I'll change that probably tomorrow, I'm short on time right now.
} }
if ($replaced) { if ($replaced) {
$b['html'] .= '<hr><p><small>' . DI::l10n()->t('(URL replace addon enabled for X, YouTube and some news sites.)') . '</small></p>'; $b['html'] .= '<hr><p><small>' . DI::l10n()->t('(URL replace addon enabled for X, YouTube, Instagram and some news sites.)') . '</small></p>';
} }
} }