Skip to content

Commit e1d04d3

Browse files
committed
Fix attachment sending
1 parent afbb57f commit e1d04d3

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

composeApp/src/commonMain/kotlin/com/hypergonial/chat/model/ChatClient.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,7 @@ class ChatClient(scope: CoroutineScope, override val maxReconnectAttempts: Int =
486486
scope.launch {
487487
try {
488488
withTimeout(5000) { gatewayConnectedJob.join() }
489-
}
490-
catch(e: TimeoutCancellationException) {
489+
} catch (e: TimeoutCancellationException) {
491490
gatewaySession?.cancel()
492491
gatewayConnectedJob = Job()
493492
_reconnectAttempts++
@@ -945,15 +944,19 @@ class ChatClient(scope: CoroutineScope, override val maxReconnectAttempts: Int =
945944

946945
override suspend fun sendMessage(
947946
channelId: Snowflake,
948-
content: String,
947+
content: String?,
949948
nonce: String?,
950949
attachments: List<PlatformFile>,
951950
): Message {
951+
require(!content.isNullOrBlank() || attachments.isNotEmpty()) { "One of content or attachments must be set" }
952+
952953
// See https://stackoverflow.com/questions/69830965/ktor-client-post-multipart-form-data
953954
val json =
954955
FormPart(
955956
"json",
956-
Json.encodeToString<MessageCreateRequest>(MessageCreateRequest(content, nonce)),
957+
Json.encodeToString<MessageCreateRequest>(
958+
MessageCreateRequest(content.let { if (it.isNullOrBlank()) null else it }, nonce)
959+
),
957960
Headers.build { append(HttpHeaders.ContentType, ContentType.Application.Json.toString()) },
958961
)
959962

@@ -1000,7 +1003,7 @@ class ChatClient(scope: CoroutineScope, override val maxReconnectAttempts: Int =
10001003
return http
10011004
.patch("channels/$channelId/messages/$messageId") {
10021005
contentType(ContentType.Application.Json)
1003-
setBody(MessageUpdateRequest(content))
1006+
setBody(MessageUpdateRequest(content.let { if (it.isNullOrBlank()) null else it }))
10041007
}
10051008
.body<Message>()
10061009
}

composeApp/src/commonMain/kotlin/com/hypergonial/chat/model/Client.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ interface Client : InstanceKeeper.Instance, EventManagerAware, CacheAware {
241241
*/
242242
suspend fun sendMessage(
243243
channelId: Snowflake,
244-
content: String,
244+
content: String?,
245245
nonce: String? = null,
246246
attachments: List<PlatformFile> = emptyList(),
247247
): Message

composeApp/src/commonMain/kotlin/com/hypergonial/chat/model/payloads/rest/MessageCreateRequest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ import kotlinx.serialization.Serializable
1010
* @param content The content of the message.
1111
* @param nonce A nonce to identify the message.
1212
*/
13-
@Serializable data class MessageCreateRequest(val content: String, val nonce: String? = null)
13+
@Serializable data class MessageCreateRequest(val content: String? = null, val nonce: String? = null)

composeApp/src/commonMain/kotlin/com/hypergonial/chat/view/composables/MessageList.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,12 @@ private fun MessageContent(component: MessageComponent, modifier: Modifier = Mod
331331
else if (state.isPending) Color.Gray else MaterialTheme.colorScheme.onBackground
332332

333333
Row(modifier = modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
334+
if (state.message.content.isNullOrBlank()) {
335+
return@Row
336+
}
337+
334338
Markdown(
335-
state.message.content ?: "TODO: No content - HANDLEME",
339+
state.message.content ?: "",
336340
imageTransformer = ChatImageTransformer,
337341
modifier = Modifier.fillMaxHeight().fillMaxWidth(0.9f),
338342
components =

0 commit comments

Comments
 (0)