Replace bookmark ajax page with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / BookmarkAjaxPage.kt
diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/BookmarkAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/BookmarkAjaxPage.kt
new file mode 100644 (file)
index 0000000..fd22f06
--- /dev/null
@@ -0,0 +1,23 @@
+package net.pterodactylus.sone.web.ajax
+
+import net.pterodactylus.sone.utils.also
+import net.pterodactylus.sone.utils.emptyToNull
+import net.pterodactylus.sone.utils.parameters
+import net.pterodactylus.sone.web.WebInterface
+import net.pterodactylus.sone.web.page.FreenetRequest
+
+/**
+ * AJAX page that lets the user bookmark a post.
+ */
+class BookmarkAjaxPage(webInterface: WebInterface) : JsonPage("bookmark.ajax", webInterface) {
+
+       override fun requiresLogin() = false
+
+       override fun createJsonObject(request: FreenetRequest) =
+                       request.parameters["post"].emptyToNull
+                                       ?.let(webInterface.core::getPost)
+                                       ?.also(webInterface.core::bookmarkPost)
+                                       ?.let { createSuccessJsonObject() }
+                                       ?: createErrorJsonObject("invalid-post-id")
+
+}