2 * Sone - GetNotificationAjaxPage.java - Copyright Ā© 2010 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 java.io.IOException;
21 import java.io.StringWriter;
23 import net.pterodactylus.sone.data.Post;
24 import net.pterodactylus.sone.data.PostReply;
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.sone.main.SonePlugin;
27 import net.pterodactylus.sone.notify.ListNotification;
28 import net.pterodactylus.sone.notify.ListNotificationFilters;
29 import net.pterodactylus.sone.web.WebInterface;
30 import net.pterodactylus.sone.web.page.FreenetRequest;
31 import net.pterodactylus.util.json.JsonObject;
32 import net.pterodactylus.util.notify.Notification;
33 import net.pterodactylus.util.notify.TemplateNotification;
34 import net.pterodactylus.util.template.TemplateContext;
37 * The āget notificationā AJAX handler returns a number of rendered
40 * @author <a href="mailto:bombe@pterodactylus.net">David āBombeā Roden</a>
42 public class GetNotificationAjaxPage extends JsonPage {
45 * Creates a new āget notificationā AJAX page.
48 * The Sone web interface
50 public GetNotificationAjaxPage(WebInterface webInterface) {
51 super("getNotification.ajax", webInterface);
62 protected boolean needsFormPassword() {
70 protected boolean requiresLogin() {
78 @SuppressWarnings("unchecked")
79 protected JsonObject createJsonObject(FreenetRequest request) {
80 String[] notificationIds = request.getHttpRequest().getParam("notifications").split(",");
81 JsonObject jsonNotifications = new JsonObject();
82 Sone currentSone = getCurrentSone(request.getToadletContext(), false);
83 for (String notificationId : notificationIds) {
84 Notification notification = webInterface.getNotifications().getNotification(notificationId);
85 if (notification == null) {
89 if ("new-post-notification".equals(notificationId)) {
90 notification = ListNotificationFilters.filterNewPostNotification((ListNotification<Post>) notification, currentSone, false);
91 } else if ("new-reply-notification".equals(notificationId)) {
92 notification = ListNotificationFilters.filterNewReplyNotification((ListNotification<PostReply>) notification, currentSone);
93 } else if ("mention-notification".equals(notificationId)) {
94 notification = ListNotificationFilters.filterNewPostNotification((ListNotification<Post>) notification, currentSone, false);
96 if (notification == null) {
100 jsonNotifications.put(notificationId, createJsonNotification(request, notification));
102 return createSuccessJsonObject().put("notifications", jsonNotifications);
110 * Creates a JSON object from the given notification.
113 * The request to load the session from
114 * @param notification
115 * The notification to create a JSON object
116 * @return The JSON object
118 private JsonObject createJsonNotification(FreenetRequest request, Notification notification) {
119 JsonObject jsonNotification = new JsonObject();
120 jsonNotification.put("id", notification.getId());
121 StringWriter notificationWriter = new StringWriter();
123 if (notification instanceof TemplateNotification) {
124 TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext().mergeContext(((TemplateNotification) notification).getTemplateContext());
125 templateContext.set("core", webInterface.getCore());
126 templateContext.set("currentSone", webInterface.getCurrentSone(request.getToadletContext(), false));
127 templateContext.set("localSones", webInterface.getCore().getLocalSones());
128 templateContext.set("request", request);
129 templateContext.set("currentVersion", SonePlugin.VERSION);
130 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
131 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
132 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
133 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
134 templateContext.set("notification", notification);
135 ((TemplateNotification) notification).render(templateContext, notificationWriter);
137 notification.render(notificationWriter);
139 } catch (IOException ioe1) {
140 /* StringWriter never throws, ignore. */
142 jsonNotification.put("text", notificationWriter.toString());
143 jsonNotification.put("createdTime", notification.getCreatedTime());
144 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
145 jsonNotification.put("dismissable", notification.isDismissable());
146 return jsonNotification;