FanoutDeletePipeline.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Jobs\DeletePipeline;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Queue\SerializesModels;
  5. use Illuminate\Queue\InteractsWithQueue;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Cache;
  9. use DB;
  10. use Illuminate\Support\Str;
  11. use App\Profile;
  12. use App\Util\ActivityPub\Helpers;
  13. use GuzzleHttp\Pool;
  14. use GuzzleHttp\Client;
  15. use GuzzleHttp\Promise;
  16. use App\Util\ActivityPub\HttpSignature;
  17. class FanoutDeletePipeline implements ShouldQueue
  18. {
  19. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  20. protected $profile;
  21. public $timeout = 300;
  22. public $tries = 1;
  23. /**
  24. * Create a new job instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct($profile)
  29. {
  30. $this->profile = $profile;
  31. }
  32. public function handle()
  33. {
  34. $profile = $this->profile;
  35. $client = new Client([
  36. 'timeout' => config('federation.activitypub.delivery.timeout')
  37. ]);
  38. $audience = Cache::remember('pf:ap:known_instances', now()->addHours(6), function() {
  39. return Profile::whereNotNull('sharedInbox')->groupBy('sharedInbox')->pluck('sharedInbox')->toArray();
  40. });
  41. $activity = [
  42. "@context" => "https://www.w3.org/ns/activitystreams",
  43. "id" => $profile->permalink('#delete'),
  44. "type" => "Delete",
  45. "actor" => $profile->permalink(),
  46. "object" => [
  47. "type" => "Person",
  48. "id" => $profile->permalink()
  49. ],
  50. ];
  51. $payload = json_encode($activity);
  52. $requests = function($audience) use ($client, $activity, $profile, $payload) {
  53. foreach($audience as $url) {
  54. $version = config('pixelfed.version');
  55. $appUrl = config('app.url');
  56. $headers = HttpSignature::sign($profile, $url, $activity, [
  57. 'Content-Type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  58. 'User-Agent' => "(Pixelfed/{$version}; +{$appUrl})",
  59. ]);
  60. yield function() use ($client, $url, $headers, $payload) {
  61. return $client->postAsync($url, [
  62. 'curl' => [
  63. CURLOPT_HTTPHEADER => $headers,
  64. CURLOPT_POSTFIELDS => $payload,
  65. CURLOPT_HEADER => true
  66. ]
  67. ]);
  68. };
  69. }
  70. };
  71. $pool = new Pool($client, $requests($audience), [
  72. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  73. 'fulfilled' => function ($response, $index) {
  74. },
  75. 'rejected' => function ($reason, $index) {
  76. }
  77. ]);
  78. $promise = $pool->promise();
  79. $promise->wait();
  80. return 1;
  81. }
  82. }