Skip to content

Commit

Permalink
Fix AvatarChanged event (#2197)
Browse files Browse the repository at this point in the history
* Fix AvatarChanged event not being dispatched when changing avatar
Also fix the uploader to trigger the event only once
  • Loading branch information
clarkwinkelmann authored Jun 15, 2020
1 parent 4da2994 commit 17c2393
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/User/AvatarUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ public function upload(User $user, Image $image)

$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');

Expand All @@ -51,6 +56,14 @@ public function remove(User $user)
$this->uploadDir->delete($avatarPath);
}
});
}

/**
* @param User $user
*/
public function remove(User $user)
{
$this->removeFileAfterSave($user);

$user->changeAvatarPath(null);
}
Expand Down
2 changes: 2 additions & 0 deletions src/User/Command/UploadAvatarHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public function handle(UploadAvatar $command)

$user->save();

$this->dispatchEventsFor($user, $actor);

return $user;
}
}

0 comments on commit 17c2393

Please sign in to comment.