mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 02:39:05 +01:00
Fix receivers of ConversationMessageReceivedEvent
These were only listening to private message events, ignoring all others
This commit is contained in:
@@ -40,9 +40,9 @@ import org.briarproject.briar.android.splash.SplashScreenActivity;
|
||||
import org.briarproject.briar.android.util.BriarNotificationBuilder;
|
||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||
import org.briarproject.briar.api.blog.event.BlogPostAddedEvent;
|
||||
import org.briarproject.briar.api.conversation.event.ConversationMessageReceivedEvent;
|
||||
import org.briarproject.briar.api.forum.event.ForumPostReceivedEvent;
|
||||
import org.briarproject.briar.api.introduction.event.IntroductionSucceededEvent;
|
||||
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent;
|
||||
import org.briarproject.briar.api.privategroup.event.GroupMessageAddedEvent;
|
||||
|
||||
import java.util.Set;
|
||||
@@ -219,8 +219,9 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
|
||||
SettingsUpdatedEvent s = (SettingsUpdatedEvent) e;
|
||||
if (s.getNamespace().equals(SETTINGS_NAMESPACE))
|
||||
settings = s.getSettings();
|
||||
} else if (e instanceof PrivateMessageReceivedEvent) {
|
||||
PrivateMessageReceivedEvent p = (PrivateMessageReceivedEvent) e;
|
||||
} else if (e instanceof ConversationMessageReceivedEvent) {
|
||||
ConversationMessageReceivedEvent p =
|
||||
(ConversationMessageReceivedEvent) e;
|
||||
showContactNotification(p.getContactId());
|
||||
} else if (e instanceof GroupMessageAddedEvent) {
|
||||
GroupMessageAddedEvent g = (GroupMessageAddedEvent) e;
|
||||
|
||||
@@ -37,8 +37,8 @@ import org.briarproject.briar.android.view.BriarRecyclerView;
|
||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
|
||||
import org.briarproject.briar.api.conversation.ConversationManager;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
||||
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent;
|
||||
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
||||
import org.briarproject.briar.api.conversation.event.ConversationMessageReceivedEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -242,15 +242,16 @@ public class ContactListFragment extends BaseFragment implements EventListener {
|
||||
} else if (e instanceof ContactRemovedEvent) {
|
||||
LOG.info("Contact removed, removing item");
|
||||
removeItem(((ContactRemovedEvent) e).getContactId());
|
||||
} else if (e instanceof PrivateMessageReceivedEvent) {
|
||||
LOG.info("Private message received, updating item");
|
||||
PrivateMessageReceivedEvent p = (PrivateMessageReceivedEvent) e;
|
||||
PrivateMessageHeader h = p.getMessageHeader();
|
||||
} else if (e instanceof ConversationMessageReceivedEvent) {
|
||||
LOG.info("Conversation message received, updating item");
|
||||
ConversationMessageReceivedEvent p =
|
||||
(ConversationMessageReceivedEvent) e;
|
||||
ConversationMessageHeader h = p.getMessageHeader();
|
||||
updateItem(p.getContactId(), h);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateItem(ContactId c, PrivateMessageHeader h) {
|
||||
private void updateItem(ContactId c, ConversationMessageHeader h) {
|
||||
runOnUiThreadUnlessDestroyed(() -> {
|
||||
adapter.incrementRevision();
|
||||
int position = adapter.findItemPosition(c);
|
||||
|
||||
@@ -3,7 +3,7 @@ package org.briarproject.briar.android.contact;
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
||||
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
||||
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
@@ -23,7 +23,7 @@ public class ContactListItem extends ContactItem {
|
||||
this.timestamp = count.getLatestMsgTime();
|
||||
}
|
||||
|
||||
void addMessage(PrivateMessageHeader h) {
|
||||
void addMessage(ConversationMessageHeader h) {
|
||||
empty = false;
|
||||
if (h.getTimestamp() > timestamp) timestamp = h.getTimestamp();
|
||||
if (!h.isRead()) unread++;
|
||||
|
||||
@@ -67,13 +67,13 @@ import org.briarproject.briar.api.conversation.ConversationManager;
|
||||
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
||||
import org.briarproject.briar.api.conversation.ConversationRequest;
|
||||
import org.briarproject.briar.api.conversation.ConversationResponse;
|
||||
import org.briarproject.briar.api.conversation.event.ConversationMessageReceivedEvent;
|
||||
import org.briarproject.briar.api.forum.ForumSharingManager;
|
||||
import org.briarproject.briar.api.introduction.IntroductionManager;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessage;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageFactory;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
||||
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent;
|
||||
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -410,11 +410,12 @@ public class ConversationActivity extends BriarActivity
|
||||
LOG.info("Contact removed");
|
||||
finishOnUiThread();
|
||||
}
|
||||
} else if (e instanceof PrivateMessageReceivedEvent) {
|
||||
PrivateMessageReceivedEvent p = (PrivateMessageReceivedEvent) e;
|
||||
} else if (e instanceof ConversationMessageReceivedEvent) {
|
||||
ConversationMessageReceivedEvent p =
|
||||
(ConversationMessageReceivedEvent) e;
|
||||
if (p.getContactId().equals(contactId)) {
|
||||
LOG.info("Message received, adding");
|
||||
onNewPrivateMessage(p.getMessageHeader());
|
||||
onNewConversationMessage(p.getMessageHeader());
|
||||
}
|
||||
} else if (e instanceof MessagesSentEvent) {
|
||||
MessagesSentEvent m = (MessagesSentEvent) e;
|
||||
@@ -452,7 +453,7 @@ public class ConversationActivity extends BriarActivity
|
||||
});
|
||||
}
|
||||
|
||||
private void onNewPrivateMessage(ConversationMessageHeader h) {
|
||||
private void onNewConversationMessage(ConversationMessageHeader h) {
|
||||
runOnUiThreadUnlessDestroyed(() -> {
|
||||
if (h instanceof ConversationRequest ||
|
||||
h instanceof ConversationResponse) {
|
||||
|
||||
@@ -12,7 +12,7 @@ import javax.annotation.concurrent.Immutable;
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class ConversationMessageReceivedEvent<H extends ConversationMessageHeader>
|
||||
public abstract class ConversationMessageReceivedEvent<H extends ConversationMessageHeader>
|
||||
extends Event {
|
||||
|
||||
private final H messageHeader;
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
@@ -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()
|
||||
@@ -3,13 +3,20 @@ package org.briarproject.briar.headless.event
|
||||
import io.javalin.json.JavalinJson.toJson
|
||||
import io.javalin.websocket.WsSession
|
||||
import io.mockk.*
|
||||
import org.briarproject.bramble.api.identity.AuthorInfo
|
||||
import org.briarproject.bramble.api.identity.AuthorInfo.Status.VERIFIED
|
||||
import org.briarproject.bramble.test.ImmediateExecutor
|
||||
import org.briarproject.bramble.test.TestUtils.getRandomId
|
||||
import org.briarproject.briar.api.client.SessionId
|
||||
import org.briarproject.briar.api.introduction.IntroductionRequest
|
||||
import org.briarproject.briar.api.introduction.event.IntroductionRequestReceivedEvent
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader
|
||||
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
|
||||
import org.briarproject.briar.headless.ControllerTest
|
||||
import org.briarproject.briar.headless.messaging.EVENT_PRIVATE_MESSAGE
|
||||
import org.briarproject.briar.headless.messaging.EVENT_CONVERSATION_MESSAGE
|
||||
import org.briarproject.briar.headless.messaging.output
|
||||
import org.eclipse.jetty.websocket.api.WebSocketException
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.IOException
|
||||
|
||||
@@ -23,7 +30,7 @@ internal class WebSocketControllerTest : ControllerTest() {
|
||||
private val header =
|
||||
PrivateMessageHeader(message.id, group.id, timestamp, true, true, true, true, emptyList())
|
||||
private val event = PrivateMessageReceivedEvent(header, contact.id)
|
||||
private val outputEvent = OutputEvent(EVENT_PRIVATE_MESSAGE, event.output(text))
|
||||
private val outputEvent = OutputEvent(EVENT_CONVERSATION_MESSAGE, event.output(text))
|
||||
|
||||
@Test
|
||||
fun testSendEvent() {
|
||||
@@ -32,7 +39,7 @@ internal class WebSocketControllerTest : ControllerTest() {
|
||||
every { session1.send(capture(slot)) } just Runs
|
||||
|
||||
controller.sessions.add(session1)
|
||||
controller.sendEvent(EVENT_PRIVATE_MESSAGE, event.output(text))
|
||||
controller.sendEvent(EVENT_CONVERSATION_MESSAGE, event.output(text))
|
||||
|
||||
assertJsonEquals(slot.captured, outputEvent)
|
||||
}
|
||||
@@ -55,13 +62,45 @@ internal class WebSocketControllerTest : ControllerTest() {
|
||||
|
||||
controller.sessions.add(session1)
|
||||
controller.sessions.add(session2)
|
||||
controller.sendEvent(EVENT_PRIVATE_MESSAGE, event.output(text))
|
||||
controller.sendEvent(EVENT_CONVERSATION_MESSAGE, event.output(text))
|
||||
|
||||
verify { session2.send(slot.captured) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOutputPrivateMessageReceivedEvent() {
|
||||
fun testIntroductionRequestEvent() {
|
||||
val sessionId = SessionId(getRandomId())
|
||||
val authorInfo = AuthorInfo(VERIFIED)
|
||||
val introductionRequest = IntroductionRequest(
|
||||
message.id,
|
||||
group.id,
|
||||
timestamp,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
sessionId,
|
||||
author,
|
||||
text,
|
||||
false,
|
||||
authorInfo
|
||||
)
|
||||
val introductionRequestEvent =
|
||||
IntroductionRequestReceivedEvent(introductionRequest, contact.id)
|
||||
val introductionOutputEvent =
|
||||
OutputEvent(EVENT_CONVERSATION_MESSAGE, introductionRequestEvent.output())
|
||||
val slot = CapturingSlot<String>()
|
||||
|
||||
every { session1.send(capture(slot)) } just Runs
|
||||
|
||||
controller.sessions.add(session1)
|
||||
controller.sendEvent(EVENT_CONVERSATION_MESSAGE, introductionRequestEvent.output())
|
||||
assertJsonEquals(slot.captured, introductionOutputEvent)
|
||||
assertEquals("IntroductionRequest", introductionRequestEvent.output()["type"])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOutputConversationMessageReceivedEvent() {
|
||||
val json = """
|
||||
{
|
||||
"type": "event",
|
||||
|
||||
@@ -161,7 +161,7 @@ internal class MessagingControllerImplTest : ControllerTest() {
|
||||
val event = PrivateMessageReceivedEvent(header, contact.id)
|
||||
|
||||
every { messagingManager.getMessageText(message.id) } returns text
|
||||
every { webSocketController.sendEvent(EVENT_PRIVATE_MESSAGE, event.output(text)) } just runs
|
||||
every { webSocketController.sendEvent(EVENT_CONVERSATION_MESSAGE, event.output(text)) } just runs
|
||||
|
||||
controller.eventOccurred(event)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user