Merge branch '1782-delete-all-messages' into 'master'

Add method to delete all private messages to REST API

Closes #1782

See merge request briar/briar!1287
This commit is contained in:
akwizgran
2020-10-13 15:46:01 +00:00
6 changed files with 80 additions and 0 deletions

View File

@@ -247,6 +247,12 @@ needs to be provided in the request body as follows:
}
```
### Deleting all private messages
`DELETE /v1/messages/{contactId}/all`
It returns with a status code `200`, if removal was successful.
### Listing blog posts
`GET /v1/blogs/posts`

View File

@@ -89,6 +89,9 @@ constructor(
path("/messages/:contactId/read") {
post { ctx -> messagingController.markMessageRead(ctx) }
}
path("/messages/:contactId/all") {
delete { ctx -> messagingController.deleteAllMessages(ctx) }
}
path("/forums") {
get { ctx -> forumController.list(ctx) }
post { ctx -> forumController.create(ctx) }

View File

@@ -10,4 +10,6 @@ interface MessagingController {
fun markMessageRead(ctx: Context): Context
fun deleteAllMessages(ctx: Context): Context
}

View File

@@ -106,6 +106,16 @@ constructor(
return MessageId(idBytes)
}
override fun deleteAllMessages(ctx: Context): Context {
val contactId = ctx.getContactIdFromPathParam()
try {
val result = conversationManager.deleteAllMessages(contactId)
return ctx.json(result.output())
} catch (e: NoSuchContactException) {
throw NotFoundResponse()
}
}
override fun eventOccurred(e: Event) {
when (e) {
is ConversationMessageReceivedEvent<*> -> {

View File

@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.sync.MessageId
import org.briarproject.bramble.api.sync.event.MessagesAckedEvent
import org.briarproject.bramble.api.sync.event.MessagesSentEvent
import org.briarproject.briar.api.conversation.ConversationMessageHeader
import org.briarproject.briar.api.conversation.DeletionResult
import org.briarproject.briar.api.messaging.PrivateMessage
import org.briarproject.briar.api.messaging.PrivateMessageHeader
import org.briarproject.briar.headless.json.JsonDict
@@ -47,6 +48,15 @@ internal fun PrivateMessage.output(contactId: ContactId, text: String) = JsonDic
"text" to text
)
internal fun DeletionResult.output() = JsonDict(
"allDeleted" to allDeleted(),
"hasIntroductionSessionInProgress" to hasIntroductionSessionInProgress(),
"hasInvitationSessionInProgress" to hasInvitationSessionInProgress(),
"hasNotAllIntroductionSelected" to hasNotAllIntroductionSelected(),
"hasNotAllInvitationSelected" to hasNotAllInvitationSelected(),
"hasNotFullyDownloaded" to hasNotFullyDownloaded()
)
internal fun MessagesAckedEvent.output() = JsonDict(
"contactId" to contactId.int,
"messageIds" to messageIds.toJson()

View File

@@ -17,6 +17,7 @@ import org.briarproject.bramble.test.ImmediateExecutor
import org.briarproject.bramble.test.TestUtils.getRandomId
import org.briarproject.bramble.util.StringUtils.getRandomString
import org.briarproject.briar.api.client.SessionId
import org.briarproject.briar.api.conversation.DeletionResult
import org.briarproject.briar.api.introduction.IntroductionRequest
import org.briarproject.briar.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_TEXT_LENGTH
import org.briarproject.briar.api.messaging.MessagingManager
@@ -32,6 +33,7 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Test
import org.spongycastle.util.encoders.Base64
import kotlin.random.Random
internal class MessagingControllerImplTest : ControllerTest() {
@@ -343,6 +345,53 @@ internal class MessagingControllerImplTest : ControllerTest() {
assertJsonEquals(json, request.output(contact.id))
}
@Test
fun testDeleteAllMessages() {
val result = DeletionResult()
every { ctx.pathParam("contactId") } returns "1"
every { conversationManager.deleteAllMessages(ContactId(1)) } returns result
every { ctx.json(result.output()) } returns ctx
controller.deleteAllMessages(ctx)
}
@Test
fun testDeleteAllMessagesInvalidContactId() {
every { ctx.pathParam("contactId") } returns "foo"
assertThrows(NotFoundResponse::class.java) {
controller.deleteAllMessages(ctx)
}
}
@Test
fun testDeleteAllMessagesNonexistentContactId() {
every { ctx.pathParam("contactId") } returns "1"
every { conversationManager.deleteAllMessages(ContactId(1)) } throws NoSuchContactException()
assertThrows(NotFoundResponse::class.java) {
controller.deleteAllMessages(ctx)
}
}
@Test
fun testOutputDeletionResult() {
val result = DeletionResult()
if (Random.nextBoolean()) result.addInvitationNotAllSelected()
if (Random.nextBoolean()) result.addInvitationSessionInProgress()
if (Random.nextBoolean()) result.addIntroductionNotAllSelected()
if (Random.nextBoolean()) result.addIntroductionSessionInProgress()
if (Random.nextBoolean()) result.addNotFullyDownloaded()
val json = """
{
"allDeleted": ${result.allDeleted()},
"hasIntroductionSessionInProgress": ${result.hasIntroductionSessionInProgress()},
"hasInvitationSessionInProgress": ${result.hasInvitationSessionInProgress()},
"hasNotAllIntroductionSelected": ${result.hasNotAllIntroductionSelected()},
"hasNotAllInvitationSelected": ${result.hasNotAllInvitationSelected()},
"hasNotFullyDownloaded": ${result.hasNotFullyDownloaded()}
}
"""
assertJsonEquals(json, result.output())
}
private fun expectGetContact() {
every { ctx.pathParam("contactId") } returns contact.id.int.toString()
every { contactManager.getContact(contact.id) } returns contact