X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FGetReplyAjaxPage.java;h=a2ee122b3cd1b9b73a348333962be8cbd921e502;hp=d00c7ddc2f0aa42ee7c123f7513c747c081cea95;hb=93fdf9e218d808f65f618abbddce183191d8c15b;hpb=378e6fafef1f68f197a944713b926e03a0c06cb9 diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java index d00c7dd..a2ee122 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetReplyAjaxPage.java @@ -1,5 +1,5 @@ /* - * Sone - GetReplyAjaxPage.java - Copyright © 2010 David Roden + * Sone - GetReplyAjaxPage.java - Copyright © 2010–2013 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 @@ -17,14 +17,19 @@ package net.pterodactylus.sone.web.ajax; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; +import java.io.StringWriter; -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; /** * This AJAX page returns the details of a reply. @@ -33,16 +38,20 @@ import net.pterodactylus.util.json.JsonObject; */ 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; /** * Creates a new “get reply” page. * * @param webInterface + * The Sone web interface + * @param replyTemplate + * The template to render */ - public GetReplyAjaxPage(WebInterface webInterface) { - super("ajax/getReply.ajax", webInterface); + public GetReplyAjaxPage(WebInterface webInterface, Template replyTemplate) { + super("getReply.ajax", webInterface); + this.replyTemplate = replyTemplate; } // @@ -53,15 +62,13 @@ public class GetReplyAjaxPage extends JsonPage { * {@inheritDoc} */ @Override - protected JsonObject createJsonObject(Request request) { + protected JsonObject createJsonObject(FreenetRequest request) { String replyId = request.getHttpRequest().getParam("reply"); - Reply reply = webInterface.getCore().getReply(replyId); - if ((reply == null) || (reply.getSone() == null)) { + Optional reply = webInterface.getCore().getPostReply(replyId); + if (!reply.isPresent()) { return createErrorJsonObject("invalid-reply-id"); } - synchronized (dateFormat) { - return new JsonObject().put("success", true).put("sone-id", reply.getSone().getId()).put("sone-name", SoneAccessor.getNiceName(reply.getSone())).put("time", reply.getTime()).put("display-time", dateFormat.format(new Date(reply.getTime()))).put("text", reply.getText()); - } + return createSuccessJsonObject().put("reply", createJsonReply(request, reply.get(), getCurrentSone(request.getToadletContext()))); } /** @@ -72,4 +79,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 JsonObject createJsonReply(FreenetRequest request, PostReply reply, Sone currentSone) { + JsonObject jsonReply = new JsonObject(); + jsonReply.put("id", reply.getId()); + jsonReply.put("postId", reply.getPost().getId()); + 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()); + } + }