From: Lanius Trolling Date: Thu, 2 Mar 2023 16:04:10 +0000 (-0500) Subject: Don't keep bothering database/NSAPI in case of anonymous user X-Git-Url: https://gitweb.starshipfights.net/?a=commitdiff_plain;h=11cb89a7cf3373ad9e6a0f280d36976e3bd15d81;p=factbooks Don't keep bothering database/NSAPI in case of anonymous user --- diff --git a/src/main/kotlin/info/mechyrdia/data/nations.kt b/src/main/kotlin/info/mechyrdia/data/nations.kt index c808d9e..417836a 100644 --- a/src/main/kotlin/info/mechyrdia/data/nations.kt +++ b/src/main/kotlin/info/mechyrdia/data/nations.kt @@ -58,12 +58,28 @@ suspend fun MutableMap, NationData>.getNation(id: Id) } } -val CallCurrentNationAttribute = AttributeKey("CurrentNation") +private val callCurrentNationAttribute = AttributeKey("CurrentNation") suspend fun ApplicationCall.currentNation(): NationData? { - attributes.getOrNull(CallCurrentNationAttribute)?.let { return it } + attributes.getOrNull(callCurrentNationAttribute)?.let { sess -> + return when (sess) { + NationSession.Anonymous -> null + is NationSession.LoggedIn -> sess.nation + } + } + + val nationId = sessions.get()?.nationId + return if (nationId == null) { + attributes.put(callCurrentNationAttribute, NationSession.Anonymous) + null + } else nationCache.getNation(nationId).also { data -> + attributes.put(callCurrentNationAttribute, NationSession.LoggedIn(data)) + } +} + +private sealed interface NationSession { + object Anonymous : NationSession - return sessions.get()?.nationId?.let { id -> - nationCache.getNation(id) - }?.also { attributes.put(CallCurrentNationAttribute, it) } + @JvmInline + value class LoggedIn(val nation: NationData) : NationSession }