Change all copyright headers to include 2012.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetLikesAjaxPage.java
index 621abe8..83fa15b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - GetLikesAjaxPage.java - Copyright © 2010 David Roden
+ * Sone - GetLikesAjaxPage.java - Copyright © 2010–2012 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
 
 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.PostReply;
+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<Sone> 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());
+                       PostReply reply = webInterface.getCore().getReply(id);
+                       Set<Sone> 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<Sone> sones) {
+               JsonArray soneArray = new JsonArray();
+               List<Sone> sortedSones = new ArrayList<Sone>(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;
+       }
+
 }