Extract hasRestrictions() into ItemInserter

This commit is contained in:
Art4 2025-01-23 09:50:27 +00:00
commit 0e054cd4a0
2 changed files with 31 additions and 30 deletions

View file

@ -757,7 +757,7 @@ class Item
* @return array item array with parent data * @return array item array with parent data
* @throws \Exception * @throws \Exception
*/ */
private static function getTopLevelParent(array $item): array private static function getTopLevelParent(array $item, ItemInserter $itemInserter): array
{ {
$fields = [ $fields = [
'uid', 'uri', 'parent-uri', 'id', 'deleted', 'uid', 'uri', 'parent-uri', 'id', 'deleted',
@ -785,7 +785,7 @@ class Item
return []; 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']]); 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 []; return [];
} }
@ -916,7 +916,7 @@ class Item
} }
if ($item['gravity'] !== self::GRAVITY_PARENT) { if ($item['gravity'] !== self::GRAVITY_PARENT) {
$toplevel_parent = self::getTopLevelParent($item); $toplevel_parent = self::getTopLevelParent($item, $itemInserter);
if (empty($toplevel_parent)) { if (empty($toplevel_parent)) {
return 0; return 0;
} }
@ -1390,33 +1390,6 @@ class Item
return $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) private static function reshareChannelPost(int $uri_id, int $reshare_id = 0)
{ {
if (!DI::config()->get('system', 'allow_relay_channels')) { if (!DI::config()->get('system', 'allow_relay_channels')) {

View file

@ -9,6 +9,7 @@ namespace Friendica\Model;
use Friendica\App\BaseURL; use Friendica\App\BaseURL;
use Friendica\Content\Item as ItemContent; use Friendica\Content\Item as ItemContent;
use Friendica\Core\Protocol;
use Friendica\Protocol\Activity; use Friendica\Protocol\Activity;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -147,6 +148,33 @@ final class ItemInserter
return $item; 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 * Get the gravity for the given item array
* *