Merge pull request #13194 from annando/relay-language

Language check moved to a separate function
This commit is contained in:
Tobias Diekershoff 2023-06-05 07:07:15 +02:00 committed by GitHub
commit ab29f91c2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 23 deletions

View File

@ -92,7 +92,7 @@ class Avatar
return $fields;
}
$filename = self::getFilename($contact['url']);
$filename = self::getFilename($contact['url'], $avatar);
$timestamp = time();
$fields['blurhash'] = $image->getBlurHash();
@ -120,7 +120,7 @@ class Avatar
return $fields;
}
$filename = self::getFilename($contact['url']);
$filename = self::getFilename($contact['url'], $contact['avatar']);
$timestamp = time();
$fields['photo'] = self::storeAvatarCache($image, $filename, Proxy::PIXEL_SMALL, $timestamp);
@ -130,9 +130,9 @@ class Avatar
return $fields;
}
private static function getFilename(string $url): string
private static function getFilename(string $url, string $host): string
{
$guid = Item::guidFromUri($url);
$guid = Item::guidFromUri($url, $host);
return substr($guid, 0, 2) . '/' . substr($guid, 3, 2) . '/' . substr($guid, 5, 3) . '/' .
substr($guid, 9, 2) .'/' . substr($guid, 11, 2) . '/' . substr($guid, 13, 4). '/' . substr($guid, 18) . '-';

View File

@ -62,6 +62,7 @@ class BBCode
const TWITTER = 8;
const BACKLINK = 8;
const ACTIVITYPUB = 9;
const BLUESKY = 10;
const TOP_ANCHOR = '<br class="top-anchor">';
const BOTTOM_ANCHOR = '<br class="button-anchor">';
@ -1771,7 +1772,7 @@ class BBCode
$text
);
if (in_array($simple_html, [self::OSTATUS, self::TWITTER])) {
if (in_array($simple_html, [self::OSTATUS, self::TWITTER, self::BLUESKY])) {
$text = preg_replace_callback("/([^#@!])\[url\=([^\]]*)\](.*?)\[\/url\]/ism", [self::class, 'expandLinksCallback'], $text);
//$text = preg_replace("/[^#@!]\[url\=([^\]]*)\](.*?)\[\/url\]/ism", ' $2 [url]$1[/url]', $text);
$text = preg_replace("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", ' $2 [url]$1[/url]', $text);

View File

@ -137,6 +137,10 @@ class Plaintext
$abstract = BBCode::getAbstract($item['body'], Protocol::STATUSNET);
break;
case BBCode::BLUESKY:
$abstract = BBCode::getAbstract($item['body'], Protocol::BLUESKY);
break;
default: // We don't know the exact target.
// We fetch an abstract since there is a posting limit.
if ($limit > 0) {

View File

@ -2773,7 +2773,7 @@ class Contact
}
$update = false;
$guid = ($ret['guid'] ?? '') ?: Item::guidFromUri($ret['url']);
$guid = ($ret['guid'] ?? '') ?: Item::guidFromUri($ret['url'], $ret['baseurl'] ?: $ret['alias']);
// make sure to not overwrite existing values with blank entries except some technical fields
$keep = ['batch', 'notify', 'poll', 'request', 'confirm', 'poco', 'baseurl'];

View File

@ -2044,7 +2044,7 @@ class Item
// Remove the scheme to make sure that "https" and "http" doesn't make a difference
unset($parsed['scheme']);
$hostPart = $host ?? $parsed['host'] ?? '';
$hostPart = $host ?: $parsed['host'] ?? '';
if (!$hostPart) {
Logger::warning('Empty host GUID part', ['uri' => $uri, 'host' => $host, 'parsed' => $parsed, 'callstack' => System::callstack(10)]);
}

View File

@ -135,22 +135,8 @@ class Relay
}
}
$languages = [];
foreach (Item::getLanguageArray($body, 10) as $language => $reliability) {
if ($reliability > 0) {
$languages[] = $language;
}
}
Logger::debug('Got languages', ['languages' => $languages, 'body' => $body, 'causer' => $causer]);
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]);
return false;
}
} elseif ($config->get('system', 'relay_deny_undetected_language')) {
Logger::info('Undetected language found - rejected', ['body' => $body, 'network' => $network, 'url' => $url, 'causer' => $causer]);
if (!self::isWantedLanguage($body)) {
Logger::info('Unwanted or Undetected language found - rejected', ['network' => $network, 'url' => $url, 'causer' => $causer]);
return false;
}
@ -163,6 +149,36 @@ class Relay
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) {
$languages[] = $language;
}
}
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;
}
} elseif (DI::config()->get('system', 'relay_deny_undetected_language')) {
Logger::info('Undetected language found', ['body' => $body]);
return false;
}
return true;
}
/**
* Update or insert a relay contact
*