Private
Public Access
1
0

Adds image attachment support

This commit is contained in:
2024-03-23 17:04:14 -07:00
parent b23ab2dfe2
commit 9f5f2d7af5
11 changed files with 308 additions and 71 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -2,8 +2,11 @@ package net.buzzert.kordophone.backend.db.model
import android.view.Display.Mode
import io.realm.kotlin.Realm
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.ext.toRealmList
import io.realm.kotlin.types.EmbeddedRealmObject
import io.realm.kotlin.types.RealmInstant
import io.realm.kotlin.types.RealmList
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey
import net.buzzert.kordophone.backend.db.model.Conversation
@@ -20,6 +23,7 @@ open class Message(
var text: String,
var sender: String?,
var date: RealmInstant,
var attachmentGUIDs: RealmList<String>,
var conversationGUID: GUID,
): RealmObject
@@ -29,6 +33,7 @@ open class Message(
text = "",
sender = null,
date = RealmInstant.now(),
attachmentGUIDs = realmListOf<String>(),
conversationGUID = ObjectId().toString(),
)
@@ -38,6 +43,7 @@ open class Message(
guid = guid,
sender = sender,
date = Date.from(date.toInstant()),
attachmentGUIDs = attachmentGUIDs.toList(),
conversation = parentConversation,
)
}
@@ -51,5 +57,8 @@ fun ModelMessage.toDatabaseMessage(outgoing: Boolean = false): Message {
sender = from.sender
date = from.date.toInstant().toRealmInstant()
conversationGUID = from.conversation.guid
from.attachmentGUIDs?.let {
attachmentGUIDs = it.toRealmList()
}
}
}

View File

@@ -17,6 +17,9 @@ data class Message(
@SerializedName("date")
val date: Date,
@SerializedName("fileTransferGUIDs")
val attachmentGUIDs: List<String>?,
@Transient
var conversation: Conversation,
) {
@@ -27,11 +30,18 @@ data class Message(
text = text,
sender = sender,
date = Date(),
attachmentGUIDs = emptyList(),
conversation = conversation,
)
}
}
val displayText: String get() {
// Filter out attachment markers
val attachmentMarker = byteArrayOf(0xEF.toByte(), 0xBF.toByte(), 0xBC.toByte()).decodeToString()
return text.replace(attachmentMarker, "")
}
override fun toString(): String {
return "Message(guid=$guid, text=$text, sender=$sender, date=$date, parent=${conversation.guid})"
}

View File

@@ -69,6 +69,9 @@ interface APIInterface {
@POST("/markConversation")
suspend fun markConversation(@Query("guid") conversationGUID: String): Response<Void>
@GET("/attachment")
suspend fun fetchAttachment(@Query("guid") guid: String, @Query("preview") preview: Boolean = false): ResponseBody
@POST("/authenticate")
suspend fun authenticate(@Body request: AuthenticationRequest): Response<AuthenticationResponse>
}

View File

@@ -21,6 +21,8 @@ import net.buzzert.kordophone.backend.model.Conversation
import net.buzzert.kordophone.backend.model.GUID
import net.buzzert.kordophone.backend.model.Message
import net.buzzert.kordophone.backend.model.UpdateItem
import okhttp3.OkHttpClient
import okio.BufferedSource
import java.lang.Error
import java.net.URL
import java.util.Queue
@@ -189,6 +191,10 @@ class ChatRepository(
Log.e(REPO_LOG, "Error marking conversation as read: ${e.message}")
}
suspend fun fetchAttachmentDataSource(guid: String, preview: Boolean): BufferedSource {
return apiInterface.fetchAttachment(guid, preview).source()
}
fun close() {
database.close()
}
@@ -262,7 +268,8 @@ class ChatRepository(
text = outgoingMessage.text,
sender = null,
conversation = it.conversation,
date = outgoingMessage.date
date = outgoingMessage.date,
attachmentGUIDs = null,
)
_messageDeliveredChannel.emit(

View File

@@ -11,6 +11,7 @@ import net.buzzert.kordophone.backend.db.CachedChatDatabase
import net.buzzert.kordophone.backend.model.Message
import net.buzzert.kordophone.backend.server.APIClient
import net.buzzert.kordophone.backend.server.APIInterface
import net.buzzert.kordophone.backend.server.Authentication
import net.buzzert.kordophone.backend.server.ChatRepository
import net.buzzert.kordophone.backend.server.RetrofitAPIClient
import net.buzzert.kordophone.backend.server.UpdateMonitor
@@ -23,7 +24,7 @@ import java.util.concurrent.TimeUnit
class BackendTests {
private fun liveRepository(host: String): Pair<ChatRepository, RetrofitAPIClient> {
val client = RetrofitAPIClient(URL(host))
val client = RetrofitAPIClient(URL(host), authentication = Authentication("test", "test"))
val database = CachedChatDatabase.testDatabase()
val repository = ChatRepository(client, database)
return Pair(repository, client)

View File

@@ -29,6 +29,7 @@ import okhttp3.WebSocket
import okhttp3.WebSocketListener
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import retrofit2.Call
import retrofit2.Response
import java.util.Date
import java.util.UUID
@@ -53,6 +54,7 @@ class MockServer {
guid = UUID.randomUUID().toString(),
sender = null,
conversation = parentConversation,
attachmentGUIDs = null,
)
}
@@ -150,7 +152,8 @@ class MockServer {
date = Date(),
guid = UUID.randomUUID().toString(),
sender = null, // me
conversation = conversation
conversation = conversation,
attachmentGUIDs = null,
)
addMessagesToConversation(conversation, listOf(message))
@@ -169,14 +172,15 @@ class MockServerClient(private val server: MockServer): APIClient, WebSocketList
override fun getWebSocketClient(
serverPath: String,
authToken: String?,
queryParams: Map<String, String>?,
listener: WebSocketListener
): WebSocket {
val webServer = server.getServer()
val params = queryParams ?: mapOf()
val baseHTTPURL: HttpUrl = webServer.url("/")
val baseURL = baseHTTPURL.toUrl()
val requestURL = baseURL.authenticatedWebSocketURL(serverPath, authToken)
val requestURL = baseURL.authenticatedWebSocketURL(serverPath, params)
val request = Request.Builder()
.url(requestURL)
.build()
@@ -260,6 +264,10 @@ class MockServerInterface(private val server: MockServer): APIInterface {
return Response.success(null)
}
override suspend fun fetchAttachment(guid: String, preview: Boolean): ResponseBody {
TODO("Not yet implemented")
}
override suspend fun authenticate(request: AuthenticationRequest): Response<AuthenticationResponse> {
// Anything goes!
val response = AuthenticationResponse(