Replace get reply ajax page with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 13 Sep 2017 18:30:46 +0000 (20:30 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 13 Sep 2017 18:30:46 +0000 (20:30 +0200)
src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java [deleted file]
src/main/kotlin/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.kt [new file with mode: 0644]

diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java
deleted file mode 100644 (file)
index c66f47a..0000000
+++ /dev/null
@@ -1,122 +0,0 @@
-/*
- * Sone - GetReplyAjaxPage.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 <http://www.gnu.org/licenses/>.
- */
-
-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.PostReply;
-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 page returns the details of a reply.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class GetReplyAjaxPage extends JsonPage {
-
-       /** The template to render. */
-       private final Template replyTemplate;
-
-       /**
-        * Creates a new “get reply” page.
-        *
-        * @param webInterface
-        *            The Sone web interface
-        * @param replyTemplate
-        *            The template to render
-        */
-       public GetReplyAjaxPage(WebInterface webInterface, Template replyTemplate) {
-               super("getReply.ajax", webInterface);
-               this.replyTemplate = replyTemplate;
-       }
-
-       //
-       // JSONPAGE METHODS
-       //
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       protected JsonReturnObject createJsonObject(FreenetRequest request) {
-               String replyId = request.getHttpRequest().getParam("reply");
-               Optional<PostReply> reply = webInterface.getCore().getPostReply(replyId);
-               if (!reply.isPresent()) {
-                       return createErrorJsonObject("invalid-reply-id");
-               }
-               return createSuccessJsonObject().put("reply", createJsonReply(request, reply.get(), getCurrentSone(request.getToadletContext())));
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       protected boolean needsFormPassword() {
-               return false;
-       }
-
-       //
-       // PRIVATE METHODS
-       //
-
-       /**
-        * Creates a JSON representation of the given reply.
-        *
-        * @param request
-        *            The request being processed
-        * @param reply
-        *            The reply to convert
-        * @param currentSone
-        *            The currently logged in Sone (to store in the template)
-        * @return The JSON representation of the reply
-        */
-       private JsonNode createJsonReply(FreenetRequest request, PostReply reply, Sone currentSone) {
-               ObjectNode jsonReply = new ObjectNode(instance);
-               jsonReply.put("id", reply.getId());
-               jsonReply.put("postId", reply.getPostId());
-               jsonReply.put("soneId", reply.getSone().getId());
-               jsonReply.put("time", reply.getTime());
-               StringWriter stringWriter = new StringWriter();
-               TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext();
-               templateContext.set("core", webInterface.getCore());
-               templateContext.set("request", request);
-               templateContext.set("reply", reply);
-               templateContext.set("currentSone", currentSone);
-               try {
-                       replyTemplate.render(templateContext, stringWriter);
-               } catch (TemplateException te1) {
-                       /* TODO - shouldn’t happen. */
-               } finally {
-                       Closer.close(stringWriter);
-               }
-               return jsonReply.put("html", stringWriter.toString());
-       }
-
-}
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 (file)
index 0000000..a9709e8
--- /dev/null
@@ -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) }
+
+}