Skip to content

Commit

Permalink
only regenerate when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Jan 18, 2024
1 parent c7f5227 commit 02909de
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Listener/GenerateAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -59,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 02909de

Please sign in to comment.