This commit is contained in:
Gabriel 2026-06-16 21:03:23 +01:00 committed by GitHub
commit aca157d6f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 4 deletions

View File

@ -1,5 +1,6 @@
package chat.stoat.api.routes.user
import chat.stoat.api.StoatAPI
import chat.stoat.api.StoatAPIError
import chat.stoat.api.StoatHttp
import chat.stoat.api.StoatJson
@ -13,6 +14,27 @@ import io.ktor.http.ContentType
import io.ktor.http.contentType
import kotlinx.serialization.SerializationException
private fun updateOutgoingRelationshipInCache(friendTag: String, userIdHint: String?) {
if (userIdHint != null) {
val byId = StoatAPI.userCache[userIdHint]
if (byId != null) {
StoatAPI.userCache[userIdHint] = byId.copy(relationship = "Outgoing")
return
}
}
val username = friendTag.substringBeforeLast("#", "")
val discriminator = friendTag.substringAfterLast("#", "")
if (username.isBlank() || discriminator.isBlank()) return
val user = StoatAPI.userCache.values.firstOrNull {
it.username == username && it.discriminator == discriminator
} ?: return
val userId = user.id ?: return
StoatAPI.userCache[userId] = user.copy(relationship = "Outgoing")
}
suspend fun blockUser(userId: String) {
val response = StoatHttp.put("/users/$userId/block".api())
.bodyAsText()
@ -37,7 +59,7 @@ suspend fun unblockUser(userId: String) {
}
}
suspend fun friendUser(username: String) {
suspend fun friendUser(username: String, userIdHint: String? = null) {
val response = StoatHttp.post("/users/friend".api()) {
contentType(ContentType.Application.Json)
setBody(mapOf("username" to username))
@ -48,7 +70,8 @@ suspend fun friendUser(username: String) {
val error = StoatJson.decodeFromString(StoatAPIError.serializer(), body)
throw Exception(error.type)
} catch (e: SerializationException) {
// Not an error
// Not an error. Apply a local cache fallback so UI updates immediately.
updateOutgoingRelationshipInCache(username, userIdHint)
}
}

View File

@ -65,7 +65,7 @@ fun UserButtons(
onClick = {
scope.launch {
try {
friendUser("${user.username}#${user.discriminator}")
friendUser("${user.username}#${user.discriminator}", user.id)
} catch (e: Exception) {
// Button did nothing, but not an error
if (e.message == "NoEffect") return@launch
@ -92,7 +92,7 @@ fun UserButtons(
onClick = {
scope.launch {
try {
friendUser("${user.username}#${user.discriminator}")
friendUser("${user.username}#${user.discriminator}", user.id)
} catch (e: Exception) {
if (e.message == "NoEffect") return@launch
logcat(LogPriority.ERROR) { e.asLog() }