Replace unlike ajax page with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / UnlikeAjaxPage.kt
diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.kt
new file mode 100644 (file)
index 0000000..c4ec236
--- /dev/null
@@ -0,0 +1,27 @@
+package net.pterodactylus.sone.web.ajax
+
+import net.pterodactylus.sone.data.Sone
+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 unlike a [net.pterodactylus.sone.data.Post].
+ */
+class UnlikeAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("unlike.ajax", webInterface) {
+
+       override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = when (request.parameters["type"]) {
+               "post" -> request.processEntity("post", currentSone::removeLikedPostId)
+               "reply" -> request.processEntity("reply", currentSone::removeLikedReplyId)
+               else -> createErrorJsonObject("invalid-type")
+       }
+
+       private fun FreenetRequest.processEntity(entity: String, likeRemover: (String) -> Unit) =
+                       parameters[entity].emptyToNull
+                                       ?.also(likeRemover)
+                                       ?.also { webInterface.core.touchConfiguration() }
+                                       ?.let { createSuccessJsonObject() }
+                                       ?: createErrorJsonObject("invalid-$entity-id")
+
+}