From 15e61e78acb4c3c768449e242141e28d5f3ab876 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 29 Sep 2020 19:48:26 +0000 Subject: [PATCH] Issue 9305: Relay deny tags are added --- src/Module/Admin/Site.php | 3 ++ src/Protocol/ActivityPub/Processor.php | 54 +++++++++++++++--------- view/templates/admin/site.tpl | 1 + view/theme/frio/templates/admin/site.tpl | 1 + 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/Module/Admin/Site.php b/src/Module/Admin/Site.php index 3c5688977a..ea2b5604e3 100644 --- a/src/Module/Admin/Site.php +++ b/src/Module/Admin/Site.php @@ -214,6 +214,7 @@ class Site extends BaseAdmin $relay_subscribe = !empty($_POST['relay_subscribe']); $relay_scope = (!empty($_POST['relay_scope']) ? Strings::escapeTags(trim($_POST['relay_scope'])) : ''); $relay_server_tags = (!empty($_POST['relay_server_tags']) ? Strings::escapeTags(trim($_POST['relay_server_tags'])) : ''); + $relay_deny_tags = (!empty($_POST['relay_deny_tags']) ? Strings::escapeTags(trim($_POST['relay_deny_tags'])) : ''); $relay_user_tags = !empty($_POST['relay_user_tags']); $active_panel = (!empty($_POST['active_panel']) ? "#" . Strings::escapeTags(trim($_POST['active_panel'])) : ''); @@ -425,6 +426,7 @@ class Site extends BaseAdmin DI::config()->set('system', 'relay_subscribe' , $relay_subscribe); DI::config()->set('system', 'relay_scope' , $relay_scope); DI::config()->set('system', 'relay_server_tags', $relay_server_tags); + DI::config()->set('system', 'relay_deny_tags' , $relay_deny_tags); DI::config()->set('system', 'relay_user_tags' , $relay_user_tags); DI::config()->set('system', 'rino_encrypt' , $rino); @@ -696,6 +698,7 @@ class Site extends BaseAdmin '$relay_directly' => ['relay_directly', DI::l10n()->t('Direct relay transfer'), DI::config()->get('system', 'relay_directly'), DI::l10n()->t('Enables the direct transfer to other servers without using the relay servers')], '$relay_scope' => ['relay_scope', DI::l10n()->t('Relay scope'), DI::config()->get('system', 'relay_scope'), DI::l10n()->t('Can be "all" or "tags". "all" means that every public post should be received. "tags" means that only posts with selected tags should be received.'), ['' => DI::l10n()->t('Disabled'), 'all' => DI::l10n()->t('all'), 'tags' => DI::l10n()->t('tags')]], '$relay_server_tags' => ['relay_server_tags', DI::l10n()->t('Server tags'), DI::config()->get('system', 'relay_server_tags'), DI::l10n()->t('Comma separated list of tags for the "tags" subscription.')], + '$relay_deny_tags' => ['relay_deny_tags', DI::l10n()->t('Deny Server tags'), DI::config()->get('system', 'relay_deny_tags'), DI::l10n()->t('Comma separated list of tags that are rejected.')], '$relay_user_tags' => ['relay_user_tags', DI::l10n()->t('Allow user tags'), DI::config()->get('system', 'relay_user_tags'), DI::l10n()->t('If enabled, the tags from the saved searches will used for the "tags" subscription in addition to the "relay_server_tags".')], '$form_security_token' => self::getFormSecurityToken('admin_site'), diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 26d953985a..75d47090b5 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -810,11 +810,6 @@ class Processor $scope = SR_SCOPE_NONE; } - if ($scope == SR_SCOPE_ALL) { - Logger::info('Server accept all posts - accepted', ['id' => $id]); - return true; - } - $replyto = JsonLD::fetchElement($activity['as:object'], 'as:inReplyTo', '@id'); if (Item::exists(['uri' => $replyto])) { Logger::info('Post is a reply to an existing post - accepted', ['id' => $id, 'replyto' => $replyto]); @@ -839,11 +834,11 @@ class Processor $systemTags = []; $userTags = []; + $denyTags = []; if ($scope == SR_SCOPE_TAGS) { - $server_tags = $config->get('system', 'relay_server_tags', []); + $server_tags = $config->get('system', 'relay_server_tags'); $tagitems = explode(',', mb_strtolower($server_tags)); - foreach ($tagitems AS $tag) { $systemTags[] = trim($tag, '# '); } @@ -853,22 +848,43 @@ class Processor } } - $content = mb_strtolower(BBCode::toPlaintext(HTML::toBBCode(JsonLD::fetchElement($activity['as:object'], 'as:content', '@value')), false)); - $tagList = array_unique(array_merge($systemTags, $userTags)); - foreach ($messageTags as $tag) { - if (in_array($tag, $tagList)) { - Logger::info('Subscribed hashtag found - accepted', ['id' => $id, 'hashtag' => $tag]); - return true; - } - // We check with "strpos" for performance issues. Only when this is true, the regular expression check is used - // RegExp is taken from here: https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed - if ((strpos($content, $tag) !== false) && preg_match('/(?<=[\s,.:;"\']|^)' . preg_quote($tag, '/') . '(?=[\s,.:;"\']|$)/', $content)) { - Logger::info('Subscribed hashtag found in content - accepted', ['id' => $id, 'hashtag' => $tag]); - return true; + + $deny_tags = $config->get('system', 'relay_deny_tags'); + $tagitems = explode(',', mb_strtolower($deny_tags)); + foreach ($tagitems AS $tag) { + $tag = trim($tag, '# '); + $denyTags[] = $tag; + } + + if (!empty($tagList) || !empty($denyTags)) { + $content = mb_strtolower(BBCode::toPlaintext(HTML::toBBCode(JsonLD::fetchElement($activity['as:object'], 'as:content', '@value')), false)); + + foreach ($messageTags as $tag) { + if (in_array($tag, $denyTags)) { + Logger::info('Unwanted hashtag found - rejected', ['id' => $id, 'hashtag' => $tag]); + return false; + } + + if (in_array($tag, $tagList)) { + Logger::info('Subscribed hashtag found - accepted', ['id' => $id, 'hashtag' => $tag]); + return true; + } + + // We check with "strpos" for performance issues. Only when this is true, the regular expression check is used + // RegExp is taken from here: https://medium.com/@shiba1014/regex-word-boundaries-with-unicode-207794f6e7ed + if ((strpos($content, $tag) !== false) && preg_match('/(?<=[\s,.:;"\']|^)' . preg_quote($tag, '/') . '(?=[\s,.:;"\']|$)/', $content)) { + Logger::info('Subscribed hashtag found in content - accepted', ['id' => $id, 'hashtag' => $tag]); + return true; + } } } + if ($scope == SR_SCOPE_ALL) { + Logger::info('Server accept all posts - accepted', ['id' => $id]); + return true; + } + Logger::info('No matching hashtags found - rejected', ['id' => $id]); return false; } diff --git a/view/templates/admin/site.tpl b/view/templates/admin/site.tpl index 0c09741343..7f72b4386a 100644 --- a/view/templates/admin/site.tpl +++ b/view/templates/admin/site.tpl @@ -133,6 +133,7 @@ {{include file="field_checkbox.tpl" field=$relay_directly}} {{include file="field_select.tpl" field=$relay_scope}} {{include file="field_input.tpl" field=$relay_server_tags}} + {{include file="field_input.tpl" field=$relay_deny_tags}} {{include file="field_checkbox.tpl" field=$relay_user_tags}}
diff --git a/view/theme/frio/templates/admin/site.tpl b/view/theme/frio/templates/admin/site.tpl index a60cb06b63..2a0cb3f48b 100644 --- a/view/theme/frio/templates/admin/site.tpl +++ b/view/theme/frio/templates/admin/site.tpl @@ -307,6 +307,7 @@ {{include file="field_checkbox.tpl" field=$relay_directly}} {{include file="field_select.tpl" field=$relay_scope}} {{include file="field_input.tpl" field=$relay_server_tags}} + {{include file="field_input.tpl" field=$relay_deny_tags}} {{include file="field_checkbox.tpl" field=$relay_user_tags}}