From: Daniel Karbach Date: Mon, 20 Feb 2023 13:42:32 +0000 (+0100) Subject: periodically sync avatars X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=35dca1d496a5298daad2fc5f926a9fe400f68dce;p=alttp.git periodically sync avatars --- diff --git a/app/Console/Commands/SyncAvatars.php b/app/Console/Commands/SyncAvatars.php new file mode 100644 index 0000000..52ee336 --- /dev/null +++ b/app/Console/Commands/SyncAvatars.php @@ -0,0 +1,51 @@ +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; + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index a0aa9ec..e04fda3 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -15,6 +15,7 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { + $schedule->command('sync:avatars')->everyFiveMinutes(); $schedule->command('sync:speedgaming')->everyFifteenMinutes(); } diff --git a/app/DiscordBotCommands/SyncUserCommand.php b/app/DiscordBotCommands/SyncUserCommand.php index b9ba6d3..520565c 100644 --- a/app/DiscordBotCommands/SyncUserCommand.php +++ b/app/DiscordBotCommands/SyncUserCommand.php @@ -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(); }); diff --git a/app/Models/User.php b/app/Models/User.php index 879624a..fb21113 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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', diff --git a/resources/js/helpers/User.js b/resources/js/helpers/User.js index b94b777..9a55444 100644 --- a/resources/js/helpers/User.js +++ b/resources/js/helpers/User.js @@ -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)) || '';