Merge branch 'release-0.8.5'
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetLikesAjaxPage.java
index 293b4b0..1a5286f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - GetLikesAjaxPage.java - Copyright © 2010–2012 David Roden
+ * Sone - GetLikesAjaxPage.java - Copyright © 2010–2013 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
@@ -31,6 +31,8 @@ import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.json.JsonArray;
 import net.pterodactylus.util.json.JsonObject;
 
+import com.google.common.base.Optional;
+
 /**
  * AJAX page that retrieves the number of “likes” a {@link Post} has.
  *
@@ -63,12 +65,18 @@ public class GetLikesAjaxPage extends JsonPage {
                        return createErrorJsonObject("invalid-" + type + "-id");
                }
                if ("post".equals(type)) {
-                       Post post = webInterface.getCore().getPost(id);
-                       Set<Sone> sones = webInterface.getCore().getLikes(post);
+                       Optional<Post> post = webInterface.getCore().getPost(id);
+                       if (!post.isPresent()) {
+                               return createErrorJsonObject("invalid-post-id");
+                       }
+                       Set<Sone> sones = webInterface.getCore().getLikes(post.get());
                        return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
                } else if ("reply".equals(type)) {
-                       PostReply reply = webInterface.getCore().getReply(id);
-                       Set<Sone> sones = webInterface.getCore().getLikes(reply);
+                       Optional<PostReply> reply = webInterface.getCore().getPostReply(id);
+                       if (!reply.isPresent()) {
+                               return createErrorJsonObject("invalid-reply-id");
+                       }
+                       Set<Sone> sones = webInterface.getCore().getLikes(reply.get());
                        return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
                }
                return createErrorJsonObject("invalid-type");