UserAccountDelete.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Instance;
  4. use App\Profile;
  5. use App\Transformer\ActivityPub\Verb\DeleteActor;
  6. use App\User;
  7. use App\Util\ActivityPub\HttpSignature;
  8. use GuzzleHttp\Client;
  9. use GuzzleHttp\Pool;
  10. use Illuminate\Console\Command;
  11. use League\Fractal;
  12. use League\Fractal\Serializer\ArraySerializer;
  13. use function Laravel\Prompts\confirm;
  14. use function Laravel\Prompts\search;
  15. use function Laravel\Prompts\table;
  16. class UserAccountDelete extends Command
  17. {
  18. /**
  19. * The name and signature of the console command.
  20. *
  21. * @var string
  22. */
  23. protected $signature = 'app:user-account-delete';
  24. /**
  25. * The console command description.
  26. *
  27. * @var string
  28. */
  29. protected $description = 'Federate Account Deletion';
  30. /**
  31. * Execute the console command.
  32. */
  33. public function handle()
  34. {
  35. $id = search(
  36. label: 'Search for the account to delete by username',
  37. placeholder: 'john.appleseed',
  38. options: fn (string $value) => strlen($value) > 0
  39. ? User::withTrashed()->whereStatus('deleted')->where('username', 'like', "%{$value}%")->pluck('username', 'id')->all()
  40. : [],
  41. );
  42. $user = User::withTrashed()->find($id);
  43. table(
  44. ['Username', 'Name', 'Email', 'Created'],
  45. [[$user->username, $user->name, $user->email, $user->created_at]]
  46. );
  47. $confirmed = confirm(
  48. label: 'Do you want to federate this account deletion?',
  49. default: false,
  50. yes: 'Proceed',
  51. no: 'Cancel',
  52. hint: 'This action is irreversible'
  53. );
  54. if (! $confirmed) {
  55. $this->error('Aborting...');
  56. exit;
  57. }
  58. $profile = Profile::withTrashed()->find($user->profile_id);
  59. $fractal = new Fractal\Manager();
  60. $fractal->setSerializer(new ArraySerializer());
  61. $resource = new Fractal\Resource\Item($profile, new DeleteActor());
  62. $activity = $fractal->createData($resource)->toArray();
  63. $audience = Instance::whereNotNull(['shared_inbox', 'nodeinfo_last_fetched'])
  64. ->where('nodeinfo_last_fetched', '>', now()->subDays(14))
  65. ->distinct()
  66. ->pluck('shared_inbox');
  67. $payload = json_encode($activity);
  68. $client = new Client([
  69. 'timeout' => 5,
  70. ]);
  71. $version = config('pixelfed.version');
  72. $appUrl = config('app.url');
  73. $userAgent = "(Pixelfed/{$version}; +{$appUrl})";
  74. $requests = function ($audience) use ($client, $activity, $profile, $payload, $userAgent) {
  75. foreach ($audience as $url) {
  76. $headers = HttpSignature::sign($profile, $url, $activity, [
  77. 'Content-Type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  78. 'User-Agent' => $userAgent,
  79. ]);
  80. yield function () use ($client, $url, $headers, $payload) {
  81. return $client->postAsync($url, [
  82. 'curl' => [
  83. CURLOPT_HTTPHEADER => $headers,
  84. CURLOPT_POSTFIELDS => $payload,
  85. CURLOPT_HEADER => true,
  86. CURLOPT_SSL_VERIFYPEER => false,
  87. CURLOPT_SSL_VERIFYHOST => false,
  88. ],
  89. ]);
  90. };
  91. }
  92. };
  93. $pool = new Pool($client, $requests($audience), [
  94. 'concurrency' => 50,
  95. 'fulfilled' => function ($response, $index) {
  96. },
  97. 'rejected' => function ($reason, $index) {
  98. },
  99. ]);
  100. $promise = $pool->promise();
  101. $promise->wait();
  102. }
  103. }