DeleteRemoteProfilePipeline.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace App\Jobs\DeletePipeline;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Queue\SerializesModels;
  5. use Illuminate\Queue\InteractsWithQueue;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use DB;
  9. use Illuminate\Support\Str;
  10. use App\{
  11. AccountInterstitial,
  12. AccountLog,
  13. Activity,
  14. Avatar,
  15. Bookmark,
  16. Collection,
  17. CollectionItem,
  18. Contact,
  19. DirectMessage,
  20. EmailVerification,
  21. Follower,
  22. FollowRequest,
  23. Hashtag,
  24. HashtagFollow,
  25. ImportData,
  26. ImportJob,
  27. Like,
  28. Media,
  29. MediaTag,
  30. Mention,
  31. Notification,
  32. OauthClient,
  33. Profile,
  34. ProfileSponsor,
  35. Report,
  36. ReportComment,
  37. ReportLog,
  38. StatusHashtag,
  39. Status,
  40. Story,
  41. StoryView,
  42. User,
  43. UserDevice,
  44. UserFilter,
  45. UserSetting,
  46. };
  47. class DeleteRemoteProfilePipeline implements ShouldQueue
  48. {
  49. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  50. protected $profile;
  51. public function __construct(Profile $profile)
  52. {
  53. $this->profile = $profile;
  54. }
  55. public function handle()
  56. {
  57. $profile = $this->profile;
  58. if($profile->domain == null || $profile->private_key) {
  59. return;
  60. }
  61. DB::transaction(function() use ($profile) {
  62. $profile->avatar->forceDelete();
  63. $id = $profile->id;
  64. MediaTag::whereProfileId($id)->delete();
  65. StatusHashtag::whereProfileId($id)->delete();
  66. DirectMessage::whereFromId($id)->delete();
  67. FollowRequest::whereFollowingId($id)
  68. ->orWhere('follower_id', $id)
  69. ->forceDelete();
  70. Follower::whereProfileId($id)
  71. ->orWhere('following_id', $id)
  72. ->forceDelete();
  73. Like::whereProfileId($id)->forceDelete();
  74. });
  75. DB::transaction(function() use ($profile) {
  76. $pid = $profile->id;
  77. StoryView::whereProfileId($pid)->delete();
  78. $stories = Story::whereProfileId($pid)->get();
  79. foreach($stories as $story) {
  80. $path = storage_path('app/'.$story->path);
  81. if(is_file($path)) {
  82. unlink($path);
  83. }
  84. $story->forceDelete();
  85. }
  86. });
  87. DB::transaction(function() use ($profile) {
  88. $medias = Media::whereProfileId($profile->id)->get();
  89. foreach($medias as $media) {
  90. $path = storage_path('app/'.$media->media_path);
  91. $thumb = storage_path('app/'.$media->thumbnail_path);
  92. if(is_file($path)) {
  93. unlink($path);
  94. }
  95. if(is_file($thumb)) {
  96. unlink($thumb);
  97. }
  98. $media->forceDelete();
  99. }
  100. });
  101. DB::transaction(function() use ($profile) {
  102. Mention::whereProfileId($profile->id)->forceDelete();
  103. Notification::whereProfileId($profile->id)
  104. ->orWhere('actor_id', $profile->id)
  105. ->forceDelete();
  106. });
  107. DB::transaction(function() use ($profile) {
  108. Status::whereProfileId($profile->id)
  109. ->cursor()
  110. ->each(function($status) {
  111. AccountInterstitial::where('item_type', 'App\Status')
  112. ->where('item_id', $status->id)
  113. ->delete();
  114. $status->forceDelete();
  115. });
  116. Report::whereProfileId($profile->id)->forceDelete();
  117. $this->deleteProfile($profile);
  118. });
  119. }
  120. protected function deleteProfile($profile) {
  121. DB::transaction(function() use ($profile) {
  122. Profile::findOrFail($profile->id)->delete();
  123. });
  124. }
  125. }