Invidious: The addon is now user definable #1449

Merged
MrPetovan merged 2 commits from heluecht/friendica-addons:invidious-user into 2023.09-rc 2023-12-18 19:33:56 +01:00
Showing only changes of commit cabdd924d0 - Show all commits

View file

@ -16,7 +16,7 @@ CONST INVIDIOUS_DEFAULT = 'https://invidio.us';
function invidious_install() function invidious_install()
{ {
Hook::register('prepare_body_final', __FILE__, 'invidious_render'); Hook::register('prepare_body_final', __FILE__, 'invidious_render');
Hook::register('addon_settings', __FILE__, 'invidious_settings'); Hook::register('addon_settings', __FILE__, 'invidious_settings');
Hook::register('addon_settings_post', __FILE__, 'invidious_settings_post'); Hook::register('addon_settings_post', __FILE__, 'invidious_settings_post');
} }
@ -25,7 +25,7 @@ function invidious_install()
*/ */
function invidious_addon_admin_post() function invidious_addon_admin_post()
{ {
DI::config()->set('invidious', 'server', trim($_POST['invidiousserver'], " \n\r\t\v\x00/")); DI::config()->set('invidious', 'server', trim($_POST['invidiousserver'], " \n\r\t\v\x00/"));
} }
/* Hook into the admin settings to let the admin choose an /* Hook into the admin settings to let the admin choose an
@ -33,13 +33,13 @@ function invidious_addon_admin_post()
*/ */
function invidious_addon_admin(string &$o) function invidious_addon_admin(string &$o)
{ {
$invidiousserver = DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT); $invidiousserver = DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT);
$t = Renderer::getMarkupTemplate('admin.tpl', 'addon/invidious/'); $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/invidious/');
$o = Renderer::replaceMacros($t, [ $o = Renderer::replaceMacros($t, [
'$settingdescription' => DI::l10n()->t('Which Invidious server shall be used for the replacements in the post bodies? Use the URL with servername and protocol. See %s for a list of available public Invidious servers.', 'https://redirect.invidious.io'), '$settingdescription' => DI::l10n()->t('Which Invidious server shall be used for the replacements in the post bodies? Use the URL with servername and protocol. See %s for a list of available public Invidious servers.', 'https://redirect.invidious.io'),
'$invidiousserver' => ['invidiousserver', DI::l10n()->t('Invidious server'), $invidiousserver, DI::l10n()->t('See %s for a list of available Invidious servers.', '<a href="https://api.invidious.io/">https://api.invidious.io/</a>')], '$invidiousserver' => ['invidiousserver', DI::l10n()->t('Invidious server'), $invidiousserver, DI::l10n()->t('See %s for a list of available Invidious servers.', '<a href="https://api.invidious.io/">https://api.invidious.io/</a>')],
'$submit' => DI::l10n()->t('Save Settings'), '$submit' => DI::l10n()->t('Save Settings'),
]); ]);
} }
function invidious_settings(array &$data) function invidious_settings(array &$data)
@ -54,7 +54,7 @@ function invidious_settings(array &$data)
$t = Renderer::getMarkupTemplate('settings.tpl', 'addon/invidious/'); $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/invidious/');
$html = Renderer::replaceMacros($t, [ $html = Renderer::replaceMacros($t, [
'$enabled' => ['enabled', DI::l10n()->t('Replace Youtube links with links to an Invidious server'), $enabled, DI::l10n()->t('If enabled, Youtube links are replaced with the links to the specified Invidious server.')], '$enabled' => ['enabled', DI::l10n()->t('Replace Youtube links with links to an Invidious server'), $enabled, DI::l10n()->t('If enabled, Youtube links are replaced with the links to the specified Invidious server.')],
'$server' => ['server', DI::l10n()->t('Invidious server'), $server, DI::l10n()->t('See %s for a list of available Invidious servers.', '<a href="https://api.invidious.io/">https://api.invidious.io/</a>')], '$server' => ['server', DI::l10n()->t('Invidious server'), $server, DI::l10n()->t('See %s for a list of available Invidious servers.', '<a href="https://api.invidious.io/">https://api.invidious.io/</a>')],
]); ]);
$data = [ $data = [
@ -70,14 +70,14 @@ function invidious_settings_post(array &$b)
return; return;
} }
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'invidious', 'enabled', (bool)$_POST['enabled']); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'invidious', 'enabled', (bool)$_POST['enabled']);
$server = trim($_POST['server'], " \n\r\t\v\x00/"); $server = trim($_POST['server'], " \n\r\t\v\x00/");
if ($server != DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT) && !empty($server)) { if ($server != DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT) && !empty($server)) {
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'invidious', 'server', $server); DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'invidious', 'server', $server);
} else { } else {
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'invidious', 'server'); DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'invidious', 'server');
} }
} }
/* /*
@ -85,19 +85,19 @@ function invidious_settings_post(array &$b)
*/ */
function invidious_render(array &$b) function invidious_render(array &$b)
{ {
if (!DI::userSession()->getLocalUserId() || !DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'enabled')) { if (!DI::userSession()->getLocalUserId() || !DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'enabled')) {
return; return;
} }
$original = $b['html']; $original = $b['html'];
$server = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'server', DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT)); $server = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'invidious', 'server', DI::config()->get('invidious', 'server', INVIDIOUS_DEFAULT));
$b['html'] = preg_replace("/https?:\/\/www.youtube.com\/watch\?v\=(.*?)/ism", $server . '/watch?v=$1', $b['html']); $b['html'] = preg_replace("/https?:\/\/www.youtube.com\/watch\?v\=(.*?)/ism", $server . '/watch?v=$1', $b['html']);
$b['html'] = preg_replace("/https?:\/\/www.youtube.com\/embed\/(.*?)/ism", $server . '/embed/$1', $b['html']); $b['html'] = preg_replace("/https?:\/\/www.youtube.com\/embed\/(.*?)/ism", $server . '/embed/$1', $b['html']);
$b['html'] = preg_replace("/https?:\/\/www.youtube.com\/shorts\/(.*?)/ism", $server . '/shorts/$1', $b['html']); $b['html'] = preg_replace("/https?:\/\/www.youtube.com\/shorts\/(.*?)/ism", $server . '/shorts/$1', $b['html']);
$b['html'] = preg_replace("/https?:\/\/youtu.be\/(.*?)/ism", $server . '/watch?v=$1', $b['html']); $b['html'] = preg_replace("/https?:\/\/youtu.be\/(.*?)/ism", $server . '/watch?v=$1', $b['html']);
if ($original != $b['html']) { if ($original != $b['html']) {
$b['html'] .= '<hr><p><small>' . DI::l10n()->t('(Invidious addon enabled: YouTube links via %s)', $server) . '</small></p>'; $b['html'] .= '<hr><p><small>' . DI::l10n()->t('(Invidious addon enabled: YouTube links via %s)', $server) . '</small></p>';
} }
} }