ProfileTransformer.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Transformer\ActivityPub;
  3. use App\Profile;
  4. use App\Services\AccountService;
  5. use League\Fractal;
  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. 'suspended' => 'toot:suspended',
  27. ],
  28. ],
  29. 'id' => $profile->permalink(),
  30. 'type' => 'Person',
  31. 'following' => $profile->permalink('/following'),
  32. 'followers' => $profile->permalink('/followers'),
  33. 'inbox' => $profile->permalink('/inbox'),
  34. 'outbox' => $profile->permalink('/outbox'),
  35. 'preferredUsername' => $profile->username,
  36. 'name' => $profile->name,
  37. 'summary' => $profile->bio,
  38. 'url' => $profile->url(),
  39. 'manuallyApprovesFollowers' => (bool) $profile->is_private,
  40. 'indexable' => (bool) $profile->indexable,
  41. 'published' => $profile->created_at->format('Y-m-d').'T00:00:00Z',
  42. 'publicKey' => [
  43. 'id' => $profile->permalink().'#main-key',
  44. 'owner' => $profile->permalink(),
  45. 'publicKeyPem' => $profile->public_key,
  46. ],
  47. 'icon' => [
  48. 'type' => 'Image',
  49. 'mediaType' => 'image/jpeg',
  50. 'url' => $profile->avatarUrl(),
  51. ],
  52. 'endpoints' => [
  53. 'sharedInbox' => config('app.url').'/f/inbox',
  54. ],
  55. ];
  56. if ($profile->status === 'delete' || $profile->deleted_at != null) {
  57. $res['suspended'] = true;
  58. $res['name'] = '';
  59. unset($res['icon']);
  60. $res['summary'] = '';
  61. $res['indexable'] = false;
  62. $res['manuallyApprovesFollowers'] = false;
  63. } else {
  64. if ($profile->aliases->count()) {
  65. $res['alsoKnownAs'] = $profile->aliases->map(fn ($alias) => $alias->uri);
  66. }
  67. if ($profile->moved_to_profile_id) {
  68. $movedTo = AccountService::get($profile->moved_to_profile_id);
  69. if ($movedTo && isset($movedTo['url'], $movedTo['id'])) {
  70. $res['movedTo'] = $movedTo['url'];
  71. }
  72. }
  73. }
  74. return $res;
  75. }
  76. }