mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-22 06:21:27 +00:00
Update FixStatusCount, improve command and support remote count resync
This commit is contained in:
parent
2f2e446c1f
commit
04f4f8baf1
2 changed files with 125 additions and 12 deletions
|
@ -4,6 +4,7 @@ namespace App\Console\Commands;
|
||||||
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use App\Profile;
|
use App\Profile;
|
||||||
|
use App\Services\AccountService;
|
||||||
|
|
||||||
class FixStatusCount extends Command
|
class FixStatusCount extends Command
|
||||||
{
|
{
|
||||||
|
@ -12,7 +13,7 @@ class FixStatusCount extends Command
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $signature = 'fix:statuscount';
|
protected $signature = 'fix:statuscount {--remote} {--resync} {--remote-only} {--dlog}';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The console command description.
|
* The console command description.
|
||||||
|
@ -38,18 +39,100 @@ class FixStatusCount extends Command
|
||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
Profile::whereNull('domain')
|
if(!$this->confirm('Are you sure you want to run the fix status command?')) {
|
||||||
->chunk(50, function($profiles) {
|
return;
|
||||||
foreach($profiles as $profile) {
|
}
|
||||||
$profile->status_count = $profile->statuses()
|
$this->line(' ');
|
||||||
->getQuery()
|
$this->info('Running fix status command...');
|
||||||
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
|
$now = now();
|
||||||
->whereNull('in_reply_to_id')
|
|
||||||
->whereNull('reblog_of_id')
|
$nulls = ['domain', 'status', 'last_fetched_at'];
|
||||||
->count();
|
|
||||||
$profile->save();
|
$resync = $this->option('resync');
|
||||||
|
$resync24hours = false;
|
||||||
|
|
||||||
|
if($resync) {
|
||||||
|
$resyncChoices = ['Only resync accounts that havent been synced in 24 hours', 'Resync all accounts'];
|
||||||
|
$rsc = $this->choice(
|
||||||
|
'Do you want to resync all accounts, or just accounts that havent been resynced for 24 hours?',
|
||||||
|
$resyncChoices,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
$rsci = array_search($rsc, $resyncChoices);
|
||||||
|
if($rsci === 0) {
|
||||||
|
$resync24hours = true;
|
||||||
|
$nulls = ['status', 'domain', 'last_fetched_at'];
|
||||||
|
} else {
|
||||||
|
$resync24hours = false;
|
||||||
|
$nulls = ['status', 'domain'];
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
|
$remote = $this->option('remote');
|
||||||
|
|
||||||
|
if($remote) {
|
||||||
|
$ni = array_search('domain', $nulls);
|
||||||
|
unset($nulls[$ni]);
|
||||||
|
$ni = array_search('last_fetched_at', $nulls);
|
||||||
|
unset($nulls[$ni]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$remoteOnly = $this->option('remote-only');
|
||||||
|
|
||||||
|
if($remoteOnly) {
|
||||||
|
$ni = array_search('domain', $nulls);
|
||||||
|
unset($nulls[$ni]);
|
||||||
|
$ni = array_search('last_fetched_at', $nulls);
|
||||||
|
unset($nulls[$ni]);
|
||||||
|
$nulls[] = 'user_id';
|
||||||
|
}
|
||||||
|
|
||||||
|
$dlog = $this->option('dlog');
|
||||||
|
|
||||||
|
$nulls = array_values($nulls);
|
||||||
|
|
||||||
|
foreach(
|
||||||
|
Profile::when($resync24hours, function($query, $resync24hours) use($nulls) {
|
||||||
|
if(in_array('domain', $nulls)) {
|
||||||
|
return $query->whereNull('domain')
|
||||||
|
->whereNull('last_fetched_at')
|
||||||
|
->orWhere('last_fetched_at', '<', now()->subHours(24));
|
||||||
|
} else {
|
||||||
|
return $query->whereNull('last_fetched_at')
|
||||||
|
->orWhere('last_fetched_at', '<', now()->subHours(24));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
->when($remoteOnly, function($query, $remoteOnly) {
|
||||||
|
return $query->whereNull('last_fetched_at')
|
||||||
|
->orWhere('last_fetched_at', '<', now()->subHours(24));
|
||||||
|
})
|
||||||
|
->whereNull($nulls)
|
||||||
|
->lazyById(50, 'id') as $profile
|
||||||
|
) {
|
||||||
|
$ogc = $profile->status_count;
|
||||||
|
$upc = $profile->statuses()
|
||||||
|
->getQuery()
|
||||||
|
->whereIn('scope', ['public', 'private', 'unlisted'])
|
||||||
|
->count();
|
||||||
|
if($ogc != $upc) {
|
||||||
|
$profile->status_count = $upc;
|
||||||
|
$profile->last_fetched_at = $now;
|
||||||
|
$profile->save();
|
||||||
|
AccountService::del($profile->id);
|
||||||
|
if($dlog) {
|
||||||
|
$this->info($profile->id . ':' . $profile->username . ' : ' . $upc);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$profile->last_fetched_at = $now;
|
||||||
|
$profile->save();
|
||||||
|
if($dlog) {
|
||||||
|
$this->info($profile->id . ':' . $profile->username . ' : ' . $upc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->line(' ');
|
||||||
|
$this->info('Finished fix status count command!');
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::table('profiles', function (Blueprint $table) {
|
||||||
|
$table->index('user_id', 'profiles_user_id_index');
|
||||||
|
$table->index('last_fetched_at', 'profiles_last_fetched_at_index');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table('profiles', function (Blueprint $table) {
|
||||||
|
$table->dropIndex('profiles_user_id_index');
|
||||||
|
$table->dropIndex('profiles_last_fetched_at_index');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
Loading…
Reference in a new issue