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