Fix logging of exception.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetNotificationsAjaxPage.java
1 /*
2  * Sone - GetNotificationsAjaxPage.java - Copyright © 2011–2012 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.Collections;
24 import java.util.List;
25
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.main.SonePlugin;
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.JsonArray;
32 import net.pterodactylus.util.json.JsonObject;
33 import net.pterodactylus.util.notify.Notification;
34 import net.pterodactylus.util.notify.TemplateNotification;
35 import net.pterodactylus.util.object.HashCode;
36 import net.pterodactylus.util.template.TemplateContext;
37
38 /**
39  * AJAX handler to return all current notifications.
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class GetNotificationsAjaxPage extends JsonPage {
44
45         /**
46          * Creates a new “get notifications” AJAX handler.
47          *
48          * @param webInterface
49          *            The Sone web interface
50          */
51         public GetNotificationsAjaxPage(WebInterface webInterface) {
52                 super("getNotifications.ajax", webInterface);
53         }
54
55         //
56         // JSONPAGE METHODS
57         //
58
59         /**
60          * {@inheritDoc}
61          */
62         @Override
63         protected boolean needsFormPassword() {
64                 return false;
65         }
66
67         /**
68          * {@inheritDoc}
69          */
70         @Override
71         protected boolean requiresLogin() {
72                 return false;
73         }
74
75         /**
76          * {@inheritDoc}
77          */
78         @Override
79         protected JsonObject createJsonObject(FreenetRequest request) {
80                 Sone currentSone = getCurrentSone(request.getToadletContext(), false);
81                 Collection<Notification> notifications = webInterface.getNotifications().getNotifications();
82                 List<Notification> filteredNotifications = ListNotificationFilters.filterNotifications(notifications, currentSone);
83                 Collections.sort(filteredNotifications, Notification.CREATED_TIME_SORTER);
84                 int notificationHash = HashCode.hashCode(filteredNotifications);
85                 JsonArray jsonNotifications = new JsonArray();
86                 for (Notification notification : filteredNotifications) {
87                         jsonNotifications.add(createJsonNotification(request, notification));
88                 }
89                 return createSuccessJsonObject().put("notificationHash", notificationHash).put("notifications", jsonNotifications).put("options", createJsonOptions(currentSone));
90         }
91
92         //
93         // PRIVATE METHODS
94         //
95
96         /**
97          * Creates a JSON object from the given notification.
98          *
99          * @param request
100          *            The request to load the session from
101          * @param notification
102          *            The notification to create a JSON object
103          * @return The JSON object
104          */
105         private JsonObject createJsonNotification(FreenetRequest request, Notification notification) {
106                 JsonObject jsonNotification = new JsonObject();
107                 jsonNotification.put("id", notification.getId());
108                 StringWriter notificationWriter = new StringWriter();
109                 try {
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);
123                         } else {
124                                 notification.render(notificationWriter);
125                         }
126                 } catch (IOException ioe1) {
127                         /* StringWriter never throws, ignore. */
128                 }
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;
134         }
135
136         /**
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}
139          * ).
140          *
141          * @param currentSone
142          *            The current Sone (may be {@code null})
143          * @return The current options
144          */
145         private JsonObject createJsonOptions(Sone currentSone) {
146                 JsonObject options = new JsonObject();
147                 if (currentSone != null) {
148                         options.put("ShowNotification/NewSones", currentSone.getOptions().getBooleanOption("ShowNotification/NewSones").get());
149                         options.put("ShowNotification/NewPosts", currentSone.getOptions().getBooleanOption("ShowNotification/NewPosts").get());
150                         options.put("ShowNotification/NewReplies", currentSone.getOptions().getBooleanOption("ShowNotification/NewReplies").get());
151                 }
152                 return options;
153         }
154
155 }