Update years in copyright line
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetLikesAjaxPage.java
1 /*
2  * Sone - GetLikesAjaxPage.java - Copyright © 2010–2015 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         /**
60          * {@inheritDoc}
61          */
62         @Override
63         protected JsonReturnObject createJsonObject(FreenetRequest request) {
64                 String type = request.getHttpRequest().getParam("type", null);
65                 String id = request.getHttpRequest().getParam(type, null);
66                 if ((id == null) || (id.length() == 0)) {
67                         return createErrorJsonObject("invalid-" + type + "-id");
68                 }
69                 if ("post".equals(type)) {
70                         Optional<Post> post = webInterface.getCore().getPost(id);
71                         if (!post.isPresent()) {
72                                 return createErrorJsonObject("invalid-post-id");
73                         }
74                         Set<Sone> sones = webInterface.getCore().getLikes(post.get());
75                         return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
76                 } else if ("reply".equals(type)) {
77                         Optional<PostReply> reply = webInterface.getCore().getPostReply(id);
78                         if (!reply.isPresent()) {
79                                 return createErrorJsonObject("invalid-reply-id");
80                         }
81                         Set<Sone> sones = webInterface.getCore().getLikes(reply.get());
82                         return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
83                 }
84                 return createErrorJsonObject("invalid-type");
85         }
86
87         /**
88          * {@inheritDoc}
89          */
90         @Override
91         protected boolean needsFormPassword() {
92                 return false;
93         }
94
95         //
96         // PRIVATE METHODS
97         //
98
99         /**
100          * Creates a JSON array (containing the IDs and the nice names) from the
101          * given Sones, after sorting them by name.
102          *
103          * @param sones
104          *            The Sones to convert to an array
105          * @return The Sones, sorted by name
106          */
107         private static JsonNode getSones(Set<Sone> sones) {
108                 ArrayNode soneArray = new ArrayNode(instance);
109                 for (Sone sone : FluentIterable.from(sones).toSortedList(NICE_NAME_COMPARATOR)) {
110                         soneArray.add(new ObjectNode(instance).put("id", sone.getId()).put("name", SoneAccessor.getNiceName(sone)));
111                 }
112                 return soneArray;
113         }
114
115 }