UndoSharePipeline.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\UndoAnnounce;
  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\FeedRemovePipeline;
  19. class UndoSharePipeline implements ShouldQueue
  20. {
  21. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  22. protected $status;
  23. public $deleteWhenMissingModels = true;
  24. public function __construct(Status $status)
  25. {
  26. $this->status = $status;
  27. }
  28. public function handle()
  29. {
  30. $status = $this->status;
  31. $actor = $status->profile;
  32. $parent = Status::find($status->reblog_of_id);
  33. FeedRemovePipeline::dispatch($status->id, $status->profile_id)->onQueue('feed');
  34. if($parent) {
  35. $target = $parent->profile_id;
  36. ReblogService::removePostReblog($parent->profile_id, $status->id);
  37. if($parent->reblogs_count > 0) {
  38. $parent->reblogs_count = $parent->reblogs_count - 1;
  39. $parent->save();
  40. StatusService::del($parent->id);
  41. }
  42. $notification = Notification::whereProfileId($target)
  43. ->whereActorId($status->profile_id)
  44. ->whereAction('share')
  45. ->whereItemId($status->reblog_of_id)
  46. ->whereItemType('App\Status')
  47. ->first();
  48. if($notification) {
  49. $notification->forceDelete();
  50. }
  51. }
  52. if ($status->uri != null) {
  53. return;
  54. }
  55. if(config('app.env') !== 'production' || config_cache('federation.activitypub.enabled') == false) {
  56. return $status->delete();
  57. } else {
  58. return $this->remoteAnnounceDeliver();
  59. }
  60. }
  61. public function remoteAnnounceDeliver()
  62. {
  63. if(config('app.env') !== 'production' || config_cache('federation.activitypub.enabled') == false) {
  64. $status->delete();
  65. return 1;
  66. }
  67. $status = $this->status;
  68. $profile = $status->profile;
  69. $fractal = new Fractal\Manager();
  70. $fractal->setSerializer(new ArraySerializer());
  71. $resource = new Fractal\Resource\Item($status, new UndoAnnounce());
  72. $activity = $fractal->createData($resource)->toArray();
  73. $audience = $status->profile->getAudienceInbox();
  74. if(empty($audience) || $status->scope != 'public') {
  75. return 1;
  76. }
  77. $payload = json_encode($activity);
  78. $client = new Client([
  79. 'timeout' => config('federation.activitypub.delivery.timeout')
  80. ]);
  81. $version = config('pixelfed.version');
  82. $appUrl = config('app.url');
  83. $userAgent = "(Pixelfed/{$version}; +{$appUrl})";
  84. $requests = function($audience) use ($client, $activity, $profile, $payload, $userAgent) {
  85. foreach($audience as $url) {
  86. $headers = HttpSignature::sign($profile, $url, $activity, [
  87. 'Content-Type' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  88. 'User-Agent' => $userAgent,
  89. ]);
  90. yield function() use ($client, $url, $headers, $payload) {
  91. return $client->postAsync($url, [
  92. 'curl' => [
  93. CURLOPT_HTTPHEADER => $headers,
  94. CURLOPT_POSTFIELDS => $payload,
  95. CURLOPT_HEADER => true
  96. ]
  97. ]);
  98. };
  99. }
  100. };
  101. $pool = new Pool($client, $requests($audience), [
  102. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  103. 'fulfilled' => function ($response, $index) {
  104. },
  105. 'rejected' => function ($reason, $index) {
  106. }
  107. ]);
  108. $promise = $pool->promise();
  109. $promise->wait();
  110. $status->delete();
  111. return 1;
  112. }
  113. }