DeleteAccountPipeline.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 DeleteAccountPipeline implements ShouldQueue
  48. {
  49. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  50. protected $user;
  51. public function __construct(User $user)
  52. {
  53. $this->user = $user;
  54. }
  55. public function handle()
  56. {
  57. $user = $this->user;
  58. DB::transaction(function() use ($user) {
  59. AccountLog::chunk(200, function($logs) use ($user) {
  60. foreach($logs as $log) {
  61. if($log->user_id == $user->id) {
  62. $log->forceDelete();
  63. }
  64. }
  65. });
  66. });
  67. DB::transaction(function() use ($user) {
  68. AccountInterstitial::whereUserId($user->id)->delete();
  69. });
  70. DB::transaction(function() use ($user) {
  71. if($user->profile) {
  72. $avatar = $user->profile->avatar;
  73. $avatar->forceDelete();
  74. }
  75. $id = $user->profile_id;
  76. ImportData::whereProfileId($id)
  77. ->cursor()
  78. ->each(function($data) {
  79. $path = storage_path('app/'.$data->path);
  80. if(is_file($path)) {
  81. unlink($path);
  82. }
  83. $data->delete();
  84. });
  85. ImportJob::whereProfileId($id)
  86. ->cursor()
  87. ->each(function($data) {
  88. $path = storage_path('app/'.$data->media_json);
  89. if(is_file($path)) {
  90. unlink($path);
  91. }
  92. $data->delete();
  93. });
  94. MediaTag::whereProfileId($id)->delete();
  95. Bookmark::whereProfileId($id)->forceDelete();
  96. EmailVerification::whereUserId($user->id)->forceDelete();
  97. StatusHashtag::whereProfileId($id)->delete();
  98. DirectMessage::whereFromId($id)->delete();
  99. FollowRequest::whereFollowingId($id)
  100. ->orWhere('follower_id', $id)
  101. ->forceDelete();
  102. Follower::whereProfileId($id)
  103. ->orWhere('following_id', $id)
  104. ->forceDelete();
  105. Like::whereProfileId($id)->forceDelete();
  106. });
  107. DB::transaction(function() use ($user) {
  108. $pid = $this->user->profile_id;
  109. StoryView::whereProfileId($pid)->delete();
  110. $stories = Story::whereProfileId($pid)->get();
  111. foreach($stories as $story) {
  112. $path = storage_path('app/'.$story->path);
  113. if(is_file($path)) {
  114. unlink($path);
  115. }
  116. $story->forceDelete();
  117. }
  118. });
  119. DB::transaction(function() use ($user) {
  120. $medias = Media::whereUserId($user->id)->get();
  121. foreach($medias as $media) {
  122. $path = storage_path('app/'.$media->media_path);
  123. $thumb = storage_path('app/'.$media->thumbnail_path);
  124. if(is_file($path)) {
  125. unlink($path);
  126. }
  127. if(is_file($thumb)) {
  128. unlink($thumb);
  129. }
  130. $media->forceDelete();
  131. }
  132. });
  133. DB::transaction(function() use ($user) {
  134. Mention::whereProfileId($user->profile_id)->forceDelete();
  135. Notification::whereProfileId($user->profile_id)
  136. ->orWhere('actor_id', $user->profile_id)
  137. ->forceDelete();
  138. });
  139. DB::transaction(function() use ($user) {
  140. $collections = Collection::whereProfileId($user->profile_id)->get();
  141. foreach ($collections as $collection) {
  142. $collection->items()->delete();
  143. $collection->delete();
  144. }
  145. Contact::whereUserId($user->id)->delete();
  146. HashtagFollow::whereUserId($user->id)->delete();
  147. OauthClient::whereUserId($user->id)->delete();
  148. ProfileSponsor::whereProfileId($user->profile_id)->delete();
  149. });
  150. DB::transaction(function() use ($user) {
  151. Status::whereProfileId($user->profile_id)
  152. ->cursor()
  153. ->each(function($status) {
  154. AccountInterstitial::where('item_type', 'App\Status')
  155. ->where('item_id', $status->id)
  156. ->delete();
  157. $status->forceDelete();
  158. });
  159. Report::whereUserId($user->id)->forceDelete();
  160. $this->deleteProfile($user);
  161. });
  162. }
  163. protected function deleteProfile($user) {
  164. DB::transaction(function() use ($user) {
  165. Profile::whereUserId($user->id)->delete();
  166. $this->deleteUserSettings($user);
  167. });
  168. }
  169. protected function deleteUserSettings($user) {
  170. DB::transaction(function() use ($user) {
  171. UserDevice::whereUserId($user->id)->forceDelete();
  172. UserFilter::whereUserId($user->id)->forceDelete();
  173. UserSetting::whereUserId($user->id)->forceDelete();
  174. $this->deleteUserColumns($user);
  175. });
  176. }
  177. protected function deleteUserColumns($user)
  178. {
  179. DB::transaction(function() use ($user) {
  180. $user->status = 'deleted';
  181. $user->name = 'deleted';
  182. $user->email = $user->id;
  183. $user->password = '';
  184. $user->remember_token = null;
  185. $user->is_admin = false;
  186. $user->{'2fa_enabled'} = false;
  187. $user->{'2fa_secret'} = null;
  188. $user->{'2fa_backup_codes'} = null;
  189. $user->{'2fa_setup_at'} = null;
  190. $user->save();
  191. });
  192. }
  193. }