FederationController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\InboxPipeline\{
  4. InboxWorker,
  5. InboxValidator
  6. };
  7. use App\Jobs\RemoteFollowPipeline\RemoteFollowPipeline;
  8. use App\{
  9. AccountLog,
  10. Like,
  11. Profile,
  12. Status,
  13. User
  14. };
  15. use App\Util\Lexer\Nickname;
  16. use App\Util\Webfinger\Webfinger;
  17. use Auth;
  18. use Cache;
  19. use Carbon\Carbon;
  20. use Illuminate\Http\Request;
  21. use League\Fractal;
  22. use App\Util\Site\Nodeinfo;
  23. use App\Util\ActivityPub\{
  24. Helpers,
  25. HttpSignature,
  26. Outbox
  27. };
  28. use Zttp\Zttp;
  29. class FederationController extends Controller
  30. {
  31. public function nodeinfoWellKnown()
  32. {
  33. abort_if(!config('federation.nodeinfo.enabled'), 404);
  34. return response()->json(Nodeinfo::wellKnown())
  35. ->header('Access-Control-Allow-Origin','*');
  36. }
  37. public function nodeinfo()
  38. {
  39. abort_if(!config('federation.nodeinfo.enabled'), 404);
  40. return response()->json(Nodeinfo::get())
  41. ->header('Access-Control-Allow-Origin','*');
  42. }
  43. public function webfinger(Request $request)
  44. {
  45. abort_if(!config('federation.webfinger.enabled'), 400);
  46. abort_if(!$request->filled('resource'), 400);
  47. $resource = $request->input('resource');
  48. $parsed = Nickname::normalizeProfileUrl($resource);
  49. if($parsed['domain'] !== config('pixelfed.domain.app')) {
  50. abort(400);
  51. }
  52. $username = $parsed['username'];
  53. $profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  54. if($profile->status != null) {
  55. return ProfileController::accountCheck($profile);
  56. }
  57. $webfinger = (new Webfinger($profile))->generate();
  58. return response()->json($webfinger, 200, [], JSON_PRETTY_PRINT)
  59. ->header('Access-Control-Allow-Origin','*');
  60. }
  61. public function hostMeta(Request $request)
  62. {
  63. abort_if(!config('federation.webfinger.enabled'), 404);
  64. $path = route('well-known.webfinger');
  65. $xml = '<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"><Link rel="lrdd" type="application/xrd+xml" template="'.$path.'?resource={uri}"/></XRD>';
  66. return response($xml)->header('Content-Type', 'application/xrd+xml');
  67. }
  68. public function userOutbox(Request $request, $username)
  69. {
  70. abort_if(!config('federation.activitypub.enabled'), 404);
  71. abort_if(!config('federation.activitypub.outbox'), 404);
  72. $profile = Profile::whereNull('domain')
  73. ->whereNull('status')
  74. ->whereIsPrivate(false)
  75. ->whereUsername($username)
  76. ->firstOrFail();
  77. $key = 'ap:outbox:latest_10:pid:' . $profile->id;
  78. $ttl = now()->addMinutes(15);
  79. $res = Cache::remember($key, $ttl, function() use($profile) {
  80. return Outbox::get($profile);
  81. });
  82. return response(json_encode($res, JSON_UNESCAPED_SLASHES))->header('Content-Type', 'application/activity+json');
  83. }
  84. public function userInbox(Request $request, $username)
  85. {
  86. abort_if(!config('federation.activitypub.enabled'), 404);
  87. abort_if(!config('federation.activitypub.inbox'), 404);
  88. $headers = $request->headers->all();
  89. $payload = $request->getContent();
  90. dispatch(new InboxValidator($username, $headers, $payload))->onQueue('high');
  91. return;
  92. }
  93. public function userFollowing(Request $request, $username)
  94. {
  95. abort_if(!config('federation.activitypub.enabled'), 404);
  96. $profile = Profile::whereNull('remote_url')
  97. ->whereUsername($username)
  98. ->whereIsPrivate(false)
  99. ->firstOrFail();
  100. if($profile->status != null) {
  101. abort(404);
  102. }
  103. $obj = [
  104. '@context' => 'https://www.w3.org/ns/activitystreams',
  105. 'id' => $request->getUri(),
  106. 'type' => 'OrderedCollectionPage',
  107. 'totalItems' => 0,
  108. 'orderedItems' => []
  109. ];
  110. return response()->json($obj);
  111. }
  112. public function userFollowers(Request $request, $username)
  113. {
  114. abort_if(!config('federation.activitypub.enabled'), 404);
  115. $profile = Profile::whereNull('remote_url')
  116. ->whereUsername($username)
  117. ->whereIsPrivate(false)
  118. ->firstOrFail();
  119. if($profile->status != null) {
  120. abort(404);
  121. }
  122. $obj = [
  123. '@context' => 'https://www.w3.org/ns/activitystreams',
  124. 'id' => $request->getUri(),
  125. 'type' => 'OrderedCollectionPage',
  126. 'totalItems' => 0,
  127. 'orderedItems' => []
  128. ];
  129. return response()->json($obj);
  130. }
  131. }