X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FGetReplyAjaxPage.kt;fp=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FGetReplyAjaxPage.kt;h=a9709e8e719d6c6ada699fdff230ffeeca533b16;hb=877644f873c449f07d59ed984d912538db7e0fa6;hp=0000000000000000000000000000000000000000;hpb=2f4d45dce241da77f5109e97ba497f0dd4d5c1ec;p=Sone.git diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.kt new file mode 100644 index 0000000..a9709e8 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.kt @@ -0,0 +1,46 @@ +package net.pterodactylus.sone.web.ajax + +import net.pterodactylus.sone.data.PostReply +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.utils.jsonObject +import net.pterodactylus.sone.utils.let +import net.pterodactylus.sone.utils.parameters +import net.pterodactylus.sone.utils.render +import net.pterodactylus.sone.web.WebInterface +import net.pterodactylus.sone.web.page.FreenetRequest +import net.pterodactylus.util.template.Template + +/** + * This AJAX page returns the details of a reply. + */ +class GetReplyAjaxPage(webInterface: WebInterface, private val template: Template) : LoggedInJsonPage("getReply.ajax", webInterface) { + + override fun needsFormPassword() = false + + override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = + request.parameters["reply"] + .let(webInterface.core::getPostReply) + ?.let { it.toJson(currentSone, request) } + ?.let { replyJson -> + createSuccessJsonObject().apply { + put("reply", replyJson) + } + } ?: createErrorJsonObject("invalid-reply-id") + + private fun PostReply.toJson(currentSone: Sone, request: FreenetRequest) = jsonObject(*mapOf( + "id" to id, + "soneId" to sone.id, + "postId" to postId, + "time" to time, + "html" to render(currentSone, request) + ).toList().toTypedArray()) + + private fun PostReply.render(currentSone: Sone, request: FreenetRequest) = + webInterface.templateContextFactory.createTemplateContext().apply { + set("core", webInterface.core) + set("request", request) + set("reply", this@render) + set("currentSone", currentSone) + }.let { template.render(it) } + +}