mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 03:09:04 +01:00
[headless] address review comments for remote contact adding
This commit is contained in:
@@ -8,7 +8,7 @@ fun Author.output() = JsonDict(
|
||||
"formatVersion" to formatVersion,
|
||||
"id" to id.bytes,
|
||||
"name" to name,
|
||||
"publicKey" to publicKey
|
||||
"publicKey" to publicKey.encoded
|
||||
)
|
||||
|
||||
fun AuthorInfo.Status.output() = name.toLowerCase()
|
||||
|
||||
@@ -66,7 +66,7 @@ constructor(
|
||||
get { ctx -> contactController.list(ctx) }
|
||||
path("add") {
|
||||
path("link") {
|
||||
get { ctx -> contactController.link(ctx) }
|
||||
get { ctx -> contactController.getLink(ctx) }
|
||||
}
|
||||
path("pending") {
|
||||
get { ctx -> contactController.listPendingContacts(ctx) }
|
||||
|
||||
@@ -5,7 +5,7 @@ import io.javalin.Context
|
||||
interface ContactController {
|
||||
|
||||
fun list(ctx: Context): Context
|
||||
fun link(ctx: Context): Context
|
||||
fun getLink(ctx: Context): Context
|
||||
fun addPendingContact(ctx: Context): Context
|
||||
fun listPendingContacts(ctx: Context): Context
|
||||
fun removePendingContact(ctx: Context): Context
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package org.briarproject.briar.headless.contact
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import io.javalin.BadRequestResponse
|
||||
import io.javalin.Context
|
||||
import io.javalin.NotFoundResponse
|
||||
import org.briarproject.bramble.api.contact.ContactManager
|
||||
import org.briarproject.bramble.api.contact.HandshakeLinkConstants.LINK_REGEX
|
||||
import org.briarproject.bramble.api.contact.PendingContactId
|
||||
import org.briarproject.bramble.api.contact.event.ContactAddedRemotelyEvent
|
||||
import org.briarproject.bramble.api.contact.event.PendingContactRemovedEvent
|
||||
@@ -12,6 +14,8 @@ import org.briarproject.bramble.api.db.NoSuchContactException
|
||||
import org.briarproject.bramble.api.db.NoSuchPendingContactException
|
||||
import org.briarproject.bramble.api.event.Event
|
||||
import org.briarproject.bramble.api.event.EventListener
|
||||
import org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH
|
||||
import org.briarproject.bramble.util.StringUtils.toUtf8
|
||||
import org.briarproject.briar.headless.event.WebSocketController
|
||||
import org.briarproject.briar.headless.getContactIdFromPathParam
|
||||
import org.briarproject.briar.headless.getFromJson
|
||||
@@ -57,7 +61,7 @@ constructor(
|
||||
return ctx.json(contacts)
|
||||
}
|
||||
|
||||
override fun link(ctx: Context): Context {
|
||||
override fun getLink(ctx: Context): Context {
|
||||
val linkDict = JsonDict("link" to contactManager.handshakeLink)
|
||||
return ctx.json(linkDict)
|
||||
}
|
||||
@@ -65,6 +69,10 @@ constructor(
|
||||
override fun addPendingContact(ctx: Context): Context {
|
||||
val link = ctx.getFromJson(objectMapper, "link")
|
||||
val alias = ctx.getFromJson(objectMapper, "alias")
|
||||
if (!LINK_REGEX.matcher(link).find()) throw BadRequestResponse("Invalid Link")
|
||||
val aliasUtf8 = toUtf8(alias)
|
||||
if (aliasUtf8.isEmpty() || aliasUtf8.size > MAX_AUTHOR_NAME_LENGTH)
|
||||
throw BadRequestResponse("Invalid Alias")
|
||||
val pendingContact = contactManager.addPendingContact(link, alias)
|
||||
return ctx.json(pendingContact.output())
|
||||
}
|
||||
|
||||
@@ -8,7 +8,12 @@ import org.briarproject.briar.headless.json.JsonDict
|
||||
internal fun Contact.output() = JsonDict(
|
||||
"contactId" to id.int,
|
||||
"author" to author.output(),
|
||||
"alias" to alias,
|
||||
"verified" to isVerified
|
||||
)
|
||||
).apply {
|
||||
handshakePublicKey?.let { put("handshakePublicKey", it.encoded) }
|
||||
}
|
||||
|
||||
internal fun ContactAddedRemotelyEvent.output() = contact.output()
|
||||
internal fun ContactAddedRemotelyEvent.output() = JsonDict(
|
||||
"contact" to contact.output()
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user