StatusActivityPubDeliver.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\Transformer\ActivityPub\Verb\CreateQuestion;
  14. use App\Util\ActivityPub\Helpers;
  15. use GuzzleHttp\Pool;
  16. use GuzzleHttp\Client;
  17. use GuzzleHttp\Promise;
  18. use App\Util\ActivityPub\HttpSignature;
  19. class StatusActivityPubDeliver implements ShouldQueue
  20. {
  21. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  22. protected $status;
  23. /**
  24. * Delete the job if its models no longer exist.
  25. *
  26. * @var bool
  27. */
  28. public $deleteWhenMissingModels = true;
  29. /**
  30. * Create a new job instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct(Status $status)
  35. {
  36. $this->status = $status;
  37. }
  38. /**
  39. * Execute the job.
  40. *
  41. * @return void
  42. */
  43. public function handle()
  44. {
  45. $status = $this->status;
  46. $profile = $status->profile;
  47. if($status->local == false || $status->url || $status->uri) {
  48. return;
  49. }
  50. $audience = $status->profile->getAudienceInbox();
  51. if(empty($audience) || !in_array($status->scope, ['public', 'unlisted', 'private'])) {
  52. // Return on profiles with no remote followers
  53. return;
  54. }
  55. switch($status->type) {
  56. case 'poll':
  57. $activitypubObject = new CreateQuestion();
  58. break;
  59. default:
  60. $activitypubObject = new CreateNote();
  61. break;
  62. }
  63. $fractal = new Fractal\Manager();
  64. $fractal->setSerializer(new ArraySerializer());
  65. $resource = new Fractal\Resource\Item($status, $activitypubObject);
  66. $activity = $fractal->createData($resource)->toArray();
  67. $payload = json_encode($activity);
  68. $client = new Client([
  69. 'timeout' => config('federation.activitypub.delivery.timeout')
  70. ]);
  71. $requests = function($audience) use ($client, $activity, $profile, $payload) {
  72. foreach($audience as $url) {
  73. $version = config('pixelfed.version');
  74. $appUrl = config('app.url');
  75. $headers = HttpSignature::sign($profile, $url, $activity, [
  76. 'Content-Type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  77. 'User-Agent' => "(Pixelfed/{$version}; +{$appUrl})",
  78. ]);
  79. yield function() use ($client, $url, $headers, $payload) {
  80. return $client->postAsync($url, [
  81. 'curl' => [
  82. CURLOPT_HTTPHEADER => $headers,
  83. CURLOPT_POSTFIELDS => $payload,
  84. CURLOPT_HEADER => true,
  85. CURLOPT_SSL_VERIFYPEER => false,
  86. CURLOPT_SSL_VERIFYHOST => false
  87. ]
  88. ]);
  89. };
  90. }
  91. };
  92. $pool = new Pool($client, $requests($audience), [
  93. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  94. 'fulfilled' => function ($response, $index) {
  95. },
  96. 'rejected' => function ($reason, $index) {
  97. }
  98. ]);
  99. $promise = $pool->promise();
  100. $promise->wait();
  101. }
  102. }