d0020ca78ef94313c487a2355f62867c65cc673e
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetNotificationsAjaxPage.java
1 /*
2  * Sone - GetNotificationsAjaxPage.java - Copyright © 2011 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 java.io.IOException;
21 import java.io.StringWriter;
22 import java.util.Collection;
23 import java.util.List;
24
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.sone.main.SonePlugin;
27 import net.pterodactylus.sone.notify.ListNotificationFilters;
28 import net.pterodactylus.sone.web.WebInterface;
29 import net.pterodactylus.sone.web.page.FreenetRequest;
30 import net.pterodactylus.util.json.JsonArray;
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.object.HashCode;
35 import net.pterodactylus.util.template.TemplateContext;
36
37 /**
38  * AJAX handler to return all current notifications.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class GetNotificationsAjaxPage extends JsonPage {
43
44         /**
45          * Creates a new “get notifications” AJAX handler.
46          *
47          * @param webInterface
48          *            The Sone web interface
49          */
50         public GetNotificationsAjaxPage(WebInterface webInterface) {
51                 super("getNotifications.ajax", webInterface);
52         }
53
54         //
55         // JSONPAGE METHODS
56         //
57
58         /**
59          * {@inheritDoc}
60          */
61         @Override
62         protected boolean needsFormPassword() {
63                 return false;
64         }
65
66         /**
67          * {@inheritDoc}
68          */
69         @Override
70         protected boolean requiresLogin() {
71                 return false;
72         }
73
74         /**
75          * {@inheritDoc}
76          */
77         @Override
78         protected JsonObject createJsonObject(FreenetRequest request) {
79                 Sone currentSone = getCurrentSone(request.getToadletContext(), false);
80                 Collection<Notification> notifications = webInterface.getNotifications().getNotifications();
81                 List<Notification> filteredNotifications = ListNotificationFilters.filterNotifications(notifications, currentSone);
82                 int notificationHash = HashCode.hashCode(filteredNotifications);
83                 JsonArray jsonNotifications = new JsonArray();
84                 for (Notification notification : filteredNotifications) {
85                         jsonNotifications.add(createJsonNotification(request, notification));
86                 }
87                 return createSuccessJsonObject().put("notificationHash", notificationHash).put("notifications", jsonNotifications).put("options", createJsonOptions(currentSone));
88         }
89
90         //
91         // PRIVATE METHODS
92         //
93
94         /**
95          * Creates a JSON object from the given notification.
96          *
97          * @param request
98          *            The request to load the session from
99          * @param notification
100          *            The notification to create a JSON object
101          * @return The JSON object
102          */
103         private JsonObject createJsonNotification(FreenetRequest request, Notification notification) {
104                 JsonObject jsonNotification = new JsonObject();
105                 jsonNotification.put("id", notification.getId());
106                 StringWriter notificationWriter = new StringWriter();
107                 try {
108                         if (notification instanceof TemplateNotification) {
109                                 TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext().mergeContext(((TemplateNotification) notification).getTemplateContext());
110                                 templateContext.set("core", webInterface.getCore());
111                                 templateContext.set("currentSone", webInterface.getCurrentSone(request.getToadletContext(), false));
112                                 templateContext.set("localSones", webInterface.getCore().getLocalSones());
113                                 templateContext.set("request", request);
114                                 templateContext.set("currentVersion", SonePlugin.VERSION);
115                                 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
116                                 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
117                                 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
118                                 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
119                                 templateContext.set("notification", notification);
120                                 ((TemplateNotification) notification).render(templateContext, notificationWriter);
121                         } else {
122                                 notification.render(notificationWriter);
123                         }
124                 } catch (IOException ioe1) {
125                         /* StringWriter never throws, ignore. */
126                 }
127                 jsonNotification.put("text", notificationWriter.toString());
128                 jsonNotification.put("createdTime", notification.getCreatedTime());
129                 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
130                 jsonNotification.put("dismissable", notification.isDismissable());
131                 return jsonNotification;
132         }
133
134         /**
135          * Creates a JSON object that contains all options that are currently in
136          * effect for the given Sone (or overall, if the given Sone is {@code null}
137          * ).
138          *
139          * @param currentSone
140          *            The current Sone (may be {@code null})
141          * @return The current options
142          */
143         private JsonObject createJsonOptions(Sone currentSone) {
144                 JsonObject options = new JsonObject();
145                 if (currentSone != null) {
146                         options.put("ShowNotification/NewSones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
147                         options.put("ShowNotification/NewPosts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
148                         options.put("ShowNotification/NewReplies", currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get());
149                 }
150                 return options;
151         }
152
153 }