StatusLocalUpdateActivityPubDeliverPipeline.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\UpdateNote;
  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 StatusLocalUpdateActivityPubDeliverPipeline 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. // ignore group posts
  47. // if($status->group_id != null) {
  48. // return;
  49. // }
  50. if($status->local == false || $status->url || $status->uri) {
  51. return;
  52. }
  53. $audience = $status->profile->getAudienceInbox();
  54. if(empty($audience) || !in_array($status->scope, ['public', 'unlisted', 'private'])) {
  55. // Return on profiles with no remote followers
  56. return;
  57. }
  58. switch($status->type) {
  59. case 'poll':
  60. // Polls not yet supported
  61. return;
  62. break;
  63. default:
  64. $activitypubObject = new UpdateNote();
  65. break;
  66. }
  67. $fractal = new Fractal\Manager();
  68. $fractal->setSerializer(new ArraySerializer());
  69. $resource = new Fractal\Resource\Item($status, $activitypubObject);
  70. $activity = $fractal->createData($resource)->toArray();
  71. $payload = json_encode($activity);
  72. $client = new Client([
  73. 'timeout' => config('federation.activitypub.delivery.timeout')
  74. ]);
  75. $version = config('pixelfed.version');
  76. $appUrl = config('app.url');
  77. $userAgent = "(Pixelfed/{$version}; +{$appUrl})";
  78. $requests = function($audience) use ($client, $activity, $profile, $payload, $userAgent) {
  79. foreach($audience as $url) {
  80. $headers = HttpSignature::sign($profile, $url, $activity, [
  81. 'Content-Type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  82. 'User-Agent' => $userAgent,
  83. ]);
  84. yield function() use ($client, $url, $headers, $payload) {
  85. return $client->postAsync($url, [
  86. 'curl' => [
  87. CURLOPT_HTTPHEADER => $headers,
  88. CURLOPT_POSTFIELDS => $payload,
  89. CURLOPT_HEADER => true,
  90. CURLOPT_SSL_VERIFYPEER => true,
  91. CURLOPT_SSL_VERIFYHOST => false
  92. ]
  93. ]);
  94. };
  95. }
  96. };
  97. $pool = new Pool($client, $requests($audience), [
  98. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  99. 'fulfilled' => function ($response, $index) {
  100. },
  101. 'rejected' => function ($reason, $index) {
  102. }
  103. ]);
  104. $promise = $pool->promise();
  105. $promise->wait();
  106. }
  107. }