2 * Sone - GetStatusAjaxPage.java - Copyright © 2010–2016 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web.ajax;
20 import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
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;
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;
43 import com.fasterxml.jackson.databind.JsonNode;
44 import com.fasterxml.jackson.databind.node.ArrayNode;
45 import com.fasterxml.jackson.databind.node.ObjectNode;
48 * The “get status” AJAX handler returns all information that is necessary to
49 * update the web interface in real-time.
51 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53 public class GetStatusAjaxPage extends JsonPage {
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;
61 * Creates a new “get status” AJAX handler.
64 * The Sone web interface
66 public GetStatusAjaxPage(WebInterface webInterface, TimeTextConverter timeTextConverter, L10nFilter l10nFilter) {
67 super("getStatus.ajax", webInterface);
68 this.timeTextConverter = timeTextConverter;
69 this.l10nFilter = l10nFilter;
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());
88 ArrayNode jsonSones = new ArrayNode(instance);
89 for (Sone sone : sones) {
93 jsonSones.add(createJsonSone(sone));
95 /* load notifications. */
96 List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications(currentSone));
97 Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
99 Collection<Post> newPosts = webInterface.getNewPosts(getCurrentSoneWithoutCreatingSession(request.getToadletContext()));
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);
110 /* load new replies. */
111 Collection<PostReply> newReplies = webInterface.getNewReplies(getCurrentSoneWithoutCreatingSession(request.getToadletContext()));
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);
122 return createSuccessJsonObject().put("loggedIn", currentSone != null).put("options", createJsonOptions(currentSone)).put("sones", jsonSones).put("notificationHash", notifications.hashCode()).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
129 protected boolean needsFormPassword() {
137 protected boolean requiresLogin() {
146 * Creates a JSON object from the given Sone.
149 * The Sone to convert to a JSON object
150 * @return The JSON representation of the given Sone
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())));
164 TimeText timeText = timeTextConverter.getTimeText(sone.getTime());
165 jsonSone.put("lastUpdatedText", l10nFilter.format(null, timeText.getL10nText(), Collections.<String, Object>emptyMap()));
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}
175 * The current Sone (may be {@code null})
176 * @return The current options
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());