DeleteAccountPipeline.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 Storage;
  10. use Illuminate\Support\Str;
  11. use App\Services\AccountService;
  12. use App\Services\FollowerService;
  13. use App\Services\PublicTimelineService;
  14. use App\{
  15. AccountInterstitial,
  16. AccountLog,
  17. Avatar,
  18. Bookmark,
  19. Collection,
  20. CollectionItem,
  21. Contact,
  22. DirectMessage,
  23. EmailVerification,
  24. Follower,
  25. FollowRequest,
  26. Hashtag,
  27. HashtagFollow,
  28. ImportData,
  29. ImportJob,
  30. Like,
  31. Media,
  32. MediaTag,
  33. Mention,
  34. Notification,
  35. OauthClient,
  36. Profile,
  37. ProfileSponsor,
  38. Report,
  39. ReportComment,
  40. ReportLog,
  41. StatusHashtag,
  42. StatusArchived,
  43. Status,
  44. Story,
  45. StoryView,
  46. User,
  47. UserDevice,
  48. UserFilter,
  49. UserSetting,
  50. };
  51. use App\Models\Conversation;
  52. use App\Models\Poll;
  53. use App\Models\PollVote;
  54. use App\Models\Portfolio;
  55. use App\Models\UserPronoun;
  56. use App\Jobs\StatusPipeline\StatusDelete;
  57. class DeleteAccountPipeline implements ShouldQueue
  58. {
  59. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  60. protected $user;
  61. public $timeout = 900;
  62. public $tries = 3;
  63. public $maxExceptions = 1;
  64. public $deleteWhenMissingModels = true;
  65. public function __construct(User $user)
  66. {
  67. $this->user = $user;
  68. }
  69. public function handle()
  70. {
  71. $user = $this->user;
  72. $profile = $user->profile;
  73. $id = $user->profile_id;
  74. Status::whereProfileId($id)->chunk(50, function($statuses) {
  75. foreach($statuses as $status) {
  76. StatusDelete::dispatch($status);
  77. }
  78. });
  79. AccountLog::whereItemType('App\User')->whereItemId($user->id)->forceDelete();
  80. AccountInterstitial::whereUserId($user->id)->delete();
  81. // Delete Avatar
  82. $profile->avatar->forceDelete();
  83. // Delete Poll Votes
  84. PollVote::whereProfileId($id)->delete();
  85. // Delete Polls
  86. Poll::whereProfileId($id)->delete();
  87. // Delete Portfolio
  88. Portfolio::whereProfileId($id)->delete();
  89. ImportData::whereProfileId($id)
  90. ->cursor()
  91. ->each(function($data) {
  92. $path = storage_path('app/'.$data->path);
  93. if(is_file($path)) {
  94. unlink($path);
  95. }
  96. $data->delete();
  97. });
  98. ImportJob::whereProfileId($id)
  99. ->cursor()
  100. ->each(function($data) {
  101. $path = storage_path('app/'.$data->media_json);
  102. if(is_file($path)) {
  103. unlink($path);
  104. }
  105. $data->delete();
  106. });
  107. MediaTag::whereProfileId($id)->delete();
  108. Bookmark::whereProfileId($id)->forceDelete();
  109. EmailVerification::whereUserId($user->id)->forceDelete();
  110. StatusHashtag::whereProfileId($id)->delete();
  111. DirectMessage::whereFromId($id)->orWhere('to_id', $id)->delete();
  112. Conversation::whereFromId($id)->orWhere('to_id', $id)->delete();
  113. StatusArchived::whereProfileId($id)->delete();
  114. UserPronoun::whereProfileId($id)->delete();
  115. FollowRequest::whereFollowingId($id)
  116. ->orWhere('follower_id', $id)
  117. ->forceDelete();
  118. Follower::whereProfileId($id)
  119. ->orWhere('following_id', $id)
  120. ->each(function($follow) {
  121. FollowerService::remove($follow->profile_id, $follow->following_id);
  122. $follow->delete();
  123. });
  124. FollowerService::delCache($id);
  125. Like::whereProfileId($id)->forceDelete();
  126. Mention::whereProfileId($id)->forceDelete();
  127. StoryView::whereProfileId($id)->delete();
  128. $stories = Story::whereProfileId($id)->get();
  129. foreach($stories as $story) {
  130. $path = storage_path('app/'.$story->path);
  131. if(is_file($path)) {
  132. unlink($path);
  133. }
  134. $story->forceDelete();
  135. }
  136. UserDevice::whereUserId($user->id)->forceDelete();
  137. UserFilter::whereUserId($user->id)->forceDelete();
  138. UserSetting::whereUserId($user->id)->forceDelete();
  139. Mention::whereProfileId($id)->forceDelete();
  140. Notification::whereProfileId($id)
  141. ->orWhere('actor_id', $id)
  142. ->forceDelete();
  143. $collections = Collection::whereProfileId($id)->get();
  144. foreach ($collections as $collection) {
  145. $collection->items()->delete();
  146. $collection->delete();
  147. }
  148. Contact::whereUserId($user->id)->delete();
  149. HashtagFollow::whereUserId($user->id)->delete();
  150. OauthClient::whereUserId($user->id)->delete();
  151. DB::table('oauth_access_tokens')->whereUserId($user->id)->delete();
  152. DB::table('oauth_auth_codes')->whereUserId($user->id)->delete();
  153. ProfileSponsor::whereProfileId($id)->delete();
  154. Report::whereUserId($user->id)->forceDelete();
  155. PublicTimelineService::warmCache(true, 400);
  156. $this->deleteUserColumns($user);
  157. AccountService::del($user->profile_id);
  158. Profile::whereUserId($user->id)->delete();
  159. }
  160. protected function deleteUserColumns($user)
  161. {
  162. DB::transaction(function() use ($user) {
  163. $user->status = 'deleted';
  164. $user->name = 'deleted';
  165. $user->email = $user->id;
  166. $user->password = '';
  167. $user->remember_token = null;
  168. $user->is_admin = false;
  169. $user->{'2fa_enabled'} = false;
  170. $user->{'2fa_secret'} = null;
  171. $user->{'2fa_backup_codes'} = null;
  172. $user->{'2fa_setup_at'} = null;
  173. $user->save();
  174. });
  175. }
  176. }