Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
26 changes: 26 additions & 0 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace App\Http\Controllers;

use App\Jobs\UpdateUserIdenticonStatus;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;

class ProfileController extends Controller
{
Expand All @@ -21,4 +23,28 @@ public function show(Request $request, ?User $user = null)

abort(404);
}

public function refresh(Request $request)
{

$user = $request->user();

if (! $user->hasConnectedGitHubAccount()) {
return back()->with('error', 'You need to connect your GitHub account to refresh your avatar.');
}

// Rate limiting: 1 request per 1 minute per user
$key = 'avatar-refresh:'.$user->id();

if (RateLimiter::tooManyAttempts($key, 1)) {
return back()->with('error', 'Please wait 1 minute(s) before refreshing your avatar again.');
}

// Record this attempt for 1 minutes
RateLimiter::hit($key, 60);

UpdateUserIdenticonStatus::dispatchSync($user);

return back()->with('success', 'Avatar refresh queued! Your profile image will be updated shortly.');
}
}
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function hasConnectedGitHubAccount(): bool
return ! is_null($this->githubId());
}

public function hasIdenticon(): bool
public function hasGithubIdenticon(): bool
{
return (bool) $this->github_has_identicon;
}
Expand Down
32 changes: 25 additions & 7 deletions resources/views/components/avatar.blade.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
@props([
'user',
'unlinked' => false,
'showRefresh' => false,
])

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

@unless ($unlinked)
<a href="{{ route('profile', $user->username()) }}">
@endunless
<div class="relative inline-block">
@unless ($unlinked)
<a href="{{ route('profile', $user->username()) }}">
@endunless

<flux:avatar
circle
Expand All @@ -21,6 +23,22 @@
{{ $attributes->merge(['class' => 'bg-gray-50']) }}
/>

@unless ($unlinked)
</a>
@endunless
@unless ($unlinked)
</a>
@endunless

@if ($showRefresh && $user->hasConnectedGitHubAccount())
<form method="POST" action="{{ route('avatar.refresh') }}" class="absolute bottom-[-1px] right-0">
@csrf
<button
type="submit"
class="flex items-center justify-center w-10 h-10 bg-white border-2 border-gray-300 rounded-full shadow-sm hover:bg-gray-50 hover:border-lio-500 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-lio-500 transition-colors"
title="Refresh avatar from GitHub"
>
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-gray-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
</svg>
</button>
</form>
@endif
</div>
7 changes: 6 additions & 1 deletion resources/views/users/profile.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ class="w-full bg-center bg-gray-800 h-60 container mx-auto"

<div class="container mx-auto">
<div class="flex justify-center lg:justify-start">
<x-avatar :user="$user" class="-mt-24 w-48 h-48 rounded-full border-8 border-white" unlinked />
<x-avatar
:user="$user"
class="-mt-24 w-48 h-48 rounded-full border-8 border-white"
unlinked
:show-refresh="$user->isLoggedInUser()"
/>
</div>

<div class="flex flex-col mt-5 p-4 lg:flex-row lg:gap-x-12">
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
Route::get('user/{username?}', [ProfileController::class, 'show'])->name('profile');
Route::put('users/{username}/block', BlockUserController::class)->name('users.block');
Route::put('users/{username}/unblock', UnblockUserController::class)->name('users.unblock');
Route::post('/avatar/refresh', [ProfileController::class, 'refresh'])->name('avatar.refresh');

// Notifications
Route::view('notifications', 'users.notifications')->name('notifications')->middleware(Authenticate::class);
Expand Down