Use Conversation Manager for message retrieval

parent 9e7a387e
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")
}
}
......@@ -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))
}
}
}
......
@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,
......
@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()
}
@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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment