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