mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
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:
@@ -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) }
|
||||
|
||||
@@ -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
|
||||
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user