Make post reply returned by provider optional.
[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 java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
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 import net.pterodactylus.util.json.JsonArray;
32 import net.pterodactylus.util.json.JsonObject;
33
34 import com.google.common.base.Optional;
35
36 /**
37  * AJAX page that retrieves the number of “likes” a {@link Post} has.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class GetLikesAjaxPage extends JsonPage {
42
43         /**
44          * Creates a new “get post likes” AJAX page.
45          *
46          * @param webInterface
47          *            The Sone web interface
48          */
49         public GetLikesAjaxPage(WebInterface webInterface) {
50                 super("getLikes.ajax", webInterface);
51         }
52
53         //
54         // JSONPAGE METHODS
55         //
56
57         /**
58          * {@inheritDoc}
59          */
60         @Override
61         protected JsonObject createJsonObject(FreenetRequest request) {
62                 String type = request.getHttpRequest().getParam("type", null);
63                 String id = request.getHttpRequest().getParam(type, null);
64                 if ((id == null) || (id.length() == 0)) {
65                         return createErrorJsonObject("invalid-" + type + "-id");
66                 }
67                 if ("post".equals(type)) {
68                         Post post = webInterface.getCore().getPost(id);
69                         Set<Sone> sones = webInterface.getCore().getLikes(post);
70                         return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
71                 } else if ("reply".equals(type)) {
72                         Optional<PostReply> reply = webInterface.getCore().getPostReply(id);
73                         if (!reply.isPresent()) {
74                                 return createErrorJsonObject("invalid-reply-id");
75                         }
76                         Set<Sone> sones = webInterface.getCore().getLikes(reply.get());
77                         return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
78                 }
79                 return createErrorJsonObject("invalid-type");
80         }
81
82         /**
83          * {@inheritDoc}
84          */
85         @Override
86         protected boolean needsFormPassword() {
87                 return false;
88         }
89
90         //
91         // PRIVATE METHODS
92         //
93
94         /**
95          * Creates a JSON array (containing the IDs and the nice names) from the
96          * given Sones, after sorting them by name.
97          *
98          * @param sones
99          *            The Sones to convert to an array
100          * @return The Sones, sorted by name
101          */
102         private static JsonArray getSones(Set<Sone> sones) {
103                 JsonArray soneArray = new JsonArray();
104                 List<Sone> sortedSones = new ArrayList<Sone>(sones);
105                 Collections.sort(sortedSones, Sone.NICE_NAME_COMPARATOR);
106                 for (Sone sone : sortedSones) {
107                         soneArray.add(new JsonObject().put("id", sone.getId()).put("name", SoneAccessor.getNiceName(sone)));
108                 }
109                 return soneArray;
110         }
111
112 }