123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use App\Instance;
- use App\Profile;
- use App\Services\InstanceService;
- use App\Jobs\InstancePipeline\FetchNodeinfoPipeline;
- use function Laravel\Prompts\select;
- use function Laravel\Prompts\confirm;
- use function Laravel\Prompts\progress;
- use function Laravel\Prompts\search;
- use function Laravel\Prompts\table;
- class InstanceManager extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'app:instance-manager';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Manage Instances';
- /**
- * Execute the console command.
- */
- public function handle()
- {
- $action = select(
- 'What action do you want to perform?',
- [
- 'Recalculate Stats',
- 'Ban Instance',
- 'Unlist Instance',
- 'Unlisted Instances',
- 'Banned Instances',
- 'Unban Instance',
- 'Relist Instance',
- ],
- );
- switch($action) {
- case 'Recalculate Stats':
- return $this->recalculateStats();
- break;
- case 'Unlisted Instances':
- return $this->viewUnlistedInstances();
- break;
- case 'Banned Instances':
- return $this->viewBannedInstances();
- break;
- case 'Unlist Instance':
- return $this->unlistInstance();
- break;
- case 'Ban Instance':
- return $this->banInstance();
- break;
- case 'Unban Instance':
- return $this->unbanInstance();
- break;
- case 'Relist Instance':
- return $this->relistInstance();
- break;
- }
- }
- protected function recalculateStats()
- {
- $instanceCount = Instance::count();
- $confirmed = confirm('Do you want to recalculate stats for all ' . $instanceCount . ' instances?');
- if(!$confirmed) {
- $this->error('Aborting...');
- exit;
- }
- $users = progress(
- label: 'Updating instance stats...',
- steps: Instance::all(),
- callback: fn ($instance) => $this->updateInstanceStats($instance),
- );
- }
- protected function updateInstanceStats($instance)
- {
- FetchNodeinfoPipeline::dispatch($instance)->onQueue('intbg');
- }
- protected function unlistInstance()
- {
- $id = search(
- 'Search by domain',
- fn (string $value) => strlen($value) > 0
- ? Instance::whereUnlisted(false)->where('domain', 'like', "%{$value}%")->pluck('domain', 'id')->all()
- : []
- );
- $instance = Instance::find($id);
- if(!$instance) {
- $this->error('Oops, an error occured');
- exit;
- }
- $tbl = [
- [
- $instance->domain,
- number_format($instance->status_count),
- number_format($instance->user_count),
- ]
- ];
- table(
- ['Domain', 'Status Count', 'User Count'],
- $tbl
- );
- $confirmed = confirm('Are you sure you want to unlist this instance?');
- if(!$confirmed) {
- $this->error('Aborting instance unlisting');
- exit;
- }
- $instance->unlisted = true;
- $instance->save();
- InstanceService::refresh();
- $this->info('Successfully unlisted ' . $instance->domain . '!');
- exit;
- }
- protected function relistInstance()
- {
- $id = search(
- 'Search by domain',
- fn (string $value) => strlen($value) > 0
- ? Instance::whereUnlisted(true)->where('domain', 'like', "%{$value}%")->pluck('domain', 'id')->all()
- : []
- );
- $instance = Instance::find($id);
- if(!$instance) {
- $this->error('Oops, an error occured');
- exit;
- }
- $tbl = [
- [
- $instance->domain,
- number_format($instance->status_count),
- number_format($instance->user_count),
- ]
- ];
- table(
- ['Domain', 'Status Count', 'User Count'],
- $tbl
- );
- $confirmed = confirm('Are you sure you want to re-list this instance?');
- if(!$confirmed) {
- $this->error('Aborting instance re-listing');
- exit;
- }
- $instance->unlisted = false;
- $instance->save();
- InstanceService::refresh();
- $this->info('Successfully re-listed ' . $instance->domain . '!');
- exit;
- }
- protected function banInstance()
- {
- $id = search(
- 'Search by domain',
- fn (string $value) => strlen($value) > 0
- ? Instance::whereBanned(false)->where('domain', 'like', "%{$value}%")->pluck('domain', 'id')->all()
- : []
- );
- $instance = Instance::find($id);
- if(!$instance) {
- $this->error('Oops, an error occured');
- exit;
- }
- $tbl = [
- [
- $instance->domain,
- number_format($instance->status_count),
- number_format($instance->user_count),
- ]
- ];
- table(
- ['Domain', 'Status Count', 'User Count'],
- $tbl
- );
- $confirmed = confirm('Are you sure you want to ban this instance?');
- if(!$confirmed) {
- $this->error('Aborting instance ban');
- exit;
- }
- $instance->banned = true;
- $instance->save();
- InstanceService::refresh();
- $this->info('Successfully banned ' . $instance->domain . '!');
- exit;
- }
- protected function unbanInstance()
- {
- $id = search(
- 'Search by domain',
- fn (string $value) => strlen($value) > 0
- ? Instance::whereBanned(true)->where('domain', 'like', "%{$value}%")->pluck('domain', 'id')->all()
- : []
- );
- $instance = Instance::find($id);
- if(!$instance) {
- $this->error('Oops, an error occured');
- exit;
- }
- $tbl = [
- [
- $instance->domain,
- number_format($instance->status_count),
- number_format($instance->user_count),
- ]
- ];
- table(
- ['Domain', 'Status Count', 'User Count'],
- $tbl
- );
- $confirmed = confirm('Are you sure you want to unban this instance?');
- if(!$confirmed) {
- $this->error('Aborting instance unban');
- exit;
- }
- $instance->banned = false;
- $instance->save();
- InstanceService::refresh();
- $this->info('Successfully un-banned ' . $instance->domain . '!');
- exit;
- }
- protected function viewBannedInstances()
- {
- $data = Instance::whereBanned(true)
- ->get(['domain', 'user_count', 'status_count'])
- ->map(function($d) {
- return [
- 'domain' => $d->domain,
- 'user_count' => number_format($d->user_count),
- 'status_count' => number_format($d->status_count),
- ];
- })
- ->toArray();
- table(
- ['Domain', 'User Count', 'Status Count'],
- $data
- );
- }
- protected function viewUnlistedInstances()
- {
- $data = Instance::whereUnlisted(true)
- ->get(['domain', 'user_count', 'status_count', 'banned'])
- ->map(function($d) {
- return [
- 'domain' => $d->domain,
- 'user_count' => number_format($d->user_count),
- 'status_count' => number_format($d->status_count),
- 'banned' => $d->banned ? '✅' : null
- ];
- })
- ->toArray();
- table(
- ['Domain', 'User Count', 'Status Count', 'Banned'],
- $data
- );
- }
- }
|