1 package net.pterodactylus.sone.web.ajax
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.utils.ifFalse
5 import net.pterodactylus.sone.utils.parameters
6 import net.pterodactylus.sone.web.WebInterface
7 import net.pterodactylus.sone.web.page.FreenetRequest
8 import javax.inject.Inject
11 * AJAX page that lets the user rename a profile field.
13 class EditProfileFieldAjaxPage @Inject constructor(webInterface: WebInterface) :
14 LoggedInJsonPage("editProfileField.ajax", webInterface) {
16 override fun createJsonObject(currentSone: Sone, request: FreenetRequest) =
17 currentSone.profile.let { profile ->
18 request.parameters["field"]!!
19 .let(profile::getFieldById)
21 request.parameters["name"]!!.trim().let { newName ->
22 newName.isBlank().ifFalse {
25 createSuccessJsonObject().also {
26 currentSone.profile = profile
28 } catch (_: IllegalArgumentException) {
29 createErrorJsonObject("duplicate-field-name")
32 } ?: createErrorJsonObject("invalid-parameter-name")
33 } ?: createErrorJsonObject("invalid-field-id")