FixMissingUserProfile.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Jobs\AvatarPipeline\CreateAvatar;
  5. use App\Follower;
  6. use App\Profile;
  7. use App\User;
  8. use App\UserSetting;
  9. use App\Services\UserFilterService;
  10. use App\Models\DefaultDomainBlock;
  11. use App\Models\UserDomainBlock;
  12. use App\Jobs\FollowPipeline\FollowPipeline;
  13. use DB;
  14. use App\Services\FollowerService;
  15. use function Laravel\Prompts\search;
  16. class FixMissingUserProfile extends Command
  17. {
  18. /**
  19. * The name and signature of the console command.
  20. *
  21. * @var string
  22. */
  23. protected $signature = 'app:fix-missing-user-profile';
  24. /**
  25. * The console command description.
  26. *
  27. * @var string
  28. */
  29. protected $description = 'Command description';
  30. /**
  31. * Execute the console command.
  32. */
  33. public function handle()
  34. {
  35. $id = search(
  36. label: 'Search for the affected username',
  37. options: fn (string $value) => strlen($value) > 0
  38. ? User::doesntHave('profile')->whereNull('status')->whereLike('username', "%{$value}%")->pluck('username', 'id')->all()
  39. : []
  40. );
  41. if(!$id) {
  42. $this->error('Found none');
  43. }
  44. $user = User::find($id);
  45. if($user->profile) {
  46. $this->error('Already has profile');
  47. return;
  48. }
  49. if(in_array($user->status, ['deleted', 'delete'])) {
  50. $this->error('User has deleted account');
  51. return;
  52. }
  53. if(Profile::whereUsername($user->username)->exists()) {
  54. $this->error('Already has profile');
  55. return;
  56. }
  57. if (empty($user->profile)) {
  58. $profile = DB::transaction(function() use($user) {
  59. $profile = new Profile();
  60. $profile->user_id = $user->id;
  61. $profile->username = $user->username;
  62. $profile->name = $user->name;
  63. $pkiConfig = [
  64. 'digest_alg' => 'sha512',
  65. 'private_key_bits' => 2048,
  66. 'private_key_type' => OPENSSL_KEYTYPE_RSA,
  67. ];
  68. $pki = openssl_pkey_new($pkiConfig);
  69. openssl_pkey_export($pki, $pki_private);
  70. $pki_public = openssl_pkey_get_details($pki);
  71. $pki_public = $pki_public['key'];
  72. $profile->private_key = $pki_private;
  73. $profile->public_key = $pki_public;
  74. $profile->save();
  75. $this->applyDefaultDomainBlocks($user);
  76. return $profile;
  77. });
  78. DB::transaction(function() use($user, $profile) {
  79. $user = User::findOrFail($user->id);
  80. $user->profile_id = $profile->id;
  81. $user->save();
  82. CreateAvatar::dispatch($profile);
  83. });
  84. if((bool) config_cache('account.autofollow') == true) {
  85. $names = config_cache('account.autofollow_usernames');
  86. $names = explode(',', $names);
  87. if(!$names || !last($names)) {
  88. return;
  89. }
  90. $profiles = Profile::whereIn('username', $names)->get();
  91. if($profiles) {
  92. foreach($profiles as $p) {
  93. $follower = new Follower;
  94. $follower->profile_id = $profile->id;
  95. $follower->following_id = $p->id;
  96. $follower->save();
  97. FollowPipeline::dispatch($follower);
  98. }
  99. }
  100. }
  101. }
  102. if (empty($user->settings)) {
  103. DB::transaction(function() use($user) {
  104. UserSetting::firstOrCreate([
  105. 'user_id' => $user->id
  106. ]);
  107. });
  108. }
  109. }
  110. protected function applyDefaultDomainBlocks($user)
  111. {
  112. if($user->profile_id == null) {
  113. return;
  114. }
  115. $defaultDomainBlocks = DefaultDomainBlock::pluck('domain')->toArray();
  116. if(!$defaultDomainBlocks || !count($defaultDomainBlocks)) {
  117. return;
  118. }
  119. foreach($defaultDomainBlocks as $domain) {
  120. UserDomainBlock::updateOrCreate([
  121. 'profile_id' => $user->profile_id,
  122. 'domain' => strtolower(trim($domain))
  123. ]);
  124. }
  125. }
  126. }