FederationController.php 4.9 KB

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