Merge pull request #2020 from pixelfed/staging

Update DeleteAccountPipeline, fixes #2016
This commit is contained in:
daniel 2020-02-15 23:56:03 -07:00 committed by GitHub
commit 6f96cafebd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 169 additions and 143 deletions

View file

@ -13,7 +13,7 @@ class UserDelete extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'user:delete {id}'; protected $signature = 'user:delete {id} {--force}';
/** /**
* The console command description. * The console command description.
@ -40,12 +40,24 @@ class UserDelete extends Command
public function handle() public function handle()
{ {
$id = $this->argument('id'); $id = $this->argument('id');
$user = User::whereUsername($id)->orWhere('id', $id)->first(); $force = $this->option('force');
if(ctype_digit($id) == true) {
$user = User::find($id);
} else {
$user = User::whereUsername($id)->first();
}
if(!$user) { if(!$user) {
$this->error('Could not find any user with that username or id.'); $this->error('Could not find any user with that username or id.');
exit; exit;
} }
if($user->status == 'deleted' && $force == false) {
$this->error('Account has already been deleted.');
return;
}
if($user->is_admin == true) { if($user->is_admin == true) {
$this->error('Cannot delete an admin account from CLI.'); $this->error('Cannot delete an admin account from CLI.');
exit; exit;
@ -62,10 +74,12 @@ class UserDelete extends Command
exit; exit;
} }
if($user->status !== 'deleted') {
$profile = $user->profile; $profile = $user->profile;
$profile->status = $user->status = 'deleted'; $profile->status = $user->status = 'deleted';
$profile->save(); $profile->save();
$user->save(); $user->save();
}
DeleteAccountPipeline::dispatch($user)->onQueue('high'); DeleteAccountPipeline::dispatch($user)->onQueue('high');
} }

View file

@ -15,16 +15,21 @@ use App\{
Avatar, Avatar,
Bookmark, Bookmark,
Collection, Collection,
CollectionItem,
Contact,
DirectMessage, DirectMessage,
EmailVerification, EmailVerification,
Follower, Follower,
FollowRequest, FollowRequest,
Hashtag, Hashtag,
HashtagFollow,
Like, Like,
Media, Media,
Mention, Mention,
Notification, Notification,
OauthClient,
Profile, Profile,
ProfileSponsor,
Report, Report,
ReportComment, ReportComment,
ReportLog, ReportLog,
@ -44,24 +49,15 @@ class DeleteAccountPipeline implements ShouldQueue
protected $user; protected $user;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(User $user) public function __construct(User $user)
{ {
$this->user = $user; $this->user = $user;
} }
/**
* Execute the job.
*
* @return void
*/
public function handle() public function handle()
{ {
$user = $this->user; $user = $this->user;
DB::transaction(function() use ($user) { DB::transaction(function() use ($user) {
AccountLog::chunk(200, function($logs) use ($user) { AccountLog::chunk(200, function($logs) use ($user) {
foreach($logs as $log) { foreach($logs as $log) {
@ -75,21 +71,20 @@ class DeleteAccountPipeline implements ShouldQueue
DB::transaction(function() use ($user) { DB::transaction(function() use ($user) {
if($user->profile) { if($user->profile) {
$avatar = $user->profile->avatar; $avatar = $user->profile->avatar;
$avatar->forceDelete(); $avatar->forceDelete();
} }
Bookmark::whereProfileId($user->profile->id)->forceDelete(); $id = $user->profile_id;
Bookmark::whereProfileId($user->profile_id)->forceDelete();
EmailVerification::whereUserId($user->id)->forceDelete(); EmailVerification::whereUserId($user->id)->forceDelete();
$id = $user->profile->id;
StatusHashtag::whereProfileId($id)->delete(); StatusHashtag::whereProfileId($id)->delete();
FollowRequest::whereFollowingId($id)
FollowRequest::whereFollowingId($id)->orWhere('follower_id', $id)->forceDelete(); ->orWhere('follower_id', $id)
->forceDelete();
Follower::whereProfileId($id)->orWhere('following_id', $id)->forceDelete(); Follower::whereProfileId($id)
->orWhere('following_id', $id)
->forceDelete();
Like::whereProfileId($id)->forceDelete(); Like::whereProfileId($id)->forceDelete();
}); });
@ -123,12 +118,26 @@ class DeleteAccountPipeline implements ShouldQueue
}); });
DB::transaction(function() use ($user) { DB::transaction(function() use ($user) {
Mention::whereProfileId($user->profile->id)->forceDelete(); Mention::whereProfileId($user->profile_id)->forceDelete();
Notification::whereProfileId($user->profile->id)->orWhere('actor_id', $user->profile->id)->forceDelete(); Notification::whereProfileId($user->profile_id)
->orWhere('actor_id', $user->profile_id)
->forceDelete();
}); });
DB::transaction(function() use ($user) { DB::transaction(function() use ($user) {
Status::whereProfileId($user->profile->id)->forceDelete(); $collections = Collection::whereProfileId($user->profile_id)->get();
foreach ($collections as $collection) {
$collection->items()->delete();
$collection->delete();
}
Contact::whereUserId($user->id)->delete();
HashtagFollow::whereUserId($user->id)->delete();
OauthClient::whereUserId($user->id)->delete();
ProfileSponsor::whereProfileId($user->profile_id)->delete();
});
DB::transaction(function() use ($user) {
Status::whereProfileId($user->profile_id)->forceDelete();
Report::whereUserId($user->id)->forceDelete(); Report::whereUserId($user->id)->forceDelete();
$this->deleteProfile($user); $this->deleteProfile($user);
}); });
@ -166,6 +175,5 @@ class DeleteAccountPipeline implements ShouldQueue
$user->{'2fa_setup_at'} = null; $user->{'2fa_setup_at'} = null;
$user->save(); $user->save();
}); });
} }
} }

View file

@ -19,6 +19,10 @@ class UserObserver
*/ */
public function saved(User $user) public function saved(User $user)
{ {
if($user->status == 'deleted') {
return;
}
if (empty($user->profile)) { if (empty($user->profile)) {
$profile = DB::transaction(function() use($user) { $profile = DB::transaction(function() use($user) {
$profile = new Profile(); $profile = new Profile();