1
0

FixStatusCount.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Profile;
  5. use App\Services\AccountService;
  6. class FixStatusCount extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'fix:statuscount {--remote} {--resync} {--remote-only} {--dlog}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'fix profile status count';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. if(!$this->confirm('Are you sure you want to run the fix status command?')) {
  37. return;
  38. }
  39. $this->line(' ');
  40. $this->info('Running fix status command...');
  41. $now = now();
  42. $nulls = ['domain', 'status', 'last_fetched_at'];
  43. $resync = $this->option('resync');
  44. $resync24hours = false;
  45. if($resync) {
  46. $resyncChoices = ['Only resync accounts that havent been synced in 24 hours', 'Resync all accounts'];
  47. $rsc = $this->choice(
  48. 'Do you want to resync all accounts, or just accounts that havent been resynced for 24 hours?',
  49. $resyncChoices,
  50. 0
  51. );
  52. $rsci = array_search($rsc, $resyncChoices);
  53. if($rsci === 0) {
  54. $resync24hours = true;
  55. $nulls = ['status', 'domain', 'last_fetched_at'];
  56. } else {
  57. $resync24hours = false;
  58. $nulls = ['status', 'domain'];
  59. }
  60. }
  61. $remote = $this->option('remote');
  62. if($remote) {
  63. $ni = array_search('domain', $nulls);
  64. unset($nulls[$ni]);
  65. $ni = array_search('last_fetched_at', $nulls);
  66. unset($nulls[$ni]);
  67. }
  68. $remoteOnly = $this->option('remote-only');
  69. if($remoteOnly) {
  70. $ni = array_search('domain', $nulls);
  71. unset($nulls[$ni]);
  72. $ni = array_search('last_fetched_at', $nulls);
  73. unset($nulls[$ni]);
  74. $nulls[] = 'user_id';
  75. }
  76. $dlog = $this->option('dlog');
  77. $nulls = array_values($nulls);
  78. foreach(
  79. Profile::when($resync24hours, function($query, $resync24hours) use($nulls) {
  80. if(in_array('domain', $nulls)) {
  81. return $query->whereNull('domain')
  82. ->whereNull('last_fetched_at')
  83. ->orWhere('last_fetched_at', '<', now()->subHours(24));
  84. } else {
  85. return $query->whereNull('last_fetched_at')
  86. ->orWhere('last_fetched_at', '<', now()->subHours(24));
  87. }
  88. })
  89. ->when($remoteOnly, function($query, $remoteOnly) {
  90. return $query->whereNull('last_fetched_at')
  91. ->orWhere('last_fetched_at', '<', now()->subHours(24));
  92. })
  93. ->whereNull($nulls)
  94. ->lazyById(50, 'id') as $profile
  95. ) {
  96. $ogc = $profile->status_count;
  97. $upc = $profile->statuses()
  98. ->getQuery()
  99. ->whereIn('scope', ['public', 'private', 'unlisted'])
  100. ->count();
  101. if($ogc != $upc) {
  102. $profile->status_count = $upc;
  103. $profile->last_fetched_at = $now;
  104. $profile->save();
  105. AccountService::del($profile->id);
  106. if($dlog) {
  107. $this->info($profile->id . ':' . $profile->username . ' : ' . $upc);
  108. }
  109. } else {
  110. $profile->last_fetched_at = $now;
  111. $profile->save();
  112. if($dlog) {
  113. $this->info($profile->id . ':' . $profile->username . ' : ' . $upc);
  114. }
  115. }
  116. }
  117. $this->line(' ');
  118. $this->info('Finished fix status count command!');
  119. return 0;
  120. }
  121. }