Verified Commit 0bb80b1a authored by akwizgran's avatar akwizgran Committed by Torsten Grote

Add JsonDict class for JSON output.

parent bda52ea5
package org.briarproject.bramble.identity
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,
"id" to id.bytes,
"name" to name,
......
......@@ -2,8 +2,9 @@ package org.briarproject.briar.headless.contact
import org.briarproject.bramble.api.contact.Contact
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,
"author" to author.output(),
"verified" to isVerified
......
package org.briarproject.briar.headless.forums
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,
"id" to id.bytes
)
......
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.briar.api.messaging.PrivateMessage
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> {
val map: HashMap<String, Any> = hashMapOf(
internal fun PrivateMessageHeader.output(contactId: ContactId, body: String?): JsonDict {
val dict = JsonDict(
"type" to "org.briarproject.briar.api.messaging.PrivateMessageHeader",
"contactId" to contactId.int,
"timestamp" to timestamp,
......@@ -16,11 +17,11 @@ internal fun PrivateMessageHeader.output(contactId: ContactId, body: String?): M
"id" to id.bytes,
"groupId" to groupId.bytes
)
if (body != null) map.put("body", body)
return map
if (body != null) dict.put("body", body)
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",
"contactId" to contactId.int,
"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.privategroup.invitation.GroupInvitationRequest
import org.briarproject.briar.api.sharing.InvitationRequest
import org.briarproject.briar.headless.json.JsonDict
internal fun PrivateRequest<*>.output(contactId: ContactId): Map<String, Any> {
val map: HashMap<String, Any> = hashMapOf(
internal fun PrivateRequest<*>.output(contactId: ContactId): JsonDict {
val dict = (this as PrivateMessageHeader).output(contactId, null)
dict.putAll(
"sessionId" to sessionId.bytes,
"name" to name,
"answered" to wasAnswered()
)
map.putAll((this as PrivateMessageHeader).output(contactId, null))
return map
return dict
}
internal fun IntroductionRequest.output(contactId: ContactId): Map<String, Any> {
val map: HashMap<String, Any> = hashMapOf(
internal fun IntroductionRequest.output(contactId: ContactId): JsonDict {
val dict = (this as PrivateRequest<*>).output(contactId)
dict.putAll(
"type" to "org.briarproject.briar.api.introduction.IntroductionRequest",
"alreadyContact" to isContact
)
map.putAll((this as PrivateRequest<*>).output(contactId))
return map
return dict
}
internal fun InvitationRequest<*>.output(contactId : ContactId): Map<String, Any> {
val map: HashMap<String, Any> = hashMapOf("canBeOpened" to canBeOpened())
map.putAll((this as PrivateRequest<*>).output(contactId))
return map
internal fun InvitationRequest<*>.output(contactId: ContactId): JsonDict {
val dict = (this as PrivateRequest<*>).output(contactId)
dict.put("canBeOpened", canBeOpened())
return dict
}
internal fun BlogInvitationRequest.output(contactId : ContactId): Map<String, Any> {
val map: HashMap<String, Any> = hashMapOf(
"type" to "org.briarproject.briar.api.blog.BlogInvitationRequest"
)
map.putAll((this as InvitationRequest<*>).output(contactId))
return map
internal fun BlogInvitationRequest.output(contactId: ContactId): JsonDict {
val dict = (this as InvitationRequest<*>).output(contactId)
dict.put("type", "org.briarproject.briar.api.blog.BlogInvitationRequest")
return dict
}
internal fun ForumInvitationRequest.output(contactId: ContactId): Map<String, Any> {
val map: HashMap<String, Any> = hashMapOf(
"type" to "org.briarproject.briar.api.forum.ForumInvitationRequest"
)
map.putAll((this as InvitationRequest<*>).output(contactId))
return map
internal fun ForumInvitationRequest.output(contactId: ContactId): JsonDict {
val dict = (this as InvitationRequest<*>).output(contactId)
dict.put("type", "org.briarproject.briar.api.forum.ForumInvitationRequest")
return dict
}
internal fun GroupInvitationRequest.output(contactId : ContactId): Map<String, Any> {
val map: HashMap<String, Any> = hashMapOf(
"type" to "org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest"
)
map.putAll((this as InvitationRequest<*>).output(contactId))
return map
internal fun GroupInvitationRequest.output(contactId: ContactId): JsonDict {
val dict = (this as InvitationRequest<*>).output(contactId)
dict.put("type", "org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest")
return dict
}
\ No newline at end of file
......@@ -11,52 +11,47 @@ import org.briarproject.briar.api.messaging.PrivateMessageHeader
import org.briarproject.briar.api.messaging.PrivateResponse
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse
import org.briarproject.briar.api.sharing.InvitationResponse
import org.briarproject.briar.headless.json.JsonDict
internal fun PrivateResponse.output(contactId: ContactId): Map<String, Any> {
val map: HashMap<String, Any> = hashMapOf(
internal fun PrivateResponse.output(contactId: ContactId): JsonDict {
val dict = (this as PrivateMessageHeader).output(contactId, null)
dict.putAll(
"sessionId" to sessionId.bytes,
"accepted" to wasAccepted()
)
map.putAll((this as PrivateMessageHeader).output(contactId, null))
return map
return dict
}
internal fun IntroductionResponse.output(contactId: ContactId): Map<String, Any> {
val map: HashMap<String, Any> = hashMapOf(
internal fun IntroductionResponse.output(contactId: ContactId): JsonDict {
val dict = (this as PrivateResponse).output(contactId)
dict.putAll(
"type" to "org.briarproject.briar.api.introduction.IntroductionResponse",
"introducedAuthor" to introducedAuthor.output(),
"introducer" to isIntroducer
)
map.putAll((this as PrivateResponse).output(contactId))
return map
return dict
}
internal fun InvitationResponse.output(contactId: ContactId): Map<String, Any> {
val map: HashMap<String, Any> = hashMapOf("shareableId" to shareableId.bytes)
map.putAll((this as PrivateResponse).output(contactId))
return map
internal fun InvitationResponse.output(contactId: ContactId): JsonDict {
val dict = (this as PrivateResponse).output(contactId)
dict.put("shareableId", shareableId.bytes)
return dict
}
internal fun BlogInvitationResponse.output(contactId: ContactId): Map<String, Any> {
val map: HashMap<String, Any> = hashMapOf(
"type" to "org.briarproject.briar.api.blog.BlogInvitationResponse"
)
map.putAll((this as InvitationResponse).output(contactId))
return map
internal fun BlogInvitationResponse.output(contactId: ContactId): JsonDict {
val dict = (this as InvitationResponse).output(contactId)
dict.put("type", "org.briarproject.briar.api.blog.BlogInvitationResponse")
return dict
}
internal fun ForumInvitationResponse.output(contactId: ContactId): Map<String, Any> {
val map: HashMap<String, Any> = hashMapOf(
"type" to "org.briarproject.briar.api.blog.BlogInvitationResponse"
)
map.putAll((this as InvitationResponse).output(contactId))
return map
internal fun ForumInvitationResponse.output(contactId: ContactId): JsonDict {
val dict = (this as InvitationResponse).output(contactId)
dict.put("type", "org.briarproject.briar.api.blog.BlogInvitationResponse")
return dict
}
internal fun GroupInvitationResponse.output(contactId: ContactId): Map<String, Any> {
val map: HashMap<String, Any> = hashMapOf(
"type" to "org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse"
)
map.putAll((this as InvitationResponse).output(contactId))
return map
internal fun GroupInvitationResponse.output(contactId: ContactId): JsonDict {
val dict = (this as InvitationResponse).output(contactId)
dict.put("type", "org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse")
return dict
}
\ No newline at end of file
......@@ -15,6 +15,7 @@ import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
import org.briarproject.briar.headless.ControllerTest
import org.briarproject.briar.headless.event.WebSocketController
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.assertThrows
import org.junit.jupiter.api.Test
......@@ -71,9 +72,9 @@ internal class MessagingControllerImplTest : ControllerTest() {
}
@Test
fun write() {
fun listPrivateMessage() {
val privateMessage = PrivateMessage(message)
val slot = CapturingSlot<Map<String, Any>>()
val slot = CapturingSlot<JsonDict>()
expectGetContact()
every { ctx.formParam("text") } returns body
......@@ -95,6 +96,8 @@ internal class MessagingControllerImplTest : ControllerTest() {
assertEquals(contact.id.int, output.get("contactId"))
assertEquals(body, output.get("body"))
assertEquals(message.id.bytes, output.get("id"))
assertEquals("org.briarproject.briar.api.messaging.PrivateMessageHeader",
output.get("type"))
}
@Test
......
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