SharePipeline.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Jobs\SharePipeline;
  3. use Cache, Log;
  4. use Illuminate\Support\Facades\Redis;
  5. use App\{Status, Notification};
  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\Announce;
  14. use GuzzleHttp\{Pool, Client, Promise};
  15. use App\Util\ActivityPub\HttpSignature;
  16. use App\Services\ReblogService;
  17. use App\Services\StatusService;
  18. use App\Jobs\HomeFeedPipeline\FeedInsertPipeline;
  19. class SharePipeline 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. $parent = Status::find($this->status->reblog_of_id);
  47. if(!$parent) {
  48. return;
  49. }
  50. $actor = $status->profile;
  51. $target = $parent->profile;
  52. if ($status->uri !== null) {
  53. // Ignore notifications to remote statuses
  54. return;
  55. }
  56. if($target->id === $status->profile_id) {
  57. $this->remoteAnnounceDeliver();
  58. return true;
  59. }
  60. ReblogService::addPostReblog($parent->profile_id, $status->id);
  61. $parent->reblogs_count = $parent->reblogs_count + 1;
  62. $parent->save();
  63. StatusService::del($parent->id);
  64. Notification::firstOrCreate(
  65. [
  66. 'profile_id' => $target->id,
  67. 'actor_id' => $actor->id,
  68. 'action' => 'share',
  69. 'item_type' => 'App\Status',
  70. 'item_id' => $status->reblog_of_id ?? $status->id,
  71. ]
  72. );
  73. FeedInsertPipeline::dispatch($status->id, $status->profile_id)->onQueue('feed');
  74. return $this->remoteAnnounceDeliver();
  75. }
  76. public function remoteAnnounceDeliver()
  77. {
  78. if(config('app.env') !== 'production' || config_cache('federation.activitypub.enabled') == false) {
  79. return true;
  80. }
  81. $status = $this->status;
  82. $profile = $status->profile;
  83. $fractal = new Fractal\Manager();
  84. $fractal->setSerializer(new ArraySerializer());
  85. $resource = new Fractal\Resource\Item($status, new Announce());
  86. $activity = $fractal->createData($resource)->toArray();
  87. $audience = $status->profile->getAudienceInbox();
  88. if(empty($audience) || $status->scope != 'public') {
  89. // Return on profiles with no remote followers
  90. return;
  91. }
  92. $payload = json_encode($activity);
  93. $client = new Client([
  94. 'timeout' => config('federation.activitypub.delivery.timeout')
  95. ]);
  96. $version = config('pixelfed.version');
  97. $appUrl = config('app.url');
  98. $userAgent = "(Pixelfed/{$version}; +{$appUrl})";
  99. $requests = function($audience) use ($client, $activity, $profile, $payload, $userAgent) {
  100. foreach($audience as $url) {
  101. $headers = HttpSignature::sign($profile, $url, $activity, [
  102. 'Content-Type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  103. 'User-Agent' => $userAgent,
  104. ]);
  105. yield function() use ($client, $url, $headers, $payload) {
  106. return $client->postAsync($url, [
  107. 'curl' => [
  108. CURLOPT_HTTPHEADER => $headers,
  109. CURLOPT_POSTFIELDS => $payload,
  110. CURLOPT_HEADER => true
  111. ]
  112. ]);
  113. };
  114. }
  115. };
  116. $pool = new Pool($client, $requests($audience), [
  117. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  118. 'fulfilled' => function ($response, $index) {
  119. },
  120. 'rejected' => function ($reason, $index) {
  121. }
  122. ]);
  123. $promise = $pool->promise();
  124. $promise->wait();
  125. }
  126. }