FanoutDeletePipeline.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "to" => [
  47. "https://www.w3.org/ns/activitystreams#Public",
  48. ],
  49. "object" => $profile->permalink(),
  50. ];
  51. $payload = json_encode($activity);
  52. $requests = function($audience) use ($client, $activity, $profile, $payload) {
  53. foreach($audience as $url) {
  54. $headers = HttpSignature::sign($profile, $url, $activity);
  55. yield function() use ($client, $url, $headers, $payload) {
  56. return $client->postAsync($url, [
  57. 'curl' => [
  58. CURLOPT_HTTPHEADER => $headers,
  59. CURLOPT_POSTFIELDS => $payload,
  60. CURLOPT_HEADER => true
  61. ]
  62. ]);
  63. };
  64. }
  65. };
  66. $pool = new Pool($client, $requests($audience), [
  67. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  68. 'fulfilled' => function ($response, $index) {
  69. },
  70. 'rejected' => function ($reason, $index) {
  71. }
  72. ]);
  73. $promise = $pool->promise();
  74. $promise->wait();
  75. return 1;
  76. }
  77. }