RelationshipTransformer.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Transformer\Api;
  3. use App\FollowRequest;
  4. use App\Models\UserDomainBlock;
  5. use App\Profile;
  6. use Auth;
  7. use League\Fractal;
  8. class RelationshipTransformer extends Fractal\TransformerAbstract
  9. {
  10. public function transform(Profile $profile)
  11. {
  12. $auth = Auth::check();
  13. if (! $auth) {
  14. return [];
  15. }
  16. $user = $auth ? Auth::user()->profile : false;
  17. $requested = false;
  18. $domainBlocking = false;
  19. if ($user) {
  20. $requested = FollowRequest::whereFollowerId($user->id)
  21. ->whereFollowingId($profile->id)
  22. ->exists();
  23. if ($profile->domain) {
  24. $domainBlocking = UserDomainBlock::whereProfileId($user->id)
  25. ->whereDomain($profile->domain)
  26. ->exists();
  27. }
  28. }
  29. return [
  30. 'id' => (string) $profile->id,
  31. 'following' => $auth ? $user->follows($profile) : false,
  32. 'followed_by' => $auth ? $user->followedBy($profile) : false,
  33. 'blocking' => $auth ? $user->blockedIds()->contains($profile->id) : false,
  34. 'muting' => $auth ? $user->mutedIds()->contains($profile->id) : false,
  35. 'muting_notifications' => false,
  36. 'requested' => $requested,
  37. 'domain_blocking' => $domainBlocking,
  38. 'showing_reblogs' => false,
  39. 'endorsed' => false,
  40. ];
  41. }
  42. }