X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FGetLikesAjaxPage.java;h=539be3d13eef13e1e11d2b958a8b795d21c27d64;hb=45f92ec63dbf8134d92ceed67294faa38117b195;hp=621abe80ce2f598a31692714425812f8acc76474;hpb=16a69fdf1e4d4b7852ca8e2abdfd09470207ec6b;p=Sone.git 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..539be3d 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java @@ -17,9 +17,18 @@ package net.pterodactylus.sone.web.ajax; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; + import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Reply; +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.template.SoneAccessor; import net.pterodactylus.sone.web.WebInterface; +import net.pterodactylus.sone.web.page.FreenetRequest; +import net.pterodactylus.util.json.JsonArray; import net.pterodactylus.util.json.JsonObject; /** @@ -36,7 +45,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 +56,22 @@ public class GetLikesAjaxPage extends JsonPage { * {@inheritDoc} */ @Override - protected JsonObject createJsonObject(Request request) { + protected JsonObject 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()); + Post post = webInterface.getCore().getPost(id); + Set sones = webInterface.getCore().getLikes(post); + 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()); + Reply reply = webInterface.getCore().getReply(id); + Set sones = webInterface.getCore().getLikes(reply); + 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 +82,26 @@ 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 JsonArray getSones(Set sones) { + JsonArray soneArray = new JsonArray(); + List sortedSones = new ArrayList(sones); + Collections.sort(sortedSones, Sone.NICE_NAME_COMPARATOR); + for (Sone sone : sortedSones) { + soneArray.add(new JsonObject().put("id", sone.getId()).put("name", SoneAccessor.getNiceName(sone))); + } + return soneArray; + } + }