From: David ‘Bombe’ Roden Date: Sun, 10 Sep 2017 15:21:02 +0000 (+0200) Subject: Replace get post ajax page with Kotlin version X-Git-Tag: 0.9.7^2~69 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=0197f5a0dda9f60255f4a19cabf7ef7f2f2eaa19;p=Sone.git Replace get post ajax page with Kotlin version --- diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java deleted file mode 100644 index 60e94ce..0000000 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Sone - GetPostAjaxPage.java - Copyright © 2010–2016 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.web.ajax; - -import java.io.StringWriter; -import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance; - -import com.google.common.base.Optional; - -import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.web.WebInterface; -import net.pterodactylus.sone.web.page.FreenetRequest; -import net.pterodactylus.util.io.Closer; -import net.pterodactylus.util.template.Template; -import net.pterodactylus.util.template.TemplateContext; -import net.pterodactylus.util.template.TemplateException; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ObjectNode; - -/** - * This AJAX handler retrieves information and rendered representation of a - * {@link Post}. - * - * @author David ‘Bombe’ Roden - */ -public class GetPostAjaxPage extends JsonPage { - - /** The template to render for posts. */ - private final Template postTemplate; - - /** - * Creates a new “get post” AJAX handler. - * - * @param webInterface - * The Sone web interface - * @param postTemplate - * The template to render for posts - */ - public GetPostAjaxPage(WebInterface webInterface, Template postTemplate) { - super("getPost.ajax", webInterface); - this.postTemplate = postTemplate; - } - - /** - * {@inheritDoc} - */ - @Override - protected JsonReturnObject createJsonObject(FreenetRequest request) { - String postId = request.getHttpRequest().getParam("post"); - Optional post = webInterface.getCore().getPost(postId); - if (!post.isPresent()) { - return createErrorJsonObject("invalid-post-id"); - } - return createSuccessJsonObject().put("post", createJsonPost(request, post.get(), getCurrentSone(request.getToadletContext()))); - } - - /** - * {@inheritDoc} - */ - @Override - protected boolean needsFormPassword() { - return false; - } - - // - // PRIVATE METHODS - // - - /** - * Creates a JSON object from the given post. The JSON object will only - * contain the ID of the post, its time, and its rendered HTML code. - * - * @param request - * The request being processed - * @param post - * The post to create a JSON object from - * @param currentSone - * The currently logged in Sone (to store in the template) - * @return The JSON representation of the post - */ - private JsonNode createJsonPost(FreenetRequest request, Post post, Sone currentSone) { - ObjectNode jsonPost = new ObjectNode(instance); - jsonPost.put("id", post.getId()); - jsonPost.put("sone", post.getSone().getId()); - jsonPost.put("recipient", post.getRecipientId().orNull()); - jsonPost.put("time", post.getTime()); - StringWriter stringWriter = new StringWriter(); - TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext(); - templateContext.set("core", webInterface.getCore()); - templateContext.set("request", request); - templateContext.set("post", post); - templateContext.set("currentSone", currentSone); - templateContext.set("localSones", webInterface.getCore().getLocalSones()); - try { - postTemplate.render(templateContext, stringWriter); - } catch (TemplateException te1) { - /* TODO - shouldn’t happen. */ - } finally { - Closer.close(stringWriter); - } - jsonPost.put("html", stringWriter.toString()); - return jsonPost; - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Json.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Json.kt index 71c8e81..ef3c95d 100644 --- a/src/main/kotlin/net/pterodactylus/sone/utils/Json.kt +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Json.kt @@ -7,7 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode fun jsonObject(block: ObjectNode.() -> Unit): ObjectNode = ObjectNode(instance).apply(block) -fun jsonObject(vararg properties: Pair) = jsonObject { +fun jsonObject(vararg properties: Pair) = jsonObject { properties.forEach { it.second.let { value -> when (value) { diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.kt new file mode 100644 index 0000000..3c35125 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetPostAjaxPage.kt @@ -0,0 +1,46 @@ +package net.pterodactylus.sone.web.ajax + +import net.pterodactylus.sone.data.Post +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.web.WebInterface +import net.pterodactylus.sone.web.page.FreenetRequest +import net.pterodactylus.util.template.Template +import net.pterodactylus.util.template.TemplateContext +import java.io.StringWriter + +/** + * This AJAX handler retrieves information and rendered representation of a [Post]. + */ +class GetPostAjaxPage(webInterface: WebInterface, private val postTemplate: Template) : LoggedInJsonPage("getPost.ajax", webInterface) { + + override fun needsFormPassword() = false + + override fun createJsonObject(currentSone: Sone, request: FreenetRequest) = + request.parameters["post"] + .let(webInterface.core::getPost) + .let { post -> + createSuccessJsonObject(). + put("post", jsonObject( + "id" to post.id, + "sone" to post.sone.id, + "time" to post.time, + "recipient" to post.recipientId.orNull(), + "html" to post.render(currentSone, request) + )) + } ?: createErrorJsonObject("invalid-post-id") + + private fun Post.render(currentSone: Sone, request: FreenetRequest) = + webInterface.templateContextFactory.createTemplateContext().apply { + set("core", webInterface.core) + set("request", request) + set("post", this@render) + set("currentSone", currentSone) + set("localSones", webInterface.core.localSones) + }.let { postTemplate.render(it) } + +} + +private fun Template.render(templateContext: TemplateContext) = StringWriter().use { it.also { render(templateContext, it) } }.toString()