kopia lustrzana https://github.com/pixelfed/pixelfed
Update FederationController, move well-known to api middleware and cache webfinger lookups
rodzic
754151dc84
commit
4505d1f0f9
|
@ -35,14 +35,14 @@ class FederationController extends Controller
|
||||||
public function nodeinfoWellKnown()
|
public function nodeinfoWellKnown()
|
||||||
{
|
{
|
||||||
abort_if(!config('federation.nodeinfo.enabled'), 404);
|
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','*');
|
->header('Access-Control-Allow-Origin','*');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function nodeinfo()
|
public function nodeinfo()
|
||||||
{
|
{
|
||||||
abort_if(!config('federation.nodeinfo.enabled'), 404);
|
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','*');
|
->header('Access-Control-Allow-Origin','*');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,6 +53,11 @@ class FederationController extends Controller
|
||||||
abort_if(!$request->filled('resource'), 400);
|
abort_if(!$request->filled('resource'), 400);
|
||||||
|
|
||||||
$resource = $request->input('resource');
|
$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);
|
$parsed = Nickname::normalizeProfileUrl($resource);
|
||||||
if(empty($parsed) || $parsed['domain'] !== config('pixelfed.domain.app')) {
|
if(empty($parsed) || $parsed['domain'] !== config('pixelfed.domain.app')) {
|
||||||
abort(404);
|
abort(404);
|
||||||
|
@ -63,8 +68,9 @@ class FederationController extends Controller
|
||||||
return ProfileController::accountCheck($profile);
|
return ProfileController::accountCheck($profile);
|
||||||
}
|
}
|
||||||
$webfinger = (new Webfinger($profile))->generate();
|
$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','*');
|
->header('Access-Control-Allow-Origin','*');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,34 +10,29 @@ class Nodeinfo {
|
||||||
|
|
||||||
public static function get()
|
public static function get()
|
||||||
{
|
{
|
||||||
$res = Cache::remember('api:nodeinfo', now()->addMinutes(15), function () {
|
$res = Cache::remember('api:nodeinfo', 300, function () {
|
||||||
$activeHalfYear = Cache::remember('api:nodeinfo:ahy', now()->addHours(12), function() {
|
$activeHalfYear = Cache::remember('api:nodeinfo:ahy', 172800, function() {
|
||||||
// todo: replace with last_active_at after July 9, 2021 (96afc3e781)
|
return User::select('last_active_at')
|
||||||
$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')
|
|
||||||
->where('last_active_at', '>', now()->subMonths(6))
|
->where('last_active_at', '>', now()->subMonths(6))
|
||||||
->pluck('profile_id')
|
->orWhere('created_at', '>', now()->subMonths(6))
|
||||||
->toArray();
|
->count();
|
||||||
$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();
|
|
||||||
});
|
});
|
||||||
$activeMonth = Cache::remember('api:nodeinfo:am', now()->addHours(2), function() {
|
|
||||||
|
$activeMonth = Cache::remember('api:nodeinfo:am', 172800, function() {
|
||||||
return User::select('last_active_at')
|
return User::select('last_active_at')
|
||||||
->where('last_active_at', '>', now()->subMonths(1))
|
->where('last_active_at', '>', now()->subMonths(1))
|
||||||
->orWhere('created_at', '>', now()->subMonths(1))
|
->orWhere('created_at', '>', now()->subMonths(1))
|
||||||
->count();
|
->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 [
|
return [
|
||||||
'metadata' => [
|
'metadata' => [
|
||||||
'nodeName' => config_cache('app.name'),
|
'nodeName' => config_cache('app.name'),
|
||||||
|
@ -59,10 +54,10 @@ class Nodeinfo {
|
||||||
'version' => config('pixelfed.version'),
|
'version' => config('pixelfed.version'),
|
||||||
],
|
],
|
||||||
'usage' => [
|
'usage' => [
|
||||||
'localPosts' => Status::whereLocal(true)->count(),
|
'localPosts' => $statuses,
|
||||||
'localComments' => 0,
|
'localComments' => 0,
|
||||||
'users' => [
|
'users' => [
|
||||||
'total' => User::count(),
|
'total' => $users,
|
||||||
'activeHalfyear' => (int) $activeHalfYear,
|
'activeHalfyear' => (int) $activeHalfYear,
|
||||||
'activeMonth' => (int) $activeMonth,
|
'activeMonth' => (int) $activeMonth,
|
||||||
],
|
],
|
||||||
|
|
|
@ -11,6 +11,12 @@ Route::post('i/actor/inbox', 'InstanceActorController@inbox');
|
||||||
Route::get('i/actor/outbox', 'InstanceActorController@outbox');
|
Route::get('i/actor/outbox', 'InstanceActorController@outbox');
|
||||||
Route::get('/stories/{username}/{id}', 'StoryController@getActivityObject');
|
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' => 'api'], function() use($middleware) {
|
||||||
|
|
||||||
Route::group(['prefix' => 'v1'], function() use($middleware) {
|
Route::group(['prefix' => 'v1'], function() use($middleware) {
|
||||||
|
|
|
@ -90,11 +90,6 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact
|
||||||
|
|
||||||
Auth::routes();
|
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('/home', 'HomeController@index')->name('home');
|
||||||
|
|
||||||
Route::get('discover/c/{slug}', 'DiscoverController@showCategory');
|
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::group(['prefix' => 'api'], function () {
|
||||||
Route::get('search', 'SearchController@searchAPI');
|
Route::get('search', 'SearchController@searchAPI');
|
||||||
Route::get('nodeinfo/2.0.json', 'FederationController@nodeinfo');
|
|
||||||
Route::post('status/view', 'StatusController@storeView');
|
Route::post('status/view', 'StatusController@storeView');
|
||||||
Route::get('v1/polls/{id}', 'PollController@getPoll');
|
Route::get('v1/polls/{id}', 'PollController@getPoll');
|
||||||
Route::post('v1/polls/{id}/votes', 'PollController@vote');
|
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::post('v1/publish', 'StoryController@publishStory');
|
||||||
Route::delete('v1/delete/{id}', 'StoryController@apiV1Delete');
|
Route::delete('v1/delete/{id}', 'StoryController@apiV1Delete');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Route::get('discover/tags/{hashtag}', 'DiscoverController@showTags');
|
Route::get('discover/tags/{hashtag}', 'DiscoverController@showTags');
|
||||||
|
|
Ładowanie…
Reference in New Issue