DeleteAccountPipeline.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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\PublicTimelineService;
  13. use App\{
  14. AccountInterstitial,
  15. AccountLog,
  16. Avatar,
  17. Bookmark,
  18. Collection,
  19. CollectionItem,
  20. Contact,
  21. DirectMessage,
  22. EmailVerification,
  23. Follower,
  24. FollowRequest,
  25. Hashtag,
  26. HashtagFollow,
  27. ImportData,
  28. ImportJob,
  29. Like,
  30. Media,
  31. MediaTag,
  32. Mention,
  33. Notification,
  34. OauthClient,
  35. Profile,
  36. ProfileSponsor,
  37. Report,
  38. ReportComment,
  39. ReportLog,
  40. StatusHashtag,
  41. StatusArchived,
  42. Status,
  43. Story,
  44. StoryView,
  45. User,
  46. UserDevice,
  47. UserFilter,
  48. UserSetting,
  49. };
  50. use App\Models\UserPronoun;
  51. class DeleteAccountPipeline implements ShouldQueue
  52. {
  53. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  54. protected $user;
  55. public function __construct(User $user)
  56. {
  57. $this->user = $user;
  58. }
  59. public function handle()
  60. {
  61. $user = $this->user;
  62. $this->deleteUserColumns($user);
  63. AccountService::del($user->profile_id);
  64. DB::transaction(function() use ($user) {
  65. AccountLog::whereItemType('App\User')->whereItemId($user->id)->forceDelete();
  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. $path = $avatar->media_path;
  74. if(!in_array($path, [
  75. 'public/avatars/default.jpg',
  76. 'public/avatars/default.png'
  77. ])) {
  78. if(config('pixelfed.cloud_storage')) {
  79. $disk = Storage::disk(config('filesystems.cloud'));
  80. $disk->delete($path);
  81. }
  82. $disk = Storage::disk(config('filesystems.local'));
  83. $disk->delete($path);
  84. }
  85. $avatar->forceDelete();
  86. }
  87. $id = $user->profile_id;
  88. ImportData::whereProfileId($id)
  89. ->cursor()
  90. ->each(function($data) {
  91. $path = storage_path('app/'.$data->path);
  92. if(is_file($path)) {
  93. unlink($path);
  94. }
  95. $data->delete();
  96. });
  97. ImportJob::whereProfileId($id)
  98. ->cursor()
  99. ->each(function($data) {
  100. $path = storage_path('app/'.$data->media_json);
  101. if(is_file($path)) {
  102. unlink($path);
  103. }
  104. $data->delete();
  105. });
  106. MediaTag::whereProfileId($id)->delete();
  107. Bookmark::whereProfileId($id)->forceDelete();
  108. EmailVerification::whereUserId($user->id)->forceDelete();
  109. StatusHashtag::whereProfileId($id)->delete();
  110. DirectMessage::whereFromId($id)->orWhere('to_id', $id)->delete();
  111. StatusArchived::whereProfileId($id)->delete();
  112. UserPronoun::whereProfileId($id)->delete();
  113. FollowRequest::whereFollowingId($id)
  114. ->orWhere('follower_id', $id)
  115. ->forceDelete();
  116. Follower::whereProfileId($id)
  117. ->orWhere('following_id', $id)
  118. ->forceDelete();
  119. Like::whereProfileId($id)->forceDelete();
  120. });
  121. DB::transaction(function() use ($user) {
  122. $pid = $this->user->profile_id;
  123. StoryView::whereProfileId($pid)->delete();
  124. $stories = Story::whereProfileId($pid)->get();
  125. foreach($stories as $story) {
  126. $path = storage_path('app/'.$story->path);
  127. if(is_file($path)) {
  128. unlink($path);
  129. }
  130. $story->forceDelete();
  131. }
  132. });
  133. DB::transaction(function() use ($user) {
  134. $medias = Media::whereUserId($user->id)->get();
  135. foreach($medias as $media) {
  136. if(config('pixelfed.cloud_storage')) {
  137. $disk = Storage::disk(config('filesystems.cloud'));
  138. $disk->delete($media->media_path);
  139. $disk->delete($media->thumbnail_path);
  140. }
  141. $disk = Storage::disk(config('filesystems.local'));
  142. $disk->delete($media->media_path);
  143. $disk->delete($media->thumbnail_path);
  144. $media->forceDelete();
  145. }
  146. });
  147. DB::transaction(function() use ($user) {
  148. Mention::whereProfileId($user->profile_id)->forceDelete();
  149. Notification::whereProfileId($user->profile_id)
  150. ->orWhere('actor_id', $user->profile_id)
  151. ->forceDelete();
  152. });
  153. DB::transaction(function() use ($user) {
  154. $collections = Collection::whereProfileId($user->profile_id)->get();
  155. foreach ($collections as $collection) {
  156. $collection->items()->delete();
  157. $collection->delete();
  158. }
  159. Contact::whereUserId($user->id)->delete();
  160. HashtagFollow::whereUserId($user->id)->delete();
  161. OauthClient::whereUserId($user->id)->delete();
  162. DB::table('oauth_access_tokens')->whereUserId($user->id)->delete();
  163. DB::table('oauth_auth_codes')->whereUserId($user->id)->delete();
  164. ProfileSponsor::whereProfileId($user->profile_id)->delete();
  165. });
  166. DB::transaction(function() use ($user) {
  167. Status::whereProfileId($user->profile_id)->forceDelete();
  168. Report::whereUserId($user->id)->forceDelete();
  169. PublicTimelineService::warmCache(true, 400);
  170. $this->deleteProfile($user);
  171. });
  172. }
  173. protected function deleteProfile($user) {
  174. DB::transaction(function() use ($user) {
  175. Profile::whereUserId($user->id)->delete();
  176. $this->deleteUserSettings($user);
  177. });
  178. }
  179. protected function deleteUserSettings($user) {
  180. DB::transaction(function() use ($user) {
  181. UserDevice::whereUserId($user->id)->forceDelete();
  182. UserFilter::whereUserId($user->id)->forceDelete();
  183. UserSetting::whereUserId($user->id)->forceDelete();
  184. });
  185. }
  186. protected function deleteUserColumns($user)
  187. {
  188. DB::transaction(function() use ($user) {
  189. $user->status = 'deleted';
  190. $user->name = 'deleted';
  191. $user->email = $user->id;
  192. $user->password = '';
  193. $user->remember_token = null;
  194. $user->is_admin = false;
  195. $user->{'2fa_enabled'} = false;
  196. $user->{'2fa_secret'} = null;
  197. $user->{'2fa_backup_codes'} = null;
  198. $user->{'2fa_setup_at'} = null;
  199. $user->save();
  200. });
  201. }
  202. }