6daa4daabc0ab453adae973d0f0d47d88d51c7ac
[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.data.Sone
4 import net.pterodactylus.sone.utils.parameters
5 import net.pterodactylus.sone.web.WebInterface
6 import net.pterodactylus.sone.web.page.*
7 import javax.inject.Inject
8
9 /**
10  * AJAX page that lets the user delete a profile field.
11  */
12 @ToadletPath("deleteProfileField.ajax")
13 class DeleteProfileFieldAjaxPage @Inject constructor(webInterface: WebInterface) : LoggedInJsonPage(webInterface) {
14
15         override fun createJsonObject(currentSone: Sone, request: FreenetRequest) =
16                         currentSone.profile.let { profile ->
17                                 request.parameters["field"]
18                                                 ?.let(profile::getFieldById)
19                                                 ?.let { field ->
20                                                         createSuccessJsonObject().also {
21                                                                 profile.removeField(field)
22                                                                 currentSone.profile = profile
23                                                                 core.touchConfiguration()
24                                                         }
25                                                 } ?: createErrorJsonObject("invalid-field-id")
26                         }
27
28 }