From 68fb214f855742695b1aba71cb5ff7edff074d3b Mon Sep 17 00:00:00 2001 From: Art4 Date: Sun, 10 Nov 2024 21:17:41 +0000 Subject: [PATCH 1/7] Fix composer installation in woodpecker pipelines --- .woodpecker/.continuous-deployment.yml | 1 + .woodpecker/.releaser.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/.woodpecker/.continuous-deployment.yml b/.woodpecker/.continuous-deployment.yml index eff4ae0cff..dde6af8ff3 100644 --- a/.woodpecker/.continuous-deployment.yml +++ b/.woodpecker/.continuous-deployment.yml @@ -45,6 +45,7 @@ steps: composer_install: image: friendicaci/php8.2:php8.2.16 commands: + - mkdir addon # create empty addon folder to appease composer - export COMPOSER_HOME=.composer - composer validate - composer install --no-dev --optimize-autoloader diff --git a/.woodpecker/.releaser.yml b/.woodpecker/.releaser.yml index 0c7caac009..794ba5fbd3 100644 --- a/.woodpecker/.releaser.yml +++ b/.woodpecker/.releaser.yml @@ -43,6 +43,7 @@ steps: composer_install: image: friendicaci/php8.2:php8.2.16 commands: + - mkdir addon # create empty addon folder to appease composer - export COMPOSER_HOME=.composer - composer validate - composer install --no-dev --optimize-autoloader From 12d5e4da44d32752eda4540674f64e8d7a1639b3 Mon Sep 17 00:00:00 2001 From: Art4 Date: Mon, 11 Nov 2024 23:40:45 +0000 Subject: [PATCH 2/7] use get_class() instead of static() in BaseCollection --- src/BaseCollection.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/BaseCollection.php b/src/BaseCollection.php index 9598b1ae78..b30dbfc6b0 100644 --- a/src/BaseCollection.php +++ b/src/BaseCollection.php @@ -89,7 +89,9 @@ class BaseCollection extends \ArrayIterator */ public function map(callable $callback): BaseCollection { - return new self(array_map($callback, $this->getArrayCopy()), $this->getTotalCount()); + $class = get_class($this); + + return new $class(array_map($callback, $this->getArrayCopy()), $this->getTotalCount()); } /** @@ -102,7 +104,9 @@ class BaseCollection extends \ArrayIterator */ public function filter(callable $callback = null, int $flag = 0): BaseCollection { - return new self(array_filter($this->getArrayCopy(), $callback, $flag)); + $class = get_class($this); + + return new $class(array_filter($this->getArrayCopy(), $callback, $flag)); } /** @@ -112,14 +116,16 @@ class BaseCollection extends \ArrayIterator */ public function reverse(): BaseCollection { - return new self(array_reverse($this->getArrayCopy()), $this->getTotalCount()); + $class = get_class($this); + + return new $class(array_reverse($this->getArrayCopy()), $this->getTotalCount()); } /** * Split the collection in smaller collections no bigger than the provided length * * @param int $length - * @return self[] + * @return static[] */ public function chunk(int $length): array { @@ -128,7 +134,9 @@ class BaseCollection extends \ArrayIterator } return array_map(function ($array) { - return new self($array); + $class = get_class($this); + + return new $class($array); }, array_chunk($this->getArrayCopy(), $length)); } From 9a04f90bbdf5d842592452f47720a73d4b1ba3d5 Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 12 Nov 2024 00:20:30 +0000 Subject: [PATCH 3/7] Improve check for unlisted receiver --- src/Protocol/ActivityPub/Receiver.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index e31b8fb3d6..0f8184d3db 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -1890,18 +1890,15 @@ class Receiver $object_data['receiver_urls'] = self::getReceiverURL($object); $object_data['receiver'] = $receivers; $object_data['reception_type'] = $reception_types; + $object_data['unlisted'] = false; if (!empty($object['pixelfed:capabilities'])) { $object_data['capabilities'] = self::getCapabilities($object); } - $object_data['unlisted'] = in_array(-1, $object_data['receiver']); - - if (array_key_exists(-1, $object_data['receiver'])) { + if (in_array(-1, $object_data['receiver'])) { + $object_data['unlisted'] = true; unset($object_data['receiver'][-1]); - } - - if (array_key_exists(-1, $object_data['reception_type'])) { unset($object_data['reception_type'][-1]); } From ca51b45f04987dc2184ba7faec564e332115c851 Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 12 Nov 2024 21:02:21 +0000 Subject: [PATCH 4/7] Upgrade to PHPStan 2.0 --- composer.json | 2 +- composer.lock | 14 +++++++------- src/Protocol/ActivityPub/Receiver.php | 5 ++++- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 2090075024..4acfe7e41a 100644 --- a/composer.json +++ b/composer.json @@ -152,7 +152,7 @@ "mikey179/vfsstream": "^1.6", "mockery/mockery": "^1.3", "php-mock/php-mock-phpunit": "^2.10", - "phpstan/phpstan": "^1.12", + "phpstan/phpstan": "^2.0", "phpunit/phpunit": "^9" }, "scripts": { diff --git a/composer.lock b/composer.lock index 7d1e831b14..8d200a3f67 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "11175d4e9806e8a5069d010140431af1", + "content-hash": "3e31a2243fb69e47e1b7000cca946fa2", "packages": [ { "name": "asika/simple-console", @@ -4834,20 +4834,20 @@ }, { "name": "phpstan/phpstan", - "version": "1.12.7", + "version": "2.0.1", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "dc2b9976bd8b0f84ec9b0e50cc35378551de7af0" + "reference": "ab4e9b4415a5fc9e4d27f7fe16c8bc9d067dcd6d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/dc2b9976bd8b0f84ec9b0e50cc35378551de7af0", - "reference": "dc2b9976bd8b0f84ec9b0e50cc35378551de7af0", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/ab4e9b4415a5fc9e4d27f7fe16c8bc9d067dcd6d", + "reference": "ab4e9b4415a5fc9e4d27f7fe16c8bc9d067dcd6d", "shasum": "" }, "require": { - "php": "^7.2|^8.0" + "php": "^7.4|^8.0" }, "conflict": { "phpstan/phpstan-shim": "*" @@ -4888,7 +4888,7 @@ "type": "github" } ], - "time": "2024-10-18T11:12:07+00:00" + "time": "2024-11-11T15:43:04+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index 0f8184d3db..06bedd4eba 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -1899,7 +1899,10 @@ class Receiver if (in_array(-1, $object_data['receiver'])) { $object_data['unlisted'] = true; unset($object_data['receiver'][-1]); - unset($object_data['reception_type'][-1]); + + if (array_key_exists(-1, $object_data['reception_type'])) { + unset($object_data['reception_type'][-1]); + } } return $object_data; From 3e523ddd0990f9b420195eed7c65a411d8500424 Mon Sep 17 00:00:00 2001 From: Artur Weigandt Date: Tue, 12 Nov 2024 22:03:09 +0100 Subject: [PATCH 5/7] Update src/Protocol/ActivityPub/Receiver.php Co-authored-by: Hypolite Petovan --- src/Protocol/ActivityPub/Receiver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index 06bedd4eba..8055ff6666 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -1890,7 +1890,7 @@ class Receiver $object_data['receiver_urls'] = self::getReceiverURL($object); $object_data['receiver'] = $receivers; $object_data['reception_type'] = $reception_types; - $object_data['unlisted'] = false; + $object_data['unlisted'] = false; if (!empty($object['pixelfed:capabilities'])) { $object_data['capabilities'] = self::getCapabilities($object); From ee2cdc7763411ff54b7f2f17a69662a2e7f19742 Mon Sep 17 00:00:00 2001 From: Art4 Date: Wed, 13 Nov 2024 07:49:29 +0000 Subject: [PATCH 6/7] replace method with anonymous function --- src/Module/Debug/ActivityPubConversion.php | 19 +++---- src/Module/Debug/Babel.php | 64 +++++++++++----------- 2 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/Module/Debug/ActivityPubConversion.php b/src/Module/Debug/ActivityPubConversion.php index 9b0465f016..da165174b8 100644 --- a/src/Module/Debug/ActivityPubConversion.php +++ b/src/Module/Debug/ActivityPubConversion.php @@ -25,6 +25,10 @@ class ActivityPubConversion extends BaseModule { $results = []; + $visible_whitespace = function (string $s): string { + return '
' . htmlspecialchars($s) . '
'; + }; + if (!empty($_REQUEST['source'])) { try { $source = json_decode($_REQUEST['source'], true); @@ -39,11 +43,11 @@ class ActivityPubConversion extends BaseModule $formatted = json_encode($source, JSON_PRETTY_PRINT); $results[] = [ 'title' => DI::l10n()->t('Formatted'), - 'content' => $this->visible_whitespace(trim(var_export($formatted, true), "'")), + 'content' => $visible_whitespace(trim(var_export($formatted, true), "'")), ]; $results[] = [ 'title' => DI::l10n()->t('Source'), - 'content' => $this->visible_whitespace(var_export($source, true)) + 'content' => $visible_whitespace(var_export($source, true)) ]; $activity = JsonLD::compact($source); if (!$activity) { @@ -51,7 +55,7 @@ class ActivityPubConversion extends BaseModule } $results[] = [ 'title' => DI::l10n()->t('Activity'), - 'content' => $this->visible_whitespace(var_export($activity, true)) + 'content' => $visible_whitespace(var_export($activity, true)) ]; $type = JsonLD::fetchElement($activity, '@type'); @@ -99,14 +103,14 @@ class ActivityPubConversion extends BaseModule $results[] = [ 'title' => DI::l10n()->t('Object data'), - 'content' => $this->visible_whitespace(var_export($object_data, true)) + 'content' => $visible_whitespace(var_export($object_data, true)) ]; $item = ActivityPub\Processor::createItem($object_data, true); $results[] = [ 'title' => DI::l10n()->t('Result Item'), - 'content' => $this->visible_whitespace(var_export($item, true)) + 'content' => $visible_whitespace(var_export($item, true)) ]; } catch (\Throwable $e) { $results[] = [ @@ -126,9 +130,4 @@ class ActivityPubConversion extends BaseModule return $o; } - - private function visible_whitespace(string $s): string - { - return '
' . htmlspecialchars($s) . '
'; - } } diff --git a/src/Module/Debug/Babel.php b/src/Module/Debug/Babel.php index 10b424850b..e7852847f6 100644 --- a/src/Module/Debug/Babel.php +++ b/src/Module/Debug/Babel.php @@ -30,6 +30,11 @@ class Babel extends BaseModule protected function content(array $request = []): string { $results = []; + + $visible_whitespace = function (string $s): string { + return '
' . htmlspecialchars($s) . '
'; + }; + if (!empty($request['text'])) { self::checkFormSecurityTokenForbiddenOnError('babel'); switch (($request['type'] ?? '') ?: 'bbcode') { @@ -37,24 +42,24 @@ class Babel extends BaseModule $bbcode = $request['text']; $results[] = [ 'title' => DI::l10n()->t('Source input'), - 'content' => $this->visible_whitespace($bbcode) + 'content' => $visible_whitespace($bbcode) ]; $plain = Text\BBCode::toPlaintext($bbcode, false); $results[] = [ 'title' => DI::l10n()->t('BBCode::toPlaintext'), - 'content' => $this->visible_whitespace($plain) + 'content' => $visible_whitespace($plain) ]; $html = Text\BBCode::convertForUriId(0, $bbcode); $results[] = [ 'title' => DI::l10n()->t('BBCode::convert (raw HTML)'), - 'content' => $this->visible_whitespace($html) + 'content' => $visible_whitespace($html) ]; $results[] = [ 'title' => DI::l10n()->t('BBCode::convert (hex)'), - 'content' => $this->visible_whitespace(bin2hex($html)), + 'content' => $visible_whitespace(bin2hex($html)), ]; $results[] = [ @@ -65,19 +70,19 @@ class Babel extends BaseModule $bbcode2 = Text\HTML::toBBCode($html); $results[] = [ 'title' => DI::l10n()->t('BBCode::convert => HTML::toBBCode'), - 'content' => $this->visible_whitespace($bbcode2) + 'content' => $visible_whitespace($bbcode2) ]; $markdown = Text\BBCode::toMarkdown($bbcode); $results[] = [ 'title' => DI::l10n()->t('BBCode::toMarkdown'), - 'content' => $this->visible_whitespace($markdown) + 'content' => $visible_whitespace($markdown) ]; $html2 = Text\Markdown::convert($markdown); $results[] = [ 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert (raw HTML)'), - 'content' => $this->visible_whitespace($html2) + 'content' => $visible_whitespace($html2) ]; $results[] = [ 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert'), @@ -87,13 +92,13 @@ class Babel extends BaseModule $bbcode3 = Text\Markdown::toBBCode($markdown); $results[] = [ 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::toBBCode'), - 'content' => $this->visible_whitespace($bbcode3) + 'content' => $visible_whitespace($bbcode3) ]; $bbcode4 = Text\HTML::toBBCode($html2); $results[] = [ 'title' => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert => HTML::toBBCode'), - 'content' => $this->visible_whitespace($bbcode4) + 'content' => $visible_whitespace($bbcode4) ]; $tags = Text\BBCode::getTags($bbcode); @@ -101,22 +106,22 @@ class Babel extends BaseModule $body = Item::setHashtags($bbcode); $results[] = [ 'title' => DI::l10n()->t('Item Body'), - 'content' => $this->visible_whitespace($body) + 'content' => $visible_whitespace($body) ]; $results[] = [ 'title' => DI::l10n()->t('Item Tags'), - 'content' => $this->visible_whitespace(var_export($tags, true)), + 'content' => $visible_whitespace(var_export($tags, true)), ]; $body2 = PageInfo::searchAndAppendToBody($bbcode, true); $results[] = [ 'title' => DI::l10n()->t('PageInfo::appendToBody'), - 'content' => $this->visible_whitespace($body2) + 'content' => $visible_whitespace($body2) ]; $html3 = Text\BBCode::convertForUriId(0, $body2); $results[] = [ 'title' => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert (raw HTML)'), - 'content' => $this->visible_whitespace($html3) + 'content' => $visible_whitespace($html3) ]; $results[] = [ 'title' => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert'), @@ -127,7 +132,7 @@ class Babel extends BaseModule $diaspora = trim($request['text']); $results[] = [ 'title' => DI::l10n()->t('Source input (Diaspora format)'), - 'content' => $this->visible_whitespace($diaspora), + 'content' => $visible_whitespace($diaspora), ]; $markdown = XML::unescape($diaspora); @@ -136,13 +141,13 @@ class Babel extends BaseModule $results[] = [ 'title' => DI::l10n()->t('Source input (Markdown)'), - 'content' => $this->visible_whitespace($markdown), + 'content' => $visible_whitespace($markdown), ]; $html = Text\Markdown::convert($markdown); $results[] = [ 'title' => DI::l10n()->t('Markdown::convert (raw HTML)'), - 'content' => $this->visible_whitespace($html), + 'content' => $visible_whitespace($html), ]; $results[] = [ @@ -153,14 +158,14 @@ class Babel extends BaseModule $bbcode = Text\Markdown::toBBCode($markdown); $results[] = [ 'title' => DI::l10n()->t('Markdown::toBBCode'), - 'content' => $this->visible_whitespace($bbcode), + 'content' => $visible_whitespace($bbcode), ]; break; case 'html' : $html = trim($request['text']); $results[] = [ 'title' => DI::l10n()->t('Raw HTML input'), - 'content' => $this->visible_whitespace($html), + 'content' => $visible_whitespace($html), ]; $results[] = [ @@ -172,12 +177,12 @@ class Babel extends BaseModule $results[] = [ 'title' => DI::l10n()->t('HTML Purified (raw)'), - 'content' => $this->visible_whitespace($purified), + 'content' => $visible_whitespace($purified), ]; $results[] = [ 'title' => DI::l10n()->t('HTML Purified (hex)'), - 'content' => $this->visible_whitespace(bin2hex($purified)), + 'content' => $visible_whitespace(bin2hex($purified)), ]; $results[] = [ @@ -188,7 +193,7 @@ class Babel extends BaseModule $bbcode = Text\HTML::toBBCode($html); $results[] = [ 'title' => DI::l10n()->t('HTML::toBBCode'), - 'content' => $this->visible_whitespace($bbcode) + 'content' => $visible_whitespace($bbcode) ]; $html2 = Text\BBCode::convertForUriId(0, $bbcode); @@ -205,25 +210,25 @@ class Babel extends BaseModule $bbcode2plain = Text\BBCode::toPlaintext($bbcode); $results[] = [ 'title' => DI::l10n()->t('HTML::toBBCode => BBCode::toPlaintext'), - 'content' => $this->visible_whitespace($bbcode2plain), + 'content' => $visible_whitespace($bbcode2plain), ]; $markdown = Text\HTML::toMarkdown($html); $results[] = [ 'title' => DI::l10n()->t('HTML::toMarkdown'), - 'content' => $this->visible_whitespace($markdown) + 'content' => $visible_whitespace($markdown) ]; $text = Text\HTML::toPlaintext($html, 0); $results[] = [ 'title' => DI::l10n()->t('HTML::toPlaintext'), - 'content' => $this->visible_whitespace($text), + 'content' => $visible_whitespace($text), ]; $text = Text\HTML::toPlaintext($html, 0, true); $results[] = [ 'title' => DI::l10n()->t('HTML::toPlaintext (compact)'), - 'content' => $this->visible_whitespace($text), + 'content' => $visible_whitespace($text), ]; break; case 'twitter': @@ -236,7 +241,7 @@ class Babel extends BaseModule $results[] = [ 'title' => DI::l10n()->t('Decoded post'), - 'content' => $this->visible_whitespace(var_export($status, true)), + 'content' => $visible_whitespace(var_export($status, true)), ]; $postarray = []; @@ -255,7 +260,7 @@ class Babel extends BaseModule $results[] = [ 'title' => DI::l10n()->t('Post array before expand entities'), - 'content' => $this->visible_whitespace(var_export($postarray, true)), + 'content' => $visible_whitespace(var_export($postarray, true)), ]; } else { $results[] = [ @@ -285,9 +290,4 @@ class Babel extends BaseModule return $o; } - - private function visible_whitespace($s): string - { - return '
' . htmlspecialchars($s) . '
'; - } } From 2ef9982f63f80c32248f54a84b9b22f269fd9023 Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 14 Nov 2024 07:32:25 +0000 Subject: [PATCH 7/7] refactor Receiver::processObject() method --- src/Protocol/ActivityPub/Receiver.php | 34 +++++++++++++-------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/Protocol/ActivityPub/Receiver.php b/src/Protocol/ActivityPub/Receiver.php index 8055ff6666..7512e6179c 100644 --- a/src/Protocol/ActivityPub/Receiver.php +++ b/src/Protocol/ActivityPub/Receiver.php @@ -1880,31 +1880,29 @@ class Receiver $object_data = self::getObjectDataFromActivity($object); - $receiverdata = self::getReceivers($object, $actor ?: $object_data['actor'] ?? '', $object_data['tags'], true, false); - $receivers = $reception_types = []; - foreach ($receiverdata as $key => $data) { - $receivers[$key] = $data['uid']; - $reception_types[$data['uid']] = $data['type'] ?? 0; - } - $object_data['receiver_urls'] = self::getReceiverURL($object); - $object_data['receiver'] = $receivers; - $object_data['reception_type'] = $reception_types; + $object_data['receiver'] = []; + $object_data['reception_type'] = []; $object_data['unlisted'] = false; + $receiverdata = self::getReceivers($object, $actor ?: $object_data['actor'] ?? '', $object_data['tags'], true, false); + + foreach ($receiverdata as $key => $data) { + if ($data['uid'] !== -1) { + $object_data['reception_type'][$data['uid']] = $data['type'] ?? 0; + } + + if ($key !== -1) { + $object_data['receiver'][$key] = $data['uid']; + } else { + $object_data['unlisted'] = true; + } + } + if (!empty($object['pixelfed:capabilities'])) { $object_data['capabilities'] = self::getCapabilities($object); } - if (in_array(-1, $object_data['receiver'])) { - $object_data['unlisted'] = true; - unset($object_data['receiver'][-1]); - - if (array_key_exists(-1, $object_data['reception_type'])) { - unset($object_data['reception_type'][-1]); - } - } - return $object_data; }