1
0

StatusDelete.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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, true);
  57. if(in_array($status->type, ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])) {
  58. $profile->status_count = $profile->status_count - 1;
  59. $profile->save();
  60. }
  61. if(config_cache('federation.activitypub.enabled') == true) {
  62. $this->fanoutDelete($status);
  63. } else {
  64. $this->unlinkRemoveMedia($status);
  65. }
  66. }
  67. public function unlinkRemoveMedia($status)
  68. {
  69. foreach ($status->media as $media) {
  70. MediaStorageService::delete($media, true);
  71. }
  72. if($status->in_reply_to_id) {
  73. DB::transaction(function() use($status) {
  74. $parent = Status::findOrFail($status->in_reply_to_id);
  75. --$parent->reply_count;
  76. $parent->save();
  77. });
  78. }
  79. DB::transaction(function() use($status) {
  80. $comments = Status::where('in_reply_to_id', $status->id)->get();
  81. foreach ($comments as $comment) {
  82. $comment->in_reply_to_id = null;
  83. $comment->save();
  84. Notification::whereItemType('App\Status')
  85. ->whereItemId($comment->id)
  86. ->delete();
  87. }
  88. $status->likes()->delete();
  89. Notification::whereItemType('App\Status')
  90. ->whereItemId($status->id)
  91. ->delete();
  92. StatusHashtag::whereStatusId($status->id)->delete();
  93. Report::whereObjectType('App\Status')
  94. ->whereObjectId($status->id)
  95. ->delete();
  96. MediaTag::where('status_id', $status->id)
  97. ->cursor()
  98. ->each(function($tag) {
  99. Notification::where('item_type', 'App\MediaTag')
  100. ->where('item_id', $tag->id)
  101. ->forceDelete();
  102. $tag->delete();
  103. });
  104. AccountInterstitial::where('item_type', 'App\Status')
  105. ->where('item_id', $status->id)
  106. ->delete();
  107. $status->forceDelete();
  108. });
  109. return true;
  110. }
  111. protected function fanoutDelete($status)
  112. {
  113. $audience = $status->profile->getAudienceInbox();
  114. $profile = $status->profile;
  115. $fractal = new Fractal\Manager();
  116. $fractal->setSerializer(new ArraySerializer());
  117. $resource = new Fractal\Resource\Item($status, new DeleteNote());
  118. $activity = $fractal->createData($resource)->toArray();
  119. $this->unlinkRemoveMedia($status);
  120. $payload = json_encode($activity);
  121. $client = new Client([
  122. 'timeout' => config('federation.activitypub.delivery.timeout')
  123. ]);
  124. $requests = function($audience) use ($client, $activity, $profile, $payload) {
  125. foreach($audience as $url) {
  126. $headers = HttpSignature::sign($profile, $url, $activity);
  127. yield function() use ($client, $url, $headers, $payload) {
  128. return $client->postAsync($url, [
  129. 'curl' => [
  130. CURLOPT_HTTPHEADER => $headers,
  131. CURLOPT_POSTFIELDS => $payload,
  132. CURLOPT_HEADER => true
  133. ]
  134. ]);
  135. };
  136. }
  137. };
  138. $pool = new Pool($client, $requests($audience), [
  139. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  140. 'fulfilled' => function ($response, $index) {
  141. },
  142. 'rejected' => function ($reason, $index) {
  143. }
  144. ]);
  145. $promise = $pool->promise();
  146. $promise->wait();
  147. }
  148. }