Skip to content

Commit 2f11c3f

Browse files
committed
Switch currentAccount to use cache over settings, fix useless passing of token
1 parent 99d71d4 commit 2f11c3f

6 files changed

Lines changed: 44 additions & 18 deletions

File tree

shared/src/commonMain/kotlin/site/remlit/snowdrop/api/verifyCredentials.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ import site.remlit.snowdrop.util.safeApiRequest
1313
import 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")

shared/src/commonMain/kotlin/site/remlit/snowdrop/model/cache/CacheEntry.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import kotlinx.serialization.ExperimentalSerializationApi
44
import kotlinx.serialization.Serializable
55
import kotlinx.serialization.decodeFromHexString
66
import site.remlit.snowdrop.util.config.cbor
7+
import kotlin.time.Clock
78
import kotlin.time.Instant
89

910
@Serializable
1011
data 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 {

shared/src/commonMain/kotlin/site/remlit/snowdrop/util/Cache.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
4468
fun cleanExpiredCacheEntries() {
4569

4670
}

shared/src/commonMain/kotlin/site/remlit/snowdrop/util/Settings.kt

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff 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 */

shared/src/commonMain/kotlin/site/remlit/snowdrop/view/LoginView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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()

shared/src/commonMain/kotlin/site/remlit/snowdrop/view/settings/SettingsDebugStorageView.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)