Fix receivers of ConversationMessageReceivedEvent

These were only listening to private message events, ignoring all others
This commit is contained in:
Torsten Grote
2018-11-07 11:31:50 -03:00
parent cccaeeda6c
commit f964d1ef07
12 changed files with 117 additions and 34 deletions

View File

@@ -1,13 +1,41 @@
package org.briarproject.briar.headless.event
import org.briarproject.briar.api.blog.BlogInvitationRequest
import org.briarproject.briar.api.blog.BlogInvitationResponse
import org.briarproject.briar.api.conversation.event.ConversationMessageReceivedEvent
import org.briarproject.briar.api.forum.ForumInvitationRequest
import org.briarproject.briar.api.forum.ForumInvitationResponse
import org.briarproject.briar.api.introduction.IntroductionRequest
import org.briarproject.briar.api.introduction.IntroductionResponse
import org.briarproject.briar.api.messaging.PrivateMessageHeader
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse
import org.briarproject.briar.headless.json.JsonDict
import org.briarproject.briar.headless.messaging.output
import javax.annotation.concurrent.Immutable
@Immutable
internal class OutputEvent(val name: String, val data: Any) {
@Suppress("unused")
internal class OutputEvent(val name: String, val data: JsonDict) {
val type = "event"
}
internal fun ConversationMessageReceivedEvent<*>.output(text: String) =
messageHeader.output(contactId, text)
internal fun ConversationMessageReceivedEvent<*>.output(text: String): JsonDict {
check(messageHeader is PrivateMessageHeader)
return (messageHeader as PrivateMessageHeader).output(contactId, text)
}
internal fun ConversationMessageReceivedEvent<*>.output() = when (messageHeader) {
// requests
is ForumInvitationRequest -> (messageHeader as ForumInvitationRequest).output(contactId)
is BlogInvitationRequest -> (messageHeader as BlogInvitationRequest).output(contactId)
is GroupInvitationRequest -> (messageHeader as GroupInvitationRequest).output(contactId)
is IntroductionRequest -> (messageHeader as IntroductionRequest).output(contactId)
// responses
is ForumInvitationResponse -> (messageHeader as ForumInvitationResponse).output(contactId)
is BlogInvitationResponse -> (messageHeader as BlogInvitationResponse).output(contactId)
is GroupInvitationResponse -> (messageHeader as GroupInvitationResponse).output(contactId)
is IntroductionResponse -> (messageHeader as IntroductionResponse).output(contactId)
// unknown
else -> throw IllegalStateException()
}

View File

@@ -38,7 +38,7 @@ import javax.annotation.concurrent.Immutable
import javax.inject.Inject
import javax.inject.Singleton
internal const val EVENT_PRIVATE_MESSAGE = "ConversationMessageReceivedEvent"
internal const val EVENT_CONVERSATION_MESSAGE = "ConversationMessageReceivedEvent"
@Immutable
@Singleton
@@ -82,8 +82,13 @@ constructor(
override fun eventOccurred(e: Event) {
when (e) {
is ConversationMessageReceivedEvent<*> -> dbExecutor.execute {
val text = messagingManager.getMessageText(e.messageHeader.id)
webSocketController.sendEvent(EVENT_PRIVATE_MESSAGE, e.output(text))
val h = e.messageHeader
if (h is PrivateMessageHeader) {
val text = messagingManager.getMessageText(h.id)
webSocketController.sendEvent(EVENT_CONVERSATION_MESSAGE, e.output(text))
} else {
webSocketController.sendEvent(EVENT_CONVERSATION_MESSAGE, e.output())
}
}
}
}

View File

@@ -3,10 +3,10 @@ package org.briarproject.briar.headless.messaging
import org.briarproject.bramble.api.contact.ContactId
import org.briarproject.briar.api.conversation.ConversationMessageHeader
import org.briarproject.briar.api.messaging.PrivateMessage
import org.briarproject.briar.api.messaging.PrivateMessageHeader
import org.briarproject.briar.headless.json.JsonDict
internal fun ConversationMessageHeader.output(contactId: ContactId) = JsonDict(
"type" to "PrivateMessage",
"contactId" to contactId.int,
"timestamp" to timestamp,
"read" to isRead,
@@ -23,6 +23,14 @@ internal fun ConversationMessageHeader.output(contactId: ContactId, text: String
return dict
}
internal fun PrivateMessageHeader.output(contactId: ContactId, text: String?): JsonDict {
val dict = (this as ConversationMessageHeader).output(contactId, text)
dict.putAll(
"type" to "PrivateMessage"
)
return dict
}
/**
* Use only for outgoing messages that were just sent
*/

View File

@@ -3,16 +3,16 @@ package org.briarproject.briar.headless.messaging
import org.briarproject.bramble.api.contact.ContactId
import org.briarproject.bramble.identity.output
import org.briarproject.briar.api.blog.BlogInvitationResponse
import org.briarproject.briar.api.conversation.ConversationMessageHeader
import org.briarproject.briar.api.conversation.ConversationResponse
import org.briarproject.briar.api.forum.ForumInvitationResponse
import org.briarproject.briar.api.introduction.IntroductionResponse
import org.briarproject.briar.api.messaging.PrivateMessageHeader
import org.briarproject.briar.api.conversation.ConversationResponse
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse
import org.briarproject.briar.api.sharing.InvitationResponse
import org.briarproject.briar.headless.json.JsonDict
internal fun ConversationResponse.output(contactId: ContactId): JsonDict {
val dict = (this as PrivateMessageHeader).output(contactId)
val dict = (this as ConversationMessageHeader).output(contactId)
dict.putAll(
"sessionId" to sessionId.bytes,
"accepted" to wasAccepted()