Replace unlock Sone ajax page with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / UnlockSoneAjaxPage.kt
diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/UnlockSoneAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/UnlockSoneAjaxPage.kt
new file mode 100644 (file)
index 0000000..2dffbed
--- /dev/null
@@ -0,0 +1,22 @@
+package net.pterodactylus.sone.web.ajax
+
+import net.pterodactylus.sone.utils.parameters
+import net.pterodactylus.sone.web.WebInterface
+import net.pterodactylus.sone.web.page.FreenetRequest
+
+/**
+ * Lets the user [unlock][net.pterodactylus.sone.core.Core.unlockSone] a [Sone][net.pterodactylus.sone.data.Sone].
+ */
+class UnlockSoneAjaxPage(webInterface: WebInterface) : JsonPage("unlockSone.ajax", webInterface) {
+
+       override fun requiresLogin() = false
+
+       override fun createJsonObject(request: FreenetRequest) =
+                       request.parameters["sone"]
+                                       ?.let(webInterface.core::getLocalSone)
+                                       ?.also(webInterface.core::unlockSone)
+                                       ?.also { webInterface.core.touchConfiguration() }
+                                       ?.let { createSuccessJsonObject() }
+                                       ?: createErrorJsonObject("invalid-sone-id")
+
+}