Skip to content

Commit 4f850e5

Browse files
committed
Update AdminApiController, include more data for getUser method
1 parent 52f9999 commit 4f850e5

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

app/Http/Controllers/Api/AdminApiController.php

+28-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
Status,
1919
User
2020
};
21+
use App\Models\Conversation;
22+
use App\Models\RemoteReport;
2123
use App\Services\AccountService;
2224
use App\Services\AdminStatsService;
2325
use App\Services\ConfigCacheService;
@@ -405,6 +407,9 @@ public function getUsers(Request $request)
405407
{
406408
abort_if(!$request->user(), 404);
407409
abort_unless($request->user()->is_admin == 1, 404);
410+
$this->validate($request, [
411+
'sort' => 'sometimes|in:asc,desc',
412+
]);
408413
$q = $request->input('q');
409414
$sort = $request->input('sort', 'desc') === 'asc' ? 'asc' : 'desc';
410415
$res = User::whereNull('status')
@@ -422,17 +427,29 @@ public function getUser(Request $request)
422427
abort_unless($request->user()->is_admin == 1, 404);
423428

424429
$id = $request->input('user_id');
425-
$user = User::findOrFail($id);
426-
$profile = $user->profile;
427-
$account = AccountService::get($user->profile_id, true);
428-
return (new AdminUser($user))->additional(['meta' => [
429-
'account' => $account,
430-
'moderation' => [
431-
'unlisted' => (bool) $profile->unlisted,
432-
'cw' => (bool) $profile->cw,
433-
'no_autolink' => (bool) $profile->no_autolink
434-
]
435-
]]);
430+
$key = 'pf-admin-api:getUser:byId:' . $id;
431+
if($request->has('refresh')) {
432+
Cache::forget($key);
433+
}
434+
return Cache::remember($key, 86400, function() use($id) {
435+
$user = User::findOrFail($id);
436+
$profile = $user->profile;
437+
$account = AccountService::get($user->profile_id, true);
438+
$res = (new AdminUser($user))->additional(['meta' => [
439+
'cached_at' => str_replace('+00:00', 'Z', now()->format(DATE_RFC3339_EXTENDED)),
440+
'account' => $account,
441+
'dms_sent' => Conversation::whereFromId($profile->id)->count(),
442+
'report_count' => Report::where('object_id', $profile->id)->orWhere('reported_profile_id', $profile->id)->count(),
443+
'remote_report_count' => RemoteReport::whereAccountId($profile->id)->count(),
444+
'moderation' => [
445+
'unlisted' => (bool) $profile->unlisted,
446+
'cw' => (bool) $profile->cw,
447+
'no_autolink' => (bool) $profile->no_autolink
448+
]
449+
]]);
450+
451+
return $res;
452+
});
436453
}
437454

438455
public function userAdminAction(Request $request)

0 commit comments

Comments
 (0)