From 0e054cd4a0820d4a68d7305d2132c4187f4900ad Mon Sep 17 00:00:00 2001 From: Art4 Date: Thu, 23 Jan 2025 09:50:27 +0000 Subject: [PATCH] Extract hasRestrictions() into ItemInserter --- src/Model/Item.php | 33 +++------------------------------ src/Model/ItemInserter.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index f9ab920bf0..556a8b4944 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -757,7 +757,7 @@ class Item * @return array item array with parent data * @throws \Exception */ - private static function getTopLevelParent(array $item): array + private static function getTopLevelParent(array $item, ItemInserter $itemInserter): array { $fields = [ 'uid', 'uri', 'parent-uri', 'id', 'deleted', @@ -785,7 +785,7 @@ class Item return []; } - if (self::hasRestrictions($item, $parent['author-id'], $parent['restrictions'])) { + if ($itemInserter->hasRestrictions($item, $parent['author-id'], $parent['restrictions'])) { DI::logger()->notice('Restrictions apply - ignoring item', ['restrictions' => $parent['restrictions'], 'verb' => $parent['verb'], 'uri-id' => $item['uri-id'], 'thr-parent-id' => $item['thr-parent-id'], 'uid' => $item['uid']]); return []; } @@ -916,7 +916,7 @@ class Item } if ($item['gravity'] !== self::GRAVITY_PARENT) { - $toplevel_parent = self::getTopLevelParent($item); + $toplevel_parent = self::getTopLevelParent($item, $itemInserter); if (empty($toplevel_parent)) { return 0; } @@ -1390,33 +1390,6 @@ class Item return $item; } - private static function hasRestrictions(array $item, int $author_id, int $restrictions = null): bool - { - if (empty($restrictions) || ($author_id == $item['author-id'])) { - return false; - } - - // We only have to apply restrictions if the post originates from our server or is federated. - // Every other time we can trust the remote system. - if (!in_array($item['network'], Protocol::FEDERATED) && !$item['origin']) { - return false; - } - - if (($restrictions & self::CANT_REPLY) && ($item['verb'] == Activity::POST)) { - return true; - } - - if (($restrictions & self::CANT_ANNOUNCE) && ($item['verb'] == Activity::ANNOUNCE)) { - return true; - } - - if (($restrictions & self::CANT_LIKE) && in_array($item['verb'], [Activity::LIKE, Activity::DISLIKE, Activity::ATTEND, Activity::ATTENDMAYBE, Activity::ATTENDNO])) { - return true; - } - - return false; - } - private static function reshareChannelPost(int $uri_id, int $reshare_id = 0) { if (!DI::config()->get('system', 'allow_relay_channels')) { diff --git a/src/Model/ItemInserter.php b/src/Model/ItemInserter.php index 77e58ee154..85b33e6dfc 100644 --- a/src/Model/ItemInserter.php +++ b/src/Model/ItemInserter.php @@ -9,6 +9,7 @@ namespace Friendica\Model; use Friendica\App\BaseURL; use Friendica\Content\Item as ItemContent; +use Friendica\Core\Protocol; use Friendica\Protocol\Activity; use Friendica\Util\DateTimeFormat; use Psr\Log\LoggerInterface; @@ -147,6 +148,33 @@ final class ItemInserter return $item; } + public function hasRestrictions(array $item, int $author_id, int $restrictions = null): bool + { + if (empty($restrictions) || ($author_id == $item['author-id'])) { + return false; + } + + // We only have to apply restrictions if the post originates from our server or is federated. + // Every other time we can trust the remote system. + if (!in_array($item['network'], Protocol::FEDERATED) && !$item['origin']) { + return false; + } + + if (($restrictions & Item::CANT_REPLY) && ($item['verb'] == Activity::POST)) { + return true; + } + + if (($restrictions & Item::CANT_ANNOUNCE) && ($item['verb'] == Activity::ANNOUNCE)) { + return true; + } + + if (($restrictions & Item::CANT_LIKE) && in_array($item['verb'], [Activity::LIKE, Activity::DISLIKE, Activity::ATTEND, Activity::ATTENDMAYBE, Activity::ATTENDNO])) { + return true; + } + + return false; + } + /** * Get the gravity for the given item array * -- 2.39.5