Remove javadoc comments from overriding methods.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetStatusAjaxPage.java
1 /*
2  * Sone - GetStatusAjaxPage.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
22 import java.text.DateFormat;
23 import java.text.SimpleDateFormat;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30
31 import net.pterodactylus.sone.data.Post;
32 import net.pterodactylus.sone.data.PostReply;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.sone.notify.ListNotificationFilters;
35 import net.pterodactylus.sone.template.SoneAccessor;
36 import net.pterodactylus.sone.web.WebInterface;
37 import net.pterodactylus.sone.web.page.FreenetRequest;
38 import net.pterodactylus.util.notify.Notification;
39
40 import com.fasterxml.jackson.databind.JsonNode;
41 import com.fasterxml.jackson.databind.node.ArrayNode;
42 import com.fasterxml.jackson.databind.node.ObjectNode;
43 import com.google.common.base.Predicate;
44 import com.google.common.collect.Collections2;
45
46 /**
47  * The “get status” AJAX handler returns all information that is necessary to
48  * update the web interface in real-time.
49  *
50  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
51  */
52 public class GetStatusAjaxPage extends JsonPage {
53
54         /** Date formatter. */
55         private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
56
57         /**
58          * Creates a new “get status” AJAX handler.
59          *
60          * @param webInterface
61          *            The Sone web interface
62          */
63         public GetStatusAjaxPage(WebInterface webInterface) {
64                 super("getStatus.ajax", webInterface);
65         }
66
67         @Override
68         protected JsonReturnObject createJsonObject(FreenetRequest request) {
69                 final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
70                 /* load Sones. always return the status of the current Sone. */
71                 Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
72                 String loadSoneIds = request.getHttpRequest().getParam("soneIds");
73                 if (loadSoneIds.length() > 0) {
74                         String[] soneIds = loadSoneIds.split(",");
75                         for (String soneId : soneIds) {
76                                 /* just add it, we skip null further down. */
77                                 sones.add(webInterface.getCore().getSone(soneId).orNull());
78                         }
79                 }
80                 ArrayNode jsonSones = new ArrayNode(instance);
81                 for (Sone sone : sones) {
82                         if (sone == null) {
83                                 continue;
84                         }
85                         jsonSones.add(createJsonSone(sone));
86                 }
87                 /* load notifications. */
88                 List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone);
89                 Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
90                 /* load new posts. */
91                 Collection<Post> newPosts = webInterface.getNewPosts();
92                 if (currentSone != null) {
93                         newPosts = Collections2.filter(newPosts, new Predicate<Post>() {
94
95                                 @Override
96                                 public boolean apply(Post post) {
97                                         return ListNotificationFilters.isPostVisible(currentSone, post);
98                                 }
99
100                         });
101                 }
102                 ArrayNode jsonPosts = new ArrayNode(instance);
103                 for (Post post : newPosts) {
104                         ObjectNode jsonPost = new ObjectNode(instance);
105                         jsonPost.put("id", post.getId());
106                         jsonPost.put("sone", post.getSone().getId());
107                         jsonPost.put("recipient", post.getRecipientId().orNull());
108                         jsonPost.put("time", post.getTime());
109                         jsonPosts.add(jsonPost);
110                 }
111                 /* load new replies. */
112                 Collection<PostReply> newReplies = webInterface.getNewReplies();
113                 if (currentSone != null) {
114                         newReplies = Collections2.filter(newReplies, new Predicate<PostReply>() {
115
116                                 @Override
117                                 public boolean apply(PostReply reply) {
118                                         return ListNotificationFilters.isReplyVisible(currentSone, reply);
119                                 }
120
121                         });
122                 }
123                 /* remove replies to unknown posts. */
124                 newReplies = Collections2.filter(newReplies, PostReply.HAS_POST_FILTER);
125                 ArrayNode jsonReplies = new ArrayNode(instance);
126                 for (PostReply reply : newReplies) {
127                         ObjectNode jsonReply = new ObjectNode(instance);
128                         jsonReply.put("id", reply.getId());
129                         jsonReply.put("sone", reply.getSone().getId());
130                         jsonReply.put("post", reply.getPostId());
131                         jsonReply.put("postSone", reply.getPost().get().getSone().getId());
132                         jsonReplies.add(jsonReply);
133                 }
134                 return createSuccessJsonObject().put("loggedIn", currentSone != null).put("options", createJsonOptions(currentSone)).put("sones", jsonSones).put("notificationHash", notifications.hashCode()).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
135         }
136
137         @Override
138         protected boolean needsFormPassword() {
139                 return false;
140         }
141
142         @Override
143         protected boolean requiresLogin() {
144                 return false;
145         }
146
147         //
148         // PRIVATE METHODS
149         //
150
151         /**
152          * Creates a JSON object from the given Sone.
153          *
154          * @param sone
155          *            The Sone to convert to a JSON object
156          * @return The JSON representation of the given Sone
157          */
158         private JsonNode createJsonSone(Sone sone) {
159                 ObjectNode jsonSone = new ObjectNode(instance);
160                 jsonSone.put("id", sone.getId());
161                 jsonSone.put("name", SoneAccessor.getNiceName(sone));
162                 jsonSone.put("local", sone.getInsertUri() != null);
163                 jsonSone.put("status", sone.getStatus().name());
164                 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
165                 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
166                 jsonSone.put("lastUpdatedUnknown", sone.getTime() == 0);
167                 synchronized (dateFormat) {
168                         jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
169                 }
170                 jsonSone.put("lastUpdatedText", GetTimesAjaxPage.getTime(webInterface, sone.getTime()).getText());
171                 return jsonSone;
172         }
173
174         /**
175          * Creates a JSON object that contains all options that are currently in
176          * effect for the given Sone (or overall, if the given Sone is {@code null}
177          * ).
178          *
179          * @param currentSone
180          *            The current Sone (may be {@code null})
181          * @return The current options
182          */
183         private static JsonNode createJsonOptions(Sone currentSone) {
184                 ObjectNode options = new ObjectNode(instance);
185                 if (currentSone != null) {
186                         options.put("ShowNotification/NewSones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
187                         options.put("ShowNotification/NewPosts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
188                         options.put("ShowNotification/NewReplies", currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get());
189                 }
190                 return options;
191         }
192
193 }