From fc600b2dbf2708990b7dd2b333745f174a604381 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 4 Jun 2023 17:18:43 +0000 Subject: [PATCH 1/3] Language check moved to a separate function --- src/Protocol/Relay.php | 48 ++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/Protocol/Relay.php b/src/Protocol/Relay.php index d8b11cf95..c5131fb3f 100644 --- a/src/Protocol/Relay.php +++ b/src/Protocol/Relay.php @@ -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 * From b5a1f13d7c67921359571333ea02d79f1e0383ad Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 5 Jun 2023 04:31:19 +0000 Subject: [PATCH 2/3] New BBCode constant for Bluesky --- src/Content/Text/BBCode.php | 3 ++- src/Content/Text/Plaintext.php | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 0e5ee9d4e..e321b13e7 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -62,6 +62,7 @@ class BBCode const TWITTER = 8; const BACKLINK = 8; const ACTIVITYPUB = 9; + const BLUESKY = 10; const TOP_ANCHOR = '
'; const BOTTOM_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); diff --git a/src/Content/Text/Plaintext.php b/src/Content/Text/Plaintext.php index b297f75b3..86d720a02 100644 --- a/src/Content/Text/Plaintext.php +++ b/src/Content/Text/Plaintext.php @@ -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) { From dfaa4b89c7f7fa2e196f1c9195c0de9d81c41619 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 5 Jun 2023 04:34:06 +0000 Subject: [PATCH 3/3] Support for contact uri that aren't HTTP URLs --- src/Contact/Avatar.php | 8 ++++---- src/Model/Contact.php | 2 +- src/Model/Item.php | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Contact/Avatar.php b/src/Contact/Avatar.php index 299c26399..fc4b7e38c 100644 --- a/src/Contact/Avatar.php +++ b/src/Contact/Avatar.php @@ -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) . '-'; diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 3f830c29a..720d2638c 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -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']; diff --git a/src/Model/Item.php b/src/Model/Item.php index fad8ffeff..cdeb6d3d1 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -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)]); }