From 02909de2b312d419aa3e75ebf45abb09e18f571c Mon Sep 17 00:00:00 2001 From: IanM Date: Thu, 18 Jan 2024 11:37:54 +0000 Subject: [PATCH] only regenerate when needed --- src/Listener/GenerateAvatar.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Listener/GenerateAvatar.php b/src/Listener/GenerateAvatar.php index 1236fe8..6e0e7e2 100644 --- a/src/Listener/GenerateAvatar.php +++ b/src/Listener/GenerateAvatar.php @@ -14,6 +14,7 @@ use Blomstra\Gdpr\Events\Erased; use Blomstra\Gdpr\Models\ErasureRequest; use Flarum\Bus\Dispatcher as BusDispatcher; +use Flarum\Settings\SettingsRepositoryInterface; use Flarum\User\Event\EmailChanged; use Flarum\User\Event\LoggedIn; use Flarum\User\Event\Registered; @@ -24,8 +25,10 @@ class GenerateAvatar { - public function __construct(public BusDispatcher $bus) - { + public function __construct( + public BusDispatcher $bus, + protected SettingsRepositoryInterface $settings + ) { } public function subscribe(EventsDispatcher $events): void @@ -36,7 +39,11 @@ public function subscribe(EventsDispatcher $events): void public function generate($event): void { - if ((!$event->user->isGuest() && empty($event->user->user_svg)) || $event instanceof Renamed || $event instanceof EmailChanged) { + if ( + (!$event->user->isGuest() && empty($event->user->user_svg)) || + ($event instanceof Renamed && $this->getIdentifier() === 'display_name') || + ($event instanceof EmailChanged && $this->getIdentifier() === 'email') + ) { $event->user = $this->bus->dispatch(new GenerateAvatarCommand( $event->user, BoringAvatar::$defaultGenerationSize, @@ -59,4 +66,9 @@ public function handleErased(Erased $event): void } } } + + protected function getIdentifier(): string + { + return $this->settings->get('ianm-boring-avatars.identifier'); + } }