Merge branch 'configurable-notifications-250' 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.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.PostReply;
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.sone.web.page.FreenetRequest;
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(FreenetRequest 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(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<PostReply> newReplies = webInterface.getNewReplies();
121                 if (currentSone != null) {
122                         newReplies = Filters.filteredSet(newReplies, new Filter<PostReply>() {
123
124                                 @Override
125                                 public boolean filterObject(PostReply reply) {
126                                         return ListNotificationFilters.isReplyVisible(currentSone, reply);
127                                 }
128
129                         });
130                 }
131                 /* remove replies to unknown posts. */
132                 newReplies = Filters.filteredSet(newReplies, new Filter<PostReply>() {
133
134                         @Override
135                         public boolean filterObject(PostReply reply) {
136                                 return (reply.getPost() != null) && (reply.getPost().getSone() != null);
137                         }
138                 });
139                 JsonArray jsonReplies = new JsonArray();
140                 for (PostReply reply : newReplies) {
141                         JsonObject jsonReply = new JsonObject();
142                         jsonReply.put("id", reply.getId());
143                         jsonReply.put("sone", reply.getSone().getId());
144                         jsonReply.put("post", reply.getPost().getId());
145                         jsonReply.put("postSone", reply.getPost().getSone().getId());
146                         jsonReplies.add(jsonReply);
147                 }
148                 return createSuccessJsonObject().put("loggedIn", currentSone != null).put("options", createJsonOptions(currentSone)).put("sones", jsonSones).put("notifications", jsonNotificationInformations).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
149         }
150
151         /**
152          * {@inheritDoc}
153          */
154         @Override
155         protected boolean needsFormPassword() {
156                 return false;
157         }
158
159         /**
160          * {@inheritDoc}
161          */
162         @Override
163         protected boolean requiresLogin() {
164                 return false;
165         }
166
167         //
168         // PRIVATE METHODS
169         //
170
171         /**
172          * Creates a JSON object from the given Sone.
173          *
174          * @param sone
175          *            The Sone to convert to a JSON object
176          * @return The JSON representation of the given Sone
177          */
178         private JsonObject createJsonSone(Sone sone) {
179                 JsonObject jsonSone = new JsonObject();
180                 jsonSone.put("id", sone.getId());
181                 jsonSone.put("name", SoneAccessor.getNiceName(sone));
182                 jsonSone.put("local", sone.getInsertUri() != null);
183                 jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).name());
184                 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
185                 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
186                 jsonSone.put("lastUpdatedUnknown", sone.getTime() == 0);
187                 synchronized (dateFormat) {
188                         jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
189                 }
190                 jsonSone.put("lastUpdatedText", GetTimesAjaxPage.getTime(webInterface, sone.getTime()).getText());
191                 return jsonSone;
192         }
193
194         /**
195          * Creates a JSON object that only contains the ID and the last-updated time
196          * of the given notification.
197          *
198          * @see Notification#getId()
199          * @see Notification#getLastUpdatedTime()
200          * @param notification
201          *            The notification
202          * @return A JSON object containing the notification ID and last-updated
203          *         time
204          */
205         private JsonObject createJsonNotificationInformation(Notification notification) {
206                 JsonObject jsonNotification = new JsonObject();
207                 jsonNotification.put("id", notification.getId());
208                 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
209                 return jsonNotification;
210         }
211
212         /**
213          * Creates a JSON object that contains all options that are currently in
214          * effect for the given Sone (or overall, if the given Sone is {@code null}
215          * ).
216          *
217          * @param currentSone
218          *            The current Sone (may be {@code null})
219          * @return The current options
220          */
221         private JsonObject createJsonOptions(Sone currentSone) {
222                 JsonObject options = new JsonObject();
223                 if (currentSone != null) {
224                         options.put("ShowNotification/NewSones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
225                         options.put("ShowNotification/NewPosts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
226                         options.put("ShowNotification/NewReplies", currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get());
227                 }
228                 return options;
229         }
230
231 }