2 * Sone - GetStatusAjaxPage.java - Copyright © 2010–2013 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 static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
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;
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;
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;
47 * The “get status” AJAX handler returns all information that is necessary to
48 * update the web interface in real-time.
50 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52 public class GetStatusAjaxPage extends JsonPage {
54 /** Date formatter. */
55 private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
58 * Creates a new “get status” AJAX handler.
61 * The Sone web interface
63 public GetStatusAjaxPage(WebInterface webInterface) {
64 super("getStatus.ajax", webInterface);
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());
80 ArrayNode jsonSones = new ArrayNode(instance);
81 for (Sone sone : sones) {
85 jsonSones.add(createJsonSone(sone));
87 /* load notifications. */
88 List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone);
89 Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
91 Collection<Post> newPosts = webInterface.getNewPosts();
92 if (currentSone != null) {
93 newPosts = Collections2.filter(newPosts, new Predicate<Post>() {
96 public boolean apply(Post post) {
97 return ListNotificationFilters.isPostVisible(currentSone, post);
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);
111 /* load new replies. */
112 Collection<PostReply> newReplies = webInterface.getNewReplies();
113 if (currentSone != null) {
114 newReplies = Collections2.filter(newReplies, new Predicate<PostReply>() {
117 public boolean apply(PostReply reply) {
118 return ListNotificationFilters.isReplyVisible(currentSone, reply);
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);
134 return createSuccessJsonObject().put("loggedIn", currentSone != null).put("options", createJsonOptions(currentSone)).put("sones", jsonSones).put("notificationHash", notifications.hashCode()).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
138 protected boolean needsFormPassword() {
143 protected boolean requiresLogin() {
152 * Creates a JSON object from the given Sone.
155 * The Sone to convert to a JSON object
156 * @return The JSON representation of the given Sone
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.isLocal());
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())));
170 jsonSone.put("lastUpdatedText", GetTimesAjaxPage.getTime(webInterface, sone.getTime()).getText());
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}
180 * The current Sone (may be {@code null})
181 * @return The current options
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());