mirror of
https://github.com/flarum/core.git
synced 2025-07-25 18:51:40 +02:00
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:
@@ -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);
|
||||||
}
|
}
|
||||||
|
@@ -80,6 +80,8 @@ class UploadAvatarHandler
|
|||||||
|
|
||||||
$user->save();
|
$user->save();
|
||||||
|
|
||||||
|
$this->dispatchEventsFor($user, $actor);
|
||||||
|
|
||||||
return $user;
|
return $user;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user