Commit c9e94f33 authored by Nico Alt's avatar Nico Alt

Expose message delivery state changes to websockets API

We already indicate whether a message was sent/acked, but we don't
inform about updates.

Needed for briar-gtk#69.

Fixes #1779
parent 4e5f2e31
Pipeline #4860 failed with stage
in 8 minutes and 10 seconds
......@@ -409,3 +409,35 @@ When the last connection is lost (the contact goes offline), it sends a `Contact
"type": "event"
}
```
### A message was sent
When Briar sent a message to a contact, it sends a `MessagesSentEvent`. This is indicated in Briar
by showing one tick next to the message.
```json
{
"data": {
"contactId": 1,
"messageIds": [{"bytes": "+AIMMgOCPFF8HDEhiEHYjbfKrg7v0G94inKxjvjYzA8="}]
},
"name": "MessagesSentEvent",
"type": "event"
}
```
### A message was acknowledged
When a contact acknowledges that they received a message, Briar sends a `MessagesAckedEvent`.
This is indicated in Briar by showing two ticks next to the message.
```json
{
"data": {
"contactId": 1,
"messageIds": [{"bytes": "+AIMMgOCPFF8HDEhiEHYjbfKrg7v0G94inKxjvjYzA8="}]
},
"name": "MessagesAckedEvent",
"type": "event"
}
```
......@@ -19,6 +19,8 @@ import org.briarproject.bramble.api.event.EventListener
import org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent
import org.briarproject.bramble.api.sync.event.MessagesAckedEvent
import org.briarproject.bramble.api.sync.event.MessagesSentEvent
import org.briarproject.bramble.util.StringUtils.toUtf8
import org.briarproject.briar.api.conversation.ConversationManager
import org.briarproject.briar.headless.event.WebSocketController
......@@ -37,6 +39,8 @@ internal const val EVENT_PENDING_CONTACT_ADDED = "PendingContactAddedEvent"
internal const val EVENT_PENDING_CONTACT_REMOVED = "PendingContactRemovedEvent"
internal const val EVENT_CONTACT_CONNECTED = "ContactConnectedEvent"
internal const val EVENT_CONTACT_DISCONNECTED = "ContactDisconnectedEvent"
internal const val EVENT_MESSAGES_ACKED = "MessagesAckedEvent"
internal const val EVENT_MESSAGES_SENT = "MessagesSentEvent"
@Immutable
@Singleton
......@@ -69,6 +73,12 @@ constructor(
is ContactDisconnectedEvent -> {
webSocket.sendEvent(EVENT_CONTACT_DISCONNECTED, e.output())
}
is MessagesSentEvent -> {
webSocket.sendEvent(EVENT_MESSAGES_SENT, e.output())
}
is MessagesAckedEvent -> {
webSocket.sendEvent(EVENT_MESSAGES_ACKED, e.output())
}
else -> {
}
}
......
......@@ -4,6 +4,8 @@ import org.briarproject.bramble.api.contact.Contact
import org.briarproject.bramble.api.contact.event.ContactAddedEvent
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent
import org.briarproject.bramble.api.sync.event.MessagesAckedEvent
import org.briarproject.bramble.api.sync.event.MessagesSentEvent
import org.briarproject.bramble.identity.output
import org.briarproject.briar.headless.json.JsonDict
......@@ -30,3 +32,13 @@ internal fun ContactConnectedEvent.output() = JsonDict(
internal fun ContactDisconnectedEvent.output() = JsonDict(
"contactId" to contactId.int
)
internal fun MessagesAckedEvent.output() = JsonDict(
"contactId" to contactId.int,
"messageIds" to getMessageIds()
)
internal fun MessagesSentEvent.output() = JsonDict(
"contactId" to contactId.int,
"messageIds" to messageIds
)
......@@ -22,9 +22,12 @@ import org.briarproject.bramble.api.db.NoSuchPendingContactException
import org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent
import org.briarproject.bramble.api.sync.event.MessagesAckedEvent
import org.briarproject.bramble.api.sync.event.MessagesSentEvent
import org.briarproject.bramble.identity.output
import org.briarproject.bramble.test.TestUtils.getPendingContact
import org.briarproject.bramble.test.TestUtils.getRandomBytes
import org.briarproject.bramble.test.TestUtils.getRandomId
import org.briarproject.bramble.util.StringUtils.getRandomString
import org.briarproject.briar.headless.ControllerTest
import org.briarproject.briar.headless.json.JsonDict
......@@ -301,6 +304,40 @@ internal class ContactControllerTest : ControllerTest() {
controller.eventOccurred(event)
}
@Test
fun testMessagesAckedEvent() {
val messageId = getRandomId()
val messageIds = new ArrayList<>()
messageIds.add(messageId)
val event = MessagesAckedEvent(contact.id, messageIds)
every {
webSocketController.sendEvent(
EVENT_MESSAGES_ACKED,
event.output()
)
} just runs
controller.eventOccurred(event)
}
@Test
fun testMessagesSentEvent() {
val messageId = getRandomId()
val messageIds = new ArrayList<>()
messageIds.add(messageId)
val event = MessagesSentEvent(contact.id, messageIds)
every {
webSocketController.sendEvent(
EVENT_MESSAGES_SENT,
event.output()
)
} just runs
controller.eventOccurred(event)
}
@Test
fun testOutputContact() {
val connected = Random.nextBoolean()
......@@ -416,4 +453,38 @@ internal class ContactControllerTest : ControllerTest() {
assertJsonEquals(json, event.output())
}
@Test
fun testOutputMessagesAckedEvent() {
val messageId = getRandomId()
val messageIds = new ArrayList<>()
messageIds.add(messageId)
val event = MessagesAckedEvent(contact.id, messageIds)
val json = """
{
"contactId": ${contact.id.int},
"messageIds": [
${toJson(messageId)}
]
}
"""
assertJsonEquals(json, event.output())
}
@Test
fun testOutputMessagesSentEvent() {
val messageId = getRandomId()
val messageIds = new ArrayList<>()
messageIds.add(messageId)
val event = MessagesSentEvent(contact.id, messageIds)
val json = """
{
"contactId": ${contact.id.int},
"messageIds": [
${toJson(messageId)}
]
}
"""
assertJsonEquals(json, event.output())
}
}
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