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
Showing only changes of commit 32f698ce10 - Show all commits

View file

@ -134,6 +134,7 @@ 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)
{
$replaced = false;
$replacements = [];
$nitter_server = DI::config()->get('url_replace', 'nitter_server') ?? URL_REPLACE_NITTER_DEFAULT;
@ -171,6 +172,7 @@ function url_replace_render(array &$b)
foreach ($replacements as $server => $replacement) {
if (strpos($b['html'], $server) !== false) {
$b['html'] = str_replace($server, $replacement, $b['html']);
$replaced = true;
}
}
@ -178,10 +180,11 @@ function url_replace_render(array &$b)
foreach ($twelvefeet_sites as $twelvefeet_site) {
if (strpos($b['html'], $twelvefeet_site) !== false) {
$b['html'] = str_replace($twelvefeet_site, 'https://12ft.io/' . $twelvefeet_site, $b['html']);
$replaced = true;
}
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 (count($replacements) > 0) {
if ($replaced) {
$b['html'] .= '<hr><p><small>' . DI::l10n()->t('(URL replace addon enabled for X, YouTube, Instagram and some news sites.)') . '</small></p>';
}
}