Update years in copyright line
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetNotificationsAjaxPage.java
1 /*
2  * Sone - GetNotificationsAjaxPage.java - Copyright © 2011–2015 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         /**
63          * {@inheritDoc}
64          */
65         @Override
66         protected boolean needsFormPassword() {
67                 return false;
68         }
69
70         /**
71          * {@inheritDoc}
72          */
73         @Override
74         protected boolean requiresLogin() {
75                 return false;
76         }
77
78         /**
79          * {@inheritDoc}
80          */
81         @Override
82         protected JsonReturnObject createJsonObject(FreenetRequest request) {
83                 Sone currentSone = getCurrentSone(request.getToadletContext(), false);
84                 Collection<Notification> notifications = webInterface.getNotifications().getNotifications();
85                 List<Notification> filteredNotifications = ListNotificationFilters.filterNotifications(notifications, currentSone);
86                 Collections.sort(filteredNotifications, Notification.CREATED_TIME_SORTER);
87                 ArrayNode jsonNotifications = new ArrayNode(instance);
88                 for (Notification notification : filteredNotifications) {
89                         jsonNotifications.add(createJsonNotification(request, notification));
90                 }
91                 return createSuccessJsonObject().put("notificationHash", filteredNotifications.hashCode()).put("notifications", jsonNotifications).put("options", createJsonOptions(currentSone));
92         }
93
94         //
95         // PRIVATE METHODS
96         //
97
98         /**
99          * Creates a JSON object from the given notification.
100          *
101          * @param request
102          *            The request to load the session from
103          * @param notification
104          *            The notification to create a JSON object
105          * @return The JSON object
106          */
107         private JsonNode createJsonNotification(FreenetRequest request, Notification notification) {
108                 ObjectNode jsonNotification = new ObjectNode(instance);
109                 jsonNotification.put("id", notification.getId());
110                 StringWriter notificationWriter = new StringWriter();
111                 try {
112                         if (notification instanceof TemplateNotification) {
113                                 TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext().mergeContext(((TemplateNotification) notification).getTemplateContext());
114                                 templateContext.set("core", webInterface.getCore());
115                                 templateContext.set("currentSone", webInterface.getCurrentSone(request.getToadletContext(), false));
116                                 templateContext.set("localSones", webInterface.getCore().getLocalSones());
117                                 templateContext.set("request", request);
118                                 templateContext.set("currentVersion", SonePlugin.VERSION);
119                                 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
120                                 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
121                                 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
122                                 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
123                                 templateContext.set("notification", notification);
124                                 ((TemplateNotification) notification).render(templateContext, notificationWriter);
125                         } else {
126                                 notification.render(notificationWriter);
127                         }
128                 } catch (IOException ioe1) {
129                         /* StringWriter never throws, ignore. */
130                 }
131                 jsonNotification.put("text", notificationWriter.toString());
132                 jsonNotification.put("createdTime", notification.getCreatedTime());
133                 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
134                 jsonNotification.put("dismissable", notification.isDismissable());
135                 return jsonNotification;
136         }
137
138         /**
139          * Creates a JSON object that contains all options that are currently in
140          * effect for the given Sone (or overall, if the given Sone is {@code null}
141          * ).
142          *
143          * @param currentSone
144          *            The current Sone (may be {@code null})
145          * @return The current options
146          */
147         private static JsonNode createJsonOptions(Sone currentSone) {
148                 ObjectNode options = new ObjectNode(instance);
149                 if (currentSone != null) {
150                         options.put("ShowNotification/NewSones", currentSone.getOptions().isShowNewSoneNotifications());
151                         options.put("ShowNotification/NewPosts", currentSone.getOptions().isShowNewPostNotifications());
152                         options.put("ShowNotification/NewReplies", currentSone.getOptions().isShowNewReplyNotifications());
153                 }
154                 return options;
155         }
156
157 }