Rename session-related Methode in web interface
[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.freenet.L10nFilter;
36 import net.pterodactylus.sone.template.SoneAccessor;
37 import net.pterodactylus.sone.text.TimeText;
38 import net.pterodactylus.sone.text.TimeTextConverter;
39 import net.pterodactylus.sone.web.WebInterface;
40 import net.pterodactylus.sone.web.page.FreenetRequest;
41 import net.pterodactylus.util.notify.Notification;
42
43 import com.fasterxml.jackson.databind.JsonNode;
44 import com.fasterxml.jackson.databind.node.ArrayNode;
45 import com.fasterxml.jackson.databind.node.ObjectNode;
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         private final TimeTextConverter timeTextConverter;
58         private final L10nFilter l10nFilter;
59
60         /**
61          * Creates a new “get status” AJAX handler.
62          *
63          * @param webInterface
64          *            The Sone web interface
65          */
66         public GetStatusAjaxPage(WebInterface webInterface, TimeTextConverter timeTextConverter, L10nFilter l10nFilter) {
67                 super("getStatus.ajax", webInterface);
68                 this.timeTextConverter = timeTextConverter;
69                 this.l10nFilter = l10nFilter;
70         }
71
72         /**
73          * {@inheritDoc}
74          */
75         @Override
76         protected JsonReturnObject createJsonObject(FreenetRequest request) {
77                 final Sone currentSone = getCurrentSoneWithoutCreatingSession(request.getToadletContext());
78                 /* load Sones. always return the status of the current Sone. */
79                 Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSoneWithoutCreatingSession(request.getToadletContext())));
80                 String loadSoneIds = request.getHttpRequest().getParam("soneIds");
81                 if (loadSoneIds.length() > 0) {
82                         String[] soneIds = loadSoneIds.split(",");
83                         for (String soneId : soneIds) {
84                                 /* just add it, we skip null further down. */
85                                 sones.add(webInterface.getCore().getSone(soneId).orNull());
86                         }
87                 }
88                 ArrayNode jsonSones = new ArrayNode(instance);
89                 for (Sone sone : sones) {
90                         if (sone == null) {
91                                 continue;
92                         }
93                         jsonSones.add(createJsonSone(sone));
94                 }
95                 /* load notifications. */
96                 List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications(currentSone));
97                 Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
98                 /* load new posts. */
99                 Collection<Post> newPosts = webInterface.getNewPosts(getCurrentSoneWithoutCreatingSession(request.getToadletContext()));
100
101                 ArrayNode jsonPosts = new ArrayNode(instance);
102                 for (Post post : newPosts) {
103                         ObjectNode jsonPost = new ObjectNode(instance);
104                         jsonPost.put("id", post.getId());
105                         jsonPost.put("sone", post.getSone().getId());
106                         jsonPost.put("recipient", post.getRecipientId().orNull());
107                         jsonPost.put("time", post.getTime());
108                         jsonPosts.add(jsonPost);
109                 }
110                 /* load new replies. */
111                 Collection<PostReply> newReplies = webInterface.getNewReplies(getCurrentSoneWithoutCreatingSession(request.getToadletContext()));
112
113                 ArrayNode jsonReplies = new ArrayNode(instance);
114                 for (PostReply reply : newReplies) {
115                         ObjectNode jsonReply = new ObjectNode(instance);
116                         jsonReply.put("id", reply.getId());
117                         jsonReply.put("sone", reply.getSone().getId());
118                         jsonReply.put("post", reply.getPostId());
119                         jsonReply.put("postSone", reply.getPost().get().getSone().getId());
120                         jsonReplies.add(jsonReply);
121                 }
122                 return createSuccessJsonObject().put("loggedIn", currentSone != null).put("options", createJsonOptions(currentSone)).put("sones", jsonSones).put("notificationHash", notifications.hashCode()).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
123         }
124
125         /**
126          * {@inheritDoc}
127          */
128         @Override
129         protected boolean needsFormPassword() {
130                 return false;
131         }
132
133         /**
134          * {@inheritDoc}
135          */
136         @Override
137         protected boolean requiresLogin() {
138                 return false;
139         }
140
141         //
142         // PRIVATE METHODS
143         //
144
145         /**
146          * Creates a JSON object from the given Sone.
147          *
148          * @param sone
149          *            The Sone to convert to a JSON object
150          * @return The JSON representation of the given Sone
151          */
152         private JsonNode createJsonSone(Sone sone) {
153                 ObjectNode jsonSone = new ObjectNode(instance);
154                 jsonSone.put("id", sone.getId());
155                 jsonSone.put("name", SoneAccessor.getNiceName(sone));
156                 jsonSone.put("local", sone.getInsertUri() != null);
157                 jsonSone.put("status", sone.getStatus().name());
158                 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
159                 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
160                 jsonSone.put("lastUpdatedUnknown", sone.getTime() == 0);
161                 synchronized (dateFormat) {
162                         jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
163                 }
164                 TimeText timeText = timeTextConverter.getTimeText(sone.getTime());
165                 jsonSone.put("lastUpdatedText", l10nFilter.format(null, timeText.getL10nText(), Collections.<String, Object>emptyMap()));
166                 return jsonSone;
167         }
168
169         /**
170          * Creates a JSON object that contains all options that are currently in
171          * effect for the given Sone (or overall, if the given Sone is {@code null}
172          * ).
173          *
174          * @param currentSone
175          *            The current Sone (may be {@code null})
176          * @return The current options
177          */
178         private static JsonNode createJsonOptions(Sone currentSone) {
179                 ObjectNode options = new ObjectNode(instance);
180                 if (currentSone != null) {
181                         options.put("ShowNotification/NewSones", currentSone.getOptions().isShowNewSoneNotifications());
182                         options.put("ShowNotification/NewPosts", currentSone.getOptions().isShowNewPostNotifications());
183                         options.put("ShowNotification/NewReplies", currentSone.getOptions().isShowNewReplyNotifications());
184                 }
185                 return options;
186         }
187
188 }