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.Reply;
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.util.filter.Filter;
35 import net.pterodactylus.util.filter.Filters;
36 import net.pterodactylus.util.json.JsonArray;
37 import net.pterodactylus.util.json.JsonObject;
38 import net.pterodactylus.util.notify.Notification;
41 * The “get status” AJAX handler returns all information that is necessary to
42 * update the web interface in real-time.
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public class GetStatusAjaxPage extends JsonPage {
48 /** Date formatter. */
49 private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
52 * Creates a new “get status” AJAX handler.
55 * The Sone web interface
57 public GetStatusAjaxPage(WebInterface webInterface) {
58 super("getStatus.ajax", webInterface);
65 protected JsonObject createJsonObject(Request request) {
66 final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
68 boolean loadAllSones = Boolean.parseBoolean(request.getHttpRequest().getParam("loadAllSones", "false"));
69 Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
71 sones.addAll(webInterface.getCore().getSones());
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));
82 JsonArray jsonSones = new JsonArray();
83 for (Sone sone : sones) {
87 JsonObject jsonSone = createJsonSone(sone);
88 jsonSones.add(jsonSone);
90 /* load notifications. */
91 List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone);
92 Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER);
93 JsonArray jsonNotificationInformations = new JsonArray();
94 for (Notification notification : notifications) {
95 jsonNotificationInformations.add(createJsonNotificationInformation(notification));
98 Set<Post> newPosts = webInterface.getNewPosts();
99 if (currentSone != null) {
100 newPosts = Filters.filteredSet(newPosts, new Filter<Post>() {
103 public boolean filterObject(Post post) {
104 return ListNotificationFilters.isPostVisible(currentSone, post);
109 JsonArray jsonPosts = new JsonArray();
110 for (Post post : newPosts) {
111 JsonObject jsonPost = new JsonObject();
112 jsonPost.put("id", post.getId());
113 jsonPost.put("sone", post.getSone().getId());
114 jsonPost.put("recipient", (post.getRecipient() != null) ? post.getRecipient().getId() : null);
115 jsonPost.put("time", post.getTime());
116 jsonPosts.add(jsonPost);
118 /* load new replies. */
119 Set<Reply> newReplies = webInterface.getNewReplies();
120 if (currentSone != null) {
121 newReplies = Filters.filteredSet(newReplies, new Filter<Reply>() {
124 public boolean filterObject(Reply reply) {
125 return ListNotificationFilters.isReplyVisible(currentSone, reply);
130 JsonArray jsonReplies = new JsonArray();
131 for (Reply reply : newReplies) {
132 JsonObject jsonReply = new JsonObject();
133 jsonReply.put("id", reply.getId());
134 jsonReply.put("sone", reply.getSone().getId());
135 jsonReply.put("post", reply.getPost().getId());
136 jsonReply.put("postSone", reply.getPost().getSone().getId());
137 jsonReplies.add(jsonReply);
139 return createSuccessJsonObject().put("loggedIn", currentSone != null).put("sones", jsonSones).put("notifications", jsonNotificationInformations).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 JsonObject createJsonSone(Sone sone) {
170 JsonObject jsonSone = new JsonObject();
171 jsonSone.put("id", sone.getId());
172 jsonSone.put("name", SoneAccessor.getNiceName(sone));
173 jsonSone.put("local", sone.getInsertUri() != null);
174 jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).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, System.currentTimeMillis() - sone.getTime()).getText());
186 * Creates a JSON object that only contains the ID and the last-updated time
187 * of the given notification.
189 * @see Notification#getId()
190 * @see Notification#getLastUpdatedTime()
191 * @param notification
193 * @return A JSON object containing the notification ID and last-updated
196 private JsonObject createJsonNotificationInformation(Notification notification) {
197 JsonObject jsonNotification = new JsonObject();
198 jsonNotification.put("id", notification.getId());
199 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
200 return jsonNotification;