StatusDelete.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Jobs\StatusPipeline;
  3. use App\{
  4. Notification,
  5. Report,
  6. Status,
  7. StatusHashtag,
  8. };
  9. use Illuminate\Bus\Queueable;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Foundation\Bus\Dispatchable;
  12. use Illuminate\Queue\InteractsWithQueue;
  13. use Illuminate\Queue\SerializesModels;
  14. use League\Fractal;
  15. use League\Fractal\Serializer\ArraySerializer;
  16. use App\Transformer\ActivityPub\Verb\DeleteNote;
  17. use App\Util\ActivityPub\Helpers;
  18. use GuzzleHttp\Pool;
  19. use GuzzleHttp\Client;
  20. use GuzzleHttp\Promise;
  21. use App\Util\ActivityPub\HttpSignature;
  22. class StatusDelete implements ShouldQueue
  23. {
  24. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  25. protected $status;
  26. /**
  27. * Delete the job if its models no longer exist.
  28. *
  29. * @var bool
  30. */
  31. public $deleteWhenMissingModels = true;
  32. /**
  33. * Create a new job instance.
  34. *
  35. * @return void
  36. */
  37. public function __construct(Status $status)
  38. {
  39. $this->status = $status;
  40. }
  41. /**
  42. * Execute the job.
  43. *
  44. * @return void
  45. */
  46. public function handle()
  47. {
  48. $status = $this->status;
  49. if(config('pixelfed.activitypub_enabled') == true) {
  50. $this->fanoutDelete($status);
  51. } else {
  52. $this->unlinkRemoveMedia($status);
  53. }
  54. }
  55. public function unlinkRemoveMedia($status)
  56. {
  57. foreach ($status->media as $media) {
  58. $thumbnail = storage_path("app/{$media->thumbnail_path}");
  59. $photo = storage_path("app/{$media->media_path}");
  60. try {
  61. if (is_file($thumbnail)) {
  62. unlink($thumbnail);
  63. }
  64. if (is_file($photo)) {
  65. unlink($photo);
  66. }
  67. $media->delete();
  68. } catch (Exception $e) {
  69. }
  70. }
  71. $comments = Status::where('in_reply_to_id', $status->id)->get();
  72. foreach ($comments as $comment) {
  73. $comment->in_reply_to_id = null;
  74. $comment->save();
  75. Notification::whereItemType('App\Status')
  76. ->whereItemId($comment->id)
  77. ->delete();
  78. }
  79. $status->likes()->delete();
  80. Notification::whereItemType('App\Status')
  81. ->whereItemId($status->id)
  82. ->delete();
  83. StatusHashtag::whereStatusId($status->id)->delete();
  84. Report::whereObjectType('App\Status')
  85. ->whereObjectId($status->id)
  86. ->delete();
  87. $status->delete();
  88. return true;
  89. }
  90. protected function fanoutDelete($status)
  91. {
  92. $audience = $status->profile->getAudienceInbox();
  93. $profile = $status->profile;
  94. $fractal = new Fractal\Manager();
  95. $fractal->setSerializer(new ArraySerializer());
  96. $resource = new Fractal\Resource\Item($status, new DeleteNote());
  97. $activity = $fractal->createData($resource)->toArray();
  98. $this->unlinkRemoveMedia($status);
  99. $payload = json_encode($activity);
  100. $client = new Client([
  101. 'timeout' => config('pixelfed.ap_delivery_timeout')
  102. ]);
  103. $requests = function($audience) use ($client, $activity, $profile, $payload) {
  104. foreach($audience as $url) {
  105. $headers = HttpSignature::sign($profile, $url, $activity);
  106. yield function() use ($client, $url, $headers, $payload) {
  107. return $client->postAsync($url, [
  108. 'curl' => [
  109. CURLOPT_HTTPHEADER => $headers,
  110. CURLOPT_POSTFIELDS => $payload,
  111. CURLOPT_HEADER => true
  112. ]
  113. ]);
  114. };
  115. }
  116. };
  117. $pool = new Pool($client, $requests($audience), [
  118. 'concurrency' => config('pixelfed.ap_delivery_concurrency'),
  119. 'fulfilled' => function ($response, $index) {
  120. },
  121. 'rejected' => function ($reason, $index) {
  122. }
  123. ]);
  124. $promise = $pool->promise();
  125. $promise->wait();
  126. }
  127. }