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 b3c2672634
commit 2261f4f220
2 changed files with 17 additions and 2 deletions

View File

@ -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);
}

View File

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