2 * Sone - GetStatusAjaxPage.java - Copyright © 2010 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web.ajax;
20 import java.text.DateFormat;
21 import java.text.SimpleDateFormat;
22 import java.util.Collections;
23 import java.util.Date;
24 import java.util.HashSet;
25 import java.util.List;
28 import net.pterodactylus.sone.data.Post;
29 import net.pterodactylus.sone.data.PostReply;
30 import net.pterodactylus.sone.data.Sone;
31 import net.pterodactylus.sone.notify.ListNotificationFilters;
32 import net.pterodactylus.sone.template.SoneAccessor;
33 import net.pterodactylus.sone.web.WebInterface;
34 import net.pterodactylus.sone.web.page.FreenetRequest;
35 import net.pterodactylus.util.filter.Filter;
36 import net.pterodactylus.util.filter.Filters;
37 import net.pterodactylus.util.json.JsonArray;
38 import net.pterodactylus.util.json.JsonObject;
39 import net.pterodactylus.util.notify.Notification;
42 * The “get status” AJAX handler returns all information that is necessary to
43 * update the web interface in real-time.
45 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public class GetStatusAjaxPage extends JsonPage {
49 /** Date formatter. */
50 private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
53 * Creates a new “get status” AJAX handler.
56 * The Sone web interface
58 public GetStatusAjaxPage(WebInterface webInterface) {
59 super("getStatus.ajax", webInterface);
66 protected JsonObject createJsonObject(FreenetRequest request) {
67 final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
69 boolean loadAllSones = Boolean.parseBoolean(request.getHttpRequest().getParam("loadAllSones", "false"));
70 Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
72 sones.addAll(webInterface.getCore().getSones());
74 String loadSoneIds = request.getHttpRequest().getParam("soneIds");
75 if (loadSoneIds.length() > 0) {
76 String[] soneIds = loadSoneIds.split(",");
77 for (String soneId : soneIds) {
78 /* just add it, we skip null further down. */
79 sones.add(webInterface.getCore().getSone(soneId, false));
83 JsonArray jsonSones = new JsonArray();
84 for (Sone sone : sones) {
88 JsonObject jsonSone = createJsonSone(sone);
89 jsonSones.add(jsonSone);
91 /* load notifications. */
92 List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone);
93 Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER);
94 JsonArray jsonNotificationInformations = new JsonArray();
95 for (Notification notification : notifications) {
96 jsonNotificationInformations.add(createJsonNotificationInformation(notification));
99 Set<Post> newPosts = webInterface.getNewPosts();
100 if (currentSone != null) {
101 newPosts = Filters.filteredSet(newPosts, new Filter<Post>() {
104 public boolean filterObject(Post post) {
105 return ListNotificationFilters.isPostVisible(currentSone, post);
110 JsonArray jsonPosts = new JsonArray();
111 for (Post post : newPosts) {
112 JsonObject jsonPost = new JsonObject();
113 jsonPost.put("id", post.getId());
114 jsonPost.put("sone", post.getSone().getId());
115 jsonPost.put("recipient", (post.getRecipient() != null) ? post.getRecipient().getId() : null);
116 jsonPost.put("time", post.getTime());
117 jsonPosts.add(jsonPost);
119 /* load new replies. */
120 Set<PostReply> newReplies = webInterface.getNewReplies();
121 if (currentSone != null) {
122 newReplies = Filters.filteredSet(newReplies, new Filter<PostReply>() {
125 public boolean filterObject(PostReply reply) {
126 return ListNotificationFilters.isReplyVisible(currentSone, reply);
131 /* remove replies to unknown posts. */
132 newReplies = Filters.filteredSet(newReplies, new Filter<PostReply>() {
135 public boolean filterObject(PostReply reply) {
136 return (reply.getPost() != null) && (reply.getPost().getSone() != null);
139 JsonArray jsonReplies = new JsonArray();
140 for (PostReply reply : newReplies) {
141 JsonObject jsonReply = new JsonObject();
142 jsonReply.put("id", reply.getId());
143 jsonReply.put("sone", reply.getSone().getId());
144 jsonReply.put("post", reply.getPost().getId());
145 jsonReply.put("postSone", reply.getPost().getSone().getId());
146 jsonReplies.add(jsonReply);
148 return createSuccessJsonObject().put("loggedIn", currentSone != null).put("options", createJsonOptions(currentSone)).put("sones", jsonSones).put("notifications", jsonNotificationInformations).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
155 protected boolean needsFormPassword() {
163 protected boolean requiresLogin() {
172 * Creates a JSON object from the given Sone.
175 * The Sone to convert to a JSON object
176 * @return The JSON representation of the given Sone
178 private JsonObject createJsonSone(Sone sone) {
179 JsonObject jsonSone = new JsonObject();
180 jsonSone.put("id", sone.getId());
181 jsonSone.put("name", SoneAccessor.getNiceName(sone));
182 jsonSone.put("local", sone.getInsertUri() != null);
183 jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).name());
184 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
185 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
186 jsonSone.put("lastUpdatedUnknown", sone.getTime() == 0);
187 synchronized (dateFormat) {
188 jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
190 jsonSone.put("lastUpdatedText", GetTimesAjaxPage.getTime(webInterface, sone.getTime()).getText());
195 * Creates a JSON object that only contains the ID and the last-updated time
196 * of the given notification.
198 * @see Notification#getId()
199 * @see Notification#getLastUpdatedTime()
200 * @param notification
202 * @return A JSON object containing the notification ID and last-updated
205 private JsonObject createJsonNotificationInformation(Notification notification) {
206 JsonObject jsonNotification = new JsonObject();
207 jsonNotification.put("id", notification.getId());
208 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
209 return jsonNotification;
213 * Creates a JSON object that contains all options that are currently in
214 * effect for the given Sone (or overall, if the given Sone is {@code null}
218 * The current Sone (may be {@code null})
219 * @return The current options
221 private JsonObject createJsonOptions(Sone currentSone) {
222 JsonObject options = new JsonObject();
223 if (currentSone != null) {
224 options.put("ShowNotification/NewSones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
225 options.put("ShowNotification/NewPosts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
226 options.put("ShowNotification/NewReplies", currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get());