FederationController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\InboxPipeline\InboxWorker;
  4. use App\Jobs\RemoteFollowPipeline\RemoteFollowPipeline;
  5. use App\Profile;
  6. use App\Transformer\ActivityPub\ProfileOutbox;
  7. use App\Util\Lexer\Nickname;
  8. use App\Util\Webfinger\Webfinger;
  9. use Auth;
  10. use Cache;
  11. use Carbon\Carbon;
  12. use Illuminate\Http\Request;
  13. use League\Fractal;
  14. class FederationController extends Controller
  15. {
  16. public function authCheck()
  17. {
  18. if (!Auth::check()) {
  19. return abort(403);
  20. }
  21. }
  22. public function authorizeFollow(Request $request)
  23. {
  24. $this->authCheck();
  25. $this->validate($request, [
  26. 'acct' => 'required|string|min:3|max:255',
  27. ]);
  28. $acct = $request->input('acct');
  29. $nickname = Nickname::normalizeProfileUrl($acct);
  30. return view('federation.authorizefollow', compact('acct', 'nickname'));
  31. }
  32. public function remoteFollow()
  33. {
  34. $this->authCheck();
  35. return view('federation.remotefollow');
  36. }
  37. public function remoteFollowStore(Request $request)
  38. {
  39. $this->authCheck();
  40. $this->validate($request, [
  41. 'url' => 'required|string',
  42. ]);
  43. if (config('pixelfed.remote_follow_enabled') !== true) {
  44. abort(403);
  45. }
  46. $follower = Auth::user()->profile;
  47. $url = $request->input('url');
  48. RemoteFollowPipeline::dispatch($follower, $url);
  49. return redirect()->back();
  50. }
  51. public function nodeinfoWellKnown()
  52. {
  53. $res = [
  54. 'links' => [
  55. [
  56. 'href' => config('pixelfed.nodeinfo.url'),
  57. 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0',
  58. ],
  59. ],
  60. ];
  61. return response()->json($res);
  62. }
  63. public function nodeinfo()
  64. {
  65. $res = Cache::remember('api:nodeinfo', 60, function () {
  66. return [
  67. 'metadata' => [
  68. 'nodeName' => config('app.name'),
  69. 'software' => [
  70. 'homepage' => 'https://pixelfed.org',
  71. 'github' => 'https://github.com/pixelfed',
  72. 'follow' => 'https://mastodon.social/@pixelfed',
  73. ],
  74. ],
  75. 'openRegistrations' => config('pixelfed.open_registration'),
  76. 'protocols' => [
  77. 'activitypub',
  78. ],
  79. 'services' => [
  80. 'inbound' => [],
  81. 'outbound' => [],
  82. ],
  83. 'software' => [
  84. 'name' => 'pixelfed',
  85. 'version' => config('pixelfed.version'),
  86. ],
  87. 'usage' => [
  88. 'localPosts' => \App\Status::whereLocal(true)->whereHas('media')->count(),
  89. 'localComments' => \App\Status::whereLocal(true)->whereNotNull('in_reply_to_id')->count(),
  90. 'users' => [
  91. 'total' => \App\User::count(),
  92. 'activeHalfyear' => \App\User::where('updated_at', '>', Carbon::now()->subMonths(6)->toDateTimeString())->count(),
  93. 'activeMonth' => \App\User::where('updated_at', '>', Carbon::now()->subMonths(1)->toDateTimeString())->count(),
  94. ],
  95. ],
  96. 'version' => '2.0',
  97. ];
  98. });
  99. return response()->json($res, 200, [], JSON_PRETTY_PRINT);
  100. }
  101. public function webfinger(Request $request)
  102. {
  103. $this->validate($request, ['resource'=>'required|string|min:3|max:255']);
  104. $hash = hash('sha256', $request->input('resource'));
  105. $webfinger = Cache::remember('api:webfinger:'.$hash, 1440, function () use ($request) {
  106. $resource = $request->input('resource');
  107. $parsed = Nickname::normalizeProfileUrl($resource);
  108. $username = $parsed['username'];
  109. $user = Profile::whereUsername($username)->firstOrFail();
  110. return (new Webfinger($user))->generate();
  111. });
  112. return response()->json($webfinger, 200, [], JSON_PRETTY_PRINT);
  113. }
  114. public function userOutbox(Request $request, $username)
  115. {
  116. if (config('pixelfed.activitypub_enabled') == false) {
  117. abort(403);
  118. }
  119. $user = Profile::whereNull('remote_url')->whereUsername($username)->firstOrFail();
  120. $timeline = $user->statuses()->orderBy('created_at', 'desc')->paginate(10);
  121. $fractal = new Fractal\Manager();
  122. $resource = new Fractal\Resource\Item($user, new ProfileOutbox());
  123. $res = $fractal->createData($resource)->toArray();
  124. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  125. }
  126. public function userInbox(Request $request, $username)
  127. {
  128. if (config('pixelfed.activitypub_enabled') == false) {
  129. abort(403);
  130. }
  131. $mimes = [
  132. 'application/activity+json',
  133. 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  134. ];
  135. if (!in_array($request->header('Content-Type'), $mimes)) {
  136. abort(500, 'Invalid request');
  137. }
  138. $profile = Profile::whereUsername($username)->firstOrFail();
  139. InboxWorker::dispatch($request, $profile, $request->all());
  140. }
  141. }