]> git.localhorst.tv Git - alttp.git/commitdiff
periodically sync avatars
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 20 Feb 2023 13:42:32 +0000 (14:42 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 20 Feb 2023 13:42:32 +0000 (14:42 +0100)
app/Console/Commands/SyncAvatars.php [new file with mode: 0644]
app/Console/Kernel.php
app/DiscordBotCommands/SyncUserCommand.php
app/Models/User.php
resources/js/helpers/User.js

diff --git a/app/Console/Commands/SyncAvatars.php b/app/Console/Commands/SyncAvatars.php
new file mode 100644 (file)
index 0000000..52ee336
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\DiscordBotCommand;
+use App\Models\User;
+use Illuminate\Console\Command;
+use Illuminate\Database\Eloquent\Builder;
+
+class SyncAvatars extends Command
+{
+       /**
+        * The name and signature of the console command.
+        *
+        * @var string
+        */
+       protected $signature = 'sync:avatars';
+
+       /**
+        * The console command description.
+        *
+        * @var string
+        */
+       protected $description = 'Resync outdated avatars';
+
+       /**
+        * Execute the console command.
+        *
+        * @return int
+        */
+       public function handle()
+       {
+               $users = User::whereNotNull('avatar')
+                       ->where(function (Builder $query) {
+                               $query->whereNull('avatar_cached');
+                               $query->orWhereColumn('avatar_cached', '<', 'updated_at');
+                               $query->orWhere('avatar_cached', '<', now()->subtract(30, 'days'));
+                       })
+                       ->get();
+
+               foreach ($users as $user) {
+                       try {
+                               DiscordBotCommand::syncUser($user->id);
+                       } catch (\Exception $e) {
+                               $this->error('error syncing avatar of user '.$user->id.': '.$e->getMessage());
+                       }
+               }
+
+               return 0;
+       }
+}
index a0aa9ecf4d663445af2f69e01d39289a0122259a..e04fda3d46696cb4c286197e797cb31b9706a64d 100644 (file)
@@ -15,6 +15,7 @@ class Kernel extends ConsoleKernel
      */
     protected function schedule(Schedule $schedule)
     {
+               $schedule->command('sync:avatars')->everyFiveMinutes();
                $schedule->command('sync:speedgaming')->everyFifteenMinutes();
     }
 
index b9ba6d362f7efbaefa27448ec16c2da92d47abb3..520565ce9ffa810448cc6bb916789febbdb40ec7 100644 (file)
@@ -30,16 +30,18 @@ class SyncUserCommand extends BaseCommand {
                                }
                                $user->avatar = $discordUser->avatarhash ?: null;
 
-                               if ($user->avatar) {
+                               $media = Storage::disk('media');
+                               if ($user->avatar && $media->missing('avatar/'.$user->id.'/'.$user->avatar.'.png')) {
                                        try {
                                                $url = $discordUser->getAvatarAttribute('png');
                                                $content = Http::get($url)->body();
-                                               $media = Storage::disk('media');
                                                $media->makeDirectory('avatar/'.$user->id);
-                                               $media->put('avatar/'.$user->id.'/'.$discordUser->avatarhash.'.png', $content);
+                                               $media->put('avatar/'.$user->id.'/'.$user->avatar.'.png', $content);
                                                $user->avatar_cached = now();
                                        } catch (\Exception $e) {
                                        }
+                               } else {
+                                       $user->avatar_cached = now();
                                }
                                $user->save();
                        });
index 879624a2072f58186e66ad107a604fd23e9f0d9f..fb21113b39497f7f9a68f4fd81d837d755386cb3 100644 (file)
@@ -215,7 +215,7 @@ class User extends Authenticatable
                'discriminator' => 'string',
                'email' => 'string',
                'avatar' => 'string',
-               'avatar_cached' => 'boolean',
+               'avatar_cached' => 'datetime',
                'verified' => 'boolean',
                'locale' => 'string',
                'mfa_enabled' => 'boolean',
index b94b777091601790abdaf2440027206b8f5c7ea4..9a554449cdfb0dd437c10c13ab6e344273c3d70e 100644 (file)
@@ -1,3 +1,5 @@
+import moment from 'moment';
+
 export const compareFinished = round => (a, b) => {
        const a_result = findResult(a, round);
        const b_result = findResult(b, round);
@@ -46,9 +48,15 @@ export const findResult = (user, round) => {
        return round.results.find(result => result.user_id == user.id);
 };
 
-export const getAvatarUrl = user => user && user.avatar
-       ? `//cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png`
-       : '/default-avatar.png';
+export const getAvatarUrl = user => {
+       if (user && user.avatar) {
+               if (user.avatar_cached) {
+                       return `/media/avatar/${user.id}/${user.avatar}.png`;
+               }
+               return `//cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png`;
+       }
+       return '/default-avatar.png';
+};
 
 export const getUserName = user => (user && (user.nickname || user.username)) || '';