File tree Expand file tree Collapse file tree
shared/src/commonMain/kotlin/site/remlit/snowdrop Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -13,11 +13,10 @@ import site.remlit.snowdrop.util.safeApiRequest
1313import site.remlit.snowdrop.util.settings
1414
1515@OptIn(ExperimentalSettingsApi ::class )
16- suspend fun verifyCredentials (token : String? = null ): ApiResponse <User > = safeApiRequest {
16+ suspend fun verifyCredentials (): ApiResponse <User > = safeApiRequest {
1717 val host = getCurrentAccountHost()
1818 val accountId = getCurrentAccountId()
19- val token = token
20- ? : settings.getString(" account_${accountId} _token" , " " )
19+ val token = settings.getString(" account_${accountId} _token" , " " )
2120
2221 val req = httpClient.get(" https://$host /api/v1/accounts/verify_credentials" ) {
2322 header(" Authorization" , " Bearer $token " )
Original file line number Diff line number Diff line change @@ -4,13 +4,14 @@ import kotlinx.serialization.ExperimentalSerializationApi
44import kotlinx.serialization.Serializable
55import kotlinx.serialization.decodeFromHexString
66import site.remlit.snowdrop.util.config.cbor
7+ import kotlin.time.Clock
78import kotlin.time.Instant
89
910@Serializable
1011data class CacheEntry (
1112 val id : String ,
1213 val content : String ,
13- val expiresAt : Instant
14+ val createdAt : Instant = Clock . System .now()
1415) {
1516 @OptIn(ExperimentalSerializationApi ::class )
1617 inline fun <reified T > getContent (): T {
Original file line number Diff line number Diff line change @@ -41,6 +41,30 @@ fun getCacheEntry(id: String): CacheEntry? {
4141 return cbor.decodeFromHexString(raw)
4242}
4343
44+ @OptIn(ExperimentalSerializationApi ::class )
45+ inline fun <reified T > putCacheEntry (
46+ id : String ,
47+ content : T
48+ ) {
49+ val entry = CacheEntry (
50+ id,
51+ cbor.encodeToHexString<T >(content)
52+ )
53+
54+ val manifest = getCacheManifest()
55+ blockingCache.putString(
56+ " manifest" ,
57+ cbor.encodeToHexString(
58+ manifest.copy(ids = manifest.ids.plus(id).distinct())
59+ )
60+ )
61+
62+ blockingCache.putString(
63+ " entry_$id " ,
64+ cbor.encodeToHexString(entry)
65+ )
66+ }
67+
4468fun cleanExpiredCacheEntries () {
4569
4670}
Original file line number Diff line number Diff line change @@ -55,27 +55,25 @@ fun getCurrentAccountObjectFlow(): Flow<User> = object : Flow<User> {
5555 if (getCurrentAccountId() == " " )
5656 return @safe
5757
58- if (settings.getStringOrNull (" account_${getCurrentAccountId()} _user " ) == null )
58+ if (getCacheEntry (" account_${getCurrentAccountId()} " ) == null )
5959 updateCurrentAccountObject()
6060
61- val user = json.decodeFromString<User >(
62- settings.getString(" account_${getCurrentAccountId()} _user" , " " )
61+ collector.emit(
62+ getCacheEntry(" account_${getCurrentAccountId()} " )!!
63+ .getContent<User >()
6364 )
64-
65- collector.emit(user)
6665 }
6766}
6867
69- suspend fun updateCurrentAccountObject (token : String? = null ) {
70- val verifyRes = verifyCredentials(token )
68+ suspend fun updateCurrentAccountObject () {
69+ val verifyRes = verifyCredentials()
7170 if (verifyRes.error) return
7271 if (verifyRes.response !is User ) return
7372
74- blockingSettings.putString (
75- " account_${getCurrentAccountId()} _user " ,
76- json.encodeToString( verifyRes.response)
73+ putCacheEntry (
74+ " account_${getCurrentAccountId()} " ,
75+ verifyRes.response
7776 )
78- blockingSettings.putString(" account_${getCurrentAccountId()} _token" , token!! )
7977}
8078
8179/* * Used for compose post FAB */
Original file line number Diff line number Diff line change @@ -119,7 +119,7 @@ fun LoginView(
119119 blockingSettings.putString(" account_${currentAccountId} _token" , res.response.accessToken)
120120 blockingSettings.putBoolean(" logged_in" , true )
121121
122- updateCurrentAccountObject(res.response.accessToken )
122+ updateCurrentAccountObject()
123123 }
124124
125125 navigateToTimeline()
Original file line number Diff line number Diff line change @@ -102,8 +102,12 @@ fun SettingsDebugStorageView(
102102 HorizontalDivider ()
103103
104104 blockingCache.keys.forEach {
105- if (! it.startsWith(" e_" )) return @forEach
106- renderKeyVal(it, getCacheEntry(it).toString())
105+ if (! it.startsWith(" entry_" )) return @forEach
106+ renderKeyVal(
107+ it,
108+ getCacheEntry(it.replace(" entry_" , " " ))
109+ .toString()
110+ )
107111 }
108112 }
109113 }
You can’t perform that action at this time.
0 commit comments