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