Merge branch 'fcp-interface' into next
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetStatusAjaxPage.java
1 /*
2  * Sone - GetStatusAjaxPage.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.web.ajax;
19
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;
27 import java.util.Set;
28
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;
40
41 /**
42  * The “get status” AJAX handler returns all information that is necessary to
43  * update the web interface in real-time.
44  *
45  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46  */
47 public class GetStatusAjaxPage extends JsonPage {
48
49         /** Date formatter. */
50         private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
51
52         /**
53          * Creates a new “get status” AJAX handler.
54          *
55          * @param webInterface
56          *            The Sone web interface
57          */
58         public GetStatusAjaxPage(WebInterface webInterface) {
59                 super("getStatus.ajax", webInterface);
60         }
61
62         /**
63          * {@inheritDoc}
64          */
65         @Override
66         protected JsonObject createJsonObject(Request request) {
67                 final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
68                 /* load Sones. */
69                 boolean loadAllSones = Boolean.parseBoolean(request.getHttpRequest().getParam("loadAllSones", "false"));
70                 Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
71                 if (loadAllSones) {
72                         sones.addAll(webInterface.getCore().getSones());
73                 } else {
74                         String loadSoneIds = request.getHttpRequest().getParam("soneIds");
75                         if (loadSoneIds.length() > 0) {
76                                 String[] soneIds = loadSoneIds.split(",");
77                                 for (String soneId : soneIds) {
78                                         /* just add it, we skip null further down. */
79                                         sones.add(webInterface.getCore().getSone(soneId, false));
80                                 }
81                         }
82                 }
83                 JsonArray jsonSones = new JsonArray();
84                 for (Sone sone : sones) {
85                         if (sone == null) {
86                                 continue;
87                         }
88                         JsonObject jsonSone = createJsonSone(sone);
89                         jsonSones.add(jsonSone);
90                 }
91                 /* load notifications. */
92                 List<Notification> notifications = ListNotificationFilters.filterNotifications(new ArrayList<Notification>(webInterface.getNotifications().getNotifications()), currentSone);
93                 Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER);
94                 JsonArray jsonNotificationInformations = new JsonArray();
95                 for (Notification notification : notifications) {
96                         jsonNotificationInformations.add(createJsonNotificationInformation(notification));
97                 }
98                 /* load new posts. */
99                 Set<Post> newPosts = webInterface.getNewPosts();
100                 if (currentSone != null) {
101                         newPosts = Filters.filteredSet(newPosts, new Filter<Post>() {
102
103                                 @Override
104                                 public boolean filterObject(Post post) {
105                                         return ListNotificationFilters.isPostVisible(currentSone, post);
106                                 }
107
108                         });
109                 }
110                 JsonArray jsonPosts = new JsonArray();
111                 for (Post post : newPosts) {
112                         JsonObject jsonPost = new JsonObject();
113                         jsonPost.put("id", post.getId());
114                         jsonPost.put("sone", post.getSone().getId());
115                         jsonPost.put("recipient", (post.getRecipient() != null) ? post.getRecipient().getId() : null);
116                         jsonPost.put("time", post.getTime());
117                         jsonPosts.add(jsonPost);
118                 }
119                 /* load new replies. */
120                 Set<Reply> newReplies = webInterface.getNewReplies();
121                 if (currentSone != null) {
122                         newReplies = Filters.filteredSet(newReplies, new Filter<Reply>() {
123
124                                 @Override
125                                 public boolean filterObject(Reply reply) {
126                                         return ListNotificationFilters.isReplyVisible(currentSone, reply);
127                                 }
128
129                         });
130                 }
131                 JsonArray jsonReplies = new JsonArray();
132                 for (Reply reply : newReplies) {
133                         JsonObject jsonReply = new JsonObject();
134                         jsonReply.put("id", reply.getId());
135                         jsonReply.put("sone", reply.getSone().getId());
136                         jsonReply.put("post", reply.getPost().getId());
137                         jsonReply.put("postSone", reply.getPost().getSone().getId());
138                         jsonReplies.add(jsonReply);
139                 }
140                 return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotificationInformations).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
141         }
142
143         /**
144          * {@inheritDoc}
145          */
146         @Override
147         protected boolean needsFormPassword() {
148                 return false;
149         }
150
151         /**
152          * {@inheritDoc}
153          */
154         @Override
155         protected boolean requiresLogin() {
156                 return false;
157         }
158
159         //
160         // PRIVATE METHODS
161         //
162
163         /**
164          * Creates a JSON object from the given Sone.
165          *
166          * @param sone
167          *            The Sone to convert to a JSON object
168          * @return The JSON representation of the given Sone
169          */
170         private JsonObject createJsonSone(Sone sone) {
171                 JsonObject jsonSone = new JsonObject();
172                 jsonSone.put("id", sone.getId());
173                 jsonSone.put("name", SoneAccessor.getNiceName(sone));
174                 jsonSone.put("local", sone.getInsertUri() != null);
175                 jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).name());
176                 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
177                 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
178                 jsonSone.put("lastUpdatedUnknown", sone.getTime() == 0);
179                 synchronized (dateFormat) {
180                         jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
181                 }
182                 jsonSone.put("lastUpdatedText", GetTimesAjaxPage.getTime(webInterface, System.currentTimeMillis() - sone.getTime()).getText());
183                 return jsonSone;
184         }
185
186         /**
187          * Creates a JSON object that only contains the ID and the last-updated time
188          * of the given notification.
189          *
190          * @see Notification#getId()
191          * @see Notification#getLastUpdatedTime()
192          * @param notification
193          *            The notification
194          * @return A JSON object containing the notification ID and last-updated
195          *         time
196          */
197         private JsonObject createJsonNotificationInformation(Notification notification) {
198                 JsonObject jsonNotification = new JsonObject();
199                 jsonNotification.put("id", notification.getId());
200                 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
201                 return jsonNotification;
202         }
203
204 }