Merge branch 'release-0.6.2'
[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", "true"));
70                 Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
71                 if (loadAllSones) {
72                         sones.addAll(webInterface.getCore().getSones());
73                 }
74                 JsonArray jsonSones = new JsonArray();
75                 for (Sone sone : sones) {
76                         if (sone == null) {
77                                 continue;
78                         }
79                         JsonObject jsonSone = createJsonSone(sone);
80                         jsonSones.add(jsonSone);
81                 }
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));
88                 }
89                 /* load new posts. */
90                 Set<Post> newPosts = webInterface.getNewPosts();
91                 if (currentSone != null) {
92                         newPosts = Filters.filteredSet(newPosts, new Filter<Post>() {
93
94                                 @Override
95                                 public boolean filterObject(Post post) {
96                                         return currentSone.hasFriend(post.getSone().getId()) || currentSone.equals(post.getSone()) || currentSone.equals(post.getRecipient());
97                                 }
98
99                         });
100                 }
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);
109                 }
110                 /* load new replies. */
111                 Set<Reply> newReplies = webInterface.getNewReplies();
112                 if (currentSone != null) {
113                         newReplies = Filters.filteredSet(newReplies, new Filter<Reply>() {
114
115                                 @Override
116                                 public boolean filterObject(Reply reply) {
117                                         return currentSone.hasFriend(reply.getPost().getSone().getId()) || currentSone.equals(reply.getPost().getSone()) || currentSone.equals(reply.getPost().getRecipient());
118                                 }
119
120                         });
121                 }
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);
130                 }
131                 return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotificationInformations).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
132         }
133
134         /**
135          * {@inheritDoc}
136          */
137         @Override
138         protected boolean needsFormPassword() {
139                 return false;
140         }
141
142         /**
143          * {@inheritDoc}
144          */
145         @Override
146         protected boolean requiresLogin() {
147                 return false;
148         }
149
150         //
151         // PRIVATE METHODS
152         //
153
154         /**
155          * Creates a JSON object from the given Sone.
156          *
157          * @param sone
158          *            The Sone to convert to a JSON object
159          * @return The JSON representation of the given Sone
160          */
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())));
172                 }
173                 jsonSone.put("lastUpdatedText", GetTimesAjaxPage.getTime(webInterface, System.currentTimeMillis() - sone.getTime()).getText());
174                 return jsonSone;
175         }
176
177         /**
178          * Creates a JSON object that only contains the ID and the last-updated time
179          * of the given notification.
180          *
181          * @see Notification#getId()
182          * @see Notification#getLastUpdatedTime()
183          * @param notification
184          *            The notification
185          * @return A JSON object containing the notification ID and last-updated
186          *         time
187          */
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;
193         }
194
195 }