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