ProfileTransformer.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Transformer\ActivityPub;
  3. use App\Profile;
  4. use League\Fractal;
  5. use App\Services\AccountService;
  6. class ProfileTransformer extends Fractal\TransformerAbstract
  7. {
  8. public function transform(Profile $profile)
  9. {
  10. $res = [
  11. '@context' => [
  12. 'https://w3id.org/security/v1',
  13. 'https://www.w3.org/ns/activitystreams',
  14. [
  15. 'toot' => 'http://joinmastodon.org/ns#',
  16. 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
  17. 'alsoKnownAs' => [
  18. '@id' => 'as:alsoKnownAs',
  19. '@type' => '@id'
  20. ],
  21. 'movedTo' => [
  22. '@id' => 'as:movedTo',
  23. '@type' => '@id'
  24. ],
  25. 'indexable' => 'toot:indexable',
  26. ],
  27. ],
  28. 'id' => $profile->permalink(),
  29. 'type' => 'Person',
  30. 'following' => $profile->permalink('/following'),
  31. 'followers' => $profile->permalink('/followers'),
  32. 'inbox' => $profile->permalink('/inbox'),
  33. 'outbox' => $profile->permalink('/outbox'),
  34. 'preferredUsername' => $profile->username,
  35. 'name' => $profile->name,
  36. 'summary' => $profile->bio,
  37. 'url' => $profile->url(),
  38. 'manuallyApprovesFollowers' => (bool) $profile->is_private,
  39. 'indexable' => (bool) $profile->indexable,
  40. 'published' => $profile->created_at->format('Y-m-d') . 'T00:00:00Z',
  41. 'publicKey' => [
  42. 'id' => $profile->permalink().'#main-key',
  43. 'owner' => $profile->permalink(),
  44. 'publicKeyPem' => $profile->public_key,
  45. ],
  46. 'icon' => [
  47. 'type' => 'Image',
  48. 'mediaType' => 'image/jpeg',
  49. 'url' => $profile->avatarUrl(),
  50. ],
  51. 'endpoints' => [
  52. 'sharedInbox' => config('app.url') . '/f/inbox'
  53. ]
  54. ];
  55. if($profile->aliases->count()) {
  56. $res['alsoKnownAs'] = $profile->aliases->map(fn($alias) => $alias->uri);
  57. }
  58. if($profile->moved_to_profile_id) {
  59. $res['movedTo'] = AccountService::get($profile->moved_to_profile_id)['url'];
  60. }
  61. return $res;
  62. }
  63. }