From 2261f4f2205329a946cc58141cafe44308c72724 Mon Sep 17 00:00:00 2001 From: Clark Winkelmann Date: Mon, 15 Jun 2020 06:20:24 +0200 Subject: [PATCH] Fix AvatarChanged event (#2197) * Fix AvatarChanged event not being dispatched when changing avatar Also fix the uploader to trigger the event only once --- framework/core/src/User/AvatarUploader.php | 17 +++++++++++++++-- .../src/User/Command/UploadAvatarHandler.php | 2 ++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/framework/core/src/User/AvatarUploader.php b/framework/core/src/User/AvatarUploader.php index 7070ff4b8..5bfb3d8a4 100644 --- a/framework/core/src/User/AvatarUploader.php +++ b/framework/core/src/User/AvatarUploader.php @@ -36,13 +36,18 @@ class AvatarUploader $avatarPath = Str::random().'.png'; - $this->remove($user); + $this->removeFileAfterSave($user); $user->changeAvatarPath($avatarPath); $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'); @@ -51,6 +56,14 @@ class AvatarUploader $this->uploadDir->delete($avatarPath); } }); + } + + /** + * @param User $user + */ + public function remove(User $user) + { + $this->removeFileAfterSave($user); $user->changeAvatarPath(null); } diff --git a/framework/core/src/User/Command/UploadAvatarHandler.php b/framework/core/src/User/Command/UploadAvatarHandler.php index 7700becf1..69a3976c1 100644 --- a/framework/core/src/User/Command/UploadAvatarHandler.php +++ b/framework/core/src/User/Command/UploadAvatarHandler.php @@ -80,6 +80,8 @@ class UploadAvatarHandler $user->save(); + $this->dispatchEventsFor($user, $actor); + return $user; } }