Do not require logins for some AJAX pages.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetStatusAjaxPage.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.text.DateFormat;
21 import java.text.SimpleDateFormat;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Date;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28
29 import net.pterodactylus.sone.data.Post;
30 import net.pterodactylus.sone.data.Reply;
31 import net.pterodactylus.sone.data.Sone;
32 import net.pterodactylus.sone.template.SoneAccessor;
33 import net.pterodactylus.sone.web.WebInterface;
34 import net.pterodactylus.util.json.JsonArray;
35 import net.pterodactylus.util.json.JsonObject;
36 import net.pterodactylus.util.notify.Notification;
37
38 /**
39  * The “get status” AJAX handler returns all information that is necessary to
40  * update the web interface in real-time.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class GetStatusAjaxPage extends JsonPage {
45
46         /** Date formatter. */
47         private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
48
49         /**
50          * Creates a new “get status” AJAX handler.
51          *
52          * @param webInterface
53          *            The Sone web interface
54          */
55         public GetStatusAjaxPage(WebInterface webInterface) {
56                 super("getStatus.ajax", webInterface);
57         }
58
59         /**
60          * {@inheritDoc}
61          */
62         @Override
63         protected JsonObject createJsonObject(Request request) {
64                 /* load Sones. */
65                 boolean loadAllSones = Boolean.parseBoolean(request.getHttpRequest().getParam("loadAllSones", "true"));
66                 Set<Sone> sones = new HashSet<Sone>(Collections.singleton(getCurrentSone(request.getToadletContext(), false)));
67                 if (loadAllSones) {
68                         sones.addAll(webInterface.getCore().getSones());
69                 }
70                 JsonArray jsonSones = new JsonArray();
71                 for (Sone sone : sones) {
72                         if (sone == null) {
73                                 continue;
74                         }
75                         JsonObject jsonSone = createJsonSone(sone);
76                         jsonSones.add(jsonSone);
77                 }
78                 /* load notifications. */
79                 List<Notification> notifications = new ArrayList<Notification>(webInterface.getNotifications().getChangedNotifications());
80                 Set<Notification> removedNotifications = webInterface.getNotifications().getRemovedNotifications();
81                 Collections.sort(notifications, Notification.LAST_UPDATED_TIME_SORTER);
82                 JsonArray jsonNotifications = new JsonArray();
83                 for (Notification notification : notifications) {
84                         jsonNotifications.add(createJsonNotification(notification));
85                 }
86                 JsonArray jsonRemovedNotifications = new JsonArray();
87                 for (Notification notification : removedNotifications) {
88                         jsonRemovedNotifications.add(createJsonNotification(notification));
89                 }
90                 /* load new posts. */
91                 Set<Post> newPosts = webInterface.getNewPosts();
92                 JsonArray jsonPosts = new JsonArray();
93                 for (Post post : newPosts) {
94                         JsonObject jsonPost = new JsonObject();
95                         jsonPost.put("id", post.getId());
96                         jsonPost.put("sone", post.getSone().getId());
97                         jsonPost.put("recipient", (post.getRecipient() != null) ? post.getRecipient().getId() : null);
98                         jsonPost.put("time", post.getTime());
99                         jsonPosts.add(jsonPost);
100                 }
101                 /* load new replies. */
102                 Set<Reply> newReplies = webInterface.getNewReplies();
103                 JsonArray jsonReplies = new JsonArray();
104                 for (Reply reply : newReplies) {
105                         JsonObject jsonReply = new JsonObject();
106                         jsonReply.put("id", reply.getId());
107                         jsonReply.put("sone", reply.getSone().getId());
108                         jsonReply.put("post", reply.getPost().getId());
109                         jsonReply.put("postSone", reply.getPost().getSone().getId());
110                         jsonReplies.add(jsonReply);
111                 }
112                 return createSuccessJsonObject().put("sones", jsonSones).put("notifications", jsonNotifications).put("removedNotifications", jsonRemovedNotifications).put("newPosts", jsonPosts).put("newReplies", jsonReplies);
113         }
114
115         /**
116          * {@inheritDoc}
117          */
118         @Override
119         protected boolean needsFormPassword() {
120                 return false;
121         }
122
123         /**
124          * {@inheritDoc}
125          */
126         @Override
127         protected boolean requiresLogin() {
128                 return false;
129         }
130
131         //
132         // PRIVATE METHODS
133         //
134
135         /**
136          * Creates a JSON object from the given Sone.
137          *
138          * @param sone
139          *            The Sone to convert to a JSON object
140          * @return The JSON representation of the given Sone
141          */
142         private JsonObject createJsonSone(Sone sone) {
143                 JsonObject jsonSone = new JsonObject();
144                 jsonSone.put("id", sone.getId());
145                 jsonSone.put("name", SoneAccessor.getNiceName(sone));
146                 jsonSone.put("local", sone.getInsertUri() != null);
147                 jsonSone.put("status", webInterface.getCore().getSoneStatus(sone).name());
148                 jsonSone.put("modified", webInterface.getCore().isModifiedSone(sone));
149                 jsonSone.put("locked", webInterface.getCore().isLocked(sone));
150                 synchronized (dateFormat) {
151                         jsonSone.put("lastUpdated", dateFormat.format(new Date(sone.getTime())));
152                 }
153                 jsonSone.put("age", (System.currentTimeMillis() - sone.getTime()) / 1000);
154                 return jsonSone;
155         }
156
157         /**
158          * Creates a JSON object from the given notification.
159          *
160          * @param notification
161          *            The notification to create a JSON object
162          * @return The JSON object
163          */
164         private static JsonObject createJsonNotification(Notification notification) {
165                 JsonObject jsonNotification = new JsonObject();
166                 jsonNotification.put("id", notification.getId());
167                 jsonNotification.put("text", notification.toString());
168                 jsonNotification.put("createdTime", notification.getCreatedTime());
169                 jsonNotification.put("lastUpdatedTime", notification.getLastUpdatedTime());
170                 jsonNotification.put("dismissable", notification.isDismissable());
171                 return jsonNotification;
172         }
173
174 }