RemoteStatusDelete.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Jobs\StatusPipeline;
  3. use App\AccountInterstitial;
  4. use App\Bookmark;
  5. use App\CollectionItem;
  6. use App\DirectMessage;
  7. use App\Jobs\MediaPipeline\MediaDeletePipeline;
  8. use App\Like;
  9. use App\Media;
  10. use App\MediaTag;
  11. use App\Mention;
  12. use App\Notification;
  13. use App\Report;
  14. use App\Services\Account\AccountStatService;
  15. use App\Services\AccountService;
  16. use App\Services\CollectionService;
  17. use App\Services\NotificationService;
  18. use App\Services\StatusService;
  19. use App\Status;
  20. use App\StatusArchived;
  21. use App\StatusHashtag;
  22. use App\StatusView;
  23. use Illuminate\Bus\Queueable;
  24. use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
  25. use Illuminate\Contracts\Queue\ShouldQueue;
  26. use Illuminate\Foundation\Bus\Dispatchable;
  27. use Illuminate\Queue\InteractsWithQueue;
  28. use Illuminate\Queue\Middleware\WithoutOverlapping;
  29. use Illuminate\Queue\SerializesModels;
  30. class RemoteStatusDelete implements ShouldBeUniqueUntilProcessing, ShouldQueue
  31. {
  32. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  33. protected $status;
  34. /**
  35. * Delete the job if its models no longer exist.
  36. *
  37. * @var bool
  38. */
  39. public $deleteWhenMissingModels = true;
  40. public $tries = 3;
  41. public $maxExceptions = 3;
  42. public $timeout = 180;
  43. public $failOnTimeout = true;
  44. /**
  45. * The number of seconds after which the job's unique lock will be released.
  46. *
  47. * @var int
  48. */
  49. public $uniqueFor = 3600;
  50. /**
  51. * Get the unique ID for the job.
  52. */
  53. public function uniqueId(): string
  54. {
  55. return 'status:remote:delete:'.$this->status->id;
  56. }
  57. /**
  58. * Get the middleware the job should pass through.
  59. *
  60. * @return array<int, object>
  61. */
  62. public function middleware(): array
  63. {
  64. return [(new WithoutOverlapping("status-remote-delete-{$this->status->id}"))->shared()->dontRelease()];
  65. }
  66. /**
  67. * Create a new job instance.
  68. *
  69. * @return void
  70. */
  71. public function __construct(Status $status)
  72. {
  73. $this->status = $status->withoutRelations();
  74. }
  75. /**
  76. * Execute the job.
  77. *
  78. * @return void
  79. */
  80. public function handle()
  81. {
  82. $status = $this->status;
  83. if ($status->deleted_at) {
  84. return;
  85. }
  86. StatusService::del($status->id, true);
  87. // AccountStatService::decrementPostCount($status->profile_id);
  88. return $this->unlinkRemoveMedia($status);
  89. }
  90. public function unlinkRemoveMedia($status)
  91. {
  92. if ($status->in_reply_to_id) {
  93. $parent = Status::find($status->in_reply_to_id);
  94. if ($parent) {
  95. if ($parent->reply_count) {
  96. $parent->reply_count = $parent->reply_count - 1;
  97. $parent->save();
  98. }
  99. StatusService::del($parent->id);
  100. }
  101. }
  102. AccountInterstitial::where('item_type', 'App\Status')
  103. ->where('item_id', $status->id)
  104. ->delete();
  105. Bookmark::whereStatusId($status->id)->delete();
  106. CollectionItem::whereObjectType('App\Status')
  107. ->whereObjectId($status->id)
  108. ->get()
  109. ->each(function ($col) {
  110. CollectionService::removeItem($col->collection_id, $col->object_id);
  111. $col->delete();
  112. });
  113. $dms = DirectMessage::whereStatusId($status->id)->get();
  114. foreach ($dms as $dm) {
  115. $not = Notification::whereItemType('App\DirectMessage')
  116. ->whereItemId($dm->id)
  117. ->first();
  118. if ($not) {
  119. NotificationService::del($not->profile_id, $not->id);
  120. $not->forceDeleteQuietly();
  121. }
  122. $dm->delete();
  123. }
  124. Like::whereStatusId($status->id)->forceDelete();
  125. Media::whereStatusId($status->id)
  126. ->get()
  127. ->each(function ($media) {
  128. MediaDeletePipeline::dispatch($media)->onQueue('mmo');
  129. });
  130. $mediaTags = MediaTag::where('status_id', $status->id)->get();
  131. foreach ($mediaTags as $mtag) {
  132. $not = Notification::whereItemType('App\MediaTag')
  133. ->whereItemId($mtag->id)
  134. ->first();
  135. if ($not) {
  136. NotificationService::del($not->profile_id, $not->id);
  137. $not->forceDeleteQuietly();
  138. }
  139. $mtag->delete();
  140. }
  141. Mention::whereStatusId($status->id)->forceDelete();
  142. Notification::whereItemType('App\Status')
  143. ->whereItemId($status->id)
  144. ->forceDelete();
  145. Report::whereObjectType('App\Status')
  146. ->whereObjectId($status->id)
  147. ->delete();
  148. StatusArchived::whereStatusId($status->id)->delete();
  149. StatusHashtag::whereStatusId($status->id)->delete();
  150. StatusView::whereStatusId($status->id)->delete();
  151. Status::whereInReplyToId($status->id)->update(['in_reply_to_id' => null]);
  152. StatusService::del($status->id, true);
  153. AccountService::del($status->profile_id);
  154. $status->forceDelete();
  155. return 1;
  156. }
  157. }