Чат, вложения
This commit is contained in:
@@ -94,7 +94,15 @@ class ChatRepositoryImpl @Inject constructor(
|
||||
}
|
||||
|
||||
override suspend fun uploadMedia(file: java.io.File): String {
|
||||
val requestFile = file.asRequestBody("image/*".toMediaTypeOrNull())
|
||||
val mimeType = when (file.extension.lowercase()) {
|
||||
"jpg", "jpeg" -> "image/jpeg"
|
||||
"png" -> "image/png"
|
||||
"webp" -> "image/webp"
|
||||
"mp4" -> "video/mp4"
|
||||
"mp3", "m4a", "wav" -> "audio/mpeg"
|
||||
else -> "application/octet-stream"
|
||||
}
|
||||
val requestFile = file.asRequestBody(mimeType.toMediaTypeOrNull())
|
||||
val body = MultipartBody.Part.createFormData("file", file.name, requestFile)
|
||||
return api.uploadFile(body).url
|
||||
}
|
||||
@@ -177,9 +185,9 @@ fun MessageDto.toDomain(baseUrl: String): Message {
|
||||
createdAt = createdAt ?: "",
|
||||
media = media.map {
|
||||
chats.domain.model.Media(
|
||||
id = it.id,
|
||||
type = it.type,
|
||||
url = it.url.ensureAbsoluteUrl(baseUrl),
|
||||
id = it.id ?: java.util.UUID.randomUUID().toString(),
|
||||
type = it.type ?: "unknown",
|
||||
url = (it.url ?: "").ensureAbsoluteUrl(baseUrl),
|
||||
filename = it.filename,
|
||||
size = it.size,
|
||||
duration = it.duration
|
||||
@@ -221,10 +229,26 @@ fun core.database.data.MessageEntity.toDomain(baseUrl: String, gson: com.google.
|
||||
}
|
||||
|
||||
val mediaTypeToken = object : com.google.gson.reflect.TypeToken<List<chats.data.remote.dto.MediaItemDto>>() {}.type
|
||||
val mediaDtos: List<chats.data.remote.dto.MediaItemDto> = gson.fromJson(mediaJson, mediaTypeToken) ?: emptyList()
|
||||
val mediaDtos: List<chats.data.remote.dto.MediaItemDto> = try {
|
||||
gson.fromJson(mediaJson, mediaTypeToken)
|
||||
} catch (e: Exception) {
|
||||
emptyList()
|
||||
} ?: emptyList()
|
||||
|
||||
val reactionsTypeToken = object : com.google.gson.reflect.TypeToken<List<chats.data.remote.dto.ReactionDto>>() {}.type
|
||||
val reactionDtos: List<chats.data.remote.dto.ReactionDto> = gson.fromJson(reactionsJson, reactionsTypeToken) ?: emptyList()
|
||||
val reactionDtos: List<chats.data.remote.dto.ReactionDto> = try {
|
||||
// Try to parse as array (List<ReactionDto>)
|
||||
gson.fromJson(reactionsJson, reactionsTypeToken)
|
||||
} catch (e: Exception) {
|
||||
// If it's an object instead of array, parse as map and convert to list
|
||||
try {
|
||||
val mapType = object : com.google.gson.reflect.TypeToken<Map<String, Int>>() {}.type
|
||||
val map: Map<String, Int> = gson.fromJson(reactionsJson, mapType) ?: emptyMap()
|
||||
map.map { chats.data.remote.dto.ReactionDto(it.key, it.value, false) }
|
||||
} catch (innerE: Exception) {
|
||||
emptyList()
|
||||
}
|
||||
} ?: emptyList()
|
||||
|
||||
return Message(
|
||||
id = id,
|
||||
@@ -237,9 +261,9 @@ fun core.database.data.MessageEntity.toDomain(baseUrl: String, gson: com.google.
|
||||
createdAt = createdAt,
|
||||
media = mediaDtos.map {
|
||||
chats.domain.model.Media(
|
||||
id = it.id,
|
||||
type = it.type,
|
||||
url = it.url.ensureAbsoluteUrl(baseUrl),
|
||||
id = it.id ?: java.util.UUID.randomUUID().toString(),
|
||||
type = it.type ?: "unknown",
|
||||
url = (it.url ?: "").ensureAbsoluteUrl(baseUrl),
|
||||
filename = it.filename,
|
||||
size = it.size,
|
||||
duration = it.duration
|
||||
|
||||
Reference in New Issue
Block a user