From e2266cbc59ed25d7c116a804719aaea0e67e5c9c Mon Sep 17 00:00:00 2001 From: David Sevilla Martin <6401250+dsevillamartin@users.noreply.github.com> Date: Sat, 13 Jul 2024 11:00:06 -0400 Subject: [PATCH] Check whether `->state` is null before checking its user id --- src/AddTagSubscriptionAttribute.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/AddTagSubscriptionAttribute.php b/src/AddTagSubscriptionAttribute.php index 438f275..3f95270 100644 --- a/src/AddTagSubscriptionAttribute.php +++ b/src/AddTagSubscriptionAttribute.php @@ -38,7 +38,10 @@ public function __invoke(TagSerializer $serializer, Tag $tag, array $attributes) */ public function getStateFor(Tag $tag, User $actor): TagState { - if (!$tag->relationLoaded('state') || $tag->state->user_id !== $actor->id) { + // If $tag->state is loaded *and* null, this might be because the actor doesn't have tag-specific state + // OR because the wrong actor has been used for loading it. `$tag->stateFor()` will return the correct state. + // If it doesn't exist, it returns a dummy state with the correct actor & tag IDs. + if (!$tag->relationLoaded('state') || is_null($tag->state) || $tag->state->user_id !== $actor->id) { $tag->setRelation('state', $tag->stateFor($actor)); }