Skip to content

Commit

Permalink
Merge branch 'achievement-distribution-indexes' of https://github.com…
Browse files Browse the repository at this point in the history
…/wescopeland/RAWeb into achievement-distribution-indexes
  • Loading branch information
wescopeland committed Oct 30, 2023
2 parents 921d9bb + fc8d432 commit 9a6bba2
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ DB_USERNAME=retroachievements
DB_PASSWORD="${DB_USERNAME}"
# TODO remove after utf8mb4 conversion
#DB_CHARSET=latin1
#DB_COLLATION=latin1_general_ci
#DB_COLLATION=latin1_swedish_ci

#LEGACY_MEDIA_PATH=

Expand Down
1 change: 0 additions & 1 deletion app/Api/Middleware/LogApiUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 7 additions & 25 deletions app/Helpers/database/ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,23 @@
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;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

function isAllowedToSubmitTickets(string $user): bool
function isAllowedToSubmitTickets(string $username): bool
{
if (!isValidUsername($user)) {
$user = User::firstWhere('User', $username);
if (!$user || $user->Created->diffInDays() < 1) {
return false;
}

$cacheKey = CacheKey::buildUserCanTicketCacheKey($user);

$value = Cache::get($cacheKey);
if ($value !== null) {
return $value;
}

$value = getUserActivityRange($user, $firstLogin, $lastLogin)
&& time() - strtotime($firstLogin) > 86400 // 86400 seconds = 1 day
&& getRecentlyPlayedGames($user, 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;
return PlayerGame::where('user_id', $user->id)
->where('time_taken', '>', 5)
->exists();
}

function submitNewTicketsJSON(
Expand Down
1 change: 0 additions & 1 deletion app/Helpers/database/user-activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions app/Helpers/database/user-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions app/Support/Cache/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
</div>
@endif

<div class="lg:rounded bg-embed border border-embed-highlight p-5 relative">
<div class="lg:rounded bg-embed border border-embed-highlight px-5 pt-3.5 pb-5 relative">
<div class="mb-2">
<p class="text-lg">Your Progress</p>
<p class="sr-only">Your Progress</p>

@if (!$hasUnlockedAnyAchievements)
<p class="leading-4 mt-2">You haven't unlocked any achievements for this game.</p>
Expand Down
9 changes: 0 additions & 9 deletions tests/Unit/CacheKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 9a6bba2

Please sign in to comment.