SharePipeline.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. class SharePipeline 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. $parent = $this->status->parent();
  46. $actor = $status->profile;
  47. $target = $parent->profile;
  48. if ($status->uri !== null) {
  49. // Ignore notifications to remote statuses
  50. return;
  51. }
  52. $exists = Notification::whereProfileId($target->id)
  53. ->whereActorId($status->profile_id)
  54. ->whereAction('share')
  55. ->whereItemId($status->reblog_of_id)
  56. ->whereItemType('App\Status')
  57. ->exists();
  58. if($target->id === $status->profile_id) {
  59. $this->remoteAnnounceDeliver();
  60. return true;
  61. }
  62. if($exists === true) {
  63. return true;
  64. }
  65. $this->remoteAnnounceDeliver();
  66. ReblogService::addPostReblog($parent->id, $status->id);
  67. $parent->reblogs_count = $parent->shares()->count();
  68. $parent->save();
  69. StatusService::del($parent->id);
  70. try {
  71. $notification = new Notification;
  72. $notification->profile_id = $target->id;
  73. $notification->actor_id = $actor->id;
  74. $notification->action = 'share';
  75. $notification->message = $status->shareToText();
  76. $notification->rendered = $status->shareToHtml();
  77. $notification->item_id = $status->reblog_of_id ?? $status->id;
  78. $notification->item_type = "App\Status";
  79. $notification->save();
  80. $redis = Redis::connection();
  81. $key = config('cache.prefix').':user.'.$status->profile_id.'.notifications';
  82. $redis->lpush($key, $notification->id);
  83. } catch (Exception $e) {
  84. Log::error($e);
  85. }
  86. }
  87. public function remoteAnnounceDeliver()
  88. {
  89. if(config_cache('federation.activitypub.enabled') == false) {
  90. return true;
  91. }
  92. $status = $this->status;
  93. $profile = $status->profile;
  94. $fractal = new Fractal\Manager();
  95. $fractal->setSerializer(new ArraySerializer());
  96. $resource = new Fractal\Resource\Item($status, new Announce());
  97. $activity = $fractal->createData($resource)->toArray();
  98. $audience = $status->profile->getAudienceInbox();
  99. if(empty($audience) || $status->scope != 'public') {
  100. // Return on profiles with no remote followers
  101. return;
  102. }
  103. $payload = json_encode($activity);
  104. $client = new Client([
  105. 'timeout' => config('federation.activitypub.delivery.timeout')
  106. ]);
  107. $requests = function($audience) use ($client, $activity, $profile, $payload) {
  108. foreach($audience as $url) {
  109. $headers = HttpSignature::sign($profile, $url, $activity);
  110. yield function() use ($client, $url, $headers, $payload) {
  111. return $client->postAsync($url, [
  112. 'curl' => [
  113. CURLOPT_HTTPHEADER => $headers,
  114. CURLOPT_POSTFIELDS => $payload,
  115. CURLOPT_HEADER => true
  116. ]
  117. ]);
  118. };
  119. }
  120. };
  121. $pool = new Pool($client, $requests($audience), [
  122. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  123. 'fulfilled' => function ($response, $index) {
  124. },
  125. 'rejected' => function ($reason, $index) {
  126. }
  127. ]);
  128. $promise = $pool->promise();
  129. $promise->wait();
  130. }
  131. }