diff --git a/briar-headless/README.md b/briar-headless/README.md index 5edb4e927..a27d9dc3d 100644 --- a/briar-headless/README.md +++ b/briar-headless/README.md @@ -41,7 +41,7 @@ You can test that things work as expected by running: [] The answer is an empty JSON array, because you don't have any contacts. -Note you the HTTP request sets an `Authorization` header with the bearer token. +Note that the HTTP request sets an `Authorization` header with the bearer token. A missing or wrong token will result in a `401` response. ## REST API @@ -55,6 +55,7 @@ Returns a JSON array of contacts: ```json { "author": { + "formatVersion": 1, "id": "y1wkIzAimAbYoCGgWxkWlr6vnq1F8t1QRA/UMPgI0E0=", "name": "Test", "publicKey": "BDu6h1S02bF4W6rgoZfZ6BMjTj/9S9hNN7EQoV05qUo=" @@ -80,7 +81,6 @@ It returns a JSON array of private messages: ```json { - "body": "test", "contactId": 1, "groupId": "oRRvCri85UE2XGcSloAKt/u8JDcMkmDc26SOMouxr4U=", "id": "ZGDrlpCxO9v7doO4Bmijh95QqQDykaS4Oji/mZVMIJ8=", @@ -88,6 +88,7 @@ It returns a JSON array of private messages: "read": true, "seen": true, "sent": true, + "text": "test", "timestamp": 1537376633850, "type": "PrivateMessage" } @@ -95,7 +96,7 @@ It returns a JSON array of private messages: If `local` is `true`, the message was sent by the Briar peer instead of its remote contact. -Attention: There can messages of other `type`s where the message `body` is `null`. +Attention: There can messages of other `type`s where the message `text` is `null`. ### Writing a private message @@ -118,16 +119,17 @@ Returns a JSON array of blog posts: ```json { "author": { + "formatVersion": 1, "id": "VNKXkaERPpXmZuFbHHwYT6Qc148D+KNNxQ4hwtx7Kq4=", "name": "Test", "publicKey": "NbwpQWjS3gHMjjDQIASIy/j+bU6NRZnSRT8X8FKDoN4=" }, "authorStatus": "ourselves", - "body": "Test Post Content", "id": "X1jmHaYfrX47kT5OEd0OD+p/bptyR92IvuOBYSgxETM=", "parentId": null, "read": true, "rssFeed": false, + "text": "Test Post Content", "timestamp": 1535397886749, "timestampReceived": 1535397886749, "type": "post" @@ -152,8 +154,8 @@ The Briar peer uses a websocket to notify a connected API client about new event `WS /v1/ws` -The authentication token needs to be provided with basic auth as the username -while the password can be left empty. +The websocket request must use basic auth, +with the authentication token as the username and a blank password. You can test connecting to the websocket with curl: @@ -175,7 +177,6 @@ it will send a JSON object to connected websocket clients: ```json { "data": { - "body": "Test Message", "contactId": 1, "groupId": "oRRvCri85UE2XGcSloAKt/u8JDcMkmDc26SOMouxr4U=", "id": "JBc+ogQIok/yr+7XtxN2iQgNfzw635mHikNaP5QOEVs=", @@ -183,6 +184,7 @@ it will send a JSON object to connected websocket clients: "read": false, "seen": false, "sent": false, + "text": "Test Message", "timestamp": 1537389146088, "type": "PrivateMessage" }, diff --git a/briar-headless/src/main/java/org/briarproject/bramble/identity/OutputAuthor.kt b/briar-headless/src/main/java/org/briarproject/bramble/identity/OutputAuthor.kt index 049537cb5..2b3b4bc5b 100644 --- a/briar-headless/src/main/java/org/briarproject/bramble/identity/OutputAuthor.kt +++ b/briar-headless/src/main/java/org/briarproject/bramble/identity/OutputAuthor.kt @@ -11,4 +11,3 @@ fun Author.output() = JsonDict( ) fun Author.Status.output() = name.toLowerCase() - diff --git a/briar-headless/src/main/java/org/briarproject/briar/headless/Main.kt b/briar-headless/src/main/java/org/briarproject/briar/headless/Main.kt index 2940eb856..14a18e7ec 100644 --- a/briar-headless/src/main/java/org/briarproject/briar/headless/Main.kt +++ b/briar-headless/src/main/java/org/briarproject/briar/headless/Main.kt @@ -27,7 +27,7 @@ private val DEFAULT_DATA_DIR = getProperty("user.home") + separator + ".briar" private class Main : CliktCommand( name = "briar-headless", - help = "A Briar client without GUI that exposes a REST and Websocket API" + help = "A Briar peer without GUI that exposes a REST and Websocket API" ) { private val debug by option("--debug", "-d", help = "Enable printing of debug messages").flag( default = false diff --git a/briar-headless/src/main/java/org/briarproject/briar/headless/blogs/OutputBlogPost.kt b/briar-headless/src/main/java/org/briarproject/briar/headless/blogs/OutputBlogPost.kt index 55466c108..65d3f1296 100644 --- a/briar-headless/src/main/java/org/briarproject/briar/headless/blogs/OutputBlogPost.kt +++ b/briar-headless/src/main/java/org/briarproject/briar/headless/blogs/OutputBlogPost.kt @@ -6,7 +6,7 @@ import org.briarproject.briar.api.blog.MessageType import org.briarproject.briar.headless.json.JsonDict internal fun BlogPostHeader.output(body: String) = JsonDict( - "body" to body, + "text" to body, "author" to author.output(), "authorStatus" to authorStatus.output(), "type" to type.output(), diff --git a/briar-headless/src/main/java/org/briarproject/briar/headless/forums/ForumControllerImpl.kt b/briar-headless/src/main/java/org/briarproject/briar/headless/forums/ForumControllerImpl.kt index a1e0ad0a1..af1ab391a 100644 --- a/briar-headless/src/main/java/org/briarproject/briar/headless/forums/ForumControllerImpl.kt +++ b/briar-headless/src/main/java/org/briarproject/briar/headless/forums/ForumControllerImpl.kt @@ -21,7 +21,7 @@ constructor(private val forumManager: ForumManager) : ForumController { } override fun create(ctx: Context): Context { - val name = ctx.getFromJson("text") + val name = ctx.getFromJson("name") if (StringUtils.utf8IsTooLong(name, MAX_FORUM_NAME_LENGTH)) throw BadRequestResponse("Forum name is too long") return ctx.json(forumManager.addForum(name).output()) diff --git a/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateMessage.kt b/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateMessage.kt index f68a03289..dcefdc13e 100644 --- a/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateMessage.kt +++ b/briar-headless/src/main/java/org/briarproject/briar/headless/messaging/OutputPrivateMessage.kt @@ -19,19 +19,22 @@ internal fun PrivateMessageHeader.output(contactId: ContactId) = JsonDict( internal fun PrivateMessageHeader.output(contactId: ContactId, body: String?): JsonDict { val dict = output(contactId) - dict["body"] = body + dict["text"] = body return dict } +/** + * Use only for outgoing messages that were just sent + */ internal fun PrivateMessage.output(contactId: ContactId, body: String) = JsonDict( "type" to "PrivateMessage", "contactId" to contactId.int, "timestamp" to message.timestamp, "read" to true, - "seen" to true, - "sent" to true, + "seen" to false, + "sent" to false, "local" to true, "id" to message.id.bytes, "groupId" to message.groupId.bytes, - "body" to body + "text" to body ) diff --git a/briar-headless/src/test/java/org/briarproject/briar/headless/ControllerTest.kt b/briar-headless/src/test/java/org/briarproject/briar/headless/ControllerTest.kt index 2067d9f5f..b0a27244e 100644 --- a/briar-headless/src/test/java/org/briarproject/briar/headless/ControllerTest.kt +++ b/briar-headless/src/test/java/org/briarproject/briar/headless/ControllerTest.kt @@ -15,6 +15,7 @@ import org.briarproject.bramble.api.system.Clock import org.briarproject.bramble.test.TestUtils.* import org.briarproject.bramble.util.StringUtils.getRandomString import org.skyscreamer.jsonassert.JSONAssert.assertEquals +import org.skyscreamer.jsonassert.JSONCompareMode.STRICT import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse @@ -38,7 +39,7 @@ abstract class ControllerTest { protected val timestamp = 42L protected fun assertJsonEquals(json: String, obj: Any) { - assertEquals(json, outputCtx.json(obj).resultString(), false) + assertEquals(json, outputCtx.json(obj).resultString(), STRICT) } } diff --git a/briar-headless/src/test/java/org/briarproject/briar/headless/blogs/BlogControllerTest.kt b/briar-headless/src/test/java/org/briarproject/briar/headless/blogs/BlogControllerTest.kt index b7a9d8c38..48a07025d 100644 --- a/briar-headless/src/test/java/org/briarproject/briar/headless/blogs/BlogControllerTest.kt +++ b/briar-headless/src/test/java/org/briarproject/briar/headless/blogs/BlogControllerTest.kt @@ -102,7 +102,7 @@ internal class BlogControllerTest : ControllerTest() { fun testOutputBlogPost() { val json = """ { - "body": "$body", + "text": "$body", "author": ${toJson(author.output())}, "authorStatus": "ourselves", "type": "post", diff --git a/briar-headless/src/test/java/org/briarproject/briar/headless/contact/ContactControllerTest.kt b/briar-headless/src/test/java/org/briarproject/briar/headless/contact/ContactControllerTest.kt index 8c339d31e..7566c9842 100644 --- a/briar-headless/src/test/java/org/briarproject/briar/headless/contact/ContactControllerTest.kt +++ b/briar-headless/src/test/java/org/briarproject/briar/headless/contact/ContactControllerTest.kt @@ -41,6 +41,7 @@ internal class ContactControllerTest : ControllerTest() { fun testOutputAuthor() { val json = """ { + "formatVersion": 1, "id": ${toJson(author.id.bytes)}, "name": "${author.name}", "publicKey": ${toJson(author.publicKey)} diff --git a/briar-headless/src/test/java/org/briarproject/briar/headless/forums/ForumControllerTest.kt b/briar-headless/src/test/java/org/briarproject/briar/headless/forums/ForumControllerTest.kt index d239bbcb5..70530f3db 100644 --- a/briar-headless/src/test/java/org/briarproject/briar/headless/forums/ForumControllerTest.kt +++ b/briar-headless/src/test/java/org/briarproject/briar/headless/forums/ForumControllerTest.kt @@ -30,7 +30,7 @@ internal class ForumControllerTest : ControllerTest() { @Test fun create() { - every { ctx.body() } returns """{"text": "${forum.name}"}""" + every { ctx.body() } returns """{"name": "${forum.name}"}""" every { forumManager.addForum(forum.name) } returns forum every { ctx.json(forum.output()) } returns ctx @@ -46,14 +46,14 @@ internal class ForumControllerTest : ControllerTest() { @Test fun createEmptyName() { - every { ctx.body() } returns """{"text": ""}""" + every { ctx.body() } returns """{"name": ""}""" assertThrows(BadRequestResponse::class.java) { controller.create(ctx) } } @Test fun createNullName() { - every { ctx.body() } returns """{"text": null}""" + every { ctx.body() } returns """{"name": null}""" assertThrows(BadRequestResponse::class.java) { controller.create(ctx) } } @@ -67,7 +67,7 @@ internal class ForumControllerTest : ControllerTest() { @Test fun createTooLongName() { - every { ctx.body() } returns """{"text": "${getRandomString(MAX_FORUM_NAME_LENGTH + 1)}"}""" + every { ctx.body() } returns """{"name": "${getRandomString(MAX_FORUM_NAME_LENGTH + 1)}"}""" assertThrows(BadRequestResponse::class.java) { controller.create(ctx) } } diff --git a/briar-headless/src/test/java/org/briarproject/briar/headless/messaging/MessagingControllerImplTest.kt b/briar-headless/src/test/java/org/briarproject/briar/headless/messaging/MessagingControllerImplTest.kt index 70c865f69..83e04523c 100644 --- a/briar-headless/src/test/java/org/briarproject/briar/headless/messaging/MessagingControllerImplTest.kt +++ b/briar-headless/src/test/java/org/briarproject/briar/headless/messaging/MessagingControllerImplTest.kt @@ -161,7 +161,7 @@ internal class MessagingControllerImplTest : ControllerTest() { fun testOutputPrivateMessageHeader() { val json = """ { - "body": "$body", + "text": "$body", "type": "PrivateMessage", "timestamp": $timestamp, "groupId": ${toJson(header.groupId.bytes)}, @@ -180,15 +180,15 @@ internal class MessagingControllerImplTest : ControllerTest() { fun testOutputPrivateMessage() { val json = """ { - "body": "$body", + "text": "$body", "type": "PrivateMessage", "timestamp": ${message.timestamp}, "groupId": ${toJson(message.groupId.bytes)}, "contactId": ${contact.id.int}, "local": true, - "seen": true, + "seen": false, "read": true, - "sent": true, + "sent": false, "id": ${toJson(message.id.bytes)} } """ @@ -203,7 +203,7 @@ internal class MessagingControllerImplTest : ControllerTest() { ) val json = """ { - "body": null, + "text": null, "type": "IntroductionRequest", "timestamp": $timestamp, "groupId": ${toJson(request.groupId.bytes)},