Replace delete profile field ajax page with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / DeleteProfileFieldAjaxPage.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import net.pterodactylus.sone.utils.parameters
4 import net.pterodactylus.sone.web.WebInterface
5 import net.pterodactylus.sone.web.page.FreenetRequest
6
7 /**
8  * AJAX page that lets the user delete a profile field.
9  */
10 class DeleteProfileFieldAjaxPage(webInterface: WebInterface) : JsonPage("deleteProfileField.ajax", webInterface) {
11
12         override fun createJsonObject(request: FreenetRequest) =
13                         getCurrentSone(request.toadletContext)!!.let { currentSone ->
14                                 currentSone.profile.let { profile ->
15                                         request.parameters["field"]
16                                                         ?.let(profile::getFieldById)
17                                                         ?.let { field ->
18                                                                 createSuccessJsonObject().also {
19                                                                         profile.removeField(field)
20                                                                         currentSone.profile = profile
21                                                                         webInterface.core.touchConfiguration()
22                                                                 }
23                                                         } ?: createErrorJsonObject("invalid-field-id")
24                                 }
25                         }
26
27 }