FederationController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. use App\Util\ActivityPub\Helpers;
  15. use App\Util\ActivityPub\HttpSignature;
  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\AccountLog::select('user_id')->whereAction('auth.login')->where('updated_at', '>',Carbon::now()->subMonths(6)->toDateTimeString())->groupBy('user_id')->get()->count(),
  95. 'activeMonth' => \App\AccountLog::select('user_id')->whereAction('auth.login')->where('updated_at', '>',Carbon::now()->subMonths(1)->toDateTimeString())->groupBy('user_id')->get()->count(),
  96. ],
  97. ],
  98. 'version' => '2.0',
  99. ];
  100. });
  101. return response()->json($res, 200, [
  102. 'Access-Control-Allow-Origin' => '*'
  103. ]);
  104. }
  105. public function webfinger(Request $request)
  106. {
  107. $this->validate($request, ['resource'=>'required|string|min:3|max:255']);
  108. $hash = hash('sha256', $request->input('resource'));
  109. $webfinger = Cache::remember('api:webfinger:'.$hash, 1440, function () use ($request) {
  110. $resource = $request->input('resource');
  111. $parsed = Nickname::normalizeProfileUrl($resource);
  112. $username = $parsed['username'];
  113. $user = Profile::whereUsername($username)->firstOrFail();
  114. return (new Webfinger($user))->generate();
  115. });
  116. return response()->json($webfinger, 200, [], JSON_PRETTY_PRINT);
  117. }
  118. public function hostMeta(Request $request)
  119. {
  120. $path = route('well-known.webfinger');
  121. $xml = <<<XML
  122. <?xml version="1.0" encoding="UTF-8"?>
  123. <XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
  124. <Link rel="lrdd" type="application/xrd+xml" template="{$path}?resource={uri}"/>
  125. </XRD>
  126. XML;
  127. return response($xml)->header('Content-Type', 'application/xrd+xml');
  128. }
  129. public function userOutbox(Request $request, $username)
  130. {
  131. if (config('pixelfed.activitypub_enabled') == false) {
  132. abort(403);
  133. }
  134. $user = Profile::whereNull('remote_url')->whereUsername($username)->firstOrFail();
  135. if($user->is_private) {
  136. return response()->json(['error'=>'403', 'msg' => 'private profile'], 403);
  137. }
  138. $timeline = $user->statuses()->whereVisibility('public')->orderBy('created_at', 'desc')->paginate(10);
  139. $fractal = new Fractal\Manager();
  140. $resource = new Fractal\Resource\Item($user, new ProfileOutbox());
  141. $res = $fractal->createData($resource)->toArray();
  142. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  143. }
  144. public function userInbox(Request $request, $username)
  145. {
  146. return;
  147. }
  148. public function userFollowing(Request $request, $username)
  149. {
  150. if (config('pixelfed.activitypub_enabled') == false) {
  151. abort(403);
  152. }
  153. $profile = Profile::whereNull('remote_url')
  154. ->whereUsername($username)
  155. ->whereIsPrivate(false)
  156. ->firstOrFail();
  157. $obj = [
  158. '@context' => 'https://www.w3.org/ns/activitystreams',
  159. 'id' => $request->getUri(),
  160. 'type' => 'OrderedCollectionPage',
  161. 'totalItems' => $profile->following()->count(),
  162. 'orderedItems' => $profile->following->map(function($f) {
  163. return $f->permalink();
  164. })
  165. ];
  166. return response()->json($obj);
  167. }
  168. public function userFollowers(Request $request, $username)
  169. {
  170. if (config('pixelfed.activitypub_enabled') == false) {
  171. abort(403);
  172. }
  173. $profile = Profile::whereNull('remote_url')
  174. ->whereUsername($username)
  175. ->whereIsPrivate(false)
  176. ->firstOrFail();
  177. $obj = [
  178. '@context' => 'https://www.w3.org/ns/activitystreams',
  179. 'id' => $request->getUri(),
  180. 'type' => 'OrderedCollectionPage',
  181. 'totalItems' => $profile->followers()->count(),
  182. 'orderedItems' => $profile->followers->map(function($f) {
  183. return $f->permalink();
  184. })
  185. ];
  186. return response()->json($obj);
  187. }
  188. }