Skip to content

Commit

Permalink
fix: batch avatar generation for larger user numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Jan 18, 2024
1 parent e01a7f7 commit ebd0ee5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A [Flarum](http://flarum.org) extension. Replace default Flarum avatars with Bor

## About

Utilize generated "Boring Avatars" on your Flarum forum, without compromising your user privacy.
Utilize generated "Boring Avatars" on your Flarum forum, without compromising your user privacy. It is a PHP port of the [React based Boring Avatars](https://github.com/boringdesigners/boring-avatars)

## Features

Expand Down
31 changes: 31 additions & 0 deletions src/Job/AvatarGenerationBatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace IanM\BoringAvatars\Job;

use Flarum\Database\Eloquent\Collection;
use Flarum\Queue\AbstractJob;
use IanM\BoringAvatars\BoringAvatar;
use IanM\BoringAvatars\Command\GenerateAvatar;
use Illuminate\Contracts\Bus\Dispatcher;

class AvatarGenerationBatch extends AbstractJob
{
public function __construct(
protected Collection $users,
protected bool $force = false
) {
}

public function handle(Dispatcher $bus): void
{
$this->users->each(function ($user) use ($bus) {
if ($this->force || $user->user_svg === null) {
if ($user->user_svg !== null) {
$user->user_svg = null;
$user->save();
}
$bus->dispatch(new GenerateAvatar($user, BoringAvatar::$defaultGenerationSize, BoringAvatar::$defaultSquareAvatar));
}
});
}
}
17 changes: 10 additions & 7 deletions src/Job/AvatarGenerationJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@

namespace IanM\BoringAvatars\Job;

use Flarum\Database\Eloquent\Collection;
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;
use Flarum\User\User;
use Illuminate\Contracts\Queue\Queue;

class AvatarGenerationJob extends AbstractJob
{
const BATCH_SIZE = 1000;

public function __construct(
protected bool $force = false
) {
}

public function handle(GenerateBoringAvatars $command, Container $container): void
public function handle(Queue $queue): void
{
$command->setLaravel($container);
$force = $this->force;

$command->run(new ArrayInput($this->force ? ['--force' => true] : []), new NullOutput());
User::query()->chunkById(self::BATCH_SIZE, function (Collection $users) use ($queue, $force) {
$queue->push(new AvatarGenerationBatch($users, $force));
});
}
}

0 comments on commit ebd0ee5

Please sign in to comment.