mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 14:19:53 +01:00
Add JsonDict class for JSON output.
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
package org.briarproject.bramble.identity
|
package org.briarproject.bramble.identity
|
||||||
|
|
||||||
import org.briarproject.bramble.api.identity.Author
|
import org.briarproject.bramble.api.identity.Author
|
||||||
|
import org.briarproject.briar.headless.json.JsonDict
|
||||||
|
|
||||||
fun Author.output() = mapOf(
|
fun Author.output() = JsonDict(
|
||||||
"formatVersion" to formatVersion,
|
"formatVersion" to formatVersion,
|
||||||
"id" to id.bytes,
|
"id" to id.bytes,
|
||||||
"name" to name,
|
"name" to name,
|
||||||
|
|||||||
@@ -2,8 +2,9 @@ package org.briarproject.briar.headless.contact
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.contact.Contact
|
import org.briarproject.bramble.api.contact.Contact
|
||||||
import org.briarproject.bramble.identity.output
|
import org.briarproject.bramble.identity.output
|
||||||
|
import org.briarproject.briar.headless.json.JsonDict
|
||||||
|
|
||||||
internal fun Contact.output() = mapOf(
|
internal fun Contact.output() = JsonDict(
|
||||||
"contactId" to id.int,
|
"contactId" to id.int,
|
||||||
"author" to author.output(),
|
"author" to author.output(),
|
||||||
"verified" to isVerified
|
"verified" to isVerified
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package org.briarproject.briar.headless.forums
|
package org.briarproject.briar.headless.forums
|
||||||
|
|
||||||
import org.briarproject.briar.api.forum.Forum
|
import org.briarproject.briar.api.forum.Forum
|
||||||
|
import org.briarproject.briar.headless.json.JsonDict
|
||||||
|
|
||||||
internal fun Forum.output() = mapOf(
|
internal fun Forum.output() = JsonDict(
|
||||||
"name" to name,
|
"name" to name,
|
||||||
"id" to id.bytes
|
"id" to id.bytes
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package org.briarproject.briar.headless.json
|
||||||
|
|
||||||
|
class JsonDict(vararg pairs: Pair<String, Any>) : HashMap<String, Any>(pairs.size) {
|
||||||
|
init {
|
||||||
|
putAll(pairs)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun putAll(vararg pairs: Pair<String, Any>) {
|
||||||
|
for (p in pairs) put(p.first, p.second)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,9 +3,10 @@ package org.briarproject.briar.headless.messaging
|
|||||||
import org.briarproject.bramble.api.contact.ContactId
|
import org.briarproject.bramble.api.contact.ContactId
|
||||||
import org.briarproject.briar.api.messaging.PrivateMessage
|
import org.briarproject.briar.api.messaging.PrivateMessage
|
||||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader
|
import org.briarproject.briar.api.messaging.PrivateMessageHeader
|
||||||
|
import org.briarproject.briar.headless.json.JsonDict
|
||||||
|
|
||||||
internal fun PrivateMessageHeader.output(contactId: ContactId, body: String?): Map<String, Any> {
|
internal fun PrivateMessageHeader.output(contactId: ContactId, body: String?): JsonDict {
|
||||||
val map: HashMap<String, Any> = hashMapOf(
|
val dict = JsonDict(
|
||||||
"type" to "org.briarproject.briar.api.messaging.PrivateMessageHeader",
|
"type" to "org.briarproject.briar.api.messaging.PrivateMessageHeader",
|
||||||
"contactId" to contactId.int,
|
"contactId" to contactId.int,
|
||||||
"timestamp" to timestamp,
|
"timestamp" to timestamp,
|
||||||
@@ -16,11 +17,11 @@ internal fun PrivateMessageHeader.output(contactId: ContactId, body: String?): M
|
|||||||
"id" to id.bytes,
|
"id" to id.bytes,
|
||||||
"groupId" to groupId.bytes
|
"groupId" to groupId.bytes
|
||||||
)
|
)
|
||||||
if (body != null) map.put("body", body)
|
if (body != null) dict.put("body", body)
|
||||||
return map
|
return dict
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun PrivateMessage.output(contactId: ContactId, body: String) = mapOf(
|
internal fun PrivateMessage.output(contactId: ContactId, body: String) = JsonDict(
|
||||||
"type" to "org.briarproject.briar.api.messaging.PrivateMessageHeader",
|
"type" to "org.briarproject.briar.api.messaging.PrivateMessageHeader",
|
||||||
"contactId" to contactId.int,
|
"contactId" to contactId.int,
|
||||||
"timestamp" to message.timestamp,
|
"timestamp" to message.timestamp,
|
||||||
|
|||||||
@@ -10,52 +10,47 @@ import org.briarproject.briar.api.messaging.PrivateMessageHeader
|
|||||||
import org.briarproject.briar.api.messaging.PrivateRequest
|
import org.briarproject.briar.api.messaging.PrivateRequest
|
||||||
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest
|
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest
|
||||||
import org.briarproject.briar.api.sharing.InvitationRequest
|
import org.briarproject.briar.api.sharing.InvitationRequest
|
||||||
|
import org.briarproject.briar.headless.json.JsonDict
|
||||||
|
|
||||||
internal fun PrivateRequest<*>.output(contactId: ContactId): Map<String, Any> {
|
internal fun PrivateRequest<*>.output(contactId: ContactId): JsonDict {
|
||||||
val map: HashMap<String, Any> = hashMapOf(
|
val dict = (this as PrivateMessageHeader).output(contactId, null)
|
||||||
|
dict.putAll(
|
||||||
"sessionId" to sessionId.bytes,
|
"sessionId" to sessionId.bytes,
|
||||||
"name" to name,
|
"name" to name,
|
||||||
"answered" to wasAnswered()
|
"answered" to wasAnswered()
|
||||||
)
|
)
|
||||||
map.putAll((this as PrivateMessageHeader).output(contactId, null))
|
return dict
|
||||||
return map
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun IntroductionRequest.output(contactId: ContactId): Map<String, Any> {
|
internal fun IntroductionRequest.output(contactId: ContactId): JsonDict {
|
||||||
val map: HashMap<String, Any> = hashMapOf(
|
val dict = (this as PrivateRequest<*>).output(contactId)
|
||||||
|
dict.putAll(
|
||||||
"type" to "org.briarproject.briar.api.introduction.IntroductionRequest",
|
"type" to "org.briarproject.briar.api.introduction.IntroductionRequest",
|
||||||
"alreadyContact" to isContact
|
"alreadyContact" to isContact
|
||||||
)
|
)
|
||||||
map.putAll((this as PrivateRequest<*>).output(contactId))
|
return dict
|
||||||
return map
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun InvitationRequest<*>.output(contactId : ContactId): Map<String, Any> {
|
internal fun InvitationRequest<*>.output(contactId: ContactId): JsonDict {
|
||||||
val map: HashMap<String, Any> = hashMapOf("canBeOpened" to canBeOpened())
|
val dict = (this as PrivateRequest<*>).output(contactId)
|
||||||
map.putAll((this as PrivateRequest<*>).output(contactId))
|
dict.put("canBeOpened", canBeOpened())
|
||||||
return map
|
return dict
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun BlogInvitationRequest.output(contactId : ContactId): Map<String, Any> {
|
internal fun BlogInvitationRequest.output(contactId: ContactId): JsonDict {
|
||||||
val map: HashMap<String, Any> = hashMapOf(
|
val dict = (this as InvitationRequest<*>).output(contactId)
|
||||||
"type" to "org.briarproject.briar.api.blog.BlogInvitationRequest"
|
dict.put("type", "org.briarproject.briar.api.blog.BlogInvitationRequest")
|
||||||
)
|
return dict
|
||||||
map.putAll((this as InvitationRequest<*>).output(contactId))
|
|
||||||
return map
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun ForumInvitationRequest.output(contactId: ContactId): Map<String, Any> {
|
internal fun ForumInvitationRequest.output(contactId: ContactId): JsonDict {
|
||||||
val map: HashMap<String, Any> = hashMapOf(
|
val dict = (this as InvitationRequest<*>).output(contactId)
|
||||||
"type" to "org.briarproject.briar.api.forum.ForumInvitationRequest"
|
dict.put("type", "org.briarproject.briar.api.forum.ForumInvitationRequest")
|
||||||
)
|
return dict
|
||||||
map.putAll((this as InvitationRequest<*>).output(contactId))
|
|
||||||
return map
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun GroupInvitationRequest.output(contactId : ContactId): Map<String, Any> {
|
internal fun GroupInvitationRequest.output(contactId: ContactId): JsonDict {
|
||||||
val map: HashMap<String, Any> = hashMapOf(
|
val dict = (this as InvitationRequest<*>).output(contactId)
|
||||||
"type" to "org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest"
|
dict.put("type", "org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest")
|
||||||
)
|
return dict
|
||||||
map.putAll((this as InvitationRequest<*>).output(contactId))
|
|
||||||
return map
|
|
||||||
}
|
}
|
||||||
@@ -11,52 +11,47 @@ import org.briarproject.briar.api.messaging.PrivateMessageHeader
|
|||||||
import org.briarproject.briar.api.messaging.PrivateResponse
|
import org.briarproject.briar.api.messaging.PrivateResponse
|
||||||
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse
|
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse
|
||||||
import org.briarproject.briar.api.sharing.InvitationResponse
|
import org.briarproject.briar.api.sharing.InvitationResponse
|
||||||
|
import org.briarproject.briar.headless.json.JsonDict
|
||||||
|
|
||||||
internal fun PrivateResponse.output(contactId: ContactId): Map<String, Any> {
|
internal fun PrivateResponse.output(contactId: ContactId): JsonDict {
|
||||||
val map: HashMap<String, Any> = hashMapOf(
|
val dict = (this as PrivateMessageHeader).output(contactId, null)
|
||||||
|
dict.putAll(
|
||||||
"sessionId" to sessionId.bytes,
|
"sessionId" to sessionId.bytes,
|
||||||
"accepted" to wasAccepted()
|
"accepted" to wasAccepted()
|
||||||
)
|
)
|
||||||
map.putAll((this as PrivateMessageHeader).output(contactId, null))
|
return dict
|
||||||
return map
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun IntroductionResponse.output(contactId: ContactId): Map<String, Any> {
|
internal fun IntroductionResponse.output(contactId: ContactId): JsonDict {
|
||||||
val map: HashMap<String, Any> = hashMapOf(
|
val dict = (this as PrivateResponse).output(contactId)
|
||||||
|
dict.putAll(
|
||||||
"type" to "org.briarproject.briar.api.introduction.IntroductionResponse",
|
"type" to "org.briarproject.briar.api.introduction.IntroductionResponse",
|
||||||
"introducedAuthor" to introducedAuthor.output(),
|
"introducedAuthor" to introducedAuthor.output(),
|
||||||
"introducer" to isIntroducer
|
"introducer" to isIntroducer
|
||||||
)
|
)
|
||||||
map.putAll((this as PrivateResponse).output(contactId))
|
return dict
|
||||||
return map
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun InvitationResponse.output(contactId: ContactId): Map<String, Any> {
|
internal fun InvitationResponse.output(contactId: ContactId): JsonDict {
|
||||||
val map: HashMap<String, Any> = hashMapOf("shareableId" to shareableId.bytes)
|
val dict = (this as PrivateResponse).output(contactId)
|
||||||
map.putAll((this as PrivateResponse).output(contactId))
|
dict.put("shareableId", shareableId.bytes)
|
||||||
return map
|
return dict
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun BlogInvitationResponse.output(contactId: ContactId): Map<String, Any> {
|
internal fun BlogInvitationResponse.output(contactId: ContactId): JsonDict {
|
||||||
val map: HashMap<String, Any> = hashMapOf(
|
val dict = (this as InvitationResponse).output(contactId)
|
||||||
"type" to "org.briarproject.briar.api.blog.BlogInvitationResponse"
|
dict.put("type", "org.briarproject.briar.api.blog.BlogInvitationResponse")
|
||||||
)
|
return dict
|
||||||
map.putAll((this as InvitationResponse).output(contactId))
|
|
||||||
return map
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun ForumInvitationResponse.output(contactId: ContactId): Map<String, Any> {
|
internal fun ForumInvitationResponse.output(contactId: ContactId): JsonDict {
|
||||||
val map: HashMap<String, Any> = hashMapOf(
|
val dict = (this as InvitationResponse).output(contactId)
|
||||||
"type" to "org.briarproject.briar.api.blog.BlogInvitationResponse"
|
dict.put("type", "org.briarproject.briar.api.blog.BlogInvitationResponse")
|
||||||
)
|
return dict
|
||||||
map.putAll((this as InvitationResponse).output(contactId))
|
|
||||||
return map
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun GroupInvitationResponse.output(contactId: ContactId): Map<String, Any> {
|
internal fun GroupInvitationResponse.output(contactId: ContactId): JsonDict {
|
||||||
val map: HashMap<String, Any> = hashMapOf(
|
val dict = (this as InvitationResponse).output(contactId)
|
||||||
"type" to "org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse"
|
dict.put("type", "org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse")
|
||||||
)
|
return dict
|
||||||
map.putAll((this as InvitationResponse).output(contactId))
|
|
||||||
return map
|
|
||||||
}
|
}
|
||||||
@@ -15,6 +15,7 @@ import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
|
|||||||
import org.briarproject.briar.headless.ControllerTest
|
import org.briarproject.briar.headless.ControllerTest
|
||||||
import org.briarproject.briar.headless.event.WebSocketController
|
import org.briarproject.briar.headless.event.WebSocketController
|
||||||
import org.briarproject.briar.headless.event.output
|
import org.briarproject.briar.headless.event.output
|
||||||
|
import org.briarproject.briar.headless.json.JsonDict
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Assertions.assertThrows
|
import org.junit.jupiter.api.Assertions.assertThrows
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
@@ -71,9 +72,9 @@ internal class MessagingControllerImplTest : ControllerTest() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun write() {
|
fun listPrivateMessage() {
|
||||||
val privateMessage = PrivateMessage(message)
|
val privateMessage = PrivateMessage(message)
|
||||||
val slot = CapturingSlot<Map<String, Any>>()
|
val slot = CapturingSlot<JsonDict>()
|
||||||
|
|
||||||
expectGetContact()
|
expectGetContact()
|
||||||
every { ctx.formParam("text") } returns body
|
every { ctx.formParam("text") } returns body
|
||||||
@@ -95,6 +96,8 @@ internal class MessagingControllerImplTest : ControllerTest() {
|
|||||||
assertEquals(contact.id.int, output.get("contactId"))
|
assertEquals(contact.id.int, output.get("contactId"))
|
||||||
assertEquals(body, output.get("body"))
|
assertEquals(body, output.get("body"))
|
||||||
assertEquals(message.id.bytes, output.get("id"))
|
assertEquals(message.id.bytes, output.get("id"))
|
||||||
|
assertEquals("org.briarproject.briar.api.messaging.PrivateMessageHeader",
|
||||||
|
output.get("type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user