Skip to content

Commit a9eb114

Browse files
feat:remove avatar from model and create migration , add profile test with action , add user observalble
1 parent 390567a commit a9eb114

File tree

13 files changed

+228
-321
lines changed

13 files changed

+228
-321
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Console\Commands;
6+
7+
use App\Models\User;
8+
use App\Traits\FormatSocialAccount;
9+
use Illuminate\Console\Command;
10+
11+
final class UpdateUserSocialAccount extends Command
12+
{
13+
use FormatSocialAccount;
14+
15+
protected $signature = 'lcm:update-user-social-account';
16+
17+
protected $description = 'Update user social account to normal format';
18+
19+
public function handle(): void
20+
{
21+
$this->info('Start updating users social account...');
22+
23+
foreach (User::verifiedUsers()->get() as $user) {
24+
$this->info('Updating '.$user->username.'...');
25+
$user->twitter_profile = $this->formatTwitterHandle($user->twitter_profile);
26+
$user->github_profile = $this->formatGithubHandle($user->github_profile);
27+
$user->linkedin_profile = $this->formatLinkedinHandle($user->linkedin_profile);
28+
$user->save();
29+
}
30+
31+
$this->info('All done!');
32+
}
33+
}

app/Http/Controllers/User/SettingController.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

app/Livewire/Components/User/Profile.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Actions\User\UpdateUserProfileAction;
88
use App\Models\User;
9+
use App\Traits\FormatSocialAccount;
910
use Filament\Forms;
1011
use Filament\Forms\Concerns\InteractsWithForms;
1112
use Filament\Forms\Contracts\HasForms;
@@ -25,6 +26,7 @@
2526
*/
2627
final class Profile extends Component implements HasForms
2728
{
29+
use FormatSocialAccount;
2830
use InteractsWithForms;
2931

3032
public ?array $data = [];
@@ -108,6 +110,9 @@ public function form(Form $form): Form
108110
->label(__('GitHub'))
109111
->placeholder('laravelcm')
110112
->unique(ignoreRecord: true)
113+
->maxLength(255)
114+
->live(true)
115+
->afterStateUpdated(fn (Forms\Set $set, ?string $state) => $set('github_profile', $this->formatGithubHandle($state)))
111116
->prefix(
112117
fn (): HtmlString => new HtmlString(Blade::render(<<<'Blade'
113118
<x-icon.github
@@ -120,6 +125,9 @@ class="size-5 text-gray-400 dark:text-gray-500"
120125
->label(__('Twitter'))
121126
->helperText(__('pages/account.settings.twitter_helper_text'))
122127
->unique(ignoreRecord: true)
128+
->maxLength(255)
129+
->live(true)
130+
->afterStateUpdated(fn (Forms\Set $set, ?string $state) => $set('twitter_profile', $this->formatTwitterHandle($state)))
123131
->prefix(
124132
fn (): HtmlString => new HtmlString(Blade::render(<<<'Blade'
125133
<x-icon.twitter
@@ -132,6 +140,9 @@ class="size-5 text-gray-400 dark:text-gray-500"
132140
->label(__('LinkedIn'))
133141
->placeholder('laravelcm')
134142
->unique(ignoreRecord: true)
143+
->maxLength(255)
144+
->live(true)
145+
->afterStateUpdated(fn (Forms\Set $set, ?string $state) => $set('linkedin_profile', $this->formatLinkedinHandle($state)))
135146
->prefix(
136147
fn (): HtmlString => new HtmlString(Blade::render(<<<'Blade'
137148
<x-icon.linkedin

app/Models/User.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ final class User extends Authenticatable implements FilamentUser, HasAvatar, Has
8080
'password',
8181
'bio',
8282
'location',
83-
'avatar',
8483
'avatar_type',
8584
'reputation',
8685
'phone_number',

app/Observers/UserObserver.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Observers;
6+
7+
use App\Models\User;
8+
9+
final class UserObserver
10+
{
11+
/**
12+
* Handle the User "updated" event.
13+
*/
14+
public function updated(User $user): void
15+
{
16+
$media = $user->getMedia('avatar')->first();
17+
18+
if ($media) {
19+
$user->avatar_type = 'storage';
20+
}
21+
22+
if (! $media && $user->providers->isNotEmpty()) {
23+
$user->avatar_type = $user->providers->first()->provider;
24+
}
25+
26+
if (! $media && $user->providers->isEmpty()) {
27+
$user->avatar_type = 'avatar';
28+
}
29+
30+
$user->saveQuietly();
31+
}
32+
}

app/Providers/AppServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Models\Reply;
1111
use App\Models\Thread;
1212
use App\Models\User;
13+
use App\Observers\UserObserver;
1314
use App\View\Composers\InactiveDiscussionsComposer;
1415
use App\View\Composers\ProfileUsersComposer;
1516
use App\View\Composers\TopContributorsComposer;
@@ -39,6 +40,7 @@ public function boot(): void
3940
$this->bootFilament();
4041

4142
ReplyResource::withoutWrapping();
43+
User::observe(UserObserver::class);
4244
}
4345

4446
public function registerBladeDirective(): void

app/Traits/FormatSocialAccount.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Traits;
6+
7+
trait FormatSocialAccount
8+
{
9+
public function formatTwitterHandle(?string $userSocial): ?string
10+
{
11+
12+
if (! $userSocial) {
13+
return null;
14+
}
15+
16+
$handle = trim($userSocial);
17+
18+
if (str_contains($handle, 'https://x.com/')) {
19+
return substr($handle, strlen('https://x.com/'));
20+
}
21+
22+
if (str_contains($handle, 'https://twitter.com/')) {
23+
return substr($handle, strlen('https://twitter.com/'));
24+
}
25+
if (str_contains($handle, '@')) {
26+
return substr($handle, strlen('@'));
27+
}
28+
29+
return $handle;
30+
}
31+
32+
public function formatGithubHandle(?string $userSocial): ?string
33+
{
34+
35+
if (! $userSocial) {
36+
return null;
37+
}
38+
39+
$handle = trim($userSocial);
40+
$domain = 'https://github.com/';
41+
42+
if (str_contains($handle, $domain)) {
43+
return substr($handle, strlen($domain));
44+
}
45+
46+
return $handle;
47+
}
48+
49+
public function formatLinkedinHandle(?string $userSocial): ?string
50+
{
51+
if (! $userSocial) {
52+
return null;
53+
}
54+
55+
$handle = trim($userSocial);
56+
$domain = 'https://www.linkedin.com/in/';
57+
58+
if (str_contains($handle, $domain)) {
59+
return substr($handle, strlen($domain));
60+
}
61+
62+
return $handle;
63+
}
64+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
return new class extends Migration
10+
{
11+
public function up(): void
12+
{
13+
Schema::table('users', static function (Blueprint $table): void {
14+
$table->dropColumn('avatar');
15+
});
16+
}
17+
18+
public function down(): void
19+
{
20+
Schema::table('users', function (Blueprint $table): void {
21+
$table->string('avatar')->nullable();
22+
});
23+
}
24+
};

resources/views/components/layouts/footer.blade.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ class="ml-2 size-6 rounded-full"
6363
<x-icon.telegram class="size-5 text-[#34AADF]" aria-hidden="true" />
6464
Telegram
6565
</x-link>
66-
<x-link href="https://chat.whatsapp.com/G8e98Ms0MgSLEOGd3Uai1i" class="inline-flex items-center gap-2 text-gray-300 hover:text-white">
67-
<x-icon.whatsapp class="size-5 text-[#28D146]" aria-hidden="true" />
68-
WhatsApp
69-
</x-link>
7066
</div>
7167
<livewire:components.change-locale />
7268
</div>

0 commit comments

Comments
 (0)