mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Use maps for JSON output.
This commit is contained in:
@@ -1,21 +1,13 @@
|
||||
package org.briarproject.bramble.identity
|
||||
|
||||
import org.briarproject.bramble.api.identity.Author
|
||||
import javax.annotation.concurrent.Immutable
|
||||
|
||||
@Immutable
|
||||
data class OutputAuthor(
|
||||
val id: ByteArray,
|
||||
val name: String,
|
||||
val publicKey: ByteArray
|
||||
) {
|
||||
constructor(author: Author) : this(
|
||||
id = author.id.bytes,
|
||||
name = author.name,
|
||||
publicKey = author.publicKey
|
||||
)
|
||||
}
|
||||
|
||||
fun Author.output() = OutputAuthor(this)
|
||||
fun Author.output() = mapOf(
|
||||
"formatVersion" to formatVersion,
|
||||
"id" to id.bytes,
|
||||
"name" to name,
|
||||
"publicKey" to publicKey
|
||||
)
|
||||
|
||||
fun Author.Status.output() = name.toLowerCase()
|
||||
|
||||
|
||||
@@ -23,12 +23,10 @@ internal class BlogControllerImpl
|
||||
) : BlogController {
|
||||
|
||||
override fun listPosts(ctx: Context): Context {
|
||||
val posts = blogManager.blogs.flatMap { blog ->
|
||||
blogManager.getPostHeaders(blog.id).map { header ->
|
||||
val body = blogManager.getPostBody(header.id)
|
||||
header.output(body)
|
||||
}
|
||||
}.sortedBy { it.timestampReceived }
|
||||
val posts = blogManager.blogs
|
||||
.flatMap { blog -> blogManager.getPostHeaders(blog.id) }
|
||||
.sortedBy { it.timeReceived }
|
||||
.map { header -> header.output(blogManager.getPostBody(header.id)) }
|
||||
return ctx.json(posts)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,38 +1,20 @@
|
||||
package org.briarproject.briar.headless.blogs
|
||||
|
||||
import org.briarproject.bramble.identity.OutputAuthor
|
||||
import org.briarproject.bramble.identity.output
|
||||
import org.briarproject.briar.api.blog.BlogPostHeader
|
||||
import org.briarproject.briar.api.blog.MessageType
|
||||
import javax.annotation.concurrent.Immutable
|
||||
|
||||
@Immutable
|
||||
internal data class OutputBlogPost(
|
||||
val body: String,
|
||||
val author: OutputAuthor,
|
||||
val authorStatus: String,
|
||||
val type: String,
|
||||
val id: ByteArray,
|
||||
val parentId: ByteArray?,
|
||||
val read: Boolean,
|
||||
val rssFeed: Boolean,
|
||||
val timestamp: Long,
|
||||
val timestampReceived: Long
|
||||
) {
|
||||
internal constructor(header: BlogPostHeader, body: String) : this(
|
||||
body = body,
|
||||
author = OutputAuthor(header.author),
|
||||
authorStatus = header.authorStatus.output(),
|
||||
type = header.type.output(),
|
||||
id = header.id.bytes,
|
||||
parentId = header.parentId?.bytes,
|
||||
read = header.isRead,
|
||||
rssFeed = header.isRssFeed,
|
||||
timestamp = header.timestamp,
|
||||
timestampReceived = header.timeReceived
|
||||
)
|
||||
}
|
||||
|
||||
internal fun BlogPostHeader.output(body: String) = OutputBlogPost(this, body)
|
||||
internal fun BlogPostHeader.output(body: String) = mapOf(
|
||||
"body" to body,
|
||||
"author" to author.output(),
|
||||
"authorStatus" to authorStatus.output(),
|
||||
"type" to type.output(),
|
||||
"id" to id.bytes,
|
||||
"parentId" to parentId?.bytes,
|
||||
"read" to isRead,
|
||||
"rssFeed" to isRssFeed,
|
||||
"timestamp" to timestamp,
|
||||
"timestampReceived" to timeReceived
|
||||
)
|
||||
|
||||
internal fun MessageType.output() = name.toLowerCase()
|
||||
|
||||
@@ -1,21 +1,10 @@
|
||||
package org.briarproject.briar.headless.contact
|
||||
|
||||
import org.briarproject.bramble.api.contact.Contact
|
||||
import org.briarproject.bramble.identity.OutputAuthor
|
||||
import org.briarproject.bramble.identity.output
|
||||
import javax.annotation.concurrent.Immutable
|
||||
|
||||
@Immutable
|
||||
internal data class OutputContact(
|
||||
val contactId: Int,
|
||||
val author: OutputAuthor,
|
||||
val verified: Boolean
|
||||
) {
|
||||
internal constructor(c: Contact) : this(
|
||||
contactId = c.id.int,
|
||||
author = c.author.output(),
|
||||
verified = c.isVerified
|
||||
)
|
||||
}
|
||||
|
||||
internal fun Contact.output() = OutputContact(this)
|
||||
internal fun Contact.output() = mapOf(
|
||||
"contactId" to id.int,
|
||||
"author" to author.output(),
|
||||
"verified" to isVerified
|
||||
)
|
||||
@@ -1,19 +1,10 @@
|
||||
package org.briarproject.briar.headless.forums
|
||||
|
||||
import org.briarproject.briar.api.forum.Forum
|
||||
import javax.annotation.concurrent.Immutable
|
||||
|
||||
@Immutable
|
||||
internal data class OutputForum(
|
||||
val name: String,
|
||||
val id: ByteArray
|
||||
) {
|
||||
constructor(forum: Forum) : this(
|
||||
name = forum.name,
|
||||
id = forum.id.bytes
|
||||
)
|
||||
}
|
||||
|
||||
internal fun Forum.output() = OutputForum(this)
|
||||
internal fun Forum.output() = mapOf(
|
||||
"name" to name,
|
||||
"id" to id.bytes
|
||||
)
|
||||
|
||||
internal fun Collection<Forum>.output() = map { it.output() }
|
||||
|
||||
@@ -40,16 +40,18 @@ internal class MessagingControllerImpl
|
||||
|
||||
override fun list(ctx: Context): Context {
|
||||
val contact = getContact(ctx)
|
||||
val messages = conversationManager.getMessageHeaders(contact.id).map { header ->
|
||||
when (header) {
|
||||
is PrivateRequest<*> -> header.output(contact.id)
|
||||
is PrivateResponse -> header.output(contact.id)
|
||||
else -> {
|
||||
val body = messagingManager.getMessageBody(header.id)
|
||||
header.output(contact.id, body)
|
||||
val messages = conversationManager.getMessageHeaders(contact.id)
|
||||
.sortedBy { it.timestamp }
|
||||
.map { header ->
|
||||
when (header) {
|
||||
is PrivateRequest<*> -> header.output(contact.id)
|
||||
is PrivateResponse -> header.output(contact.id)
|
||||
else -> {
|
||||
val body = messagingManager.getMessageBody(header.id)
|
||||
header.output(contact.id, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
}.sortedBy { it.timestamp }
|
||||
return ctx.json(messages)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,56 +3,32 @@ 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 javax.annotation.concurrent.Immutable
|
||||
|
||||
@Immutable
|
||||
internal abstract class OutputPrivateMessage(
|
||||
protected open val iHeader: PrivateMessageHeader,
|
||||
protected open val iContactId: ContactId,
|
||||
open val body: String?
|
||||
) {
|
||||
|
||||
abstract val type: String
|
||||
val contactId: Int get() = iContactId.int
|
||||
val timestamp: Long get() = iHeader.timestamp
|
||||
val read: Boolean get() = iHeader.isRead
|
||||
val seen: Boolean get() = iHeader.isSeen
|
||||
val sent: Boolean get() = iHeader.isSent
|
||||
val local: Boolean get() = iHeader.isLocal
|
||||
val id: ByteArray get() = iHeader.id.bytes
|
||||
val groupId: ByteArray get() = iHeader.groupId.bytes
|
||||
|
||||
}
|
||||
|
||||
@Immutable
|
||||
internal data class OutputPrivateMessageHeader(
|
||||
override val iHeader: PrivateMessageHeader,
|
||||
override val iContactId: ContactId,
|
||||
override val body: String?
|
||||
) : OutputPrivateMessage(iHeader, iContactId, body) {
|
||||
|
||||
override val type = "org.briarproject.briar.api.messaging.PrivateMessageHeader"
|
||||
|
||||
/**
|
||||
* Only meant for own [PrivateMessage]s directly after creation.
|
||||
*/
|
||||
internal constructor(m: PrivateMessage, contactId: ContactId, body: String) : this(
|
||||
PrivateMessageHeader(
|
||||
m.message.id,
|
||||
m.message.groupId,
|
||||
m.message.timestamp,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true
|
||||
), contactId, body
|
||||
internal fun PrivateMessageHeader.output(contactId: ContactId, body: String?): Map<String, Any> {
|
||||
val map: HashMap<String, Any> = hashMapOf(
|
||||
"type" to "org.briarproject.briar.api.messaging.PrivateMessageHeader",
|
||||
"contactId" to contactId.int,
|
||||
"timestamp" to timestamp,
|
||||
"read" to isRead,
|
||||
"seen" to isSeen,
|
||||
"sent" to isSent,
|
||||
"local" to isLocal,
|
||||
"id" to id.bytes,
|
||||
"groupId" to groupId.bytes
|
||||
)
|
||||
if (body != null) map.put("body", body)
|
||||
return map
|
||||
}
|
||||
|
||||
internal fun PrivateMessageHeader.output(
|
||||
contactId: ContactId,
|
||||
body: String?
|
||||
) = OutputPrivateMessageHeader(this, contactId, body)
|
||||
|
||||
internal fun PrivateMessage.output(contactId: ContactId, body: String) =
|
||||
OutputPrivateMessageHeader(this, contactId, body)
|
||||
internal fun PrivateMessage.output(contactId: ContactId, body: String) = mapOf(
|
||||
"type" to "org.briarproject.briar.api.messaging.PrivateMessageHeader",
|
||||
"contactId" to contactId.int,
|
||||
"timestamp" to message.timestamp,
|
||||
"read" to true,
|
||||
"seen" to true,
|
||||
"sent" to true,
|
||||
"local" to true,
|
||||
"id" to message.id.bytes,
|
||||
"groupId" to message.groupId.bytes,
|
||||
"body" to body
|
||||
)
|
||||
@@ -6,54 +6,56 @@ import org.briarproject.bramble.api.contact.ContactId
|
||||
import org.briarproject.briar.api.blog.BlogInvitationRequest
|
||||
import org.briarproject.briar.api.forum.ForumInvitationRequest
|
||||
import org.briarproject.briar.api.introduction.IntroductionRequest
|
||||
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 javax.annotation.concurrent.Immutable
|
||||
|
||||
@Immutable
|
||||
internal abstract class OutputPrivateRequest(header: PrivateRequest<*>, contactId: ContactId) :
|
||||
OutputPrivateMessage(header, contactId, header.message) {
|
||||
|
||||
val sessionId: ByteArray = header.sessionId.bytes
|
||||
val name: String = header.name
|
||||
val answered = header.wasAnswered()
|
||||
internal fun PrivateRequest<*>.output(contactId: ContactId): Map<String, Any> {
|
||||
val map: HashMap<String, Any> = hashMapOf(
|
||||
"sessionId" to sessionId.bytes,
|
||||
"name" to name,
|
||||
"answered" to wasAnswered()
|
||||
)
|
||||
map.putAll((this as PrivateMessageHeader).output(contactId, null))
|
||||
return map
|
||||
}
|
||||
|
||||
@Immutable
|
||||
internal data class OutputIntroductionRequest(
|
||||
override val iHeader: IntroductionRequest,
|
||||
override val iContactId: ContactId
|
||||
) : OutputPrivateRequest(iHeader, iContactId) {
|
||||
|
||||
override val type = "org.briarproject.briar.api.introduction.IntroductionRequest"
|
||||
val alreadyContact get() = iHeader.isContact
|
||||
|
||||
internal fun IntroductionRequest.output(contactId: ContactId): Map<String, Any> {
|
||||
val map: HashMap<String, Any> = hashMapOf(
|
||||
"type" to "org.briarproject.briar.api.introduction.IntroductionRequest",
|
||||
"alreadyContact" to isContact
|
||||
)
|
||||
map.putAll((this as PrivateRequest<*>).output(contactId))
|
||||
return map
|
||||
}
|
||||
|
||||
@Immutable
|
||||
internal data class OutputInvitationRequest(
|
||||
override val iHeader: InvitationRequest<*>,
|
||||
override val iContactId: ContactId
|
||||
) : OutputPrivateRequest(iHeader, iContactId) {
|
||||
|
||||
override val type = when (iHeader) {
|
||||
is ForumInvitationRequest -> "org.briarproject.briar.api.forum.ForumInvitationRequest"
|
||||
is BlogInvitationRequest -> "org.briarproject.briar.api.blog.BlogInvitationRequest"
|
||||
is GroupInvitationRequest -> "org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest"
|
||||
else -> throw AssertionError("Unknown InvitationRequest")
|
||||
}
|
||||
val canBeOpened get() = iHeader.canBeOpened()
|
||||
|
||||
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 PrivateRequest<*>.output(contactId: ContactId): OutputPrivateMessage {
|
||||
return when (this) {
|
||||
is IntroductionRequest -> OutputIntroductionRequest(this, contactId)
|
||||
is InvitationRequest -> OutputInvitationRequest(this, contactId)
|
||||
else -> throw AssertionError("Unknown PrivateRequest")
|
||||
}
|
||||
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 IntroductionRequest.output(contactId: ContactId) =
|
||||
OutputIntroductionRequest(this, contactId)
|
||||
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 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
|
||||
}
|
||||
@@ -7,49 +7,56 @@ import org.briarproject.bramble.identity.output
|
||||
import org.briarproject.briar.api.blog.BlogInvitationResponse
|
||||
import org.briarproject.briar.api.forum.ForumInvitationResponse
|
||||
import org.briarproject.briar.api.introduction.IntroductionResponse
|
||||
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 javax.annotation.concurrent.Immutable
|
||||
|
||||
@Immutable
|
||||
internal abstract class OutputPrivateResponse(header: PrivateResponse, contactId: ContactId) :
|
||||
OutputPrivateMessage(header, contactId, null) {
|
||||
|
||||
val sessionId: ByteArray = header.sessionId.bytes
|
||||
val accepted = header.wasAccepted()
|
||||
internal fun PrivateResponse.output(contactId: ContactId): Map<String, Any> {
|
||||
val map: HashMap<String, Any> = hashMapOf(
|
||||
"sessionId" to sessionId.bytes,
|
||||
"accepted" to wasAccepted()
|
||||
)
|
||||
map.putAll((this as PrivateMessageHeader).output(contactId, null))
|
||||
return map
|
||||
}
|
||||
|
||||
@Immutable
|
||||
internal data class OutputIntroductionResponse(
|
||||
override val iHeader: IntroductionResponse,
|
||||
override val iContactId: ContactId
|
||||
) : OutputPrivateResponse(iHeader, iContactId) {
|
||||
|
||||
override val type = "org.briarproject.briar.api.introduction.IntroductionResponse"
|
||||
val introducedAuthor get() = iHeader.introducedAuthor.output()
|
||||
val introducer get() = iHeader.isIntroducer
|
||||
internal fun IntroductionResponse.output(contactId: ContactId): Map<String, Any> {
|
||||
val map: HashMap<String, Any> = hashMapOf(
|
||||
"type" to "org.briarproject.briar.api.introduction.IntroductionResponse",
|
||||
"introducedAuthor" to introducedAuthor.output(),
|
||||
"introducer" to isIntroducer
|
||||
)
|
||||
map.putAll((this as PrivateResponse).output(contactId))
|
||||
return map
|
||||
}
|
||||
|
||||
@Immutable
|
||||
internal data class OutputInvitationResponse(
|
||||
override val iHeader: InvitationResponse,
|
||||
override val iContactId: ContactId
|
||||
) : OutputPrivateResponse(iHeader, iContactId) {
|
||||
|
||||
override val type = when (iHeader) {
|
||||
is ForumInvitationResponse -> "org.briarproject.briar.api.forum.ForumInvitationResponse"
|
||||
is BlogInvitationResponse -> "org.briarproject.briar.api.blog.BlogInvitationResponse"
|
||||
is GroupInvitationResponse -> "org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse"
|
||||
else -> throw AssertionError("Unknown InvitationResponse")
|
||||
}
|
||||
val shareableId: ByteArray get() = iHeader.shareableId.bytes
|
||||
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 PrivateResponse.output(contactId: ContactId): OutputPrivateMessage {
|
||||
return when (this) {
|
||||
is IntroductionResponse -> OutputIntroductionResponse(this, contactId)
|
||||
is InvitationResponse -> OutputInvitationResponse(this, contactId)
|
||||
else -> throw AssertionError("Unknown PrivateResponse")
|
||||
}
|
||||
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 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 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
|
||||
}
|
||||
Reference in New Issue
Block a user