X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FLikeAjaxPage.kt;fp=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FLikeAjaxPage.kt;h=3b206c8c47e3c0465422b21235e1048c5d64ae28;hb=d142608024302ca4106a70b95daabf27aa5362a4;hp=0000000000000000000000000000000000000000;hpb=0b8fe498389b99374691506cb8c3bbb643476c3f;p=Sone.git diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/LikeAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/LikeAjaxPage.kt new file mode 100644 index 0000000..3b206c8 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/LikeAjaxPage.kt @@ -0,0 +1,32 @@ +package net.pterodactylus.sone.web.ajax + +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.utils.let +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 like a [net.pterodactylus.sone.data.Post]. + */ +class LikeAjaxPage(webInterface: WebInterface) : LoggedInJsonPage("like.ajax", webInterface) { + + override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = + when (request.parameters["type"]) { + "post" -> request.parameters["post"] + .let(webInterface.core::getPost) + ?.let { currentSone.addLikedPostId(it.id) } + ?.also { webInterface.core.touchConfiguration() } + ?.let { createSuccessJsonObject() } + ?: createErrorJsonObject("invalid-post-id") + "reply" -> request.parameters["reply"] + .let(webInterface.core::getPostReply) + ?.let { currentSone.addLikedReplyId(it.id) } + ?.also { webInterface.core.touchConfiguration() } + ?.let { createSuccessJsonObject() } + ?: createErrorJsonObject("invalid-reply-id") + else -> createErrorJsonObject("invalid-type") + } + + +}