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 /* remove replies to unknown posts. */
131 newReplies = Filters.filteredSet(newReplies, new Filter<Reply>() {
134 public boolean filterObject(Reply reply) {
135 return reply.getPost() != null;
138 JsonArray jsonReplies = new JsonArray();
139 for (Reply reply : newReplies) {
140 JsonObject jsonReply = new JsonObject();
141 jsonReply.put("id", reply.getId());
142 jsonReply.put("sone", reply.getSone().getId());
143 jsonReply.put("post", reply.getPost().getId());
144 jsonReply.put("postSone", reply.getPost().getSone().getId());
145 jsonReplies.add(jsonReply);
147 return createSuccessJsonObject().put("loggedIn", currentSone != null).put("sones", jsonSones).put("notifications", jsonNotificationInformations).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
154 protected boolean needsFormPassword() {
162 protected boolean requiresLogin() {
171 * Creates a JSON object from the given Sone.
174 * The Sone to convert to a JSON object
175 * @return The JSON representation of the given Sone
177 private JsonObject createJsonSone(Sone sone) {
178 JsonObject jsonSone = new JsonObject();
179 jsonSone.put("id", sone.getId());
180 jsonSone.put("name", SoneAccessor.getNiceName(sone));
181 jsonSone.put("local", sone.getInsertUri() != null);
182 jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).name());
183 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
184 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
185 jsonSone.put("lastUpdatedUnknown", sone.getTime() == 0);
186 synchronized (dateFormat) {
187 jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
189 jsonSone.put("lastUpdatedText", GetTimesAjaxPage.getTime(webInterface, System.currentTimeMillis() - sone.getTime()).getText());
194 * Creates a JSON object that only contains the ID and the last-updated time
195 * of the given notification.
197 * @see Notification#getId()
198 * @see Notification#getLastUpdatedTime()
199 * @param notification
201 * @return A JSON object containing the notification ID and last-updated
204 private JsonObject createJsonNotificationInformation(Notification notification) {
205 JsonObject jsonNotification = new JsonObject();
206 jsonNotification.put("id", notification.getId());
207 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
208 return jsonNotification;