X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FGetLikesAjaxPage.java;h=3f6c0d4a8f3bf317f29e9ef5884d82b918d25968;hp=621abe80ce2f598a31692714425812f8acc76474;hb=419098bcd6215125408b29e60bd888e60979d37b;hpb=16a69fdf1e4d4b7852ca8e2abdfd09470207ec6b diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java index 621abe8..3f6c0d4 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java @@ -1,5 +1,5 @@ /* - * Sone - GetLikesAjaxPage.java - Copyright © 2010 David Roden + * Sone - GetLikesAjaxPage.java - Copyright © 2010–2015 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,10 +17,23 @@ package net.pterodactylus.sone.web.ajax; +import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance; +import static net.pterodactylus.sone.data.Sone.NICE_NAME_COMPARATOR; + +import java.util.Set; + import net.pterodactylus.sone.data.Post; -import net.pterodactylus.sone.data.Reply; +import net.pterodactylus.sone.data.PostReply; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.template.SoneAccessor; import net.pterodactylus.sone.web.WebInterface; -import net.pterodactylus.util.json.JsonObject; +import net.pterodactylus.sone.web.page.FreenetRequest; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.base.Optional; +import com.google.common.collect.FluentIterable; /** * AJAX page that retrieves the number of “likes” a {@link Post} has. @@ -36,7 +49,7 @@ public class GetLikesAjaxPage extends JsonPage { * The Sone web interface */ public GetLikesAjaxPage(WebInterface webInterface) { - super("ajax/getLikes.ajax", webInterface); + super("getLikes.ajax", webInterface); } // @@ -47,20 +60,28 @@ public class GetLikesAjaxPage extends JsonPage { * {@inheritDoc} */ @Override - protected JsonObject createJsonObject(Request request) { + protected JsonReturnObject createJsonObject(FreenetRequest request) { String type = request.getHttpRequest().getParam("type", null); String id = request.getHttpRequest().getParam(type, null); if ((id == null) || (id.length() == 0)) { - return new JsonObject().put("success", false).put("error", "invalid-" + type + "-id"); + return createErrorJsonObject("invalid-" + type + "-id"); } if ("post".equals(type)) { - Post post = webInterface.core().getPost(id); - return new JsonObject().put("success", true).put("likes", webInterface.core().getLikes(post).size()); + Optional post = webInterface.getCore().getPost(id); + if (!post.isPresent()) { + return createErrorJsonObject("invalid-post-id"); + } + Set sones = webInterface.getCore().getLikes(post.get()); + return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones)); } else if ("reply".equals(type)) { - Reply reply = webInterface.core().getReply(id); - return new JsonObject().put("success", true).put("likes", webInterface.core().getLikes(reply).size()); + Optional reply = webInterface.getCore().getPostReply(id); + if (!reply.isPresent()) { + return createErrorJsonObject("invalid-reply-id"); + } + Set sones = webInterface.getCore().getLikes(reply.get()); + return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones)); } - return new JsonObject().put("success", false).put("error", "invalid-type"); + return createErrorJsonObject("invalid-type"); } /** @@ -71,4 +92,24 @@ public class GetLikesAjaxPage extends JsonPage { return false; } + // + // PRIVATE METHODS + // + + /** + * Creates a JSON array (containing the IDs and the nice names) from the + * given Sones, after sorting them by name. + * + * @param sones + * The Sones to convert to an array + * @return The Sones, sorted by name + */ + private static JsonNode getSones(Set sones) { + ArrayNode soneArray = new ArrayNode(instance); + for (Sone sone : FluentIterable.from(sones).toSortedList(NICE_NAME_COMPARATOR)) { + soneArray.add(new ObjectNode(instance).put("id", sone.getId()).put("name", SoneAccessor.getNiceName(sone))); + } + return soneArray; + } + }