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);
71 protected JsonReturnObject createJsonObject(FreenetRequest request) {
72 final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
73 /* load Sones. always return the status of the current Sone. */
74 Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
75 String loadSoneIds = request.getHttpRequest().getParam("soneIds");
76 if (loadSoneIds.length() > 0) {
77 String[] soneIds = loadSoneIds.split(",");
78 for (String soneId : soneIds) {
79 /* just add it, we skip null further down. */
80 sones.add(webInterface.getCore().getSone(soneId).orNull());
83 ArrayNode jsonSones = new ArrayNode(instance);
84 for (Sone sone : sones) {
88 jsonSones.add(createJsonSone(sone));
90 /* load notifications. */
91 List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone);
92 Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
94 Collection<Post> newPosts = webInterface.getNewPosts();
95 if (currentSone != null) {
96 newPosts = Collections2.filter(newPosts, new Predicate<Post>() {
99 public boolean apply(Post post) {
100 return ListNotificationFilters.isPostVisible(currentSone, post);
105 ArrayNode jsonPosts = new ArrayNode(instance);
106 for (Post post : newPosts) {
107 ObjectNode jsonPost = new ObjectNode(instance);
108 jsonPost.put("id", post.getId());
109 jsonPost.put("sone", post.getSone().getId());
110 jsonPost.put("recipient", post.getRecipientId().orNull());
111 jsonPost.put("time", post.getTime());
112 jsonPosts.add(jsonPost);
114 /* load new replies. */
115 Collection<PostReply> newReplies = webInterface.getNewReplies();
116 if (currentSone != null) {
117 newReplies = Collections2.filter(newReplies, new Predicate<PostReply>() {
120 public boolean apply(PostReply reply) {
121 return ListNotificationFilters.isReplyVisible(currentSone, reply);
126 /* remove replies to unknown posts. */
127 newReplies = Collections2.filter(newReplies, PostReply.HAS_POST_FILTER);
128 ArrayNode jsonReplies = new ArrayNode(instance);
129 for (PostReply reply : newReplies) {
130 ObjectNode jsonReply = new ObjectNode(instance);
131 jsonReply.put("id", reply.getId());
132 jsonReply.put("sone", reply.getSone().getId());
133 jsonReply.put("post", reply.getPostId());
134 jsonReply.put("postSone", reply.getPost().get().getSone().getId());
135 jsonReplies.add(jsonReply);
137 return createSuccessJsonObject().put("loggedIn", currentSone != null).put("options", createJsonOptions(currentSone)).put("sones", jsonSones).put("notificationHash", notifications.hashCode()).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
144 protected boolean needsFormPassword() {
152 protected boolean requiresLogin() {
161 * Creates a JSON object from the given Sone.
164 * The Sone to convert to a JSON object
165 * @return The JSON representation of the given Sone
167 private JsonNode createJsonSone(Sone sone) {
168 ObjectNode jsonSone = new ObjectNode(instance);
169 jsonSone.put("id", sone.getId());
170 jsonSone.put("name", SoneAccessor.getNiceName(sone));
171 jsonSone.put("local", sone.getInsertUri() != null);
172 jsonSone.put("status", sone.getStatus().name());
173 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
174 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
175 jsonSone.put("lastUpdatedUnknown", sone.getTime() == 0);
176 synchronized (dateFormat) {
177 jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
179 jsonSone.put("lastUpdatedText", GetTimesAjaxPage.getTime(webInterface, sone.getTime()).getText());
184 * Creates a JSON object that contains all options that are currently in
185 * effect for the given Sone (or overall, if the given Sone is {@code null}
189 * The current Sone (may be {@code null})
190 * @return The current options
192 private static JsonNode createJsonOptions(Sone currentSone) {
193 ObjectNode options = new ObjectNode(instance);
194 if (currentSone != null) {
195 options.put("ShowNotification/NewSones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
196 options.put("ShowNotification/NewPosts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
197 options.put("ShowNotification/NewReplies", currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get());