If no current Sone exists, return an error.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetNotificationsAjaxPage.java
1 /*
2  * Sone - GetNotificationsAjaxPage.java - Copyright © 2011–2013 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 static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
21
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;
27
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;
36
37 import com.fasterxml.jackson.databind.JsonNode;
38 import com.fasterxml.jackson.databind.node.ArrayNode;
39 import com.fasterxml.jackson.databind.node.ObjectNode;
40
41 /**
42  * AJAX handler to return all current notifications.
43  *
44  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45  */
46 public class GetNotificationsAjaxPage extends JsonPage {
47
48         /**
49          * Creates a new “get notifications” AJAX handler.
50          *
51          * @param webInterface
52          *            The Sone web interface
53          */
54         public GetNotificationsAjaxPage(WebInterface webInterface) {
55                 super("getNotifications.ajax", webInterface);
56         }
57
58         //
59         // JSONPAGE METHODS
60         //
61
62         @Override
63         protected boolean needsFormPassword() {
64                 return false;
65         }
66
67         @Override
68         protected boolean requiresLogin() {
69                 return false;
70         }
71
72         @Override
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));
81                 }
82                 return createSuccessJsonObject().put("notificationHash", filteredNotifications.hashCode()).put("notifications", jsonNotifications).put("options", createJsonOptions(currentSone));
83         }
84
85         //
86         // PRIVATE METHODS
87         //
88
89         /**
90          * Creates a JSON object from the given notification.
91          *
92          * @param request
93          *            The request to load the session from
94          * @param notification
95          *            The notification to create a JSON object
96          * @return The JSON object
97          */
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();
102                 try {
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);
116                         } else {
117                                 notification.render(notificationWriter);
118                         }
119                 } catch (IOException ioe1) {
120                         /* StringWriter never throws, ignore. */
121                 }
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;
127         }
128
129         /**
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}
132          * ).
133          *
134          * @param currentSone
135          *            The current Sone (may be {@code null})
136          * @return The current options
137          */
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());
144                 }
145                 return options;
146         }
147
148 }