]> git.mxchange.org Git - friendica.git/commitdiff
Accept relay posts based on the selected user languages
authorMichael <heluecht@pirati.ca>
Fri, 3 Nov 2023 22:54:29 +0000 (22:54 +0000)
committerMichael <heluecht@pirati.ca>
Fri, 3 Nov 2023 22:54:29 +0000 (22:54 +0000)
src/Model/User.php
src/Protocol/ActivityPub/Processor.php
src/Protocol/Relay.php
static/defaults.config.php

index 24a75b5d36e72502bcb8e4e87768359f70fb3c4e..7c128fbe824c0102313f42f4d9edc7309f84d747 100644 (file)
@@ -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
         *
index 21a2c4143e2c90ef1eb6c9fcda51c532942e75ef..81b15f90ee3390d33c7af6e5b163d324b40e4f18 100644 (file)
@@ -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 {
index bf48352019c947fbcea1440ce0b1747544e2fc94..d2e5af0b7372d3242324f5a043b201664d64709d 100644 (file)
@@ -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;
index eb39767b66a5d082a7aa5904306a3f0ee1343a8e..57a073e376665d81041d68dd6df2620fc1c68fa4 100644 (file)
@@ -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,