Return an optional Sone from the current session.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetStatusAjaxPage.java
1 /*
2  * Sone - GetStatusAjaxPage.java - Copyright © 2010–2013 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 static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
21
22 import java.text.DateFormat;
23 import java.text.SimpleDateFormat;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30
31 import net.pterodactylus.sone.data.Post;
32 import net.pterodactylus.sone.data.PostReply;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.sone.notify.ListNotificationFilters;
35 import net.pterodactylus.sone.template.SoneAccessor;
36 import net.pterodactylus.sone.web.WebInterface;
37 import net.pterodactylus.sone.web.page.FreenetRequest;
38 import net.pterodactylus.util.notify.Notification;
39
40 import com.fasterxml.jackson.databind.JsonNode;
41 import com.fasterxml.jackson.databind.node.ArrayNode;
42 import com.fasterxml.jackson.databind.node.ObjectNode;
43 import com.google.common.base.Optional;
44 import com.google.common.base.Predicate;
45 import com.google.common.collect.Collections2;
46
47 /**
48  * The “get status” AJAX handler returns all information that is necessary to
49  * update the web interface in real-time.
50  *
51  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52  */
53 public class GetStatusAjaxPage extends JsonPage {
54
55         /** Date formatter. */
56         private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
57
58         /**
59          * Creates a new “get status” AJAX handler.
60          *
61          * @param webInterface
62          *            The Sone web interface
63          */
64         public GetStatusAjaxPage(WebInterface webInterface) {
65                 super("getStatus.ajax", webInterface);
66         }
67
68         /**
69          * {@inheritDoc}
70          */
71         @Override
72         protected JsonReturnObject createJsonObject(FreenetRequest request) {
73                 final Optional<Sone> currentSone = getCurrentSone(request.getToadletContext(), false);
74                 /* load Sones. always return the status of the current Sone. */
75                 Set<Sone> sones = new HashSet<Sone>(currentSone.asSet());
76                 String loadSoneIds = request.getHttpRequest().getParam("soneIds");
77                 if (loadSoneIds.length() > 0) {
78                         String[] soneIds = loadSoneIds.split(",");
79                         for (String soneId : soneIds) {
80                                 /* just add it, we skip null further down. */
81                                 sones.add(webInterface.getCore().getSone(soneId).orNull());
82                         }
83                 }
84                 ArrayNode jsonSones = new ArrayNode(instance);
85                 for (Sone sone : sones) {
86                         if (sone == null) {
87                                 continue;
88                         }
89                         jsonSones.add(createJsonSone(sone));
90                 }
91                 /* load notifications. */
92                 List<Notification> notifications = ListNotificationFilters.filterNotifications(webInterface.getNotifications().getNotifications(), currentSone.orNull());
93                 Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
94                 /* load new posts. */
95                 Collection<Post> newPosts = webInterface.getNewPosts();
96                 if (currentSone.isPresent()) {
97                         newPosts = Collections2.filter(newPosts, new Predicate<Post>() {
98
99                                 @Override
100                                 public boolean apply(Post post) {
101                                         return ListNotificationFilters.isPostVisible(currentSone.get(), post);
102                                 }
103
104                         });
105                 }
106                 ArrayNode jsonPosts = new ArrayNode(instance);
107                 for (Post post : newPosts) {
108                         ObjectNode jsonPost = new ObjectNode(instance);
109                         jsonPost.put("id", post.getId());
110                         jsonPost.put("sone", post.getSone().getId());
111                         jsonPost.put("recipient", post.getRecipientId().orNull());
112                         jsonPost.put("time", post.getTime());
113                         jsonPosts.add(jsonPost);
114                 }
115                 /* load new replies. */
116                 Collection<PostReply> newReplies = webInterface.getNewReplies();
117                 if (currentSone.isPresent()) {
118                         newReplies = Collections2.filter(newReplies, new Predicate<PostReply>() {
119
120                                 @Override
121                                 public boolean apply(PostReply reply) {
122                                         return ListNotificationFilters.isReplyVisible(currentSone.get(), reply);
123                                 }
124
125                         });
126                 }
127                 /* remove replies to unknown posts. */
128                 newReplies = Collections2.filter(newReplies, PostReply.HAS_POST_FILTER);
129                 ArrayNode jsonReplies = new ArrayNode(instance);
130                 for (PostReply reply : newReplies) {
131                         ObjectNode jsonReply = new ObjectNode(instance);
132                         jsonReply.put("id", reply.getId());
133                         jsonReply.put("sone", reply.getSone().getId());
134                         jsonReply.put("post", reply.getPostId());
135                         jsonReply.put("postSone", reply.getPost().get().getSone().getId());
136                         jsonReplies.add(jsonReply);
137                 }
138                 return createSuccessJsonObject().put("loggedIn", currentSone.isPresent()).put("options", createJsonOptions(currentSone)).put("sones", jsonSones).put("notificationHash", notifications.hashCode()).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
139         }
140
141         /**
142          * {@inheritDoc}
143          */
144         @Override
145         protected boolean needsFormPassword() {
146                 return false;
147         }
148
149         /**
150          * {@inheritDoc}
151          */
152         @Override
153         protected boolean requiresLogin() {
154                 return false;
155         }
156
157         //
158         // PRIVATE METHODS
159         //
160
161         /**
162          * Creates a JSON object from the given Sone.
163          *
164          * @param sone
165          *            The Sone to convert to a JSON object
166          * @return The JSON representation of the given Sone
167          */
168         private JsonNode createJsonSone(Sone sone) {
169                 ObjectNode jsonSone = new ObjectNode(instance);
170                 jsonSone.put("id", sone.getId());
171                 jsonSone.put("name", SoneAccessor.getNiceName(sone));
172                 jsonSone.put("local", sone.getInsertUri() != null);
173                 jsonSone.put("status", sone.getStatus().name());
174                 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
175                 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
176                 jsonSone.put("lastUpdatedUnknown", sone.getTime() == 0);
177                 synchronized (dateFormat) {
178                         jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
179                 }
180                 jsonSone.put("lastUpdatedText", GetTimesAjaxPage.getTime(webInterface, sone.getTime()).getText());
181                 return jsonSone;
182         }
183
184         /**
185          * Creates a JSON object that contains all options that are currently in
186          * effect for the given Sone (or overall, if the given Sone is {@code null}
187          * ).
188          *
189          * @param currentSone
190          *            The current Sone (may be {@code null})
191          * @return The current options
192          */
193         private static JsonNode createJsonOptions(Optional<Sone> currentSone) {
194                 ObjectNode options = new ObjectNode(instance);
195                 if (currentSone.isPresent()) {
196                         options.put("ShowNotification/NewSones", currentSone.get().getOptions().isShowNewSoneNotifications());
197                         options.put("ShowNotification/NewPosts", currentSone.get().getOptions().isShowNewPostNotifications());
198                         options.put("ShowNotification/NewReplies", currentSone.get().getOptions().isShowNewReplyNotifications());
199                 }
200                 return options;
201         }
202
203 }