Add method to change contact alias to REST API

Needed for https://code.briarproject.org/briar/briar-gtk/-/issues/14 and
https://code.briarproject.org/briar/python-briar-wrapper/-/issues/6.

Fixes #1781
This commit is contained in:
Nico Alt
2020-10-13 23:33:26 +02:00
parent b88f012880
commit 44f6f5d416
5 changed files with 97 additions and 3 deletions

View File

@@ -81,6 +81,9 @@ constructor(
path("/:contactId") {
delete { ctx -> contactController.delete(ctx) }
}
path("/:contactId/alias") {
put { ctx -> contactController.setContactAlias(ctx) }
}
}
path("/messages/:contactId") {
get { ctx -> messagingController.list(ctx) }

View File

@@ -9,6 +9,7 @@ interface ContactController {
fun addPendingContact(ctx: Context): Context
fun listPendingContacts(ctx: Context): Context
fun removePendingContact(ctx: Context): Context
fun setContactAlias(ctx: Context): Context
fun delete(ctx: Context): Context
}

View File

@@ -92,9 +92,7 @@ constructor(
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")
checkAliasLength(alias)
val pendingContact = contactManager.addPendingContact(link, alias)
return ctx.json(pendingContact.output())
}
@@ -125,6 +123,18 @@ constructor(
return ctx
}
override fun setContactAlias(ctx: Context): Context {
val contactId = ctx.getContactIdFromPathParam()
val alias = ctx.getFromJson(objectMapper, "alias")
checkAliasLength(alias)
try {
contactManager.setContactAlias(contactId, alias)
} catch (e: NoSuchContactException) {
throw NotFoundResponse()
}
return ctx
}
override fun delete(ctx: Context): Context {
val contactId = ctx.getContactIdFromPathParam()
try {
@@ -135,4 +145,10 @@ constructor(
return ctx
}
private fun checkAliasLength(alias: String) {
val aliasUtf8 = toUtf8(alias)
if (aliasUtf8.isEmpty() || aliasUtf8.size > MAX_AUTHOR_NAME_LENGTH)
throw BadRequestResponse("Invalid Alias")
}
}