1
0

StatusActivityPubDeliver.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Jobs\StatusPipeline;
  3. use Cache, Log;
  4. use App\Status;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use League\Fractal;
  11. use League\Fractal\Serializer\ArraySerializer;
  12. use App\Transformer\ActivityPub\Verb\CreateNote;
  13. use App\Util\ActivityPub\Helpers;
  14. use GuzzleHttp\Pool;
  15. use GuzzleHttp\Client;
  16. use GuzzleHttp\Promise;
  17. use App\Util\ActivityPub\HttpSignature;
  18. class StatusActivityPubDeliver implements ShouldQueue
  19. {
  20. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  21. protected $status;
  22. /**
  23. * Delete the job if its models no longer exist.
  24. *
  25. * @var bool
  26. */
  27. public $deleteWhenMissingModels = true;
  28. /**
  29. * Create a new job instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct(Status $status)
  34. {
  35. $this->status = $status;
  36. }
  37. /**
  38. * Execute the job.
  39. *
  40. * @return void
  41. */
  42. public function handle()
  43. {
  44. $status = $this->status;
  45. $profile = $status->profile;
  46. if($status->local == false || $status->url || $status->uri) {
  47. return;
  48. }
  49. $audience = $status->profile->getAudienceInbox();
  50. if(empty($audience) || !in_array($status->scope, ['public', 'unlisted', 'private'])) {
  51. // Return on profiles with no remote followers
  52. return;
  53. }
  54. $fractal = new Fractal\Manager();
  55. $fractal->setSerializer(new ArraySerializer());
  56. $resource = new Fractal\Resource\Item($status, new CreateNote());
  57. $activity = $fractal->createData($resource)->toArray();
  58. $payload = json_encode($activity);
  59. $client = new Client([
  60. 'timeout' => config('federation.activitypub.delivery.timeout')
  61. ]);
  62. $requests = function($audience) use ($client, $activity, $profile, $payload) {
  63. foreach($audience as $url) {
  64. $headers = HttpSignature::sign($profile, $url, $activity);
  65. yield function() use ($client, $url, $headers, $payload) {
  66. return $client->postAsync($url, [
  67. 'curl' => [
  68. CURLOPT_HTTPHEADER => $headers,
  69. CURLOPT_POSTFIELDS => $payload,
  70. CURLOPT_HEADER => true
  71. ]
  72. ]);
  73. };
  74. }
  75. };
  76. $pool = new Pool($client, $requests($audience), [
  77. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  78. 'fulfilled' => function ($response, $index) {
  79. },
  80. 'rejected' => function ($reason, $index) {
  81. }
  82. ]);
  83. $promise = $pool->promise();
  84. $promise->wait();
  85. }
  86. }