From 159fd34c0c526d89d97385e69ad8161d8cdb9d9e Mon Sep 17 00:00:00 2001
From: Torsten Grote <t@grobox.de>
Date: Mon, 3 Sep 2018 11:46:22 -0300
Subject: [PATCH] Use Conversation Manager for message retrieval

---
 .../briar/headless/messaging/Extensions.kt    | 33 ++++++++++++--
 .../messaging/MessagingControllerImpl.kt      | 26 ++++++-----
 .../messaging/OutputPrivateMessage.kt         | 14 ++++--
 .../messaging/OutputPrivateRequest.kt         | 42 ++++++++++++++++++
 .../messaging/OutputPrivateResponse.kt        | 43 +++++++++++++++++++
 5 files changed, 142 insertions(+), 16 deletions(-)
 create mode 100644 briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateRequest.kt
 create mode 100644 briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateResponse.kt

diff --git a/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/Extensions.kt b/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/Extensions.kt
index c23dbee7b3..b9332b4573 100644
--- a/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/Extensions.kt
+++ b/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/Extensions.kt
@@ -1,15 +1,42 @@
 package org.briarproject.briar.headless.messaging
 
 import org.briarproject.bramble.api.contact.ContactId
+import org.briarproject.briar.api.introduction.IntroductionRequest
+import org.briarproject.briar.api.introduction.IntroductionResponse
 import org.briarproject.briar.api.messaging.PrivateMessage
 import org.briarproject.briar.api.messaging.PrivateMessageHeader
+import org.briarproject.briar.api.messaging.PrivateRequest
+import org.briarproject.briar.api.messaging.PrivateResponse
 import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
+import org.briarproject.briar.api.sharing.InvitationRequest
+import org.briarproject.briar.api.sharing.InvitationResponse
 
-internal fun PrivateMessageHeader.output(contactId: ContactId, body: String) =
-    OutputPrivateMessage(this, contactId, body)
+internal fun PrivateMessageHeader.output(
+    contactId: ContactId,
+    body: String?
+) = OutputPrivateMessage(this, contactId, body)
 
 internal fun PrivateMessage.output(contactId: ContactId, body: String) =
     OutputPrivateMessage(this, contactId, body)
 
-internal fun PrivateMessageReceivedEvent.output(body: String) =
+internal fun PrivateMessageReceivedEvent<*>.output(body: String) =
     messageHeader.output(contactId, body)
+
+internal fun IntroductionRequest.output(contactId: ContactId) =
+    OutputIntroductionRequest(this, contactId)
+
+internal fun PrivateRequest<*>.output(contactId: ContactId): OutputPrivateMessage {
+    return when (this) {
+        is IntroductionRequest -> OutputIntroductionRequest(this, contactId)
+        is InvitationRequest -> OutputInvitationRequest(this, contactId)
+        else -> throw AssertionError("Unknown PrivateRequest")
+    }
+}
+
+internal fun PrivateResponse.output(contactId: ContactId): OutputPrivateMessage {
+    return when (this) {
+        is IntroductionResponse -> OutputIntroductionResponse(this, contactId)
+        is InvitationResponse -> OutputInvitationResponse(this, contactId)
+        else -> throw AssertionError("Unknown PrivateResponse")
+    }
+}
diff --git a/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/MessagingControllerImpl.kt b/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/MessagingControllerImpl.kt
index 952fc77d67..2c545d4ac4 100644
--- a/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/MessagingControllerImpl.kt
+++ b/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/MessagingControllerImpl.kt
@@ -11,9 +11,8 @@ import org.briarproject.bramble.api.db.NoSuchContactException
 import org.briarproject.bramble.api.event.Event
 import org.briarproject.bramble.api.event.EventListener
 import org.briarproject.bramble.api.system.Clock
+import org.briarproject.briar.api.messaging.*
 import org.briarproject.briar.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH
-import org.briarproject.briar.api.messaging.MessagingManager
-import org.briarproject.briar.api.messaging.PrivateMessageFactory
 import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
 import org.briarproject.briar.headless.WebSocketController
 import java.util.concurrent.Executor
@@ -21,11 +20,15 @@ import javax.annotation.concurrent.Immutable
 import javax.inject.Inject
 import javax.inject.Singleton
 
