Merge branch 'release-0.9.6'
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetPostAjaxPage.java
1 /*
2  * Sone - GetPostAjaxPage.java - Copyright © 2010–2016 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.web.ajax;
19
20 import java.io.StringWriter;
21 import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
22
23 import com.google.common.base.Optional;
24
25 import net.pterodactylus.sone.data.Post;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.web.WebInterface;
28 import net.pterodactylus.sone.web.page.FreenetRequest;
29 import net.pterodactylus.util.io.Closer;
30 import net.pterodactylus.util.template.Template;
31 import net.pterodactylus.util.template.TemplateContext;
32 import net.pterodactylus.util.template.TemplateException;
33
34 import com.fasterxml.jackson.databind.JsonNode;
35 import com.fasterxml.jackson.databind.node.ObjectNode;
36
37 /**
38  * This AJAX handler retrieves information and rendered representation of a
39  * {@link Post}.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class GetPostAjaxPage extends JsonPage {
44
45         /** The template to render for posts. */
46         private final Template postTemplate;
47
48         /**
49          * Creates a new “get post” AJAX handler.
50          *
51          * @param webInterface
52          *            The Sone web interface
53          * @param postTemplate
54          *            The template to render for posts
55          */
56         public GetPostAjaxPage(WebInterface webInterface, Template postTemplate) {
57                 super("getPost.ajax", webInterface);
58                 this.postTemplate = postTemplate;
59         }
60
61         /**
62          * {@inheritDoc}
63          */
64         @Override
65         protected JsonReturnObject createJsonObject(FreenetRequest request) {
66                 String postId = request.getHttpRequest().getParam("post");
67                 Optional<Post> post = webInterface.getCore().getPost(postId);
68                 if (!post.isPresent()) {
69                         return createErrorJsonObject("invalid-post-id");
70                 }
71                 return createSuccessJsonObject().put("post", createJsonPost(request, post.get(), getCurrentSone(request.getToadletContext())));
72         }
73
74         /**
75          * {@inheritDoc}
76          */
77         @Override
78         protected boolean needsFormPassword() {
79                 return false;
80         }
81
82         //
83         // PRIVATE METHODS
84         //
85
86         /**
87          * Creates a JSON object from the given post. The JSON object will only
88          * contain the ID of the post, its time, and its rendered HTML code.
89          *
90          * @param request
91          *            The request being processed
92          * @param post
93          *            The post to create a JSON object from
94          * @param currentSone
95          *            The currently logged in Sone (to store in the template)
96          * @return The JSON representation of the post
97          */
98         private JsonNode createJsonPost(FreenetRequest request, Post post, Sone currentSone) {
99                 ObjectNode jsonPost = new ObjectNode(instance);
100                 jsonPost.put("id", post.getId());
101                 jsonPost.put("sone", post.getSone().getId());
102                 jsonPost.put("recipient", post.getRecipientId().orNull());
103                 jsonPost.put("time", post.getTime());
104                 StringWriter stringWriter = new StringWriter();
105                 TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext();
106                 templateContext.set("core", webInterface.getCore());
107                 templateContext.set("request", request);
108                 templateContext.set("post", post);
109                 templateContext.set("currentSone", currentSone);
110                 templateContext.set("localSones", webInterface.getCore().getLocalSones());
111                 try {
112                         postTemplate.render(templateContext, stringWriter);
113                 } catch (TemplateException te1) {
114                         /* TODO - shouldn’t happen. */
115                 } finally {
116                         Closer.close(stringWriter);
117                 }
118                 jsonPost.put("html", stringWriter.toString());
119                 return jsonPost;
120         }
121
122 }