From 4069731a22c8bb2a58bd71e4d4b575895dd02595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ca=C5=82ka?= Date: Sat, 27 Jul 2024 20:31:28 +0200 Subject: [PATCH 1/3] fix: load tag state for correct user Co-authored-by: SychO9 --- extensions/tags/src/TagRepository.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/extensions/tags/src/TagRepository.php b/extensions/tags/src/TagRepository.php index 8ea1b683b1..c1f08571c8 100644 --- a/extensions/tags/src/TagRepository.php +++ b/extensions/tags/src/TagRepository.php @@ -57,7 +57,13 @@ public function getAuthorizedRelations($relations, User $actor): array $query->whereVisibleTo($actor); }; } else { - $relationsArray[] = $relation; + if ($relation === 'state') { + $relationsArray['state'] = function ($query) use ($actor) { + $query->where('user_id', $actor->id); + }; + } else { + $relationsArray[] = $relation; + } } } From 50dba271d6f9d9007ff9b381c8740b450fb67fac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ca=C5=82ka?= Date: Sat, 27 Jul 2024 21:51:11 +0200 Subject: [PATCH 2/3] fix: prevent loading state if loaded previously Co-authored-by: SychO9 --- extensions/tags/src/Tag.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/extensions/tags/src/Tag.php b/extensions/tags/src/Tag.php index 2bcbab5c15..24b5f9e988 100644 --- a/extensions/tags/src/Tag.php +++ b/extensions/tags/src/Tag.php @@ -163,9 +163,18 @@ public function state() * @param User $user * @return TagState */ - public function stateFor(User $user) + public function stateFor(User $user): TagState { - $state = $this->state()->where('user_id', $user->id)->first(); + // Use the loaded state if the relation is loaded, and either: + // 1. The state is null, or + // 2. The state belongs to the given user. + // This ensures that if a non-null state is loaded, it belongs to the correct user. + // If these conditions are not met, we query the database for the user's state. + if ($this->relationLoaded('state') && (! $this->state || $this->state->user_id === $user->id)) { + $state = $this->state; + } else { + $state = $this->state()->where('user_id', $user->id)->first(); + } if (! $state) { $state = new TagState; From 247b74e664289cb3af198f8168bfc0d66b74148a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ca=C5=82ka?= Date: Sat, 27 Jul 2024 22:09:25 +0200 Subject: [PATCH 3/3] fix(PHPStan): TagState can be null --- extensions/tags/src/Tag.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/tags/src/Tag.php b/extensions/tags/src/Tag.php index 24b5f9e988..3b80356ecd 100644 --- a/extensions/tags/src/Tag.php +++ b/extensions/tags/src/Tag.php @@ -36,7 +36,7 @@ * @property int $last_posted_user_id * @property string $icon * - * @property TagState $state + * @property TagState|null $state * @property Tag|null $parent * @property-read Collection $children * @property-read Collection $discussions