From 7c0b5be70fbe269e6a66194796e2bb5e7e153e62 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 3 Nov 2023 22:54:29 +0000 Subject: [PATCH] Accept relay posts based on the selected user languages --- src/Model/User.php | 40 ++++++++++++++++++++++++++ src/Protocol/ActivityPub/Processor.php | 2 +- src/Protocol/Relay.php | 21 ++++++++++---- static/defaults.config.php | 4 --- 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/src/Model/User.php b/src/Model/User.php index 24a75b5d36..7c128fbe82 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -575,6 +575,46 @@ class User return DI::l10n()->toISO6391(DI::config()->get('system', 'language')); } + /** + * Get a list of all languages that are used by the users + * + * @return array + */ + public static function getLanguages(): array + { + $supported = array_keys(DI::l10n()->getLanguageCodes()); + $languages = []; + $uids = []; + + $users = DBA::select('user', ['uid', 'language'], ["`verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired` AND `uid` > ?", 0]); + while ($user = DBA::fetch($users)) { + $uids[] = $user['uid']; + $code = DI::l10n()->toISO6391($user['language']); + if (!in_array($code, $supported)) { + continue; + } + $languages[$code] = $code; + } + DBA::close($users); + + $channels = DBA::select('pconfig', ['uid', 'v'], ["`cat` = ? AND `k` = ? AND `v` != ?", 'channel', 'languages', '']); + while ($channel = DBA::fetch($channels)) { + if (!in_array($channel['uid'], $uids)) { + continue; + } + $values = unserialize($channel['v']); + if (!empty($values) && is_array($values)) { + foreach ($values as $language) { + $language = DI::l10n()->toISO6391($language); + $languages[$language] = $language; + } + } + } + DBA::close($channels); + + return array_keys($languages); + } + /** * Authenticate a user with a clear text password * diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 21a2c4143e..81b15f90ee 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -1001,7 +1001,7 @@ class Processor } $tags = array_column(Tag::getByURIId($item['uri-id'], [Tag::HASHTAG]), 'name'); - if (Relay::isSolicitedPost($tags, $item['body'], $item['author-id'], $item['uri'], Protocol::ACTIVITYPUB, $activity['thread-completion'] ?? 0)) { + if (Relay::isSolicitedPost($tags, $item['title'] . ' ' . ($item['content-warning'] ?? '') . ' ' . $item['body'], $item['author-id'], $item['uri'], Protocol::ACTIVITYPUB, $activity['thread-completion'] ?? 0)) { Logger::debug('Post is accepted because of the relay settings', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri']]); return true; } else { diff --git a/src/Protocol/Relay.php b/src/Protocol/Relay.php index bf48352019..d2e5af0b73 100644 --- a/src/Protocol/Relay.php +++ b/src/Protocol/Relay.php @@ -23,6 +23,7 @@ namespace Friendica\Protocol; use Friendica\Content\Smilies; use Friendica\Content\Text\BBCode; +use Friendica\Core\Cache\Enum\Duration; use Friendica\Core\Logger; use Friendica\Core\Protocol; use Friendica\Database\DBA; @@ -34,6 +35,7 @@ use Friendica\Model\Item; use Friendica\Model\Post; use Friendica\Model\Search; use Friendica\Model\Tag; +use Friendica\Model\User; use Friendica\Util\DateTimeFormat; use Friendica\Util\Strings; @@ -185,13 +187,22 @@ class Relay } } - Logger::debug('Got languages', ['languages' => $languages, 'body' => $body]); - if (!empty($languages)) { - if (in_array($languages[0], DI::config()->get('system', 'relay_deny_languages'))) { - Logger::info('Unwanted language found', ['language' => $languages[0]]); - return false; + $cachekey = 'relay:isWantedLanguage'; + $user_languages = DI::cache()->get($cachekey); + if (is_null($user_languages)) { + $user_languages = User::getLanguages(); + DI::cache()->set($cachekey, $user_languages, Duration::HALF_HOUR); } + + foreach ($languages as $language) { + if (in_array($language, $user_languages)) { + Logger::debug('Wanted language found', ['language' => $language, 'languages' => $languages, 'userlang' => $user_languages, 'body' => $body]); + return true; + } + } + Logger::debug('No wanted language found', ['languages' => $languages, 'userlang' => $user_languages, 'body' => $body]); + return false; } elseif (DI::config()->get('system', 'relay_deny_undetected_language')) { Logger::info('Undetected language found', ['body' => $body]); return false; diff --git a/static/defaults.config.php b/static/defaults.config.php index eb39767b66..57a073e376 100644 --- a/static/defaults.config.php +++ b/static/defaults.config.php @@ -558,10 +558,6 @@ return [ // Redistribute incoming activities via ActivityPub 'redistribute_activities' => true, - // relay_deny_languages (Array) - // Array of languages (two digit format) that are rejected. - 'relay_deny_languages' => [], - // relay_deny_undetected_language (Boolean) // Deny undetected languages 'relay_deny_undetected_language' => false, -- 2.39.5