2 * Sone - GetNotificationsAjaxPage.java - Copyright © 2011–2013 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.io.IOException;
23 import java.io.StringWriter;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.sone.main.SonePlugin;
30 import net.pterodactylus.sone.notify.ListNotificationFilters;
31 import net.pterodactylus.sone.web.WebInterface;
32 import net.pterodactylus.sone.web.page.FreenetRequest;
33 import net.pterodactylus.util.notify.Notification;
34 import net.pterodactylus.util.notify.TemplateNotification;
35 import net.pterodactylus.util.template.TemplateContext;
37 import com.fasterxml.jackson.databind.JsonNode;
38 import com.fasterxml.jackson.databind.node.ArrayNode;
39 import com.fasterxml.jackson.databind.node.ObjectNode;
42 * AJAX handler to return all current notifications.
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public class GetNotificationsAjaxPage extends JsonPage {
49 * Creates a new “get notifications” AJAX handler.
52 * The Sone web interface
54 public GetNotificationsAjaxPage(WebInterface webInterface) {
55 super("getNotifications.ajax", webInterface);
63 protected boolean needsFormPassword() {
68 protected boolean requiresLogin() {
73 protected JsonReturnObject createJsonObject(FreenetRequest request) {
74 Sone currentSone = getCurrentSone(request.getToadletContext(), false);
75 Collection<Notification> notifications = webInterface.getNotifications().getNotifications();
76 List<Notification> filteredNotifications = ListNotificationFilters.filterNotifications(notifications, currentSone);
77 Collections.sort(filteredNotifications, Notification.CREATED_TIME_SORTER);
78 ArrayNode jsonNotifications = new ArrayNode(instance);
79 for (Notification notification : filteredNotifications) {
80 jsonNotifications.add(createJsonNotification(request, notification));
82 return createSuccessJsonObject().put("notificationHash", filteredNotifications.hashCode()).put("notifications", jsonNotifications).put("options", createJsonOptions(currentSone));
90 * Creates a JSON object from the given notification.
93 * The request to load the session from
95 * The notification to create a JSON object
96 * @return The JSON object
98 private JsonNode createJsonNotification(FreenetRequest request, Notification notification) {
99 ObjectNode jsonNotification = new ObjectNode(instance);
100 jsonNotification.put("id", notification.getId());
101 StringWriter notificationWriter = new StringWriter();
103 if (notification instanceof TemplateNotification) {
104 TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext().mergeContext(((TemplateNotification) notification).getTemplateContext());
105 templateContext.set("core", webInterface.getCore());
106 templateContext.set("currentSone", webInterface.getCurrentSone(request.getToadletContext(), false));
107 templateContext.set("localSones", webInterface.getCore().getLocalSones());
108 templateContext.set("request", request);
109 templateContext.set("currentVersion", SonePlugin.VERSION);
110 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
111 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
112 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
113 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
114 templateContext.set("notification", notification);
115 ((TemplateNotification) notification).render(templateContext, notificationWriter);
117 notification.render(notificationWriter);
119 } catch (IOException ioe1) {
120 /* StringWriter never throws, ignore. */
122 jsonNotification.put("text", notificationWriter.toString());
123 jsonNotification.put("createdTime", notification.getCreatedTime());
124 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
125 jsonNotification.put("dismissable", notification.isDismissable());
126 return jsonNotification;
130 * Creates a JSON object that contains all options that are currently in
131 * effect for the given Sone (or overall, if the given Sone is {@code null}
135 * The current Sone (may be {@code null})
136 * @return The current options
138 private static JsonNode createJsonOptions(Sone currentSone) {
139 ObjectNode options = new ObjectNode(instance);
140 if (currentSone != null) {
141 options.put("ShowNotification/NewSones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
142 options.put("ShowNotification/NewPosts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
143 options.put("ShowNotification/NewReplies", currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get());