StatusDelete.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. class StatusDelete implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. protected $status;
  18. /**
  19. * Delete the job if its models no longer exist.
  20. *
  21. * @var bool
  22. */
  23. public $deleteWhenMissingModels = true;
  24. /**
  25. * Create a new job instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct(Status $status)
  30. {
  31. $this->status = $status;
  32. }
  33. /**
  34. * Execute the job.
  35. *
  36. * @return void
  37. */
  38. public function handle()
  39. {
  40. $status = $this->status;
  41. $this->unlinkRemoveMedia($status);
  42. }
  43. public function unlinkRemoveMedia($status)
  44. {
  45. foreach ($status->media as $media) {
  46. $thumbnail = storage_path("app/{$media->thumbnail_path}");
  47. $photo = storage_path("app/{$media->media_path}");
  48. try {
  49. if (is_file($thumbnail)) {
  50. unlink($thumbnail);
  51. }
  52. if (is_file($photo)) {
  53. unlink($photo);
  54. }
  55. $media->delete();
  56. } catch (Exception $e) {
  57. }
  58. }
  59. $comments = Status::where('in_reply_to_id', $status->id)->get();
  60. foreach ($comments as $comment) {
  61. $comment->in_reply_to_id = null;
  62. $comment->save();
  63. Notification::whereItemType('App\Status')
  64. ->whereItemId($comment->id)
  65. ->delete();
  66. }
  67. $status->likes()->delete();
  68. Notification::whereItemType('App\Status')
  69. ->whereItemId($status->id)
  70. ->delete();
  71. StatusHashtag::whereStatusId($status->id)->delete();
  72. Report::whereObjectType('App\Status')
  73. ->whereObjectId($status->id)
  74. ->delete();
  75. $status->delete();
  76. return true;
  77. }
  78. }