From f824ffeb59932a0b2154d693117882cbff205a8f Mon Sep 17 00:00:00 2001 From: IanM Date: Thu, 18 Jan 2024 10:24:44 +0000 Subject: [PATCH] feat: generate on enable and setting change --- extend.php | 7 ++++++- src/Extend/Lifecycle.php | 29 +++++++++++++++++++++++++++++ src/Job/AvatarGenerationJob.php | 19 +++++++++++++++++++ src/Listener/GenerateAvatar.php | 2 +- src/Listener/SettingsChanged.php | 31 +++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/Extend/Lifecycle.php create mode 100644 src/Job/AvatarGenerationJob.php create mode 100644 src/Listener/SettingsChanged.php diff --git a/extend.php b/extend.php index 05bb706..a645ebb 100644 --- a/extend.php +++ b/extend.php @@ -16,9 +16,11 @@ use Flarum\Api\Serializer\ForumSerializer; use Flarum\Extend; use Flarum\Frontend\Document; +use Flarum\Settings\Event\Saved; use Flarum\User\User; use IanM\BoringAvatars\Api\Serializer\AddBoringAvatarAttributes; use IanM\BoringAvatars\Api\Serializer\AddForumAttributes; +use IanM\BoringAvatars\Extend\Lifecycle; return [ (new Extend\Frontend('forum')) @@ -37,6 +39,8 @@ (new Extend\Model(User::class)) ->cast('user_svg', 'string'), + (new Lifecycle()), + (new Extend\Routes('api')) ->get('/users/{id}/boring-avatar', 'users.boring-avatar', Api\Controller\ShowBoringAvatarController::class), @@ -61,7 +65,8 @@ ->default('ianm-boring-avatars.theme', Component\Beam::$name), (new Extend\Event()) - ->subscribe(Listener\GenerateAvatar::class), + ->subscribe(Listener\GenerateAvatar::class) + ->listen(Saved::class, Listener\SettingsChanged::class), (new Extend\Console()) ->command(Console\GenerateBoringAvatars::class), diff --git a/src/Extend/Lifecycle.php b/src/Extend/Lifecycle.php new file mode 100644 index 0000000..690a50f --- /dev/null +++ b/src/Extend/Lifecycle.php @@ -0,0 +1,29 @@ +make(Queue::class) + ->push(new AvatarGenerationJob()); + } + + public function onDisable(Container $container, Extension $extension) + { + // Do nothing + } + + public function extend(Container $container, Extension $extension = null) + { + // Do nothing + } +} diff --git a/src/Job/AvatarGenerationJob.php b/src/Job/AvatarGenerationJob.php new file mode 100644 index 0000000..ff0edc7 --- /dev/null +++ b/src/Job/AvatarGenerationJob.php @@ -0,0 +1,19 @@ +setLaravel($container); + + $command->run(new ArrayInput(['--force' => true]), new NullOutput()); + } +} diff --git a/src/Listener/GenerateAvatar.php b/src/Listener/GenerateAvatar.php index d57b626..e8feb77 100644 --- a/src/Listener/GenerateAvatar.php +++ b/src/Listener/GenerateAvatar.php @@ -35,7 +35,7 @@ public function subscribe(EventsDispatcher $events): void public function generate($event): void { - if (($event->user && empty($event->user->user_svg)) || $event instanceof Renamed) { + if ((!$event->user->isGuest() && empty($event->user->user_svg)) || $event instanceof Renamed) { $event->user = $this->bus->dispatch(new GenerateAvatarCommand( $event->user, BoringAvatar::$defaultGenerationSize, diff --git a/src/Listener/SettingsChanged.php b/src/Listener/SettingsChanged.php new file mode 100644 index 0000000..069dba7 --- /dev/null +++ b/src/Listener/SettingsChanged.php @@ -0,0 +1,31 @@ +settings; + + // Check if any keys in the settings array start with 'ianm-boring-avatars.' + $prefix = 'ianm-boring-avatars.'; + $keysWithPrefix = array_filter($settings, function (string $key) use ($prefix) { + return Str::startsWith($key, $prefix); + }, ARRAY_FILTER_USE_KEY); + + if (!empty($keysWithPrefix)) { + $this->queue->push(new AvatarGenerationJob()); + } + } +}