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