From 36df0d8373d87ef705ceafe63480ff3706230712 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Thu, 12 Oct 2023 21:29:51 -0600 Subject: [PATCH] Update nodeinfo --- app/Http/Controllers/Api/ApiV2Controller.php | 8 +- app/Services/LandingService.php | 8 +- app/Util/Site/Nodeinfo.php | 157 ++++++++++--------- 3 files changed, 89 insertions(+), 84 deletions(-) diff --git a/app/Http/Controllers/Api/ApiV2Controller.php b/app/Http/Controllers/Api/ApiV2Controller.php index 757e14dce..2ca5b96c5 100644 --- a/app/Http/Controllers/Api/ApiV2Controller.php +++ b/app/Http/Controllers/Api/ApiV2Controller.php @@ -34,6 +34,7 @@ use App\Transformer\Api\Mastodon\v1\{ use App\Transformer\Api\{ RelationshipTransformer, }; +use App\Util\Site\Nodeinfo; class ApiV2Controller extends Controller { @@ -77,12 +78,7 @@ class ApiV2Controller extends Controller 'description' => config_cache('app.short_description'), 'usage' => [ 'users' => [ - 'active_month' => (int) Cache::remember('api:nodeinfo:am', 172800, function() { - return User::select('last_active_at', 'created_at') - ->where('last_active_at', '>', now()->subMonths(1)) - ->orWhere('created_at', '>', now()->subMonths(1)) - ->count(); - }) + 'active_month' => (int) Nodeinfo::activeUsersMonthly() ] ], 'thumbnail' => [ diff --git a/app/Services/LandingService.php b/app/Services/LandingService.php index 2a5acda07..ba16af5b6 100644 --- a/app/Services/LandingService.php +++ b/app/Services/LandingService.php @@ -9,17 +9,13 @@ use Illuminate\Support\Facades\Redis; use App\Status; use App\User; use App\Services\AccountService; +use App\Util\Site\Nodeinfo; class LandingService { public static function get($json = true) { - $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(); - }); + $activeMonth = Nodeinfo::activeUsersMonthly(); $totalUsers = Cache::remember('api:nodeinfo:users', 43200, function() { return User::count(); diff --git a/app/Util/Site/Nodeinfo.php b/app/Util/Site/Nodeinfo.php index 166b6bc6a..0458299c5 100644 --- a/app/Util/Site/Nodeinfo.php +++ b/app/Util/Site/Nodeinfo.php @@ -2,85 +2,98 @@ namespace App\Util\Site; -use Cache; -use App\{Like, Profile, Status, User}; +use Illuminate\Support\Facades\Cache; +use App\Like; +use App\Profile; +use App\Status; +use App\User; use Illuminate\Support\Str; -class Nodeinfo { +class Nodeinfo +{ + public static function get() + { + $res = Cache::remember('api:nodeinfo', 900, function () { + $activeHalfYear = self::activeUsersHalfYear(); + $activeMonth = self::activeUsersMonthly(); - public static function get() - { - $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)) - ->orWhere('created_at', '>', now()->subMonths(6)) - ->count(); - }); + $users = Cache::remember('api:nodeinfo:users', 43200, function() { + return User::count(); + }); - $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(); - }); + $statuses = Cache::remember('api:nodeinfo:statuses', 21600, function() { + return Status::whereLocal(true)->count(); + }); - $users = Cache::remember('api:nodeinfo:users', 43200, function() { - return User::count(); - }); + $features = [ 'features' => \App\Util\Site\Config::get()['features'] ]; - $statuses = Cache::remember('api:nodeinfo:statuses', 21600, function() { - return Status::whereLocal(true)->count(); - }); + return [ + 'metadata' => [ + 'nodeName' => config_cache('app.name'), + 'software' => [ + 'homepage' => 'https://pixelfed.org', + 'repo' => 'https://github.com/pixelfed/pixelfed', + ], + 'config' => $features + ], + 'protocols' => [ + 'activitypub', + ], + 'services' => [ + 'inbound' => [], + 'outbound' => [], + ], + 'software' => [ + 'name' => 'pixelfed', + 'version' => config('pixelfed.version'), + ], + 'usage' => [ + 'localPosts' => (int) $statuses, + 'localComments' => 0, + 'users' => [ + 'total' => (int) $users, + 'activeHalfyear' => (int) $activeHalfYear, + 'activeMonth' => (int) $activeMonth, + ], + ], + 'version' => '2.0', + ]; + }); + $res['openRegistrations'] = (bool) config_cache('pixelfed.open_registration'); + return $res; + } - $features = [ 'features' => \App\Util\Site\Config::get()['features'] ]; + public static function wellKnown() + { + return [ + 'links' => [ + [ + 'href' => config('pixelfed.nodeinfo.url'), + 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0', + ], + ], + ]; + } - return [ - 'metadata' => [ - 'nodeName' => config_cache('app.name'), - 'software' => [ - 'homepage' => 'https://pixelfed.org', - 'repo' => 'https://github.com/pixelfed/pixelfed', - ], - 'config' => $features - ], - 'protocols' => [ - 'activitypub', - ], - 'services' => [ - 'inbound' => [], - 'outbound' => [], - ], - 'software' => [ - 'name' => 'pixelfed', - 'version' => config('pixelfed.version'), - ], - 'usage' => [ - 'localPosts' => (int) $statuses, - 'localComments' => 0, - 'users' => [ - 'total' => (int) $users, - 'activeHalfyear' => (int) $activeHalfYear, - 'activeMonth' => (int) $activeMonth, - ], - ], - 'version' => '2.0', - ]; - }); - $res['openRegistrations'] = (bool) config_cache('pixelfed.open_registration'); - return $res; - } - - public static function wellKnown() - { - return [ - 'links' => [ - [ - 'href' => config('pixelfed.nodeinfo.url'), - 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0', - ], - ], - ]; - } + public static function activeUsersMonthly() + { + return Cache::remember('api:nodeinfo:active-users-monthly', 43200, function() { + return User::withTrashed() + ->select('last_active_at, updated_at') + ->where('updated_at', '>', now()->subWeeks(5)) + ->orWhere('last_active_at', '>', now()->subWeeks(5)) + ->count(); + }); + } + public static function activeUsersHalfYear() + { + return Cache::remember('api:nodeinfo:active-users-half-year', 43200, function() { + return User::withTrashed() + ->select('last_active_at, updated_at') + ->where('last_active_at', '>', now()->subMonths(6)) + ->orWhere('updated_at', '>', now()->subMonths(6)) + ->count(); + }); + } }