Fix AvatarChanged event (#2197)

* Fix AvatarChanged event not being dispatched when changing avatar
Also fix the uploader to trigger the event only once
This commit is contained in:
Clark Winkelmann 2020-06-15 06:20:24 +02:00 committed by GitHub
parent 4da2994d1f
commit 17c239388a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -36,13 +36,18 @@ class AvatarUploader
$avatarPath = Str::random().'.png'; $avatarPath = Str::random().'.png';
$this->remove($user); $this->removeFileAfterSave($user);
$user->changeAvatarPath($avatarPath); $user->changeAvatarPath($avatarPath);
$this->uploadDir->put($avatarPath, $encodedImage); $this->uploadDir->put($avatarPath, $encodedImage);
} }
public function remove(User $user) /**
* Handle the removal of the old avatar file after a successful user save
* We don't place this in remove() because otherwise we would call changeAvatarPath 2 times when uploading.
* @param User $user
*/
protected function removeFileAfterSave(User $user)
{ {
$avatarPath = $user->getOriginal('avatar_url'); $avatarPath = $user->getOriginal('avatar_url');
@ -51,6 +56,14 @@ class AvatarUploader
$this->uploadDir->delete($avatarPath); $this->uploadDir->delete($avatarPath);
} }
}); });
}
/**
* @param User $user
*/
public function remove(User $user)
{
$this->removeFileAfterSave($user);
$user->changeAvatarPath(null); $user->changeAvatarPath(null);
} }

View File

@ -80,6 +80,8 @@ class UploadAvatarHandler
$user->save(); $user->save();
$this->dispatchEventsFor($user, $actor);
return $user; return $user;
} }
} }