1
0

StatusActivityPubDeliver.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Jobs\StatusPipeline;
  3. use Cache, Log;
  4. use App\Profile;
  5. use App\Status;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. use League\Fractal;
  12. use League\Fractal\Serializer\ArraySerializer;
  13. use App\Transformer\ActivityPub\Verb\CreateNote;
  14. use App\Transformer\ActivityPub\Verb\CreateQuestion;
  15. use App\Util\ActivityPub\Helpers;
  16. use GuzzleHttp\Pool;
  17. use GuzzleHttp\Client;
  18. use GuzzleHttp\Promise;
  19. use App\Util\ActivityPub\HttpSignature;
  20. class StatusActivityPubDeliver implements ShouldQueue
  21. {
  22. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  23. protected $status;
  24. /**
  25. * Delete the job if its models no longer exist.
  26. *
  27. * @var bool
  28. */
  29. public $deleteWhenMissingModels = true;
  30. /**
  31. * Create a new job instance.
  32. *
  33. * @return void
  34. */
  35. public function __construct(Status $status)
  36. {
  37. $this->status = $status;
  38. }
  39. /**
  40. * Execute the job.
  41. *
  42. * @return void
  43. */
  44. public function handle()
  45. {
  46. $status = $this->status;
  47. $profile = $status->profile;
  48. // ignore group posts
  49. // if($status->group_id != null) {
  50. // return;
  51. // }
  52. if($status->local == false || $status->url || $status->uri) {
  53. return;
  54. }
  55. $audience = $status->profile->getAudienceInbox();
  56. $parentInbox = [];
  57. $mentions = $status->mentions
  58. ->filter(function($f) { return $f->domain !== null;})
  59. ->values()
  60. ->map(function($m) { return $m->sharedInbox ?? $m->inbox_url; })
  61. ->toArray();
  62. if($status->in_reply_to_profile_id) {
  63. $parent = Profile::find($status->in_reply_to_profile_id);
  64. if($parent && $parent->domain !== null) {
  65. $parentInbox = [
  66. $parent->sharedInbox ?? $parent->inbox_url
  67. ];
  68. }
  69. }
  70. $audience = array_values(array_unique(array_merge($audience, $mentions, $parentInbox)));
  71. if(empty($audience) || !in_array($status->scope, ['public', 'unlisted', 'private'])) {
  72. // Return on profiles with no remote followers
  73. return;
  74. }
  75. switch($status->type) {
  76. case 'poll':
  77. $activitypubObject = new CreateQuestion();
  78. break;
  79. default:
  80. $activitypubObject = new CreateNote();
  81. break;
  82. }
  83. $fractal = new Fractal\Manager();
  84. $fractal->setSerializer(new ArraySerializer());
  85. $resource = new Fractal\Resource\Item($status, $activitypubObject);
  86. $activity = $fractal->createData($resource)->toArray();
  87. $payload = json_encode($activity);
  88. $client = new Client([
  89. 'timeout' => config('federation.activitypub.delivery.timeout')
  90. ]);
  91. $version = config('pixelfed.version');
  92. $appUrl = config('app.url');
  93. $userAgent = "(Pixelfed/{$version}; +{$appUrl})";
  94. $requests = function($audience) use ($client, $activity, $profile, $payload, $userAgent) {
  95. foreach($audience as $url) {
  96. $headers = HttpSignature::sign($profile, $url, $activity, [
  97. 'Content-Type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  98. 'User-Agent' => $userAgent,
  99. ]);
  100. yield function() use ($client, $url, $headers, $payload) {
  101. return $client->postAsync($url, [
  102. 'curl' => [
  103. CURLOPT_HTTPHEADER => $headers,
  104. CURLOPT_POSTFIELDS => $payload,
  105. CURLOPT_HEADER => true,
  106. CURLOPT_SSL_VERIFYPEER => false,
  107. CURLOPT_SSL_VERIFYHOST => false
  108. ]
  109. ]);
  110. };
  111. }
  112. };
  113. $pool = new Pool($client, $requests($audience), [
  114. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  115. 'fulfilled' => function ($response, $index) {
  116. },
  117. 'rejected' => function ($reason, $index) {
  118. }
  119. ]);
  120. $promise = $pool->promise();
  121. $promise->wait();
  122. }
  123. }