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