From 19c853425c4a664c2ccd6525cdd41fc10926cc27 Mon Sep 17 00:00:00 2001 From: luchaos Date: Sun, 29 Oct 2023 15:59:28 +0100 Subject: [PATCH 1/6] fix: user created timestamp not cast to Carbon --- app/Api/Middleware/LogApiUsage.php | 1 - app/Helpers/database/ticket.php | 3 +-- app/Helpers/database/user-activity.php | 1 - app/Helpers/database/user-auth.php | 2 -- 4 files changed, 1 insertion(+), 6 deletions(-) diff --git a/app/Api/Middleware/LogApiUsage.php b/app/Api/Middleware/LogApiUsage.php index b233254c61..dae7d1033e 100644 --- a/app/Api/Middleware/LogApiUsage.php +++ b/app/Api/Middleware/LogApiUsage.php @@ -15,7 +15,6 @@ public function handle(Request $request, Closure $next): mixed /** @var User $user */ $user = $request->user('api-token'); - $user->timestamps = false; $user->increment('APIUses'); return $next($request); diff --git a/app/Helpers/database/ticket.php b/app/Helpers/database/ticket.php index 6bf4d621dc..70cec8365d 100644 --- a/app/Helpers/database/ticket.php +++ b/app/Helpers/database/ticket.php @@ -28,8 +28,7 @@ function isAllowedToSubmitTickets(string $user): bool return $value; } - $value = getUserActivityRange($user, $firstLogin, $lastLogin) - && time() - strtotime($firstLogin) > 86400 // 86400 seconds = 1 day + $value = $userModel->Created->diffInDays() > 1 && getRecentlyPlayedGames($user, 0, 1, $userInfo) && $userInfo[0]['GameID']; diff --git a/app/Helpers/database/user-activity.php b/app/Helpers/database/user-activity.php index 12ea4615d4..cff3e61a80 100644 --- a/app/Helpers/database/user-activity.php +++ b/app/Helpers/database/user-activity.php @@ -90,7 +90,6 @@ function postActivity(string|User $userIn, int $type, ?int $data = null, ?int $d // update UserAccount $user->LastLogin = Carbon::now(); $user->LastActivityID = $activity->ID; - $user->timestamps = false; $user->save(); return true; diff --git a/app/Helpers/database/user-auth.php b/app/Helpers/database/user-auth.php index 15b24852f2..d157fcf7cf 100644 --- a/app/Helpers/database/user-auth.php +++ b/app/Helpers/database/user-auth.php @@ -78,7 +78,6 @@ function authenticateForConnect(?string $username, ?string $pass = null, ?string // update appTokenExpiry $user->appTokenExpiry = Carbon::now()->clone()->addDays(14); - $user->timestamps = false; $user->save(); postActivity($user, ActivityType::Login); @@ -208,7 +207,6 @@ function authenticateFromCookie( // valid active account. update the last activity timestamp $user->LastLogin = Carbon::now(); - $user->timestamps = false; $user->save(); // validate permissions for the current page if required From 749add42936cdaa48437e33d85f2d4963d992825 Mon Sep 17 00:00:00 2001 From: luchaos Date: Sun, 29 Oct 2023 16:02:12 +0100 Subject: [PATCH 2/6] add missing request user --- app/Helpers/database/ticket.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Helpers/database/ticket.php b/app/Helpers/database/ticket.php index 70cec8365d..9ed578c397 100644 --- a/app/Helpers/database/ticket.php +++ b/app/Helpers/database/ticket.php @@ -28,6 +28,7 @@ function isAllowedToSubmitTickets(string $user): bool return $value; } + $userModel = request()->user(); $value = $userModel->Created->diffInDays() > 1 && getRecentlyPlayedGames($user, 0, 1, $userInfo) && $userInfo[0]['GameID']; From d210284fdb2c27ed65de02ed1bf3d28c29df49dc Mon Sep 17 00:00:00 2001 From: luchaos Date: Sun, 29 Oct 2023 16:05:10 +0100 Subject: [PATCH 3/6] cleanup --- app/Helpers/database/ticket.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Helpers/database/ticket.php b/app/Helpers/database/ticket.php index 9ed578c397..fdddcf4448 100644 --- a/app/Helpers/database/ticket.php +++ b/app/Helpers/database/ticket.php @@ -15,22 +15,22 @@ use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; -function isAllowedToSubmitTickets(string $user): bool +function isAllowedToSubmitTickets(string $username): bool { - if (!isValidUsername($user)) { + if (!isValidUsername($username)) { return false; } - $cacheKey = CacheKey::buildUserCanTicketCacheKey($user); + $cacheKey = CacheKey::buildUserCanTicketCacheKey($username); $value = Cache::get($cacheKey); if ($value !== null) { return $value; } - $userModel = request()->user(); - $value = $userModel->Created->diffInDays() > 1 - && getRecentlyPlayedGames($user, 0, 1, $userInfo) + $user = User::firstWhere('User', $username); + $value = $user->Created->diffInDays() > 1 + && getRecentlyPlayedGames($username, 0, 1, $userInfo) && $userInfo[0]['GameID']; if ($value) { From b2f1bc27942df59e39ff040177c68efc4b66f685 Mon Sep 17 00:00:00 2001 From: luchaos Date: Sun, 29 Oct 2023 16:14:59 +0100 Subject: [PATCH 4/6] cleanup --- app/Helpers/database/ticket.php | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/app/Helpers/database/ticket.php b/app/Helpers/database/ticket.php index fdddcf4448..e70cbb4b16 100644 --- a/app/Helpers/database/ticket.php +++ b/app/Helpers/database/ticket.php @@ -23,27 +23,28 @@ function isAllowedToSubmitTickets(string $username): bool $cacheKey = CacheKey::buildUserCanTicketCacheKey($username); - $value = Cache::get($cacheKey); - if ($value !== null) { - return $value; + if (Cache::has($cacheKey)) { + return Cache::get($cacheKey); } $user = User::firstWhere('User', $username); - $value = $user->Created->diffInDays() > 1 + $isAllowed = $user->Created->diffInDays() > 1 && getRecentlyPlayedGames($username, 0, 1, $userInfo) && $userInfo[0]['GameID']; - if ($value) { - // if the user can create tickets, they should be able to create tickets forever - // more. expire once every 30 days so we can purge inactive users - Cache::put($cacheKey, $value, Carbon::now()->addDays(30)); - } else { - // user can't create tickets, which means the account is less than a day old - // or has no games played. only cache the value for an hour - Cache::put($cacheKey, $value, Carbon::now()->addHour()); - } - - return $value; + Cache::put( + $cacheKey, + $isAllowed, + $isAllowed + // if the user can create tickets, they should be able to create tickets forever + // more. expire once every 30 days so we can purge inactive users + ? Carbon::now()->addDays(30) + // user can't create tickets, which means the account is less than a day old + // or has no games played. only cache the value for an hour + : Carbon::now()->addHour() + ); + + return $isAllowed; } function submitNewTicketsJSON( From 8bbe110fd76f6fabf42f8596df0c39d3a09e1186 Mon Sep 17 00:00:00 2001 From: luchaos Date: Sun, 29 Oct 2023 16:51:15 +0100 Subject: [PATCH 5/6] feedback --- app/Helpers/database/ticket.php | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/app/Helpers/database/ticket.php b/app/Helpers/database/ticket.php index e70cbb4b16..31bc2c5a27 100644 --- a/app/Helpers/database/ticket.php +++ b/app/Helpers/database/ticket.php @@ -9,6 +9,7 @@ use App\Community\ViewModels\Ticket as TicketViewModel; use App\Platform\Enums\AchievementFlag; use App\Platform\Enums\UnlockMode; +use App\Platform\Models\PlayerGame; use App\Site\Models\User; use App\Support\Cache\CacheKey; use Carbon\Carbon; @@ -17,34 +18,14 @@ function isAllowedToSubmitTickets(string $username): bool { - if (!isValidUsername($username)) { + $user = User::firstWhere('User', $username); + if (!$user || $user->Created->diffInDays() < 1) { return false; } - $cacheKey = CacheKey::buildUserCanTicketCacheKey($username); - - if (Cache::has($cacheKey)) { - return Cache::get($cacheKey); - } - - $user = User::firstWhere('User', $username); - $isAllowed = $user->Created->diffInDays() > 1 - && getRecentlyPlayedGames($username, 0, 1, $userInfo) - && $userInfo[0]['GameID']; - - Cache::put( - $cacheKey, - $isAllowed, - $isAllowed - // if the user can create tickets, they should be able to create tickets forever - // more. expire once every 30 days so we can purge inactive users - ? Carbon::now()->addDays(30) - // user can't create tickets, which means the account is less than a day old - // or has no games played. only cache the value for an hour - : Carbon::now()->addHour() - ); - - return $isAllowed; + return PlayerGame::where('user_id', $user->id) + ->where('time_taken', '>', 5) + ->exists(); } function submitNewTicketsJSON( From ea238e5d0169684a295cd71f79b748907b76a9c9 Mon Sep 17 00:00:00 2001 From: luchaos Date: Sun, 29 Oct 2023 17:54:56 +0100 Subject: [PATCH 6/6] feedback --- app/Support/Cache/CacheKey.php | 5 ----- tests/Unit/CacheKeyTest.php | 9 --------- 2 files changed, 14 deletions(-) diff --git a/app/Support/Cache/CacheKey.php b/app/Support/Cache/CacheKey.php index d569a59846..b293b57cd8 100644 --- a/app/Support/Cache/CacheKey.php +++ b/app/Support/Cache/CacheKey.php @@ -16,11 +16,6 @@ public static function buildUserLastLoginCacheKey(string $username): string return self::buildNormalizedUserCacheKey($username, "last-login"); } - public static function buildUserCanTicketCacheKey(string $username): string - { - return self::buildNormalizedUserCacheKey($username, "can-ticket"); - } - public static function buildUserCardDataCacheKey(string $username): string { return self::buildNormalizedUserCacheKey($username, "card-data"); diff --git a/tests/Unit/CacheKeyTest.php b/tests/Unit/CacheKeyTest.php index 26c9620c28..49d0dd24e5 100644 --- a/tests/Unit/CacheKeyTest.php +++ b/tests/Unit/CacheKeyTest.php @@ -10,15 +10,6 @@ final class CacheKeyTest extends TestCase { - public function testBuildUserCanTicketCacheKey(): void - { - $username = "UserName"; - - $cacheKey = CacheKey::buildUserCanTicketCacheKey($username); - - $this->assertEquals("user:username:can-ticket", $cacheKey); - } - public function testBuildUserCardDataCacheKey(): void { $username = "UserName";