Skip to content

Commit

Permalink
feat: generate on enable and setting change
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Jan 18, 2024
1 parent c8d0cef commit f824ffe
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
7 changes: 6 additions & 1 deletion extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand All @@ -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),

Expand All @@ -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),
Expand Down
29 changes: 29 additions & 0 deletions src/Extend/Lifecycle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace IanM\BoringAvatars\Extend;

use Flarum\Extend\ExtenderInterface;
use Flarum\Extend\LifecycleInterface;
use Flarum\Extension\Extension;
use IanM\BoringAvatars\Job\AvatarGenerationJob;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Queue\Queue;

class Lifecycle implements ExtenderInterface, LifecycleInterface
{
public function onEnable(Container $container, Extension $extension): void
{
$container->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
}
}
19 changes: 19 additions & 0 deletions src/Job/AvatarGenerationJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace IanM\BoringAvatars\Job;

use Flarum\Queue\AbstractJob;
use IanM\BoringAvatars\Console\GenerateBoringAvatars;
use Illuminate\Contracts\Container\Container;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

class AvatarGenerationJob extends AbstractJob
{
public function handle(GenerateBoringAvatars $command, Container $container): void
{
$command->setLaravel($container);

$command->run(new ArrayInput(['--force' => true]), new NullOutput());
}
}
2 changes: 1 addition & 1 deletion src/Listener/GenerateAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions src/Listener/SettingsChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace IanM\BoringAvatars\Listener;

use Flarum\Settings\Event\Saved;
use IanM\BoringAvatars\Job\AvatarGenerationJob;
use Illuminate\Contracts\Queue\Queue;
use Illuminate\Support\Str;

class SettingsChanged
{
public function __construct(
protected Queue $queue
) {
}

public function __invoke(Saved $event)
{
$settings = $event->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());
}
}
}

0 comments on commit f824ffe

Please sign in to comment.