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 java.text.DateFormat;
21 import java.text.SimpleDateFormat;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Date;
25 import java.util.HashSet;
26 import java.util.List;
29 import net.pterodactylus.sone.data.Post;
30 import net.pterodactylus.sone.data.PostReply;
31 import net.pterodactylus.sone.data.Sone;
32 import net.pterodactylus.sone.notify.ListNotificationFilters;
33 import net.pterodactylus.sone.template.SoneAccessor;
34 import net.pterodactylus.sone.web.WebInterface;
35 import net.pterodactylus.sone.web.page.FreenetRequest;
36 import net.pterodactylus.util.json.JsonArray;
37 import net.pterodactylus.util.json.JsonObject;
38 import net.pterodactylus.util.notify.Notification;
39 import net.pterodactylus.util.object.HashCode;
41 import com.google.common.base.Predicate;
42 import com.google.common.collect.Collections2;
45 * The “get status” AJAX handler returns all information that is necessary to
46 * update the web interface in real-time.
48 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
50 public class GetStatusAjaxPage extends JsonPage {
52 /** Date formatter. */
53 private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
56 * Creates a new “get status” AJAX handler.
59 * The Sone web interface
61 public GetStatusAjaxPage(WebInterface webInterface) {
62 super("getStatus.ajax", webInterface);
69 protected JsonObject createJsonObject(FreenetRequest request) {
70 final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
71 /* load Sones. always return the status of the current Sone. */
72 Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
73 String loadSoneIds = request.getHttpRequest().getParam("soneIds");
74 if (loadSoneIds.length() > 0) {
75 String[] soneIds = loadSoneIds.split(",");
76 for (String soneId : soneIds) {
77 /* just add it, we skip null further down. */
78 sones.add(webInterface.getCore().getSone(soneId, false));
81 JsonArray jsonSones = new JsonArray();
82 for (Sone sone : sones) {
86 JsonObject jsonSone = createJsonSone(sone);
87 jsonSones.add(jsonSone);
89 /* load notifications. */
90 List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone);
91 Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
92 int notificationHash = HashCode.hashCode(notifications);
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 JsonArray jsonPosts = new JsonArray();
106 for (Post post : newPosts) {
107 JsonObject jsonPost = new JsonObject();
108 jsonPost.put("id", post.getId());
109 jsonPost.put("sone", post.getSone().getId());
110 jsonPost.put("recipient", (post.getRecipient() != null) ? post.getRecipient().getId() : null);
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, new Predicate<PostReply>() {
130 public boolean apply(PostReply reply) {
131 return (reply.getPost() != null) && (reply.getPost().getSone() != null);
134 JsonArray jsonReplies = new JsonArray();
135 for (PostReply reply : newReplies) {
136 JsonObject jsonReply = new JsonObject();
137 jsonReply.put("id", reply.getId());
138 jsonReply.put("sone", reply.getSone().getId());
139 jsonReply.put("post", reply.getPost().getId());
140 jsonReply.put("postSone", reply.getPost().getSone().getId());
141 jsonReplies.add(jsonReply);
143 return createSuccessJsonObject().put("loggedIn", currentSone != null).put("options", createJsonOptions(currentSone)).put("sones", jsonSones).put("notificationHash", notificationHash).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
150 protected boolean needsFormPassword() {
158 protected boolean requiresLogin() {
167 * Creates a JSON object from the given Sone.
170 * The Sone to convert to a JSON object
171 * @return The JSON representation of the given Sone
173 private JsonObject createJsonSone(Sone sone) {
174 JsonObject jsonSone = new JsonObject();
175 jsonSone.put("id", sone.getId());
176 jsonSone.put("name", SoneAccessor.getNiceName(sone));
177 jsonSone.put("local", sone.getInsertUri() != null);
178 jsonSone.put("status", sone.getStatus().name());
179 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
180 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
181 jsonSone.put("lastUpdatedUnknown", sone.getTime() == 0);
182 synchronized (dateFormat) {
183 jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
185 jsonSone.put("lastUpdatedText", GetTimesAjaxPage.getTime(webInterface, sone.getTime()).getText());
190 * Creates a JSON object that contains all options that are currently in
191 * effect for the given Sone (or overall, if the given Sone is {@code null}
195 * The current Sone (may be {@code null})
196 * @return The current options
198 private static JsonObject createJsonOptions(Sone currentSone) {
199 JsonObject options = new JsonObject();
200 if (currentSone != null) {
201 options.put("ShowNotification/NewSones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
202 options.put("ShowNotification/NewPosts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
203 options.put("ShowNotification/NewReplies", currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get());