Update ApiV1Controller, improve instance endpoint

This commit is contained in:
Daniel Supernault 2022-01-05 23:37:27 -07:00
parent ff211dbec5
commit cdf78e23a4
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
3 changed files with 48 additions and 43 deletions

View file

@ -170,6 +170,8 @@ trait AdminSettingsController
$json[] = $val; $json[] = $val;
ConfigCacheService::put('app.rules', json_encode(array_values($json))); ConfigCacheService::put('app.rules', json_encode(array_values($json)));
} }
Cache::forget('api:v1:instance-data:rules');
Cache::forget('api:v1:instance-data-response');
} }
if($request->filled('account_autofollow_usernames')) { if($request->filled('account_autofollow_usernames')) {

View file

@ -16,6 +16,7 @@ use App\{
Follower, Follower,
FollowRequest, FollowRequest,
Hashtag, Hashtag,
Instance,
Like, Like,
Media, Media,
Notification, Notification,
@ -1166,49 +1167,53 @@ class ApiV1Controller extends Controller
*/ */
public function instance(Request $request) public function instance(Request $request)
{ {
$res = Cache::remember('api:v1:instance-data', now()->addMinutes(15), function () { $res = Cache::remember('api:v1:instance-data-response', 900, function () {
$rules = config_cache('app.rules') ? collect(json_decode(config_cache('app.rules'), true)) $contact = Cache::remember('api:v1:instance-data:contact', 604800, function () {
->map(function($rule, $key) { $admin = User::whereIsAdmin(true)->first();
$id = $key + 1; return $admin && isset($admin->profile_id) ?
return [ AccountService::getMastodon($admin->profile_id, true) :
'id' => "{$id}", null;
'text' => $rule });
];
})
->toArray() : [];
$res = [
'approval_required' => false,
'contact_account' => null,
'description' => config_cache('app.description'),
'email' => config('instance.email'),
'invites_enabled' => false,
'rules' => $rules,
'short_description' => 'Pixelfed - Photo sharing for everyone',
'languages' => ['en'],
'max_toot_chars' => (int) config('pixelfed.max_caption_length'),
'registrations' => (bool) config_cache('pixelfed.open_registration'),
'stats' => [
'user_count' => 0,
'status_count' => 0,
'domain_count' => 0
],
'thumbnail' => config('app.url') . '/img/pixelfed-icon-color.png',
'title' => config_cache('app.name'),
'uri' => config('pixelfed.domain.app'),
'urls' => [],
'version' => '2.7.2 (compatible; Pixelfed ' . config('pixelfed.version') . ')',
'environment' => [
'max_photo_size' => (int) config_cache('pixelfed.max_photo_size'),
'max_avatar_size' => (int) config('pixelfed.max_avatar_size'),
'max_caption_length' => (int) config('pixelfed.max_caption_length'),
'max_bio_length' => (int) config('pixelfed.max_bio_length'),
'max_album_length' => (int) config_cache('pixelfed.max_album_length'),
'mobile_apis' => (bool) config_cache('pixelfed.oauth_enabled')
] $stats = Cache::remember('api:v1:instance-data:stats', 43200, function () {
return [
'user_count' => User::count(),
'status_count' => Status::whereNull('uri')->count(),
'domain_count' => Instance::count(),
];
});
$rules = Cache::remember('api:v1:instance-data:rules', 604800, function () {
return config_cache('app.rules') ?
collect(json_decode(config_cache('app.rules'), true))
->map(function($rule, $key) {
$id = $key + 1;
return [
'id' => "{$id}",
'text' => $rule
];
})
->toArray() : [];
});
return [
'uri' => config('pixelfed.domain.app'),
'title' => config('app.name'),
'short_description' => 'Pixelfed is an image sharing platform, an ethical alternative to centralized platforms',
'description' => 'Pixelfed is an image sharing platform, an ethical alternative to centralized platforms',
'email' => config('instance.email'),
'version' => config('pixelfed.version'),
'urls' => [],
'stats' => $stats,
'thumbnail' => url('headers/default.jpg'),
'languages' => ['en'],
'registrations' => (bool) config('pixelfed.open_registration'),
'approval_required' => false,
'contact_account' => $contact,
'rules' => $rules
]; ];
return $res;
}); });
return response()->json($res); return response()->json($res);
} }

View file

@ -1,6 +1,4 @@
@extends('admin.partial.template') @extends('admin.partial.template-full')
@include('admin.settings.sidebar')
@section('section') @section('section')
<div class="title mb-4"> <div class="title mb-4">