+private const val EVENT_PRIVATE_MESSAGE =
+    "org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent"
+
 @Immutable
 @Singleton
 internal class MessagingControllerImpl @Inject
 constructor(
     private val messagingManager: MessagingManager,
+    private val conversationManager: ConversationManager,
     private val privateMessageFactory: PrivateMessageFactory,
     private val contactManager: ContactManager,
     private val webSocketController: WebSocketController,
@@ -35,10 +38,15 @@ constructor(
 
     override fun list(ctx: Context): Context {
         val contact = getContact(ctx)
-
-        val messages = messagingManager.getMessageHeaders(contact.id).map { header ->
-            val body = messagingManager.getMessageBody(header.id)
-            header.output(contact.id, body)
+        val messages = conversationManager.getMessageHeaders(contact.id).map { header ->
+            when (header) {
+                is PrivateRequest<*> -> header.output(contact.id)
+                is PrivateResponse -> header.output(contact.id)
+                else -> {
+                    val body = messagingManager.getMessageBody(header.id)
+                    header.output(contact.id, body)
+                }
+            }
         }
         return ctx.json(messages)
     }
@@ -62,11 +70,9 @@ constructor(
 
     override fun eventOccurred(e: Event) {
         when (e) {
-            is PrivateMessageReceivedEvent -> dbExecutor.run {
-                val name =
-                    "org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent"
+            is PrivateMessageReceivedEvent<*> -> dbExecutor.run {
                 val body = messagingManager.getMessageBody(e.messageHeader.id)
-                webSocketController.sendEvent(name, e.output(body))
+                webSocketController.sendEvent(EVENT_PRIVATE_MESSAGE, e.output(body))
             }
         }
     }
diff --git a/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateMessage.kt b/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateMessage.kt
index b448121f81..2af0a35aa7 100644
--- a/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateMessage.kt
+++ b/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateMessage.kt
@@ -1,3 +1,5 @@
+@file:Suppress("MemberVisibilityCanBePrivate", "unused")
+
 package org.briarproject.briar.headless.messaging
 
 import org.briarproject.bramble.api.contact.ContactId
@@ -6,8 +8,8 @@ import org.briarproject.briar.api.messaging.PrivateMessageHeader
 import javax.annotation.concurrent.Immutable
 
 @Immutable
-internal data class OutputPrivateMessage(
-    val body: String,
+internal open class OutputPrivateMessage(
+    val body: String?,
     val timestamp: Long,
     val read: Boolean,
     val seen: Boolean,
@@ -17,7 +19,13 @@ internal data class OutputPrivateMessage(
     val groupId: ByteArray,
     val contactId: Int
 ) {
-    internal constructor(header: PrivateMessageHeader, contactId: ContactId, body: String) : this(
+    open val type = "org.briarproject.briar.api.messaging.PrivateMessageHeader"
+
+    internal constructor(
+        header: PrivateMessageHeader,
+        contactId: ContactId,
+        body: String?
+    ) : this(
         body = body,
         timestamp = header.timestamp,
         read = header.isRead,
diff --git a/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateRequest.kt b/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateRequest.kt
new file mode 100644
index 0000000000..2429edae69
--- /dev/null
+++ b/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateRequest.kt
@@ -0,0 +1,42 @@
+@file:Suppress("MemberVisibilityCanBePrivate", "unused")
+
+package org.briarproject.briar.headless.messaging
+
+import org.briarproject.bramble.api.contact.ContactId
+import org.briarproject.briar.api.blog.BlogInvitationRequest
+import org.briarproject.briar.api.forum.ForumInvitationRequest
+import org.briarproject.briar.api.introduction.IntroductionRequest
+import org.briarproject.briar.api.messaging.PrivateRequest
+import org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest
+import org.briarproject.briar.api.sharing.InvitationRequest
+import javax.annotation.concurrent.Immutable
+
+@Immutable
+internal abstract class OutputPrivateRequest(header: PrivateRequest<*>, contactId: ContactId) :
+    OutputPrivateMessage(header, contactId, header.message) {
+
+    val sessionId: ByteArray = header.sessionId.bytes
+    val name: String = header.name
+    val answered = header.wasAnswered()
+}
+
+@Immutable
+internal class OutputIntroductionRequest(header: IntroductionRequest, contactId: ContactId) :
+    OutputPrivateRequest(header, contactId) {
+
+    override val type = "org.briarproject.briar.api.introduction.IntroductionRequest"
+    val alreadyContact = header.isContact
+}
+
+@Immutable
+internal class OutputInvitationRequest(header: InvitationRequest<*>, contactId: ContactId) :
+    OutputPrivateRequest(header, contactId) {
+
+    override val type = when (header) {
+        is ForumInvitationRequest -> "org.briarproject.briar.api.forum.ForumInvitationRequest"
+        is BlogInvitationRequest -> "org.briarproject.briar.api.blog.BlogInvitationRequest"
+        is GroupInvitationRequest -> "org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest"
+        else -> throw AssertionError("Unknown InvitationRequest")
+    }
+    val canBeOpened = header.canBeOpened()
+}
diff --git a/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateResponse.kt b/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateResponse.kt
new file mode 100644
index 0000000000..e9eab936cc
--- /dev/null
+++ b/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateResponse.kt
@@ -0,0 +1,43 @@
+@file:Suppress("MemberVisibilityCanBePrivate", "unused")
+
+package org.briarproject.briar.headless.messaging
+
+import org.briarproject.bramble.api.contact.ContactId
+import org.briarproject.briar.api.blog.BlogInvitationResponse
+import org.briarproject.briar.api.forum.ForumInvitationResponse
+import org.briarproject.briar.api.introduction.IntroductionResponse
+import org.briarproject.briar.api.messaging.PrivateResponse
+import org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse
+import org.briarproject.briar.api.sharing.InvitationResponse
+import org.briarproject.briar.headless.output
+import javax.annotation.concurrent.Immutable
+
+@Immutable
+internal abstract class OutputPrivateResponse(header: PrivateResponse, contactId: ContactId) :
+    OutputPrivateMessage(header, contactId, null) {
+
+    val sessionId: ByteArray = header.sessionId.bytes
+    val accepted = header.wasAccepted()
+}
+
+@Immutable
+internal class OutputIntroductionResponse(header: IntroductionResponse, contactId: ContactId) :
+    OutputPrivateResponse(header, contactId) {
+
+    override val type = "org.briarproject.briar.api.introduction.IntroductionResponse"
+    val introducedAuthor = header.introducedAuthor.output()
+    val introducer = header.isIntroducer
+}
+
+@Immutable
+internal class OutputInvitationResponse(header: InvitationResponse, contactId: ContactId) :
+    OutputPrivateResponse(header, contactId) {
+
+    override val type = when (header) {
+        is ForumInvitationResponse -> "org.briarproject.briar.api.forum.ForumInvitationResponse"
+        is BlogInvitationResponse -> "org.briarproject.briar.api.blog.BlogInvitationResponse"
+        is GroupInvitationResponse -> "org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse"
+        else -> throw AssertionError("Unknown InvitationResponse")
+    }
+    val shareableId: ByteArray = header.shareableId.bytes
+}
-- 
GitLab