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.ArrayList;
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.Reply;
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.util.filter.Filter;
36 import net.pterodactylus.util.filter.Filters;
37 import net.pterodactylus.util.json.JsonArray;
38 import net.pterodactylus.util.json.JsonObject;
39 import net.pterodactylus.util.notify.Notification;
42 * The “get status” AJAX handler returns all information that is necessary to
43 * update the web interface in real-time.
45 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public class GetStatusAjaxPage extends JsonPage {
49 /** Date formatter. */
50 private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
53 * Creates a new “get status” AJAX handler.
56 * The Sone web interface
58 public GetStatusAjaxPage(WebInterface webInterface) {
59 super("getStatus.ajax", webInterface);
66 protected JsonObject createJsonObject(Request request) {
67 final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
69 boolean loadAllSones = Boolean.parseBoolean(request.getHttpRequest().getParam("loadAllSones", "true"));
70 Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
72 sones.addAll(webInterface.getCore().getSones());
74 JsonArray jsonSones = new JsonArray();
75 for (Sone sone : sones) {
79 JsonObject jsonSone = createJsonSone(sone);
80 jsonSones.add(jsonSone);
82 /* load notifications. */
83 List<Notification> notifications = ListNotificationFilters.filterNotifications(new ArrayList<Notification>(webInterface.getNotifications().getNotifications()), currentSone);
84 Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER);
85 JsonArray jsonNotificationInformations = new JsonArray();
86 for (Notification notification : notifications) {
87 jsonNotificationInformations.add(createJsonNotificationInformation(notification));
90 Set<Post> newPosts = webInterface.getNewPosts();
91 if (currentSone != null) {
92 newPosts = Filters.filteredSet(newPosts, new Filter<Post>() {
95 public boolean filterObject(Post post) {
96 return currentSone.hasFriend(post.getSone().getId()) || currentSone.equals(post.getSone()) || currentSone.equals(post.getRecipient());
101 JsonArray jsonPosts = new JsonArray();
102 for (Post post : newPosts) {
103 JsonObject jsonPost = new JsonObject();
104 jsonPost.put("id", post.getId());
105 jsonPost.put("sone", post.getSone().getId());
106 jsonPost.put("recipient", (post.getRecipient() != null) ? post.getRecipient().getId() : null);
107 jsonPost.put("time", post.getTime());
108 jsonPosts.add(jsonPost);
110 /* load new replies. */
111 Set<Reply> newReplies = webInterface.getNewReplies();
112 if (currentSone != null) {
113 newReplies = Filters.filteredSet(newReplies, new Filter<Reply>() {
116 public boolean filterObject(Reply reply) {
117 return currentSone.hasFriend(reply.getPost().getSone().getId()) || currentSone.equals(reply.getPost().getSone()) || currentSone.equals(reply.getPost().getRecipient());
122 JsonArray jsonReplies = new JsonArray();
123 for (Reply reply : newReplies) {
124 JsonObject jsonReply = new JsonObject();
125 jsonReply.put("id", reply.getId());
126 jsonReply.put("sone", reply.getSone().getId());
127 jsonReply.put("post", reply.getPost().getId());
128 jsonReply.put("postSone", reply.getPost().getSone().getId());
129 jsonReplies.add(jsonReply);
131 return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotificationInformations).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
138 protected boolean needsFormPassword() {
146 protected boolean requiresLogin() {
155 * Creates a JSON object from the given Sone.
158 * The Sone to convert to a JSON object
159 * @return The JSON representation of the given Sone
161 private JsonObject createJsonSone(Sone sone) {
162 JsonObject jsonSone = new JsonObject();
163 jsonSone.put("id", sone.getId());
164 jsonSone.put("name", SoneAccessor.getNiceName(sone));
165 jsonSone.put("local", sone.getInsertUri() != null);
166 jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).name());
167 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
168 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
169 jsonSone.put("lastUpdatedUnknown", sone.getTime() == 0);
170 synchronized (dateFormat) {
171 jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
173 jsonSone.put("lastUpdatedText", GetTimesAjaxPage.getTime(webInterface, System.currentTimeMillis() - sone.getTime()).getText());
178 * Creates a JSON object that only contains the ID and the last-updated time
179 * of the given notification.
181 * @see Notification#getId()
182 * @see Notification#getLastUpdatedTime()
183 * @param notification
185 * @return A JSON object containing the notification ID and last-updated
188 private JsonObject createJsonNotificationInformation(Notification notification) {
189 JsonObject jsonNotification = new JsonObject();
190 jsonNotification.put("id", notification.getId());
191 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
192 return jsonNotification;