UndoSharePipeline.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. class UndoSharePipeline implements ShouldQueue
  19. {
  20. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  21. protected $status;
  22. public $deleteWhenMissingModels = true;
  23. public function __construct(Status $status)
  24. {
  25. $this->status = $status;
  26. }
  27. public function handle()
  28. {
  29. $status = $this->status;
  30. $actor = $status->profile;
  31. $parent = $status->parent();
  32. $target = $status->parent()->profile;
  33. ReblogService::removePostReblog($parent->id, $status->id);
  34. if ($status->uri !== null) {
  35. return;
  36. }
  37. if($target->domain === null) {
  38. Notification::whereProfileId($target->id)
  39. ->whereActorId($status->profile_id)
  40. ->whereAction('share')
  41. ->whereItemId($status->reblog_of_id)
  42. ->whereItemType('App\Status')
  43. ->delete();
  44. }
  45. $this->remoteAnnounceDeliver();
  46. if($parent->reblogs_count > 0) {
  47. $parent->reblogs_count = $parent->reblogs_count - 1;
  48. $parent->save();
  49. StatusService::del($parent->id);
  50. }
  51. $status->forceDelete();
  52. return 1;
  53. }
  54. public function remoteAnnounceDeliver()
  55. {
  56. if(config_cache('federation.activitypub.enabled') == false) {
  57. return 1;
  58. }
  59. $status = $this->status;
  60. $profile = $status->profile;
  61. $fractal = new Fractal\Manager();
  62. $fractal->setSerializer(new ArraySerializer());
  63. $resource = new Fractal\Resource\Item($status, new UndoAnnounce());
  64. $activity = $fractal->createData($resource)->toArray();
  65. $audience = $status->profile->getAudienceInbox();
  66. if(empty($audience) || $status->scope != 'public') {
  67. return 1;
  68. }
  69. $payload = json_encode($activity);
  70. $client = new Client([
  71. 'timeout' => config('federation.activitypub.delivery.timeout')
  72. ]);
  73. $requests = function($audience) use ($client, $activity, $profile, $payload) {
  74. foreach($audience as $url) {
  75. $headers = HttpSignature::sign($profile, $url, $activity);
  76. yield function() use ($client, $url, $headers, $payload) {
  77. return $client->postAsync($url, [
  78. 'curl' => [
  79. CURLOPT_HTTPHEADER => $headers,
  80. CURLOPT_POSTFIELDS => $payload,
  81. CURLOPT_HEADER => true
  82. ]
  83. ]);
  84. };
  85. }
  86. };
  87. $pool = new Pool($client, $requests($audience), [
  88. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  89. 'fulfilled' => function ($response, $index) {
  90. },
  91. 'rejected' => function ($reason, $index) {
  92. }
  93. ]);
  94. $promise = $pool->promise();
  95. $promise->wait();
  96. }
  97. }