diff --git a/app/Http/Controllers/FederationController.php b/app/Http/Controllers/FederationController.php index 8c121a898..bc90737d8 100644 --- a/app/Http/Controllers/FederationController.php +++ b/app/Http/Controllers/FederationController.php @@ -35,14 +35,14 @@ class FederationController extends Controller public function nodeinfoWellKnown() { abort_if(!config('federation.nodeinfo.enabled'), 404); - return response()->json(Nodeinfo::wellKnown()) + return response()->json(Nodeinfo::wellKnown(), 200, [], JSON_UNESCAPED_SLASHES) ->header('Access-Control-Allow-Origin','*'); } public function nodeinfo() { abort_if(!config('federation.nodeinfo.enabled'), 404); - return response()->json(Nodeinfo::get()) + return response()->json(Nodeinfo::get(), 200, [], JSON_UNESCAPED_SLASHES) ->header('Access-Control-Allow-Origin','*'); } @@ -53,6 +53,11 @@ class FederationController extends Controller abort_if(!$request->filled('resource'), 400); $resource = $request->input('resource'); + $hash = hash('sha256', $resource); + $key = 'federation:webfinger:sha256:' . $hash; + if($cached = Cache::get($key)) { + return response()->json($cached, 200, [], JSON_UNESCAPED_SLASHES); + } $parsed = Nickname::normalizeProfileUrl($resource); if(empty($parsed) || $parsed['domain'] !== config('pixelfed.domain.app')) { abort(404); @@ -63,8 +68,9 @@ class FederationController extends Controller return ProfileController::accountCheck($profile); } $webfinger = (new Webfinger($profile))->generate(); + Cache::put($key, $webfinger, 43200); - return response()->json($webfinger, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES) + return response()->json($webfinger, 200, [], JSON_UNESCAPED_SLASHES) ->header('Access-Control-Allow-Origin','*'); } diff --git a/app/Util/Site/Nodeinfo.php b/app/Util/Site/Nodeinfo.php index 36ac72729..fbcc2ee8e 100644 --- a/app/Util/Site/Nodeinfo.php +++ b/app/Util/Site/Nodeinfo.php @@ -10,34 +10,29 @@ class Nodeinfo { public static function get() { - $res = Cache::remember('api:nodeinfo', now()->addMinutes(15), function () { - $activeHalfYear = Cache::remember('api:nodeinfo:ahy', now()->addHours(12), function() { - // todo: replace with last_active_at after July 9, 2021 (96afc3e781) - $count = collect([]); - $likes = Like::select('profile_id')->with('actor')->where('created_at', '>', now()->subMonths(6)->toDateTimeString())->groupBy('profile_id')->get()->filter(function($like) {return $like->actor && $like->actor->domain == null;})->pluck('profile_id')->toArray(); - $count = $count->merge($likes); - $statuses = Status::select('profile_id')->whereLocal(true)->where('created_at', '>', now()->subMonths(6)->toDateTimeString())->groupBy('profile_id')->pluck('profile_id')->toArray(); - $count = $count->merge($statuses); - $profiles = User::select('profile_id', 'last_active_at') - ->whereNotNull('last_active_at') + $res = Cache::remember('api:nodeinfo', 300, function () { + $activeHalfYear = Cache::remember('api:nodeinfo:ahy', 172800, function() { + return User::select('last_active_at') ->where('last_active_at', '>', now()->subMonths(6)) - ->pluck('profile_id') - ->toArray(); - $newProfiles = User::select('profile_id', 'last_active_at', 'created_at') - ->whereNull('last_active_at') - ->where('created_at', '>', now()->subMonths(6)) - ->pluck('profile_id') - ->toArray(); - $count = $count->merge($newProfiles); - $count = $count->merge($profiles); - return $count->unique()->count(); + ->orWhere('created_at', '>', now()->subMonths(6)) + ->count(); }); - $activeMonth = Cache::remember('api:nodeinfo:am', now()->addHours(2), function() { + + $activeMonth = Cache::remember('api:nodeinfo:am', 172800, function() { return User::select('last_active_at') ->where('last_active_at', '>', now()->subMonths(1)) ->orWhere('created_at', '>', now()->subMonths(1)) ->count(); }); + + $users = Cache::remember('api:nodeinfo:users', 43200, function() { + return User::count(); + }); + + $statuses = Cache::remember('api:nodeinfo:statuses', 21600, function() { + return Status::whereLocal(true)->count(); + }); + return [ 'metadata' => [ 'nodeName' => config_cache('app.name'), @@ -59,10 +54,10 @@ class Nodeinfo { 'version' => config('pixelfed.version'), ], 'usage' => [ - 'localPosts' => Status::whereLocal(true)->count(), + 'localPosts' => $statuses, 'localComments' => 0, 'users' => [ - 'total' => User::count(), + 'total' => $users, 'activeHalfyear' => (int) $activeHalfYear, 'activeMonth' => (int) $activeMonth, ], diff --git a/routes/api.php b/routes/api.php index 7c3d5f3b7..56c733709 100644 --- a/routes/api.php +++ b/routes/api.php @@ -11,6 +11,12 @@ Route::post('i/actor/inbox', 'InstanceActorController@inbox'); Route::get('i/actor/outbox', 'InstanceActorController@outbox'); Route::get('/stories/{username}/{id}', 'StoryController@getActivityObject'); +Route::get('.well-known/webfinger', 'FederationController@webfinger')->name('well-known.webfinger'); +Route::get('.well-known/nodeinfo', 'FederationController@nodeinfoWellKnown')->name('well-known.nodeinfo'); +Route::get('.well-known/host-meta', 'FederationController@hostMeta')->name('well-known.hostMeta'); +Route::redirect('.well-known/change-password', '/settings/password'); +Route::get('api/nodeinfo/2.0.json', 'FederationController@nodeinfo'); + Route::group(['prefix' => 'api'], function() use($middleware) { Route::group(['prefix' => 'v1'], function() use($middleware) { diff --git a/routes/web.php b/routes/web.php index 4086f28dc..23a0080b2 100644 --- a/routes/web.php +++ b/routes/web.php @@ -90,11 +90,6 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact Auth::routes(); - Route::get('.well-known/webfinger', 'FederationController@webfinger')->name('well-known.webfinger'); - Route::get('.well-known/nodeinfo', 'FederationController@nodeinfoWellKnown')->name('well-known.nodeinfo'); - Route::get('.well-known/host-meta', 'FederationController@hostMeta')->name('well-known.hostMeta'); - Route::redirect('.well-known/change-password', '/settings/password'); - Route::get('/home', 'HomeController@index')->name('home'); Route::get('discover/c/{slug}', 'DiscoverController@showCategory'); @@ -105,7 +100,6 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact Route::group(['prefix' => 'api'], function () { Route::get('search', 'SearchController@searchAPI'); - Route::get('nodeinfo/2.0.json', 'FederationController@nodeinfo'); Route::post('status/view', 'StatusController@storeView'); Route::get('v1/polls/{id}', 'PollController@getPoll'); Route::post('v1/polls/{id}/votes', 'PollController@vote'); @@ -251,7 +245,6 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact Route::post('v1/publish', 'StoryController@publishStory'); Route::delete('v1/delete/{id}', 'StoryController@apiV1Delete'); }); - }); Route::get('discover/tags/{hashtag}', 'DiscoverController@showTags');