X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FUnlikeAjaxPage.kt;fp=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FUnlikeAjaxPage.kt;h=c4ec2361ce0a9c6662de0a7747eac9d93c89d67f;hp=0000000000000000000000000000000000000000;hb=48a7e99c47320d9097ef16d822ecfd3c14374a92;hpb=c56cbae5dd54a70fcdaea024d606dee69525c7e9 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 index 0000000..c4ec236 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/UnlikeAjaxPage.kt @@ -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") + +}