Move template rendering extension method to Templates.kt
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetReplyAjaxPage.java
index 292f3bf..c66f47a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - GetReplyAjaxPage.java - Copyright © 2010 David Roden
+ * 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
 package net.pterodactylus.sone.web.ajax;
 
 import java.io.StringWriter;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.Date;
+import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
 
-import net.pterodactylus.sone.data.Reply;
-import net.pterodactylus.sone.template.SoneAccessor;
+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.json.JsonObject;
 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.
  *
@@ -37,9 +41,6 @@ import net.pterodactylus.util.template.TemplateException;
  */
 public class GetReplyAjaxPage extends JsonPage {
 
-       /** Date formatter. */
-       private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
-
        /** The template to render. */
        private final Template replyTemplate;
 
@@ -52,7 +53,7 @@ public class GetReplyAjaxPage extends JsonPage {
         *            The template to render
         */
        public GetReplyAjaxPage(WebInterface webInterface, Template replyTemplate) {
-               super("ajax/getReply.ajax", webInterface);
+               super("getReply.ajax", webInterface);
                this.replyTemplate = replyTemplate;
        }
 
@@ -64,25 +65,13 @@ public class GetReplyAjaxPage extends JsonPage {
         * {@inheritDoc}
         */
        @Override
-       protected JsonObject createJsonObject(Request request) {
+       protected JsonReturnObject createJsonObject(FreenetRequest request) {
                String replyId = request.getHttpRequest().getParam("reply");
-               Reply reply = webInterface.getCore().getReply(replyId);
-               if ((reply == null) || (reply.getSone() == null)) {
+               Optional<PostReply> reply = webInterface.getCore().getPostReply(replyId);
+               if (!reply.isPresent()) {
                        return createErrorJsonObject("invalid-reply-id");
                }
-               replyTemplate.set("reply", reply);
-               replyTemplate.set("currentSone", getCurrentSone(request.getToadletContext()));
-               StringWriter templateWriter = new StringWriter();
-               try {
-                       replyTemplate.render(templateWriter);
-               } catch (TemplateException te1) {
-                       /* TODO - shouldn’t happen. */
-               } finally {
-                       Closer.close(templateWriter);
-               }
-               synchronized (dateFormat) {
-                       return new JsonObject().put("success", true).put("soneId", reply.getSone().getId()).put("soneName", SoneAccessor.getNiceName(reply.getSone())).put("time", reply.getTime()).put("displayTime", dateFormat.format(new Date(reply.getTime()))).put("text", reply.getText()).put("html", templateWriter.toString());
-               }
+               return createSuccessJsonObject().put("reply", createJsonReply(request, reply.get(), getCurrentSone(request.getToadletContext())));
        }
 
        /**
@@ -93,4 +82,41 @@ public class GetReplyAjaxPage extends JsonPage {
                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());
+       }
+
 }