commonMain.net.folivo.trixnity.client.room.message.image.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of trixnity-client-jvm Show documentation
Show all versions of trixnity-client-jvm Show documentation
Multiplatform Kotlin SDK for matrix-protocol
package net.folivo.trixnity.client.room.message
import io.ktor.http.*
import kotlinx.coroutines.flow.first
import net.folivo.trixnity.core.model.events.m.room.EncryptedFile
import net.folivo.trixnity.core.model.events.m.room.ImageInfo
import net.folivo.trixnity.core.model.events.m.room.RoomMessageEventContent
import net.folivo.trixnity.utils.ByteArrayFlow
suspend fun MessageBuilder.image(
body: String,
image: ByteArrayFlow,
format: String? = null,
formattedBody: String? = null,
fileName: String? = null,
type: ContentType? = null,
size: Int? = null,
height: Int? = null,
width: Int? = null
) {
val info: ImageInfo?
val url: String?
val encryptedFile: EncryptedFile?
val isEncryptedRoom = roomService.getById(roomId).first()?.encrypted == true
if (isEncryptedRoom) {
val (thumbnailFile, thumbnailInfo) = mediaService.prepareUploadEncryptedThumbnail(image, type)
?: Pair(null, null)
encryptedFile = mediaService.prepareUploadEncryptedMedia(image)
info = ImageInfo(
height = height,
width = width,
mimeType = type?.toString(),
size = size,
thumbnailUrl = null,
thumbnailFile = thumbnailFile,
thumbnailInfo = thumbnailInfo
)
url = null
} else {
url = mediaService.prepareUploadMedia(image, type)
val (thumbnailUrl, thumbnailInfo) = mediaService.prepareUploadThumbnail(image, type) ?: Pair(null, null)
info = ImageInfo(
height = height,
width = width,
mimeType = type?.toString(),
size = size,
thumbnailUrl = thumbnailUrl,
thumbnailFile = null,
thumbnailInfo = thumbnailInfo
)
encryptedFile = null
}
roomMessageBuilder(body, format, formattedBody) {
RoomMessageEventContent.FileBased.Image(
body = this.body,
format = this.format,
formattedBody = this.formattedBody,
fileName = fileName,
info = info,
url = url,
file = encryptedFile,
relatesTo = relatesTo,
mentions = mentions,
)
}
}