Skip to content

Commit

Permalink
Retrieve the *correct* tag state for the right user (#73)
Browse files Browse the repository at this point in the history
* Retrieve the *correct* tag state for the right user

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
dsevillamartin and StyleCIBot authored Jul 12, 2024
1 parent f054a0c commit f11d7e1
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/AddTagSubscriptionAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,34 @@
use Flarum\Tags\Api\Serializer\TagSerializer;
use Flarum\Tags\Tag;
use Flarum\Tags\TagState;
use Flarum\User\User;

class AddTagSubscriptionAttribute
{
public function __invoke(TagSerializer $serializer, Tag $tag, array $attributes): array
{
/** @var TagState $state */
$state = $tag->state;
$state = $this->getStateFor($tag, $serializer->getActor());

$attributes['subscription'] = $state->subscription ?? null;

return $attributes;
}

/**
* Get the *correct* state for the tag based on the actor.
* If a state hasn't been already loaded OR the loaded state is not for the actor, load the correct state.
*
* @param Tag $tag
* @param User $actor
*
* @return TagState
*/
public function getStateFor(Tag $tag, User $actor): TagState
{
if (!$tag->relationLoaded('state') || $tag->state->user_id !== $actor->id) {
$tag->setRelation('state', $tag->stateFor($actor));
}

return $tag->state;
}
}

0 comments on commit f11d7e1

Please sign in to comment.