pixelfed/app/Http/Controllers/Api/AdminApiController.php

866 lines
31 KiB
PHP
Raw Permalink Normal View History

2019-08-20 01:07:25 +00:00
<?php
namespace App\Http\Controllers\Api;
2024-07-17 10:17:47 +00:00
use App\AccountInterstitial;
2019-08-20 01:07:25 +00:00
use App\Http\Controllers\Controller;
2024-07-17 10:17:47 +00:00
use App\Http\Resources\AdminInstance;
use App\Http\Resources\AdminUser;
use App\Instance;
use App\Jobs\DeletePipeline\DeleteAccountPipeline;
use App\Jobs\DeletePipeline\DeleteRemoteProfilePipeline;
2019-08-20 01:07:25 +00:00
use App\Jobs\StatusPipeline\StatusDelete;
use App\Models\Conversation;
use App\Models\RemoteReport;
2024-07-17 10:17:47 +00:00
use App\Notification;
use App\Profile;
use App\Report;
2023-01-15 03:33:46 +00:00
use App\Services\AccountService;
use App\Services\AdminStatsService;
use App\Services\ConfigCacheService;
2023-01-15 09:36:55 +00:00
use App\Services\InstanceService;
2023-01-15 03:33:46 +00:00
use App\Services\ModLogService;
2023-01-15 09:36:55 +00:00
use App\Services\NetworkTimelineService;
2019-08-20 01:07:25 +00:00
use App\Services\NotificationService;
2024-07-17 10:17:47 +00:00
use App\Services\PublicTimelineService;
use App\Services\SnowflakeService;
use App\Services\StatusService;
use App\Status;
use App\User;
use Cache;
use DB;
use Illuminate\Http\Request;
2019-08-20 01:07:25 +00:00
class AdminApiController extends Controller
{
2023-01-15 03:33:46 +00:00
public function supported(Request $request)
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404);
2023-01-15 03:33:46 +00:00
return response()->json(['supported' => true]);
}
public function getStats(Request $request)
2019-08-20 01:07:25 +00:00
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404);
2023-01-15 03:33:46 +00:00
$res = AdminStatsService::summary();
$res['autospam_count'] = AccountInterstitial::whereType('post.autospam')
->whereNull('appeal_handled_at')
->count();
2024-07-17 10:17:47 +00:00
2023-01-15 03:33:46 +00:00
return $res;
2019-08-20 01:07:25 +00:00
}
2023-01-15 03:33:46 +00:00
public function autospam(Request $request)
2019-08-20 01:07:25 +00:00
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404);
2023-01-15 03:33:46 +00:00
$appeals = AccountInterstitial::whereType('post.autospam')
->whereNull('appeal_handled_at')
->latest()
->simplePaginate(6)
2024-07-17 10:17:47 +00:00
->map(function ($report) {
2023-01-15 03:33:46 +00:00
$r = [
'id' => $report->id,
'type' => $report->type,
'item_id' => $report->item_id,
'item_type' => $report->item_type,
2024-07-17 10:17:47 +00:00
'created_at' => $report->created_at,
2023-01-15 03:33:46 +00:00
];
2024-07-17 10:17:47 +00:00
if ($report->item_type === 'App\\Status') {
2023-01-15 03:33:46 +00:00
$status = StatusService::get($report->item_id, false);
2024-07-17 10:17:47 +00:00
if (! $status) {
2023-01-15 03:33:46 +00:00
return;
}
$r['status'] = $status;
2024-07-17 10:17:47 +00:00
if ($status['in_reply_to_id']) {
2023-01-15 03:33:46 +00:00
$r['parent'] = StatusService::get($status['in_reply_to_id'], false);
}
}
2024-07-17 10:17:47 +00:00
2023-01-15 03:33:46 +00:00
return $r;
});
return $appeals;
2019-08-20 01:07:25 +00:00
}
2023-01-15 03:33:46 +00:00
public function autospamHandle(Request $request)
2019-08-20 01:07:25 +00:00
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:write'), 404);
2023-01-15 03:33:46 +00:00
2019-08-20 01:07:25 +00:00
$this->validate($request, [
'action' => 'required|in:dismiss,approve,dismiss-all,approve-all,delete-post,delete-account',
2024-07-17 10:17:47 +00:00
'id' => 'required',
2019-08-20 01:07:25 +00:00
]);
2023-01-15 03:33:46 +00:00
$action = $request->input('action');
2019-08-20 01:07:25 +00:00
$id = $request->input('id');
2023-01-15 03:33:46 +00:00
$appeal = AccountInterstitial::whereType('post.autospam')
->whereNull('appeal_handled_at')
->findOrFail($id);
$now = now();
$res = ['status' => 'success'];
$meta = json_decode($appeal->meta);
$user = $appeal->user;
$profile = $user->profile;
2023-01-15 03:33:46 +00:00
2024-07-17 10:17:47 +00:00
if ($action == 'dismiss') {
2023-01-15 03:33:46 +00:00
$appeal->is_spam = true;
$appeal->appeal_handled_at = $now;
$appeal->save();
2024-07-17 10:17:47 +00:00
Cache::forget('pf:bouncer_v0:exemption_by_pid:'.$profile->id);
Cache::forget('pf:bouncer_v0:recent_by_pid:'.$profile->id);
Cache::forget('admin-dash:reports:spam-count');
2024-07-17 10:17:47 +00:00
return $res;
}
2024-07-17 10:17:47 +00:00
if ($action == 'delete-post') {
$appeal->appeal_handled_at = now();
$appeal->is_spam = true;
$appeal->save();
ModLogService::boot()
->objectUid($profile->id)
->objectId($appeal->status->id)
->objectType('App\Status::class')
->user($request->user())
->action('admin.status.delete')
->accessLevel('admin')
->save();
PublicTimelineService::deleteByProfileId($profile->id);
StatusDelete::dispatch($appeal->status)->onQueue('high');
Cache::forget('admin-dash:reports:spam-count');
2024-07-17 10:17:47 +00:00
return $res;
}
2024-07-17 10:17:47 +00:00
if ($action == 'delete-account') {
abort_if($user->is_admin, 400, 'Cannot delete an admin account.');
$appeal->appeal_handled_at = now();
$appeal->is_spam = true;
$appeal->save();
ModLogService::boot()
->objectUid($profile->id)
->objectId($profile->id)
->objectType('App\User::class')
->user($request->user())
->action('admin.user.delete')
->accessLevel('admin')
->save();
PublicTimelineService::deleteByProfileId($profile->id);
DeleteAccountPipeline::dispatch($appeal->user)->onQueue('high');
2023-01-15 03:33:46 +00:00
Cache::forget('admin-dash:reports:spam-count');
2024-07-17 10:17:47 +00:00
2023-01-15 03:33:46 +00:00
return $res;
}
2024-07-17 10:17:47 +00:00
if ($action == 'dismiss-all') {
2023-01-15 03:33:46 +00:00
AccountInterstitial::whereType('post.autospam')
->whereItemType('App\Status')
->whereNull('appeal_handled_at')
->whereUserId($appeal->user_id)
->update(['appeal_handled_at' => $now, 'is_spam' => true]);
2024-07-17 10:17:47 +00:00
Cache::forget('pf:bouncer_v0:exemption_by_pid:'.$appeal->user->profile_id);
Cache::forget('pf:bouncer_v0:recent_by_pid:'.$appeal->user->profile_id);
2023-01-15 03:33:46 +00:00
Cache::forget('admin-dash:reports:spam-count');
2024-07-17 10:17:47 +00:00
2023-01-15 03:33:46 +00:00
return $res;
}
2024-07-17 10:17:47 +00:00
if ($action == 'approve') {
2023-01-15 03:33:46 +00:00
$status = $appeal->status;
$status->is_nsfw = $meta->is_nsfw;
$status->scope = 'public';
$status->visibility = 'public';
$status->save();
$appeal->is_spam = false;
$appeal->appeal_handled_at = now();
$appeal->save();
StatusService::del($status->id);
Notification::whereAction('autospam.warning')
->whereProfileId($appeal->user->profile_id)
->get()
2024-07-17 10:17:47 +00:00
->each(function ($n) use ($appeal) {
NotificationService::del($appeal->user->profile_id, $n->id);
$n->forceDelete();
});
2024-07-17 10:17:47 +00:00
Cache::forget('pf:bouncer_v0:exemption_by_pid:'.$appeal->user->profile_id);
Cache::forget('pf:bouncer_v0:recent_by_pid:'.$appeal->user->profile_id);
2023-01-15 03:33:46 +00:00
Cache::forget('admin-dash:reports:spam-count');
2024-07-17 10:17:47 +00:00
2023-01-15 03:33:46 +00:00
return $res;
}
2024-07-17 10:17:47 +00:00
if ($action == 'approve-all') {
2023-01-15 03:33:46 +00:00
AccountInterstitial::whereType('post.autospam')
->whereItemType('App\Status')
->whereNull('appeal_handled_at')
->whereUserId($appeal->user_id)
->get()
2024-07-17 10:17:47 +00:00
->each(function ($report) use ($meta) {
2023-01-15 03:33:46 +00:00
$report->is_spam = false;
$report->appeal_handled_at = now();
$report->save();
$status = Status::find($report->item_id);
2024-07-17 10:17:47 +00:00
if ($status) {
2023-01-15 03:33:46 +00:00
$status->is_nsfw = $meta->is_nsfw;
$status->scope = 'public';
$status->visibility = 'public';
$status->save();
StatusService::del($status->id, true);
}
Notification::whereAction('autospam.warning')
->whereProfileId($report->user->profile_id)
->get()
2024-07-17 10:17:47 +00:00
->each(function ($n) use ($report) {
NotificationService::del($report->user->profile_id, $n->id);
$n->forceDelete();
});
2023-01-15 03:33:46 +00:00
});
2024-07-17 10:17:47 +00:00
Cache::forget('pf:bouncer_v0:exemption_by_pid:'.$appeal->user->profile_id);
Cache::forget('pf:bouncer_v0:recent_by_pid:'.$appeal->user->profile_id);
2023-01-15 03:33:46 +00:00
Cache::forget('admin-dash:reports:spam-count');
2024-07-17 10:17:47 +00:00
2023-01-15 03:33:46 +00:00
return $res;
}
return $res;
}
public function modReports(Request $request)
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404);
2023-01-15 03:33:46 +00:00
$reports = Report::whereNull('admin_seen')
2024-07-17 10:17:47 +00:00
->orderBy('created_at', 'desc')
2023-01-15 03:33:46 +00:00
->paginate(6)
2024-07-17 10:17:47 +00:00
->map(function ($report) {
2023-01-15 03:33:46 +00:00
$r = [
'id' => $report->id,
'type' => $report->type,
'message' => $report->message,
'object_id' => $report->object_id,
'object_type' => $report->object_type,
2024-07-17 10:17:47 +00:00
'created_at' => $report->created_at,
2023-01-15 03:33:46 +00:00
];
2024-07-17 10:17:47 +00:00
if ($report->profile_id) {
2023-01-15 03:33:46 +00:00
$r['reported_by_account'] = AccountService::get($report->profile_id, true);
}
2024-07-17 10:17:47 +00:00
if ($report->object_type === 'App\\Status') {
2023-01-15 03:33:46 +00:00
$status = StatusService::get($report->object_id, false);
2024-07-17 10:17:47 +00:00
if (! $status) {
2023-01-15 03:33:46 +00:00
return;
}
$r['status'] = $status;
2024-07-17 10:17:47 +00:00
if (isset($status['in_reply_to_id'])) {
2023-01-15 03:33:46 +00:00
$r['parent'] = StatusService::get($status['in_reply_to_id'], false);
}
}
2024-07-17 10:17:47 +00:00
if ($report->object_type === 'App\\Profile') {
$acct = AccountService::get($report->object_id, true);
if ($acct) {
2024-07-17 10:16:33 +00:00
$r['account'] = $acct;
}
2023-01-15 03:33:46 +00:00
}
2024-07-17 10:17:47 +00:00
2023-01-15 03:33:46 +00:00
return $r;
})
->filter()
->values();
return $reports;
}
public function modReportHandle(Request $request)
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:write'), 404);
2023-01-15 03:33:46 +00:00
$this->validate($request, [
2024-07-17 10:17:47 +00:00
'action' => 'required|string',
'id' => 'required',
2023-01-15 03:33:46 +00:00
]);
2019-08-20 01:07:25 +00:00
$action = $request->input('action');
2023-01-15 03:33:46 +00:00
$id = $request->input('id');
$actions = [
'ignore',
'cw',
2024-07-17 10:17:47 +00:00
'unlist',
2023-01-15 03:33:46 +00:00
];
2024-07-17 10:17:47 +00:00
if (! in_array($action, $actions)) {
2023-01-15 03:33:46 +00:00
return abort(403);
}
$report = Report::findOrFail($id);
$item = $report->reported();
$report->admin_seen = now();
switch ($action) {
case 'ignore':
$report->not_interested = true;
break;
2019-08-20 01:07:25 +00:00
2023-01-15 03:33:46 +00:00
case 'cw':
Cache::forget('status:thumb:'.$item->id);
$item->is_nsfw = true;
$item->save();
$report->nsfw = true;
StatusService::del($item->id, true);
break;
case 'unlist':
$item->visibility = 'unlisted';
$item->save();
StatusService::del($item->id, true);
break;
default:
$report->admin_seen = null;
break;
2019-08-20 01:07:25 +00:00
}
2023-01-15 03:33:46 +00:00
$report->save();
Cache::forget('admin-dash:reports:list-cache');
Cache::forget('admin:dashboard:home:data:v0:15min');
return ['success' => true];
}
public function getConfiguration(Request $request)
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404);
2023-01-15 03:33:46 +00:00
abort_unless(config('instance.enable_cc'), 400);
return collect([
[
'name' => 'ActivityPub Federation',
'description' => 'Enable activitypub federation support, compatible with Pixelfed, Mastodon and other platforms.',
2024-07-17 10:17:47 +00:00
'key' => 'federation.activitypub.enabled',
2023-01-15 03:33:46 +00:00
],
[
'name' => 'Open Registration',
'description' => 'Allow new account registrations.',
2024-07-17 10:17:47 +00:00
'key' => 'pixelfed.open_registration',
2023-01-15 03:33:46 +00:00
],
[
'name' => 'Stories',
'description' => 'Enable the ephemeral Stories feature.',
2024-07-17 10:17:47 +00:00
'key' => 'instance.stories.enabled',
2023-01-15 03:33:46 +00:00
],
[
'name' => 'Require Email Verification',
'description' => 'Require new accounts to verify their email address.',
2024-07-17 10:17:47 +00:00
'key' => 'pixelfed.enforce_email_verification',
2023-01-15 03:33:46 +00:00
],
[
'name' => 'AutoSpam Detection',
'description' => 'Detect and remove spam from public timelines.',
2024-07-17 10:17:47 +00:00
'key' => 'pixelfed.bouncer.enabled',
2023-01-15 03:33:46 +00:00
],
])
2024-07-17 10:17:47 +00:00
->map(function ($s) {
$s['state'] = (bool) config_cache($s['key']);
return $s;
});
2019-08-20 01:07:25 +00:00
}
2023-01-15 03:33:46 +00:00
public function updateConfiguration(Request $request)
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:write'), 404);
2023-01-15 03:33:46 +00:00
abort_unless(config('instance.enable_cc'), 400);
$this->validate($request, [
'key' => 'required',
2024-07-17 10:17:47 +00:00
'value' => 'required',
2023-01-15 03:33:46 +00:00
]);
$allowedKeys = [
'federation.activitypub.enabled',
'pixelfed.open_registration',
'instance.stories.enabled',
'pixelfed.enforce_email_verification',
'pixelfed.bouncer.enabled',
];
$key = $request->input('key');
$value = (bool) filter_var($request->input('value'), FILTER_VALIDATE_BOOLEAN);
2024-07-17 10:17:47 +00:00
abort_if(! in_array($key, $allowedKeys), 400, 'Invalid cache key.');
2023-01-15 03:33:46 +00:00
ConfigCacheService::put($key, $value);
2024-07-17 10:17:47 +00:00
return collect([
2023-01-15 03:33:46 +00:00
[
'name' => 'ActivityPub Federation',
'description' => 'Enable activitypub federation support, compatible with Pixelfed, Mastodon and other platforms.',
2024-07-17 10:17:47 +00:00
'key' => 'federation.activitypub.enabled',
2023-01-15 03:33:46 +00:00
],
[
'name' => 'Open Registration',
'description' => 'Allow new account registrations.',
2024-07-17 10:17:47 +00:00
'key' => 'pixelfed.open_registration',
2023-01-15 03:33:46 +00:00
],
[
'name' => 'Stories',
'description' => 'Enable the ephemeral Stories feature.',
2024-07-17 10:17:47 +00:00
'key' => 'instance.stories.enabled',
2023-01-15 03:33:46 +00:00
],
[
'name' => 'Require Email Verification',
'description' => 'Require new accounts to verify their email address.',
2024-07-17 10:17:47 +00:00
'key' => 'pixelfed.enforce_email_verification',
2023-01-15 03:33:46 +00:00
],
[
'name' => 'AutoSpam Detection',
'description' => 'Detect and remove spam from public timelines.',
2024-07-17 10:17:47 +00:00
'key' => 'pixelfed.bouncer.enabled',
2023-01-15 03:33:46 +00:00
],
])
2024-07-17 10:17:47 +00:00
->map(function ($s) {
$s['state'] = (bool) config_cache($s['key']);
return $s;
});
2023-01-15 03:33:46 +00:00
}
public function getUsers(Request $request)
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404);
$this->validate($request, [
'sort' => 'sometimes|in:asc,desc',
]);
2023-01-15 03:33:46 +00:00
$q = $request->input('q');
$sort = $request->input('sort', 'desc') === 'asc' ? 'asc' : 'desc';
$res = User::whereNull('status')
2024-07-17 10:17:47 +00:00
->when($q, function ($query, $q) {
return $query->where('username', 'like', '%'.$q.'%');
2023-01-15 03:33:46 +00:00
})
->orderBy('id', $sort)
->cursorPaginate(10);
2024-07-17 10:17:47 +00:00
2023-01-15 03:33:46 +00:00
return AdminUser::collection($res);
}
public function getUser(Request $request)
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404);
2023-01-15 03:33:46 +00:00
$id = $request->input('user_id');
2024-07-17 10:17:47 +00:00
$key = 'pf-admin-api:getUser:byId:'.$id;
if ($request->has('refresh')) {
Cache::forget($key);
}
2024-07-17 10:17:47 +00:00
return Cache::remember($key, 86400, function () use ($id) {
$user = User::findOrFail($id);
$profile = $user->profile;
$account = AccountService::get($user->profile_id, true);
$res = (new AdminUser($user))->additional(['meta' => [
'cached_at' => str_replace('+00:00', 'Z', now()->format(DATE_RFC3339_EXTENDED)),
'account' => $account,
'dms_sent' => Conversation::whereFromId($profile->id)->count(),
'report_count' => Report::where('object_id', $profile->id)->orWhere('reported_profile_id', $profile->id)->count(),
'remote_report_count' => RemoteReport::whereAccountId($profile->id)->count(),
'moderation' => [
'unlisted' => (bool) $profile->unlisted,
'cw' => (bool) $profile->cw,
2024-07-17 10:17:47 +00:00
'no_autolink' => (bool) $profile->no_autolink,
],
]]);
return $res;
});
2023-01-15 03:33:46 +00:00
}
public function userAdminAction(Request $request)
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:write'), 404);
2023-01-15 03:33:46 +00:00
$this->validate($request, [
'id' => 'required',
'action' => 'required|in:unlisted,cw,no_autolink,refresh_stats,verify_email,delete',
2024-07-17 10:17:47 +00:00
'value' => 'sometimes',
2023-01-15 03:33:46 +00:00
]);
$id = $request->input('id');
$user = User::findOrFail($id);
$profile = Profile::findOrFail($user->profile_id);
$action = $request->input('action');
abort_if($user->is_admin == true && $action !== 'refresh_stats', 400, 'Cannot moderate admin accounts');
2024-07-17 10:17:47 +00:00
if ($action === 'delete') {
if (config('pixelfed.account_deletion') == false) {
abort(404);
}
abort_if($user->is_admin, 400, 'Cannot delete an admin account.');
$ts = now()->addMonth();
$user->status = 'delete';
$user->delete_after = $ts;
$user->save();
$profile->status = 'delete';
$profile->delete_after = $ts;
$profile->save();
ModLogService::boot()
->objectUid($profile->id)
->objectId($profile->id)
->objectType('App\Profile::class')
->user($request->user())
->action('admin.user.delete')
->accessLevel('admin')
->save();
PublicTimelineService::deleteByProfileId($profile->id);
NetworkTimelineService::deleteByProfileId($profile->id);
2024-07-17 10:17:47 +00:00
if ($profile->user_id) {
DB::table('oauth_access_tokens')->whereUserId($user->id)->delete();
DB::table('oauth_auth_codes')->whereUserId($user->id)->delete();
$user->email = $user->id;
$user->password = '';
$user->status = 'delete';
$user->save();
$profile->status = 'delete';
$profile->delete_after = now()->addMonth();
$profile->save();
AccountService::del($profile->id);
DeleteAccountPipeline::dispatch($user)->onQueue('high');
} else {
$profile->status = 'delete';
$profile->delete_after = now()->addMonth();
$profile->save();
AccountService::del($profile->id);
DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('high');
}
2024-07-17 10:17:47 +00:00
return [
'status' => 200,
'msg' => 'deleted',
];
2024-07-17 10:17:47 +00:00
} elseif ($action === 'refresh_stats') {
2023-01-15 03:33:46 +00:00
$profile->following_count = DB::table('followers')->whereProfileId($user->profile_id)->count();
$profile->followers_count = DB::table('followers')->whereFollowingId($user->profile_id)->count();
$statusCount = Status::whereProfileId($user->profile_id)
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->whereIn('scope', ['public', 'unlisted', 'private'])
->count();
$profile->status_count = $statusCount;
$profile->save();
2024-07-17 10:17:47 +00:00
} elseif ($action === 'verify_email') {
2023-01-15 03:33:46 +00:00
$user->email_verified_at = now();
$user->save();
ModLogService::boot()
->objectUid($user->id)
->objectId($user->id)
->objectType('App\User::class')
->user($request->user())
->action('admin.user.moderate')
->metadata([
'action' => 'Manually verified email address',
2024-07-17 10:17:47 +00:00
'message' => 'Success!',
2023-01-15 03:33:46 +00:00
])
->accessLevel('admin')
->save();
2024-07-17 10:17:47 +00:00
} elseif ($action === 'unlisted') {
ModLogService::boot()
->objectUid($profile->id)
->objectId($profile->id)
->objectType('App\Profile::class')
->user($request->user())
->action('admin.user.moderate')
->metadata([
'action' => $action,
2024-07-17 10:17:47 +00:00
'message' => 'Success!',
])
->accessLevel('admin')
->save();
2024-07-17 10:17:47 +00:00
$profile->unlisted = ! $profile->unlisted;
$profile->save();
2024-07-17 10:17:47 +00:00
} elseif ($action === 'cw') {
ModLogService::boot()
->objectUid($profile->id)
->objectId($profile->id)
->objectType('App\Profile::class')
->user($request->user())
->action('admin.user.moderate')
->metadata([
'action' => $action,
2024-07-17 10:17:47 +00:00
'message' => 'Success!',
])
->accessLevel('admin')
->save();
2024-07-17 10:17:47 +00:00
$profile->cw = ! $profile->cw;
$profile->save();
2024-07-17 10:17:47 +00:00
} elseif ($action === 'no_autolink') {
ModLogService::boot()
->objectUid($profile->id)
->objectId($profile->id)
->objectType('App\Profile::class')
->user($request->user())
->action('admin.user.moderate')
->metadata([
'action' => $action,
2024-07-17 10:17:47 +00:00
'message' => 'Success!',
])
->accessLevel('admin')
->save();
2024-07-17 10:17:47 +00:00
$profile->no_autolink = ! $profile->no_autolink;
$profile->save();
2023-01-15 03:33:46 +00:00
} else {
$profile->{$action} = filter_var($request->input('value'), FILTER_VALIDATE_BOOLEAN);
$profile->save();
ModLogService::boot()
->objectUid($user->id)
->objectId($user->id)
->objectType('App\User::class')
->user($request->user())
->action('admin.user.moderate')
->metadata([
'action' => $action,
2024-07-17 10:17:47 +00:00
'message' => 'Success!',
2023-01-15 03:33:46 +00:00
])
->accessLevel('admin')
->save();
}
AccountService::del($user->profile_id);
$account = AccountService::get($user->profile_id, true);
return (new AdminUser($user))->additional(['meta' => [
'account' => $account,
'moderation' => [
'unlisted' => (bool) $profile->unlisted,
'cw' => (bool) $profile->cw,
2024-07-17 10:17:47 +00:00
'no_autolink' => (bool) $profile->no_autolink,
],
2023-01-15 03:33:46 +00:00
]]);
}
2023-01-15 09:36:55 +00:00
public function instances(Request $request)
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:write'), 404);
2023-01-15 09:36:55 +00:00
$this->validate($request, [
'q' => 'sometimes',
'sort' => 'sometimes|in:asc,desc',
'sort_by' => 'sometimes|in:id,status_count,user_count,domain',
'filter' => 'sometimes|in:all,unlisted,auto_cw,banned',
]);
$q = $request->input('q');
$sort = $request->input('sort', 'desc') === 'asc' ? 'asc' : 'desc';
$sortBy = $request->input('sort_by', 'id');
$filter = $request->input('filter');
2024-07-17 10:17:47 +00:00
$res = Instance::when($q, function ($query, $q) {
return $query->where('domain', 'like', '%'.$q.'%');
})
->when($filter, function ($query, $filter) {
if ($filter === 'all') {
2023-01-15 09:36:55 +00:00
return $query;
} else {
return $query->where($filter, true);
}
})
2024-07-17 10:17:47 +00:00
->when($sortBy, function ($query, $sortBy) use ($sort) {
2023-01-15 09:36:55 +00:00
return $query->orderBy($sortBy, $sort);
2024-07-17 10:17:47 +00:00
}, function ($query) {
2023-01-15 09:36:55 +00:00
return $query->orderBy('id', 'desc');
})
->cursorPaginate(10)
->withQueryString();
return AdminInstance::collection($res);
}
public function getInstance(Request $request)
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404);
2023-01-15 09:36:55 +00:00
$id = $request->input('id');
$res = Instance::findOrFail($id);
return new AdminInstance($res);
}
public function moderateInstance(Request $request)
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:write'), 404);
2023-01-15 09:36:55 +00:00
$this->validate($request, [
'id' => 'required',
'key' => 'required|in:unlisted,auto_cw,banned',
2024-07-17 10:17:47 +00:00
'value' => 'required',
2023-01-15 09:36:55 +00:00
]);
$id = $request->input('id');
$key = $request->input('key');
$value = (bool) filter_var($request->input('value'), FILTER_VALIDATE_BOOLEAN);
$res = Instance::findOrFail($id);
$res->{$key} = $value;
$res->save();
InstanceService::refresh();
NetworkTimelineService::warmCache(true);
return new AdminInstance($res);
}
public function refreshInstanceStats(Request $request)
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:write'), 404);
2023-01-15 09:36:55 +00:00
$this->validate($request, [
'id' => 'required',
]);
$id = $request->input('id');
$instance = Instance::findOrFail($id);
$instance->user_count = Profile::whereDomain($instance->domain)->count();
$instance->status_count = Profile::whereDomain($instance->domain)->leftJoin('statuses', 'profiles.id', '=', 'statuses.profile_id')->count();
$instance->save();
return new AdminInstance($instance);
}
public function getAllStats(Request $request)
{
2024-07-17 10:17:47 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin === 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404);
2024-07-17 10:17:47 +00:00
if ($request->has('refresh')) {
2023-06-20 11:09:15 +00:00
Cache::forget('admin-api:instance-all-stats-v1');
}
2024-07-17 10:17:47 +00:00
return Cache::remember('admin-api:instance-all-stats-v1', 1209600, function () {
2023-06-20 11:09:15 +00:00
$days = range(1, 7);
$res = [
'cached_at' => now()->format('c'),
];
2023-06-20 11:09:15 +00:00
$minStatusId = SnowflakeService::byDate(now()->subDays(7));
2024-07-17 10:17:47 +00:00
foreach ($days as $day) {
$label = now()->subDays($day)->format('D');
$labelShort = substr($label, 0, 1);
$res['users']['days'][] = [
'date' => now()->subDays($day)->format('M j Y'),
'label_full' => $label,
'label' => $labelShort,
2024-07-17 10:17:47 +00:00
'count' => User::whereDate('created_at', now()->subDays($day))->count(),
];
$res['posts']['days'][] = [
'date' => now()->subDays($day)->format('M j Y'),
'label_full' => $label,
'label' => $labelShort,
2024-07-17 10:17:47 +00:00
'count' => Status::whereNull('uri')->where('id', '>', $minStatusId)->whereDate('created_at', now()->subDays($day))->count(),
];
$res['instances']['days'][] = [
'date' => now()->subDays($day)->format('M j Y'),
'label_full' => $label,
'label' => $labelShort,
2024-07-17 10:17:47 +00:00
'count' => Instance::whereDate('created_at', now()->subDays($day))->count(),
];
}
$res['users']['total'] = DB::table('users')->count();
$res['users']['min'] = collect($res['users']['days'])->min('count');
2023-06-20 11:09:15 +00:00
$res['users']['max'] = collect($res['users']['days'])->max('count');
2024-07-17 10:17:47 +00:00
$res['users']['change'] = collect($res['users']['days'])->sum('count');
$res['posts']['total'] = DB::table('statuses')->whereNull('uri')->count();
$res['posts']['min'] = collect($res['posts']['days'])->min('count');
$res['posts']['max'] = collect($res['posts']['days'])->max('count');
2023-06-20 11:09:15 +00:00
$res['posts']['change'] = collect($res['posts']['days'])->sum('count');
$res['instances']['total'] = DB::table('instances')->count();
$res['instances']['min'] = collect($res['instances']['days'])->min('count');
$res['instances']['max'] = collect($res['instances']['days'])->max('count');
2023-06-20 11:09:15 +00:00
$res['instances']['change'] = collect($res['instances']['days'])->sum('count');
return $res;
});
}
2023-01-15 03:33:46 +00:00
}