Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions app/Console/Commands/BackfillIdenticons.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Console\Commands;

use App\Jobs\UpdateUserIdenticonStatus;
use App\Models\User;
use Illuminate\Console\Command;

class BackfillIdenticons extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:backfill-identicons';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Backfills the github_has_identicon column for users';

/**
* Execute the console command.
*/
public function handle()
{
User::whereNotNull('github_id')
->chunk(100, function ($users) {
foreach ($users as $user) {
UpdateUserIdenticonStatus::dispatch($user);
}

sleep(2);
});
}
}
7 changes: 6 additions & 1 deletion app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Controllers\Controller;
use App\Http\Requests\RegisterRequest;
use App\Jobs\RegisterUser;
use App\Jobs\UpdateUserIdenticonStatus;
use App\Models\User;
use App\Providers\AppServiceProvider;
use Illuminate\Auth\Events\Registered;
Expand Down Expand Up @@ -71,6 +72,10 @@ protected function create(RegisterRequest $request): User
{
$this->dispatchSync(RegisterUser::fromRequest($request));

return User::findByEmailAddress($request->emailAddress());
$user = User::findByEmailAddress($request->emailAddress());

$this->dispatch(new UpdateUserIdenticonStatus($user));

return $user;
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function show()
$communityMembers = Cache::remember(
'communityMembers',
now()->addMinutes(5),
fn () => User::notBanned()->inRandomOrder()->take(100)->get()->chunk(20)
fn () => User::notBanned()->withAvatar()->inRandomOrder()->take(100)->get()->chunk(20)
);

$totalUsers = Cache::remember('totalUsers', now()->addDay(), fn () => number_format(User::notBanned()->count()));
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/UnVerifyAuthor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class UnVerifyAuthor implements ShouldQueue
final class UnVerifyAuthor implements ShouldQueue
{
use Queueable;

Expand Down
25 changes: 25 additions & 0 deletions app/Jobs/UpdateUserIdenticonStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\User;
use App\Social\GithubUserApi;

final class UpdateUserIdenticonStatus implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(protected User $user) {}

public function handle(GithubUserApi $github): void
{
$hasIdenticon = $github->hasIdenticon($this->user->githubId());

$this->user->update(['github_has_identicon' => $hasIdenticon]);
}
}
2 changes: 1 addition & 1 deletion app/Jobs/VerifyAuthor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class VerifyAuthor implements ShouldQueue
final class VerifyAuthor implements ShouldQueue
{
use Queueable;

Expand Down
12 changes: 12 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ final class User extends Authenticatable implements MustVerifyEmail
'remember_token',
'bio',
'banned_reason',
'github_has_identicon'
];

/**
Expand All @@ -75,6 +76,7 @@ protected function casts(): array
return [
'allowed_notifications' => 'array',
'author_verified_at' => 'datetime',
'github_has_identicon' => 'boolean',
];
}

Expand Down Expand Up @@ -113,6 +115,11 @@ public function githubUsername(): string
return $this->github_username ?? '';
}

public function hasIdenticon(): bool
{
return (bool) $this->github_has_identicon;
}

public function twitter(): ?string
{
return $this->twitter;
Expand Down Expand Up @@ -411,6 +418,11 @@ public function scopeModerators(Builder $query)
]);
}

public function scopeWithAvatar(Builder $query)
{
return $query->where('github_has_identicon', false);
}

public function scopeNotBanned(Builder $query)
{
return $query->whereNull('banned_at');
Expand Down
20 changes: 19 additions & 1 deletion app/Social/GithubUserApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;

class GithubUserApi
final class GithubUserApi
{
public function find(int|string $id): ?GitHubUser
{
Expand All @@ -14,4 +14,22 @@ public function find(int|string $id): ?GitHubUser

return $response->failed() ? null : new GitHubUser($response->json());
}

public function hasIdenticon(int|string $id): bool
{
$response = Http::retry(3, 300, fn ($exception) => $exception instanceof ConnectionException)
->get("https://avatars.githubusercontent.com/u/{$id}?v=4&s=40");

if ($response->failed()) {
return true;
}

if (! $info = getimagesizefromstring($response->body())) {
return true;
}

[$width, $height] = $info;

return ! ($width === 420 && $height === 420);
}
}
1 change: 1 addition & 0 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function definition(): array
'remember_token' => Str::random(10),
'github_id' => $this->faker->unique()->numberBetween(10000, 99999),
'github_username' => $this->faker->unique()->userName(),
'github_has_identicon' => $this->faker->boolean(),
'twitter' => $this->faker->unique()->userName(),
'bluesky' => $this->faker->unique()->userName(),
'website' => 'https://laravel.io',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('github_has_identicon')->after('github_username')->default(false);
});
}
};
2 changes: 1 addition & 1 deletion database/seeders/UserSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function run(): void
DB::beginTransaction();

User::factory()
->count(100)
->count(300)
->has(Thread::factory()->count(2), 'threadsRelation')
->has(
Article::factory()
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/avatar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
])

<?php
$src = $user->githubId()
$src = $user->githubId() && ! $user->hasIdenticon()
? sprintf('https://avatars.githubusercontent.com/u/%s', $user->githubId())
: asset('https://laravel.io/images/laravelio-icon-gray.svg');
?>
Expand Down