StatusDelete.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. class StatusDelete implements ShouldQueue
  28. {
  29. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  30. protected $status;
  31. /**
  32. * Delete the job if its models no longer exist.
  33. *
  34. * @var bool
  35. */
  36. public $deleteWhenMissingModels = true;
  37. /**
  38. * Create a new job instance.
  39. *
  40. * @return void
  41. */
  42. public function __construct(Status $status)
  43. {
  44. $this->status = $status;
  45. }
  46. /**
  47. * Execute the job.
  48. *
  49. * @return void
  50. */
  51. public function handle()
  52. {
  53. $status = $this->status;
  54. $profile = $this->status->profile;
  55. StatusService::del($status->id);
  56. $count = $profile->statuses()
  57. ->getQuery()
  58. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  59. ->whereNull('in_reply_to_id')
  60. ->whereNull('reblog_of_id')
  61. ->count();
  62. $profile->status_count = ($count - 1);
  63. $profile->save();
  64. if(config('federation.activitypub.enabled') == true) {
  65. $this->fanoutDelete($status);
  66. } else {
  67. $this->unlinkRemoveMedia($status);
  68. }
  69. }
  70. public function unlinkRemoveMedia($status)
  71. {
  72. foreach ($status->media as $media) {
  73. $thumbnail = storage_path("app/{$media->thumbnail_path}");
  74. $photo = storage_path("app/{$media->media_path}");
  75. try {
  76. if (is_file($thumbnail)) {
  77. unlink($thumbnail);
  78. }
  79. if (is_file($photo)) {
  80. unlink($photo);
  81. }
  82. if( config('pixelfed.cloud_storage') == true) {
  83. if( Str::of($media->media_path)
  84. ->startsWith('public/') &&
  85. Storage::disk(config('filesystems.cloud'))
  86. ->exists($media->media_path)
  87. ) {
  88. Storage::disk(config('filesystems.cloud'))
  89. ->delete($media->media_path);
  90. }
  91. if( Str::of($media->thumbnail_path)
  92. ->startsWith('public/') &&
  93. Storage::disk(config('filesystems.cloud'))
  94. ->exists($media->thumbnail_path)
  95. ) {
  96. Storage::disk(config('filesystems.cloud'))
  97. ->delete($media->thumbnail_path);
  98. }
  99. }
  100. $media->delete();
  101. } catch (Exception $e) {
  102. }
  103. }
  104. if($status->in_reply_to_id) {
  105. DB::transaction(function() use($status) {
  106. $parent = Status::findOrFail($status->in_reply_to_id);
  107. --$parent->reply_count;
  108. $parent->save();
  109. });
  110. }
  111. DB::transaction(function() use($status) {
  112. $comments = Status::where('in_reply_to_id', $status->id)->get();
  113. foreach ($comments as $comment) {
  114. $comment->in_reply_to_id = null;
  115. $comment->save();
  116. Notification::whereItemType('App\Status')
  117. ->whereItemId($comment->id)
  118. ->delete();
  119. }
  120. $status->likes()->delete();
  121. Notification::whereItemType('App\Status')
  122. ->whereItemId($status->id)
  123. ->delete();
  124. StatusHashtag::whereStatusId($status->id)->delete();
  125. Report::whereObjectType('App\Status')
  126. ->whereObjectId($status->id)
  127. ->delete();
  128. MediaTag::where('status_id', $status->id)
  129. ->cursor()
  130. ->each(function($tag) {
  131. Notification::where('item_type', 'App\MediaTag')
  132. ->where('item_id', $tag->id)
  133. ->forceDelete();
  134. $tag->delete();
  135. });
  136. AccountInterstitial::where('item_type', 'App\Status')
  137. ->where('item_id', $status->id)
  138. ->delete();
  139. $status->forceDelete();
  140. });
  141. return true;
  142. }
  143. protected function fanoutDelete($status)
  144. {
  145. $audience = $status->profile->getAudienceInbox();
  146. $profile = $status->profile;
  147. $fractal = new Fractal\Manager();
  148. $fractal->setSerializer(new ArraySerializer());
  149. $resource = new Fractal\Resource\Item($status, new DeleteNote());
  150. $activity = $fractal->createData($resource)->toArray();
  151. $this->unlinkRemoveMedia($status);
  152. $payload = json_encode($activity);
  153. $client = new Client([
  154. 'timeout' => config('federation.activitypub.delivery.timeout')
  155. ]);
  156. $requests = function($audience) use ($client, $activity, $profile, $payload) {
  157. foreach($audience as $url) {
  158. $headers = HttpSignature::sign($profile, $url, $activity);
  159. yield function() use ($client, $url, $headers, $payload) {
  160. return $client->postAsync($url, [
  161. 'curl' => [
  162. CURLOPT_HTTPHEADER => $headers,
  163. CURLOPT_POSTFIELDS => $payload,
  164. CURLOPT_HEADER => true
  165. ]
  166. ]);
  167. };
  168. }
  169. };
  170. $pool = new Pool($client, $requests($audience), [
  171. 'concurrency' => config('federation.activitypub.delivery.concurrency'),
  172. 'fulfilled' => function ($response, $index) {
  173. },
  174. 'rejected' => function ($reason, $index) {
  175. }
  176. ]);
  177. $promise = $pool->promise();
  178. $promise->wait();
  179. }
  180. }