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.Arrays;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Date;
28 import java.util.HashSet;
29 import java.util.List;
32 import net.pterodactylus.sone.data.Post;
33 import net.pterodactylus.sone.data.PostReply;
34 import net.pterodactylus.sone.data.Sone;
35 import net.pterodactylus.sone.notify.ListNotificationFilters;
36 import net.pterodactylus.sone.template.SoneAccessor;
37 import net.pterodactylus.sone.utils.Optionals;
38 import net.pterodactylus.sone.web.WebInterface;
39 import net.pterodactylus.sone.web.page.FreenetRequest;
40 import net.pterodactylus.util.notify.Notification;
42 import com.fasterxml.jackson.databind.JsonNode;
43 import com.fasterxml.jackson.databind.node.ArrayNode;
44 import com.fasterxml.jackson.databind.node.ObjectNode;
45 import com.google.common.base.Optional;
46 import com.google.common.base.Predicate;
47 import com.google.common.collect.Collections2;
48 import com.google.common.collect.FluentIterable;
51 * The “get status” AJAX handler returns all information that is necessary to
52 * update the web interface in real-time.
54 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
56 public class GetStatusAjaxPage extends JsonPage {
58 /** Date formatter. */
59 private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
62 * Creates a new “get status” AJAX handler.
65 * The Sone web interface
67 public GetStatusAjaxPage(WebInterface webInterface) {
68 super("getStatus.ajax", webInterface);
75 protected JsonReturnObject createJsonObject(FreenetRequest request) {
76 final Optional<Sone> currentSone = getCurrentSone(request.getToadletContext(), false);
77 /* load Sones. always return the status of the current Sone. */
78 Set<Sone> sones = new HashSet<Sone>(currentSone.asSet());
79 String loadSoneIds = request.getHttpRequest().getParam("soneIds");
80 if (loadSoneIds.length() > 0) {
81 String[] soneIds = loadSoneIds.split(",");
82 FluentIterable.from(Arrays.asList(soneIds))
83 .transform(webInterface.getCore().soneLoader())
84 .filter(Optionals.isPresent())
85 .transform(Optionals.<Sone>get())
88 ArrayNode jsonSones = new ArrayNode(instance);
89 for (Sone sone : sones) {
90 jsonSones.add(createJsonSone(sone));
92 /* load notifications. */
93 List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone.orNull());
94 Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
96 Collection<Post> newPosts = webInterface.getNewPosts();
97 if (currentSone.isPresent()) {
98 newPosts = Collections2.filter(newPosts, new Predicate<Post>() {
101 public boolean apply(Post post) {
102 return ListNotificationFilters.isPostVisible(currentSone.get(), post);
107 ArrayNode jsonPosts = new ArrayNode(instance);
108 for (Post post : newPosts) {
109 ObjectNode jsonPost = new ObjectNode(instance);
110 jsonPost.put("id", post.getId());
111 jsonPost.put("sone", post.getSone().getId());
112 jsonPost.put("recipient", post.getRecipientId().orNull());
113 jsonPost.put("time", post.getTime());
114 jsonPosts.add(jsonPost);
116 /* load new replies. */
117 Collection<PostReply> newReplies = webInterface.getNewReplies();
118 if (currentSone.isPresent()) {
119 newReplies = Collections2.filter(newReplies, new Predicate<PostReply>() {
122 public boolean apply(PostReply reply) {
123 return ListNotificationFilters.isReplyVisible(currentSone.get(), reply);
128 /* remove replies to unknown posts. */
129 newReplies = Collections2.filter(newReplies, PostReply.HAS_POST_FILTER);
130 ArrayNode jsonReplies = new ArrayNode(instance);
131 for (PostReply reply : newReplies) {
132 ObjectNode jsonReply = new ObjectNode(instance);
133 jsonReply.put("id", reply.getId());
134 jsonReply.put("sone", reply.getSone().getId());
135 jsonReply.put("post", reply.getPostId());
136 jsonReply.put("postSone", reply.getPost().get().getSone().getId());
137 jsonReplies.add(jsonReply);
139 return createSuccessJsonObject().put("loggedIn", currentSone.isPresent()).put("options", createJsonOptions(currentSone)).put("sones", jsonSones).put("notificationHash", notifications.hashCode()).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
146 protected boolean needsFormPassword() {
154 protected boolean requiresLogin() {
163 * Creates a JSON object from the given Sone.
166 * The Sone to convert to a JSON object
167 * @return The JSON representation of the given Sone
169 private JsonNode createJsonSone(Sone sone) {
170 ObjectNode jsonSone = new ObjectNode(instance);
171 jsonSone.put("id", sone.getId());
172 jsonSone.put("name", SoneAccessor.getNiceName(sone));
173 jsonSone.put("local", sone.getInsertUri() != null);
174 jsonSone.put("status", sone.getStatus().name());
175 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
176 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
177 jsonSone.put("lastUpdatedUnknown", sone.getTime() == 0);
178 synchronized (dateFormat) {
179 jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
181 jsonSone.put("lastUpdatedText", GetTimesAjaxPage.getTime(webInterface, sone.getTime()).getText());
186 * Creates a JSON object that contains all options that are currently in
187 * effect for the given Sone (or overall, if the given Sone is {@code null}
191 * The current Sone (may be {@code null})
192 * @return The current options
194 private static JsonNode createJsonOptions(Optional<Sone> currentSone) {
195 ObjectNode options = new ObjectNode(instance);
196 if (currentSone.isPresent()) {
197 options.put("ShowNotification/NewSones", currentSone.get().getOptions().isShowNewSoneNotifications());
198 options.put("ShowNotification/NewPosts", currentSone.get().getOptions().isShowNewPostNotifications());
199 options.put("ShowNotification/NewReplies", currentSone.get().getOptions().isShowNewReplyNotifications());