From fc600b2dbf2708990b7dd2b333745f174a604381 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 4 Jun 2023 17:18:43 +0000 Subject: [PATCH] Language check moved to a separate function --- src/Protocol/Relay.php | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/Protocol/Relay.php b/src/Protocol/Relay.php index d8b11cf95b..c5131fb3fd 100644 --- a/src/Protocol/Relay.php +++ b/src/Protocol/Relay.php @@ -135,6 +135,28 @@ class Relay } } + if (!self::isWantedLanguage($body)) { + Logger::info('Unwanted or Undetected language found - rejected', ['network' => $network, 'url' => $url, 'causer' => $causer]); + return false; + } + + if ($scope == self::SCOPE_ALL) { + Logger::info('Server accept all posts - accepted', ['network' => $network, 'url' => $url, 'causer' => $causer]); + return true; + } + + Logger::info('No matching hashtags found - rejected', ['network' => $network, 'url' => $url, 'causer' => $causer]); + return false; + } + + /** + * Detect the language of a post and decide if the post should be accepted + * + * @param string $body + * @return boolean + */ + public static function isWantedLanguage(string $body) + { $languages = []; foreach (Item::getLanguageArray($body, 10) as $language => $reliability) { if ($reliability > 0) { @@ -142,25 +164,19 @@ class Relay } } - Logger::debug('Got languages', ['languages' => $languages, 'body' => $body, 'causer' => $causer]); + Logger::debug('Got languages', ['languages' => $languages, 'body' => $body]); if (!empty($languages)) { - if (in_array($languages[0], $config->get('system', 'relay_deny_languages'))) { - Logger::info('Unwanted language found - rejected', ['language' => $languages[0], 'network' => $network, 'url' => $url, 'causer' => $causer]); + if (in_array($languages[0], DI::config()->get('system', 'relay_deny_languages'))) { + Logger::info('Unwanted language found', ['language' => $languages[0]]); return false; } - } elseif ($config->get('system', 'relay_deny_undetected_language')) { - Logger::info('Undetected language found - rejected', ['body' => $body, 'network' => $network, 'url' => $url, 'causer' => $causer]); + } elseif (DI::config()->get('system', 'relay_deny_undetected_language')) { + Logger::info('Undetected language found', ['body' => $body]); return false; } - if ($scope == self::SCOPE_ALL) { - Logger::info('Server accept all posts - accepted', ['network' => $network, 'url' => $url, 'causer' => $causer]); - return true; - } - - Logger::info('No matching hashtags found - rejected', ['network' => $network, 'url' => $url, 'causer' => $causer]); - return false; + return true; } /** -- 2.39.5