Skip to content

Commit

Permalink
fix: avatar not regenerated after email change (#3)
Browse files Browse the repository at this point in the history
* fix: avatar not regenerated after email change

* only regenerate when needed
  • Loading branch information
imorland committed Jan 18, 2024
1 parent c9b4ed1 commit 54e91e5
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/Listener/GenerateAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
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;
use Flarum\User\Event\Renamed;
Expand All @@ -23,19 +25,25 @@

class GenerateAvatar
{
public function __construct(public BusDispatcher $bus)
{
public function __construct(
public BusDispatcher $bus,
protected SettingsRepositoryInterface $settings
) {
}

public function subscribe(EventsDispatcher $events): void
{
$events->listen([Registered::class, LoggedIn::class, Renamed::class], [$this, 'generate']);
$events->listen([Registered::class, LoggedIn::class, Renamed::class, EmailChanged::class], [$this, 'generate']);
$events->listen(Erased::class, [$this, 'handleErased']);
}

public function generate($event): void
{
if ((!$event->user->isGuest() && empty($event->user->user_svg)) || $event instanceof Renamed) {
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,
Expand All @@ -58,4 +66,9 @@ public function handleErased(Erased $event): void
}
}
}

protected function getIdentifier(): string
{
return $this->settings->get('ianm-boring-avatars.identifier');
}
}

0 comments on commit 54e91e5

Please sign in to comment.