--- /dev/null
+<?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;
+ }
+}
*/
protected function schedule(Schedule $schedule)
{
+ $schedule->command('sync:avatars')->everyFiveMinutes();
$schedule->command('sync:speedgaming')->everyFifteenMinutes();
}
}
$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();
});
'discriminator' => 'string',
'email' => 'string',
'avatar' => 'string',
- 'avatar_cached' => 'boolean',
+ 'avatar_cached' => 'datetime',
'verified' => 'boolean',
'locale' => 'string',
'mfa_enabled' => 'boolean',
+import moment from 'moment';
+
export const compareFinished = round => (a, b) => {
const a_result = findResult(a, round);
const b_result = findResult(b, 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)) || '';