2 * Sone - GetNotificationsAjaxPage.java - Copyright © 2011–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.io.IOException;
23 import java.io.StringWriter;
24 import java.util.ArrayList;
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.web.WebInterface;
31 import net.pterodactylus.sone.web.page.FreenetRequest;
32 import net.pterodactylus.util.notify.Notification;
33 import net.pterodactylus.util.notify.TemplateNotification;
34 import net.pterodactylus.util.template.TemplateContext;
36 import com.fasterxml.jackson.databind.JsonNode;
37 import com.fasterxml.jackson.databind.node.ArrayNode;
38 import com.fasterxml.jackson.databind.node.ObjectNode;
41 * AJAX handler to return all current notifications.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class GetNotificationsAjaxPage extends JsonPage {
48 * Creates a new “get notifications” AJAX handler.
51 * The Sone web interface
53 public GetNotificationsAjaxPage(WebInterface webInterface) {
54 super("getNotifications.ajax", webInterface);
65 protected boolean needsFormPassword() {
73 protected boolean requiresLogin() {
81 protected JsonReturnObject createJsonObject(FreenetRequest request) {
82 Sone currentSone = getCurrentSone(request.getToadletContext(), false);
83 List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications(currentSone));
84 Collections.sort(notifications, Notification.CREATED_TIME_SORTER);
85 ArrayNode jsonNotifications = new ArrayNode(instance);
86 for (Notification notification : notifications) {
87 jsonNotifications.add(createJsonNotification(request, notification));
89 return createSuccessJsonObject().put("notificationHash", notifications.hashCode()).put("notifications", jsonNotifications).put("options", createJsonOptions(currentSone));
97 * Creates a JSON object from the given notification.
100 * The request to load the session from
101 * @param notification
102 * The notification to create a JSON object
103 * @return The JSON object
105 private JsonNode createJsonNotification(FreenetRequest request, Notification notification) {
106 ObjectNode jsonNotification = new ObjectNode(instance);
107 jsonNotification.put("id", notification.getId());
108 StringWriter notificationWriter = new StringWriter();
110 if (notification instanceof TemplateNotification) {
111 TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext().mergeContext(((TemplateNotification) notification).getTemplateContext());
112 templateContext.set("core", webInterface.getCore());
113 templateContext.set("currentSone", webInterface.getCurrentSone(request.getToadletContext(), false));
114 templateContext.set("localSones", webInterface.getCore().getLocalSones());
115 templateContext.set("request", request);
116 templateContext.set("currentVersion", SonePlugin.VERSION);
117 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
118 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
119 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
120 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
121 templateContext.set("notification", notification);
122 ((TemplateNotification) notification).render(templateContext, notificationWriter);
124 notification.render(notificationWriter);
126 } catch (IOException ioe1) {
127 /* StringWriter never throws, ignore. */
129 jsonNotification.put("text", notificationWriter.toString());
130 jsonNotification.put("createdTime", notification.getCreatedTime());
131 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
132 jsonNotification.put("dismissable", notification.isDismissable());
133 return jsonNotification;
137 * Creates a JSON object that contains all options that are currently in
138 * effect for the given Sone (or overall, if the given Sone is {@code null}
142 * The current Sone (may be {@code null})
143 * @return The current options
145 private static JsonNode createJsonOptions(Sone currentSone) {
146 ObjectNode options = new ObjectNode(instance);
147 if (currentSone != null) {
148 options.put("ShowNotification/NewSones", currentSone.getOptions().isShowNewSoneNotifications());
149 options.put("ShowNotification/NewPosts", currentSone.getOptions().isShowNewPostNotifications());
150 options.put("ShowNotification/NewReplies", currentSone.getOptions().isShowNewReplyNotifications());