SharePipeline.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. class SharePipeline implements ShouldQueue
  17. {
  18. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  19. protected $status;
  20. /**
  21. * Delete the job if its models no longer exist.
  22. *
  23. * @var bool
  24. */
  25. public $deleteWhenMissingModels = true;
  26. /**
  27. * Create a new job instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct(Status $status)
  32. {
  33. $this->status = $status;
  34. }
  35. /**
  36. * Execute the job.
  37. *
  38. * @return void
  39. */
  40. public function handle()
  41. {
  42. $status = $this->status;
  43. $actor = $status->profile;
  44. $target = $status->parent()->profile;
  45. if ($status->uri !== null) {
  46. // Ignore notifications to remote statuses
  47. return;
  48. }
  49. $exists = Notification::whereProfileId($target->id)
  50. ->whereActorId($status->profile_id)
  51. ->whereAction('share')
  52. ->whereItemId($status->reblog_of_id)
  53. ->whereItemType('App\Status')
  54. ->count();
  55. if ($target->id === $status->profile_id) {
  56. $this->remoteAnnounceDeliver();
  57. return true;
  58. }
  59. if( $exists !== 0) {
  60. return true;
  61. }
  62. $this->remoteAnnounceDeliver();
  63. try {
  64. $notification = new Notification;
  65. $notification->profile_id = $target->id;
  66. $notification->actor_id = $actor->id;
  67. $notification->action = 'share';
  68. $notification->message = $status->shareToText();
  69. $notification->rendered = $status->shareToHtml();
  70. $notification->item_id = $status->reblog_of_id ?? $status->id;
  71. $notification->item_type = "App\Status";
  72. $notification->save();
  73. $redis = Redis::connection();
  74. $key = config('cache.prefix').':user.'.$status->profile_id.'.notifications';
  75. $redis->lpush($key, $notification->id);
  76. } catch (Exception $e) {
  77. Log::error($e);
  78. }
  79. }
  80. public function remoteAnnounceDeliver()
  81. {
  82. if(config('federation.activitypub.enabled') == false) {
  83. return true;
  84. }
  85. $status = $this->status;
  86. $profile = $status->profile;
  87. $fractal = new Fractal\Manager();
  88. $fractal->setSerializer(new ArraySerializer());
  89. $resource = new Fractal\Resource\Item($status, new Announce());
  90. $activity = $fractal->createData($resource)->toArray();
  91. $audience = $status->profile->getAudienceInbox();
  92. if(empty($audience) || $status->scope != 'public') {
  93. // Return on profiles with no remote followers
  94. return;
  95. }
  96. $payload = json_encode($activity);
  97. $client = new Client([
  98. 'timeout' => config('federation.activitypub.delivery.timeout')
  99. ]);
  100. $requests = function($audience) use ($client, $activity, $profile, $payload) {
  101. foreach($audience as $url) {
  102. $headers = HttpSignature::sign($profile, $url, $activity);
  103. yield function() use ($client, $url, $headers, $payload) {
  104. return $client->postAsync($url, [
  105. 'curl' => [
  106. CURLOPT_HTTPHEADER => $headers,
  107. CURLOPT_POSTFIELDS => $payload,
  108. CURLOPT_HEADER => true
  109. ]
  110. ]);
  111. };
  112. }
  113. };
  114. $pool = new Pool($client, $requests($audience), [
  115. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  116. 'fulfilled' => function ($response, $index) {
  117. },
  118. 'rejected' => function ($reason, $index) {
  119. }
  120. ]);
  121. $promise = $pool->promise();
  122. $promise->wait();
  123. }
  124. }