From 97585083d98b3e859ddf553470a4fd4d476bc97a Mon Sep 17 00:00:00 2001 From: Hank Grabowski Date: Mon, 20 Mar 2023 17:42:08 -0400 Subject: [PATCH 1/2] Add visibility to user's statuses in Mastodon API - and remove view count object if not user's status --- src/Factory/Api/Mastodon/Status.php | 10 ++-- .../Mastodon/Status/FriendicaExtension.php | 15 ++++-- .../Mastodon/Status/FriendicaVisibility.php | 51 +++++++++++++++++++ 3 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 src/Object/Api/Mastodon/Status/FriendicaVisibility.php diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index 190f1e0e21..416321c248 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -35,6 +35,7 @@ use Friendica\Model\Verb; use Friendica\Network\HTTPException; use Friendica\Object\Api\Mastodon\Status\FriendicaDeliveryData; use Friendica\Object\Api\Mastodon\Status\FriendicaExtension; +use Friendica\Object\Api\Mastodon\Status\FriendicaVisibility; use Friendica\Protocol\Activity; use Friendica\Protocol\ActivityPub; use ImagickException; @@ -100,7 +101,7 @@ class Status extends BaseFactory { $fields = ['uri-id', 'uid', 'author-id', 'causer-id', 'author-uri-id', 'author-link', 'causer-uri-id', 'post-reason', 'starred', 'app', 'title', 'body', 'raw-body', 'content-warning', 'question-id', 'created', 'edited', 'commented', 'received', 'changed', 'network', 'thr-parent-id', 'parent-author-id', 'language', 'uri', 'plink', 'private', 'vid', 'gravity', 'featured', 'has-media', 'quote-uri-id', - 'delivery_queue_count', 'delivery_queue_done','delivery_queue_failed']; + 'delivery_queue_count', 'delivery_queue_done','delivery_queue_failed', 'allow_cid', 'deny_cid', 'allow_gid', 'deny_gid']; $item = Post::selectFirst($fields, ['uri-id' => $uriId, 'uid' => [0, $uid]], ['order' => ['uid' => true]]); if (!$item) { $mail = DBA::selectFirst('mail', ['id'], ['uri-id' => $uriId, 'uid' => $uid]); @@ -290,8 +291,9 @@ class Status extends BaseFactory $in_reply = []; } - $delivery_data = new FriendicaDeliveryData($item['delivery_queue_count'], $item['delivery_queue_done'], $item['delivery_queue_failed']); - $friendica = new FriendicaExtension($item['title'], $item['changed'], $item['commented'], $item['received'], $counts->dislikes, $delivery_data); + $delivery_data = $uid != $item['uid'] ? null : new FriendicaDeliveryData($item['delivery_queue_count'], $item['delivery_queue_done'], $item['delivery_queue_failed']); + $visibility_data = $uid != $item['uid'] ? null : new FriendicaVisibility($item['allow_cid'], $item['deny_cid'], $item['allow_gid'], $item['deny_gid']); + $friendica = new FriendicaExtension($item['title'], $item['changed'], $item['commented'], $item['received'], $counts->dislikes, $delivery_data, $visibility_data); return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $in_reply, $reshare, $friendica, $quote, $poll); } @@ -357,7 +359,7 @@ class Status extends BaseFactory $attachments = []; $in_reply = []; $reshare = []; - $friendica = new FriendicaExtension('', null, null, null, 0, new FriendicaDeliveryData(0, 0, 0)); + $friendica = new FriendicaExtension('', null, null, null, 0, null, null); return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $in_reply, $reshare, $friendica); } diff --git a/src/Object/Api/Mastodon/Status/FriendicaExtension.php b/src/Object/Api/Mastodon/Status/FriendicaExtension.php index 5702c0678d..be49fe3c55 100644 --- a/src/Object/Api/Mastodon/Status/FriendicaExtension.php +++ b/src/Object/Api/Mastodon/Status/FriendicaExtension.php @@ -45,14 +45,18 @@ class FriendicaExtension extends BaseDataTransferObject /** @var string|null (Datetime) */ protected $received_at; - /** @var FriendicaDeliveryData */ + /** @var FriendicaDeliveryData|null */ protected $delivery_data; /** @var int */ protected $dislikes_count; + /** + * @var FriendicaVisibility|null + */ + protected $visibility; /** - * Creates a status count object + * Creates a FriendicaExtension object * * @param string $title * @param string|null $changed_at @@ -60,7 +64,8 @@ class FriendicaExtension extends BaseDataTransferObject * @param string|null $edited_at * @param string|null $received_at * @param int $dislikes_count - * @param FriendicaDeliveryData $delivery_data + * @param FriendicaDeliveryData|null $delivery_data + * @param FriendicaVisibility|null $visibility */ public function __construct( string $title, @@ -68,7 +73,8 @@ class FriendicaExtension extends BaseDataTransferObject ?string $commented_at, ?string $received_at, int $dislikes_count, - FriendicaDeliveryData $delivery_data + ?FriendicaDeliveryData $delivery_data, + ?FriendicaVisibility $visibility ) { $this->title = $title; $this->changed_at = $changed_at ? DateTimeFormat::utc($changed_at, DateTimeFormat::JSON) : null; @@ -76,6 +82,7 @@ class FriendicaExtension extends BaseDataTransferObject $this->received_at = $received_at ? DateTimeFormat::utc($received_at, DateTimeFormat::JSON) : null; $this->delivery_data = $delivery_data; $this->dislikes_count = $dislikes_count; + $this->visibility = $visibility; } /** diff --git a/src/Object/Api/Mastodon/Status/FriendicaVisibility.php b/src/Object/Api/Mastodon/Status/FriendicaVisibility.php new file mode 100644 index 0000000000..2eeaf2ecd4 --- /dev/null +++ b/src/Object/Api/Mastodon/Status/FriendicaVisibility.php @@ -0,0 +1,51 @@ +. + * + */ + +namespace Friendica\Object\Api\Mastodon\Status; + +use Friendica\BaseDataTransferObject; + +/** + * Class FriendicaVisibility + * + * Fields for the user's visibility settings on a post if they own that post + * + * @see https://docs.joinmastodon.org/entities/status + */ +class FriendicaVisibility extends BaseDataTransferObject +{ + /** @var string|null */ + protected $allow_cid; + /** @var string|null */ + protected $deny_cid; + /** @var string|null */ + protected $allow_gid; + /** @var string|null */ + protected $deny_gid; + + public function __construct(?string $allow_cid, ?string $deny_cid, ?string $allow_gid, ?string $deny_gid) + { + $this->allow_cid = $allow_cid; + $this->deny_cid = $deny_cid; + $this->allow_gid = $allow_gid; + $this->deny_gid = $deny_gid; + } +} From 60f2c67b66511da599c7764ed6a7186ed9ab2b43 Mon Sep 17 00:00:00 2001 From: Hank Grabowski Date: Tue, 21 Mar 2023 13:15:42 -0400 Subject: [PATCH 2/2] Change visibility data on Mastodon posts to be array of IDs --- src/Factory/Api/Mastodon/Status.php | 4 +++- src/Object/Api/Mastodon/Status/FriendicaVisibility.php | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Factory/Api/Mastodon/Status.php b/src/Factory/Api/Mastodon/Status.php index 416321c248..62941978ec 100644 --- a/src/Factory/Api/Mastodon/Status.php +++ b/src/Factory/Api/Mastodon/Status.php @@ -28,6 +28,7 @@ use Friendica\Content\Text\BBCode; use Friendica\Core\Logger; use Friendica\Database\Database; use Friendica\Database\DBA; +use Friendica\DI; use Friendica\Model\Item; use Friendica\Model\Post; use Friendica\Model\Tag as TagModel; @@ -291,8 +292,9 @@ class Status extends BaseFactory $in_reply = []; } + $aclFormatter = DI::aclFormatter(); $delivery_data = $uid != $item['uid'] ? null : new FriendicaDeliveryData($item['delivery_queue_count'], $item['delivery_queue_done'], $item['delivery_queue_failed']); - $visibility_data = $uid != $item['uid'] ? null : new FriendicaVisibility($item['allow_cid'], $item['deny_cid'], $item['allow_gid'], $item['deny_gid']); + $visibility_data = $uid != $item['uid'] ? null : new FriendicaVisibility($aclFormatter->expand($item['allow_cid']), $aclFormatter->expand($item['deny_cid']), $aclFormatter->expand($item['allow_gid']), $aclFormatter->expand($item['deny_gid'])); $friendica = new FriendicaExtension($item['title'], $item['changed'], $item['commented'], $item['received'], $counts->dislikes, $delivery_data, $visibility_data); return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $in_reply, $reshare, $friendica, $quote, $poll); diff --git a/src/Object/Api/Mastodon/Status/FriendicaVisibility.php b/src/Object/Api/Mastodon/Status/FriendicaVisibility.php index 2eeaf2ecd4..6ded2bfc36 100644 --- a/src/Object/Api/Mastodon/Status/FriendicaVisibility.php +++ b/src/Object/Api/Mastodon/Status/FriendicaVisibility.php @@ -32,16 +32,16 @@ use Friendica\BaseDataTransferObject; */ class FriendicaVisibility extends BaseDataTransferObject { - /** @var string|null */ + /** @var array */ protected $allow_cid; - /** @var string|null */ + /** @var array */ protected $deny_cid; - /** @var string|null */ + /** @var array */ protected $allow_gid; - /** @var string|null */ + /** @var array */ protected $deny_gid; - public function __construct(?string $allow_cid, ?string $deny_cid, ?string $allow_gid, ?string $deny_gid) + public function __construct(array $allow_cid, array $deny_cid, array $allow_gid, array $deny_gid) { $this->allow_cid = $allow_cid; $this->deny_cid = $deny_cid;