Merge 9b6669da76 into 584ce90b64
This commit is contained in:
commit
aca157d6f2
|
|
@ -1,5 +1,6 @@
|
||||||
package chat.stoat.api.routes.user
|
package chat.stoat.api.routes.user
|
||||||
|
|
||||||
|
import chat.stoat.api.StoatAPI
|
||||||
import chat.stoat.api.StoatAPIError
|
import chat.stoat.api.StoatAPIError
|
||||||
import chat.stoat.api.StoatHttp
|
import chat.stoat.api.StoatHttp
|
||||||
import chat.stoat.api.StoatJson
|
import chat.stoat.api.StoatJson
|
||||||
|
|
@ -13,6 +14,27 @@ import io.ktor.http.ContentType
|
||||||
import io.ktor.http.contentType
|
import io.ktor.http.contentType
|
||||||
import kotlinx.serialization.SerializationException
|
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) {
|
suspend fun blockUser(userId: String) {
|
||||||
val response = StoatHttp.put("/users/$userId/block".api())
|
val response = StoatHttp.put("/users/$userId/block".api())
|
||||||
.bodyAsText()
|
.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()) {
|
val response = StoatHttp.post("/users/friend".api()) {
|
||||||
contentType(ContentType.Application.Json)
|
contentType(ContentType.Application.Json)
|
||||||
setBody(mapOf("username" to username))
|
setBody(mapOf("username" to username))
|
||||||
|
|
@ -48,7 +70,8 @@ suspend fun friendUser(username: String) {
|
||||||
val error = StoatJson.decodeFromString(StoatAPIError.serializer(), body)
|
val error = StoatJson.decodeFromString(StoatAPIError.serializer(), body)
|
||||||
throw Exception(error.type)
|
throw Exception(error.type)
|
||||||
} catch (e: SerializationException) {
|
} catch (e: SerializationException) {
|
||||||
// Not an error
|
// Not an error. Apply a local cache fallback so UI updates immediately.
|
||||||
|
updateOutgoingRelationshipInCache(username, userIdHint)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -65,7 +65,7 @@ fun UserButtons(
|
||||||
onClick = {
|
onClick = {
|
||||||
scope.launch {
|
scope.launch {
|
||||||
try {
|
try {
|
||||||
friendUser("${user.username}#${user.discriminator}")
|
friendUser("${user.username}#${user.discriminator}", user.id)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
// Button did nothing, but not an error
|
// Button did nothing, but not an error
|
||||||
if (e.message == "NoEffect") return@launch
|
if (e.message == "NoEffect") return@launch
|
||||||
|
|
@ -92,7 +92,7 @@ fun UserButtons(
|
||||||
onClick = {
|
onClick = {
|
||||||
scope.launch {
|
scope.launch {
|
||||||
try {
|
try {
|
||||||
friendUser("${user.username}#${user.discriminator}")
|
friendUser("${user.username}#${user.discriminator}", user.id)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
if (e.message == "NoEffect") return@launch
|
if (e.message == "NoEffect") return@launch
|
||||||
logcat(LogPriority.ERROR) { e.asLog() }
|
logcat(LogPriority.ERROR) { e.asLog() }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue