4838a4e09f66d8c781f989f7de0bfd2eb6a26878
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetLikesAjaxPage.java
1 /*
2  * Sone - GetLikesAjaxPage.java - Copyright © 2010–2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.web.ajax;
19
20 import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
21 import static net.pterodactylus.sone.data.Sone.NICE_NAME_COMPARATOR;
22
23 import java.util.Set;
24
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;
31
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;
37
38 /**
39  * AJAX page that retrieves the number of “likes” a {@link Post} has.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class GetLikesAjaxPage extends JsonPage {
44
45         /**
46          * Creates a new “get post likes” AJAX page.
47          *
48          * @param webInterface
49          *            The Sone web interface
50          */
51         public GetLikesAjaxPage(WebInterface webInterface) {
52                 super("getLikes.ajax", webInterface);
53         }
54
55         //
56         // JSONPAGE METHODS
57         //
58
59         @Override
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");
65                 }
66                 if ("post".equals(type)) {
67                         Optional<Post> post = webInterface.getCore().getDatabase().getPost(id);
68                         if (!post.isPresent()) {
69                                 return createErrorJsonObject("invalid-post-id");
70                         }
71                         Set<Sone> sones = webInterface.getCore().getLikes(post.get());
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");
77                         }
78                         Set<Sone> sones = webInterface.getCore().getLikes(reply.get());
79                         return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
80                 }
81                 return createErrorJsonObject("invalid-type");
82         }
83
84         @Override
85         protected boolean needsFormPassword() {
86                 return false;
87         }
88
89         //
90         // PRIVATE METHODS
91         //
92
93         /**
94          * Creates a JSON array (containing the IDs and the nice names) from the
95          * given Sones, after sorting them by name.
96          *
97          * @param sones
98          *            The Sones to convert to an array
99          * @return The Sones, sorted by name
100          */
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)));
105                 }
106                 return soneArray;
107         }
108
109 }