Remove replies to unknown posts from status.
[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.Collections;
23 import java.util.Date;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27
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;
39
40 /**
41  * The “get status” AJAX handler returns all information that is necessary to
42  * update the web interface in real-time.
43  *
44  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45  */
46 public class GetStatusAjaxPage extends JsonPage {
47
48         /** Date formatter. */
49         private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
50
51         /**
52          * Creates a new “get status” AJAX handler.
53          *
54          * @param webInterface
55          *            The Sone web interface
56          */
57         public GetStatusAjaxPage(WebInterface webInterface) {
58                 super("getStatus.ajax", webInterface);
59         }
60
61         /**
62          * {@inheritDoc}
63          */
64         @Override
65         protected JsonObject createJsonObject(Request request) {
66                 final Sone currentSone = getCurrentSone(request.getToadletContext(), false);
67                 /* load Sones. */
68                 boolean loadAllSones = Boolean.parseBoolean(request.getHttpRequest().getParam("loadAllSones", "false"));
69                 Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
70                 if (loadAllSones) {
71                         sones.addAll(webInterface.getCore().getSones());
72                 } else {
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));
79                                 }
80                         }
81                 }
82                 JsonArray jsonSones = new JsonArray();
83                 for (Sone sone : sones) {
84                         if (sone == null) {
85                                 continue;
86                         }
87                         JsonObject jsonSone = createJsonSone(sone);
88                         jsonSones.add(jsonSone);
89                 }
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));
96                 }
97                 /* load new posts. */
98                 Set<Post> newPosts = webInterface.getNewPosts();
99                 if (currentSone != null) {
100                         newPosts = Filters.filteredSet(newPosts, new Filter<Post>() {
101
102                                 @Override
103                                 public boolean filterObject(Post post) {
104                                         return ListNotificationFilters.isPostVisible(currentSone, post);
105                                 }
106
107                         });
108                 }
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);
117                 }
118                 /* load new replies. */
119                 Set<Reply> newReplies = webInterface.getNewReplies();
120                 if (currentSone != null) {
121                         newReplies = Filters.filteredSet(newReplies, new Filter<Reply>() {
122
123                                 @Override
124                                 public boolean filterObject(Reply reply) {
125                                         return ListNotificationFilters.isReplyVisible(currentSone, reply);
126                                 }
127
128                         });
129                 }
130                 /* remove replies to unknown posts. */
131                 newReplies = Filters.filteredSet(newReplies, new Filter<Reply>() {
132
133                         @Override
134                         public boolean filterObject(Reply reply) {
135                                 return reply.getPost() != null;
136                         }
137                 });
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);
146                 }
147                 return createSuccessJsonObject().put("loggedIn", currentSone != null).put("sones", jsonSones).put("notifications", jsonNotificationInformations).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
148         }
149
150         /**
151          * {@inheritDoc}
152          */
153         @Override
154         protected boolean needsFormPassword() {
155                 return false;
156         }
157
158         /**
159          * {@inheritDoc}
160          */
161         @Override
162         protected boolean requiresLogin() {
163                 return false;
164         }
165
166         //
167         // PRIVATE METHODS
168         //
169
170         /**
171          * Creates a JSON object from the given Sone.
172          *
173          * @param sone
174          *            The Sone to convert to a JSON object
175          * @return The JSON representation of the given Sone
176          */
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())));
188                 }
189                 jsonSone.put("lastUpdatedText", GetTimesAjaxPage.getTime(webInterface, System.currentTimeMillis() - sone.getTime()).getText());
190                 return jsonSone;
191         }
192
193         /**
194          * Creates a JSON object that only contains the ID and the last-updated time
195          * of the given notification.
196          *
197          * @see Notification#getId()
198          * @see Notification#getLastUpdatedTime()
199          * @param notification
200          *            The notification
201          * @return A JSON object containing the notification ID and last-updated
202          *         time
203          */
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;
209         }
210
211 }