StatusDelete.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Jobs\StatusPipeline;
  3. use DB, Storage;
  4. use App\{
  5. AccountInterstitial,
  6. MediaTag,
  7. Notification,
  8. Report,
  9. Status,
  10. StatusHashtag,
  11. };
  12. use Illuminate\Bus\Queueable;
  13. use Illuminate\Contracts\Queue\ShouldQueue;
  14. use Illuminate\Foundation\Bus\Dispatchable;
  15. use Illuminate\Queue\InteractsWithQueue;
  16. use Illuminate\Queue\SerializesModels;
  17. use League\Fractal;
  18. use Illuminate\Support\Str;
  19. use League\Fractal\Serializer\ArraySerializer;
  20. use App\Transformer\ActivityPub\Verb\DeleteNote;
  21. use App\Util\ActivityPub\Helpers;
  22. use GuzzleHttp\Pool;
  23. use GuzzleHttp\Client;
  24. use GuzzleHttp\Promise;
  25. use App\Util\ActivityPub\HttpSignature;
  26. use App\Services\StatusService;
  27. use App\Services\MediaStorageService;
  28. class StatusDelete implements ShouldQueue
  29. {
  30. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  31. protected $status;
  32. /**
  33. * Delete the job if its models no longer exist.
  34. *
  35. * @var bool
  36. */
  37. public $deleteWhenMissingModels = true;
  38. /**
  39. * Create a new job instance.
  40. *
  41. * @return void
  42. */
  43. public function __construct(Status $status)
  44. {
  45. $this->status = $status;
  46. }
  47. /**
  48. * Execute the job.
  49. *
  50. * @return void
  51. */
  52. public function handle()
  53. {
  54. $status = $this->status;
  55. $profile = $this->status->profile;
  56. StatusService::del($status->id);
  57. $count = $profile->statuses()
  58. ->getQuery()
  59. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  60. ->whereNull('in_reply_to_id')
  61. ->whereNull('reblog_of_id')
  62. ->count();
  63. $profile->status_count = ($count - 1);
  64. $profile->save();
  65. if(config('federation.activitypub.enabled') == true) {
  66. $this->fanoutDelete($status);
  67. } else {
  68. $this->unlinkRemoveMedia($status);
  69. }
  70. }
  71. public function unlinkRemoveMedia($status)
  72. {
  73. foreach ($status->media as $media) {
  74. MediaStorageService::delete($media, true);
  75. }
  76. if($status->in_reply_to_id) {
  77. DB::transaction(function() use($status) {
  78. $parent = Status::findOrFail($status->in_reply_to_id);
  79. --$parent->reply_count;
  80. $parent->save();
  81. });
  82. }
  83. DB::transaction(function() use($status) {
  84. $comments = Status::where('in_reply_to_id', $status->id)->get();
  85. foreach ($comments as $comment) {
  86. $comment->in_reply_to_id = null;
  87. $comment->save();
  88. Notification::whereItemType('App\Status')
  89. ->whereItemId($comment->id)
  90. ->delete();
  91. }
  92. $status->likes()->delete();
  93. Notification::whereItemType('App\Status')
  94. ->whereItemId($status->id)
  95. ->delete();
  96. StatusHashtag::whereStatusId($status->id)->delete();
  97. Report::whereObjectType('App\Status')
  98. ->whereObjectId($status->id)
  99. ->delete();
  100. MediaTag::where('status_id', $status->id)
  101. ->cursor()
  102. ->each(function($tag) {
  103. Notification::where('item_type', 'App\MediaTag')
  104. ->where('item_id', $tag->id)
  105. ->forceDelete();
  106. $tag->delete();
  107. });
  108. AccountInterstitial::where('item_type', 'App\Status')
  109. ->where('item_id', $status->id)
  110. ->delete();
  111. $status->forceDelete();
  112. });
  113. return true;
  114. }
  115. protected function fanoutDelete($status)
  116. {
  117. $audience = $status->profile->getAudienceInbox();
  118. $profile = $status->profile;
  119. $fractal = new Fractal\Manager();
  120. $fractal->setSerializer(new ArraySerializer());
  121. $resource = new Fractal\Resource\Item($status, new DeleteNote());
  122. $activity = $fractal->createData($resource)->toArray();
  123. $this->unlinkRemoveMedia($status);
  124. $payload = json_encode($activity);
  125. $client = new Client([
  126. 'timeout' => config('federation.activitypub.delivery.timeout')
  127. ]);
  128. $requests = function($audience) use ($client, $activity, $profile, $payload) {
  129. foreach($audience as $url) {
  130. $headers = HttpSignature::sign($profile, $url, $activity);
  131. yield function() use ($client, $url, $headers, $payload) {
  132. return $client->postAsync($url, [
  133. 'curl' => [
  134. CURLOPT_HTTPHEADER => $headers,
  135. CURLOPT_POSTFIELDS => $payload,
  136. CURLOPT_HEADER => true
  137. ]
  138. ]);
  139. };
  140. }
  141. };
  142. $pool = new Pool($client, $requests($audience), [
  143. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  144. 'fulfilled' => function ($response, $index) {
  145. },
  146. 'rejected' => function ($reason, $index) {
  147. }
  148. ]);
  149. $promise = $pool->promise();
  150. $promise->wait();
  151. }
  152. }