1
0

AvatarSync.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Avatar;
  5. use App\Profile;
  6. use App\Jobs\AvatarPipeline\RemoteAvatarFetch;
  7. use App\Util\ActivityPub\Helpers;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Str;
  10. class AvatarSync extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'avatars:sync';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Perform actions on avatars';
  24. public $found = 0;
  25. public $notFetched = 0;
  26. public $fixed = 0;
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return int
  40. */
  41. public function handle()
  42. {
  43. $this->info('Welcome to the avatar sync manager');
  44. $this->line(' ');
  45. $this->line(' ');
  46. $this->error('This command is deprecated and will be removed in a future version');
  47. $this->error('You should use the following command instead: ');
  48. $this->line(' ');
  49. $this->info('php artisan avatar:storage');
  50. $this->line(' ');
  51. $confirm = $this->confirm('Are you sure you want to use this deprecated command even though it is no longer supported?');
  52. if(!$confirm) {
  53. return;
  54. }
  55. $actions = [
  56. 'Analyze',
  57. 'Full Analyze',
  58. 'Fetch - Fetch missing remote avatars',
  59. 'Fix - Fix remote accounts without avatar record',
  60. 'Sync - Store latest remote avatars',
  61. ];
  62. $name = $this->choice(
  63. 'Select an action',
  64. $actions,
  65. 0,
  66. 1,
  67. false
  68. );
  69. $this->info('Selected: ' . $name);
  70. switch($name) {
  71. case $actions[0]:
  72. $this->analyze();
  73. break;
  74. case $actions[1]:
  75. $this->fullAnalyze();
  76. break;
  77. case $actions[2]:
  78. $this->fetch();
  79. break;
  80. case $actions[3]:
  81. $this->fix();
  82. break;
  83. case $actions[4]:
  84. $this->sync();
  85. break;
  86. }
  87. return Command::SUCCESS;
  88. }
  89. protected function incr($name)
  90. {
  91. switch($name) {
  92. case 'found':
  93. $this->found = $this->found + 1;
  94. break;
  95. case 'notFetched':
  96. $this->notFetched = $this->notFetched + 1;
  97. break;
  98. case 'fixed':
  99. $this->fixed++;
  100. break;
  101. }
  102. }
  103. protected function analyze()
  104. {
  105. $count = Avatar::whereIsRemote(true)->whereNull('cdn_url')->count();
  106. $this->info('Found ' . $count . ' profiles with blank avatars.');
  107. $this->line(' ');
  108. $this->comment('We suggest running php artisan avatars:sync again and selecting the sync option');
  109. $this->line(' ');
  110. }
  111. protected function fullAnalyze()
  112. {
  113. $count = Profile::count();
  114. $bar = $this->output->createProgressBar($count);
  115. $bar->start();
  116. Profile::chunk(50, function($profiles) use ($bar) {
  117. foreach($profiles as $profile) {
  118. if($profile->domain == null) {
  119. $bar->advance();
  120. continue;
  121. }
  122. $avatar = Avatar::whereProfileId($profile->id)->first();
  123. if(!$avatar || $avatar->cdn_url == null) {
  124. $this->incr('notFetched');
  125. }
  126. $this->incr('found');
  127. $bar->advance();
  128. }
  129. });
  130. $this->line(' ');
  131. $this->line(' ');
  132. $this->info('Found ' . $this->found . ' remote accounts');
  133. $this->info('Found ' . $this->notFetched . ' remote avatars to fetch');
  134. }
  135. protected function fetch()
  136. {
  137. $this->error('This action has been deprecated, please run the following command instead:');
  138. $this->line(' ');
  139. $this->info('php artisan avatar:storage');
  140. $this->line(' ');
  141. return;
  142. }
  143. protected function fix()
  144. {
  145. Profile::chunk(5000, function($profiles) {
  146. foreach($profiles as $profile) {
  147. if($profile->domain == null || $profile->private_key) {
  148. continue;
  149. }
  150. $avatar = Avatar::whereProfileId($profile->id)->first();
  151. if($avatar) {
  152. continue;
  153. }
  154. $avatar = new Avatar;
  155. $avatar->is_remote = true;
  156. $avatar->profile_id = $profile->id;
  157. $avatar->save();
  158. $this->incr('fixed');
  159. }
  160. });
  161. $this->line(' ');
  162. $this->line(' ');
  163. $this->info('Fixed ' . $this->fixed . ' accounts with a blank avatar');
  164. }
  165. protected function sync()
  166. {
  167. $this->error('This action has been deprecated, please run the following command instead:');
  168. $this->line(' ');
  169. $this->info('php artisan avatar:storage');
  170. $this->line(' ');
  171. return;
  172. }
  173. }