Accept relay posts based on the selected user languages

This commit is contained in:
Michael 2023-11-03 22:54:29 +00:00
parent 8b02c28547
commit 7c0b5be70f
4 changed files with 57 additions and 10 deletions

View 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
*

View 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 {

View 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;

View 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,