FederationController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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(empty($parsed) || $parsed['domain'] !== config('pixelfed.domain.app')) {
  50. abort(404);
  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|JSON_UNESCAPED_SLASHES)
  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 sharedInbox(Request $request)
  94. {
  95. abort_if(!config('federation.activitypub.enabled'), 404);
  96. abort_if(!config('federation.activitypub.sharedInbox'), 404);
  97. $headers = $request->headers->all();
  98. $payload = $request->getContent();
  99. dispatch(new InboxWorker($headers, $payload))->onQueue('high');
  100. return;
  101. }
  102. public function userFollowing(Request $request, $username)
  103. {
  104. abort_if(!config('federation.activitypub.enabled'), 404);
  105. $profile = Profile::whereNull('remote_url')
  106. ->whereUsername($username)
  107. ->whereIsPrivate(false)
  108. ->firstOrFail();
  109. if($profile->status != null) {
  110. abort(404);
  111. }
  112. $obj = [
  113. '@context' => 'https://www.w3.org/ns/activitystreams',
  114. 'id' => $request->getUri(),
  115. 'type' => 'OrderedCollectionPage',
  116. 'totalItems' => 0,
  117. 'orderedItems' => []
  118. ];
  119. return response()->json($obj);
  120. }
  121. public function userFollowers(Request $request, $username)
  122. {
  123. abort_if(!config('federation.activitypub.enabled'), 404);
  124. $profile = Profile::whereNull('remote_url')
  125. ->whereUsername($username)
  126. ->whereIsPrivate(false)
  127. ->firstOrFail();
  128. if($profile->status != null) {
  129. abort(404);
  130. }
  131. $obj = [
  132. '@context' => 'https://www.w3.org/ns/activitystreams',
  133. 'id' => $request->getUri(),
  134. 'type' => 'OrderedCollectionPage',
  135. 'totalItems' => 0,
  136. 'orderedItems' => []
  137. ];
  138. return response()->json($obj);
  139. }
  140. }