StoryDelete.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Jobs\StoryPipeline;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use Storage;
  10. use App\Story;
  11. use League\Fractal;
  12. use League\Fractal\Serializer\ArraySerializer;
  13. use App\Transformer\ActivityPub\Verb\DeleteStory;
  14. use App\Util\ActivityPub\Helpers;
  15. use GuzzleHttp\Pool;
  16. use GuzzleHttp\Client;
  17. use GuzzleHttp\Promise;
  18. use App\Util\ActivityPub\HttpSignature;
  19. use App\Services\FollowerService;
  20. use App\Services\StoryService;
  21. class StoryDelete implements ShouldQueue
  22. {
  23. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  24. protected $story;
  25. /**
  26. * Delete the job if its models no longer exist.
  27. *
  28. * @var bool
  29. */
  30. public $deleteWhenMissingModels = true;
  31. /**
  32. * Create a new job instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct(Story $story)
  37. {
  38. $this->story = $story;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. $story = $this->story;
  48. if($story->local == false) {
  49. return;
  50. }
  51. StoryService::removeRotateQueue($story->id);
  52. StoryService::delLatest($story->profile_id);
  53. StoryService::delById($story->id);
  54. if(Storage::exists($story->path) == true) {
  55. Storage::delete($story->path);
  56. }
  57. $story->views()->delete();
  58. $profile = $story->profile;
  59. $activity = [
  60. '@context' => 'https://www.w3.org/ns/activitystreams',
  61. 'id' => $story->url() . '#delete',
  62. 'type' => 'Delete',
  63. 'actor' => $profile->permalink(),
  64. 'object' => [
  65. 'id' => $story->url(),
  66. 'type' => 'Story',
  67. ],
  68. ];
  69. $this->fanoutExpiry($profile, $activity);
  70. // delete notifications
  71. // delete polls
  72. // delete reports
  73. $story->delete();
  74. return;
  75. }
  76. protected function fanoutExpiry($profile, $activity)
  77. {
  78. $audience = FollowerService::softwareAudience($profile->id, 'pixelfed');
  79. if(empty($audience)) {
  80. // Return on profiles with no remote followers
  81. return;
  82. }
  83. $payload = json_encode($activity);
  84. $client = new Client([
  85. 'timeout' => config('federation.activitypub.delivery.timeout')
  86. ]);
  87. $requests = function($audience) use ($client, $activity, $profile, $payload) {
  88. foreach($audience as $url) {
  89. $headers = HttpSignature::sign($profile, $url, $activity);
  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. }
  111. }