2 * Sone - GetLikesAjaxPage.java - Copyright © 2010–2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web.ajax;
20 import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
21 import static net.pterodactylus.sone.data.Sone.NICE_NAME_COMPARATOR;
25 import net.pterodactylus.sone.data.Post;
26 import net.pterodactylus.sone.data.PostReply;
27 import net.pterodactylus.sone.data.Sone;
28 import net.pterodactylus.sone.template.SoneAccessor;
29 import net.pterodactylus.sone.web.WebInterface;
30 import net.pterodactylus.sone.web.page.FreenetRequest;
32 import com.fasterxml.jackson.databind.JsonNode;
33 import com.fasterxml.jackson.databind.node.ArrayNode;
34 import com.fasterxml.jackson.databind.node.ObjectNode;
35 import com.google.common.base.Optional;
36 import com.google.common.collect.FluentIterable;
39 * AJAX page that retrieves the number of “likes” a {@link Post} has.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class GetLikesAjaxPage extends JsonPage {
46 * Creates a new “get post likes” AJAX page.
49 * The Sone web interface
51 public GetLikesAjaxPage(WebInterface webInterface) {
52 super("getLikes.ajax", webInterface);
60 protected JsonReturnObject createJsonObject(FreenetRequest request) {
61 String type = request.getHttpRequest().getParam("type", null);
62 String id = request.getHttpRequest().getParam(type, null);
63 if ((id == null) || (id.length() == 0)) {
64 return createErrorJsonObject("invalid-" + type + "-id");
66 if ("post".equals(type)) {
67 Optional<Post> post = webInterface.getCore().getDatabase().getPost(id);
68 if (!post.isPresent()) {
69 return createErrorJsonObject("invalid-post-id");
71 Set<Sone> sones = post.get().getLikes();
72 return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
73 } else if ("reply".equals(type)) {
74 Optional<PostReply> reply = webInterface.getCore().getDatabase().getPostReply(id);
75 if (!reply.isPresent()) {
76 return createErrorJsonObject("invalid-reply-id");
78 Set<Sone> sones = webInterface.getCore().getLikes(reply.get());
79 return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
81 return createErrorJsonObject("invalid-type");
85 protected boolean needsFormPassword() {
94 * Creates a JSON array (containing the IDs and the nice names) from the
95 * given Sones, after sorting them by name.
98 * The Sones to convert to an array
99 * @return The Sones, sorted by name
101 private static JsonNode getSones(Set<Sone> sones) {
102 ArrayNode soneArray = new ArrayNode(instance);
103 for (Sone sone : FluentIterable.from(sones).toSortedList(NICE_NAME_COMPARATOR)) {
104 soneArray.add(new ObjectNode(instance).put("id", sone.getId()).put("name", SoneAccessor.getNiceName(sone)));