Add AJAX page that returns a number of notifications.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetNotificationAjaxPage.java
1 /*
2  * Sone - GetStatusAjaxPage.java - Copyright © 2010 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.ArrayList;
23
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.data.Reply;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.sone.main.SonePlugin;
28 import net.pterodactylus.sone.notify.ListNotification;
29 import net.pterodactylus.sone.notify.ListNotificationFilters;
30 import net.pterodactylus.sone.web.WebInterface;
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.template.TemplateContext;
35
36 /**
37  * The “get notification” AJAX handler returns a number of rendered
38  * notifications.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class GetNotificationAjaxPage extends JsonPage {
43
44         /**
45          * Creates a new “get notification” AJAX page.
46          *
47          * @param webInterface
48          *            The Sone web interface
49          */
50         public GetNotificationAjaxPage(WebInterface webInterface) {
51                 super("getNotification.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         @SuppressWarnings("unchecked")
79         protected JsonObject createJsonObject(Request request) {
80                 String[] notificationIds = request.getHttpRequest().getParam("notifications").split(",");
81                 JsonObject jsonNotifications = new JsonObject();
82                 Sone currentSone = webInterface.getCurrentSone(request.getToadletContext(), false);
83                 for (String notificationId : notificationIds) {
84                         Notification notification = webInterface.getNotifications().getNotification(notificationId);
85                         ListNotificationFilters.filterNotifications(new ArrayList<Notification>(), currentSone);
86                         if ("new-post-notification".equals(notificationId)) {
87                                 notification = ListNotificationFilters.filterNewPostNotification((ListNotification<Post>) notification, currentSone);
88                         } else if ("new-reply-notification".equals(notificationId)) {
89                                 notification = ListNotificationFilters.filterNewReplyNotification((ListNotification<Reply>) notification, currentSone);
90                         }
91                         if (notification == null) {
92                                 // TODO - show error
93                                 continue;
94                         }
95                         jsonNotifications.put(notificationId, createJsonNotification(request, notification));
96                 }
97                 return createSuccessJsonObject().put("notifications", jsonNotifications);
98         }
99
100         //
101         // PRIVATE METHODS
102         //
103
104         /**
105          * Creates a JSON object from the given notification.
106          *
107          * @param request
108          *            The request to load the session from
109          * @param notification
110          *            The notification to create a JSON object
111          * @return The JSON object
112          */
113         private JsonObject createJsonNotification(Request request, Notification notification) {
114                 JsonObject jsonNotification = new JsonObject();
115                 jsonNotification.put("id", notification.getId());
116                 StringWriter notificationWriter = new StringWriter();
117                 try {
118                         if (notification instanceof TemplateNotification) {
119                                 TemplateContext templateContext = webInterface.getTemplateContextFactory().createTemplateContext().mergeContext(((TemplateNotification) notification).getTemplateContext());
120                                 templateContext.set("currentSone", webInterface.getCurrentSone(request.getToadletContext(), false));
121                                 templateContext.set("localSones", webInterface.getCore().getLocalSones());
122                                 templateContext.set("request", request);
123                                 templateContext.set("currentVersion", SonePlugin.VERSION);
124                                 templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion());
125                                 templateContext.set("latestEdition", webInterface.getCore().getUpdateChecker().getLatestEdition());
126                                 templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion());
127                                 templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate());
128                                 templateContext.set("notification", notification);
129                                 ((TemplateNotification) notification).render(templateContext, notificationWriter);
130                         } else {
131                                 notification.render(notificationWriter);
132                         }
133                 } catch (IOException ioe1) {
134                         /* StringWriter never throws, ignore. */
135                 }
136                 jsonNotification.put("text", notificationWriter.toString());
137                 jsonNotification.put("createdTime", notification.getCreatedTime());
138                 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
139                 jsonNotification.put("dismissable", notification.isDismissable());
140                 return jsonNotification;
141         }
142
143 }