429a275bdf90f4ced710cfbfae033a10f5e9c17b
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetTimesAjaxPage.java
1 /*
2  * Sone - GetTimesAjaxPage.java - Copyright © 2010–2016 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation, either version 3 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * 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.text.DateFormat;
23 import java.text.SimpleDateFormat;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.concurrent.TimeUnit;
27
28 import net.pterodactylus.sone.data.Post;
29 import net.pterodactylus.sone.data.PostReply;
30 import net.pterodactylus.sone.freenet.L10nFilter;
31 import net.pterodactylus.sone.text.TimeText;
32 import net.pterodactylus.sone.text.TimeTextConverter;
33 import net.pterodactylus.sone.web.WebInterface;
34 import net.pterodactylus.sone.web.page.FreenetRequest;
35
36 import com.fasterxml.jackson.databind.node.ObjectNode;
37 import com.google.common.base.Optional;
38
39 /**
40  * Ajax page that returns a formatted, relative timestamp for replies or posts.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class GetTimesAjaxPage extends JsonPage {
45
46         /** Formatter for tooltips. */
47         private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
48         private final TimeTextConverter timeTextConverter;
49         private final L10nFilter l10nFilter;
50
51         /**
52          * Creates a new get times AJAX page.
53          *
54          * @param webInterface
55          *            The Sone web interface
56          */
57         public GetTimesAjaxPage(WebInterface webInterface, TimeTextConverter timeTextConverter, L10nFilter l10nFilter) {
58                 super("getTimes.ajax", webInterface);
59                 this.timeTextConverter = timeTextConverter;
60                 this.l10nFilter = l10nFilter;
61         }
62
63         /**
64          * {@inheritDoc}
65          */
66         @Override
67         protected JsonReturnObject createJsonObject(FreenetRequest request) {
68                 String allIds = request.getHttpRequest().getParam("posts");
69                 ObjectNode postTimes = new ObjectNode(instance);
70                 if (allIds.length() > 0) {
71                         String[] ids = allIds.split(",");
72                         for (String id : ids) {
73                                 Optional<Post> post = webInterface.getCore().getPost(id);
74                                 if (!post.isPresent()) {
75                                         continue;
76                                 }
77                                 ObjectNode postTime = new ObjectNode(instance);
78                                 Time time = getTime(post.get().getTime());
79                                 postTime.put("timeText", time.getText());
80                                 postTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh()));
81                                 synchronized (dateFormat) {
82                                         postTime.put("tooltip", dateFormat.format(new Date(post.get().getTime())));
83                                 }
84                                 postTimes.put(id, postTime);
85                         }
86                 }
87                 ObjectNode replyTimes = new ObjectNode(instance);
88                 allIds = request.getHttpRequest().getParam("replies");
89                 if (allIds.length() > 0) {
90                         String[] ids = allIds.split(",");
91                         for (String id : ids) {
92                                 Optional<PostReply> reply = webInterface.getCore().getPostReply(id);
93                                 if (!reply.isPresent()) {
94                                         continue;
95                                 }
96                                 ObjectNode replyTime = new ObjectNode(instance);
97                                 Time time = getTime(reply.get().getTime());
98                                 replyTime.put("timeText", time.getText());
99                                 replyTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh()));
100                                 synchronized (dateFormat) {
101                                         replyTime.put("tooltip", dateFormat.format(new Date(reply.get().getTime())));
102                                 }
103                                 replyTimes.put(id, replyTime);
104                         }
105                 }
106                 return createSuccessJsonObject().put("postTimes", postTimes).put("replyTimes", replyTimes);
107         }
108
109         /**
110          * {@inheritDoc}
111          */
112         @Override
113         protected boolean needsFormPassword() {
114                 return false;
115         }
116
117         /**
118          * {@inheritDoc}
119          */
120         @Override
121         protected boolean requiresLogin() {
122                 return false;
123         }
124
125         //
126         // PRIVATE METHODS
127         //
128
129         /**
130          * Returns the formatted relative time for a given time.
131          *
132          * @param time
133          *            The time to format the difference from (in milliseconds)
134          * @return The formatted age
135          */
136         private Time getTime(long time) {
137                 TimeText timeText = timeTextConverter.getTimeText(time);
138                 return new Time(l10nFilter.format(null, timeText.getL10nText(), Collections.<String, Object>emptyMap()), timeText.getRefreshTime());
139         }
140
141         /**
142          * Container for a formatted time.
143          *
144          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
145          */
146         public static class Time {
147
148                 /** The formatted time. */
149                 private final String text;
150
151                 /** The time after which to refresh the time. */
152                 private final long refresh;
153
154                 /**
155                  * Creates a new formatted time container.
156                  *
157                  * @param text
158                  *            The formatted time
159                  * @param refresh
160                  *            The time after which to refresh the time (in milliseconds)
161                  */
162                 public Time(String text, long refresh) {
163                         this.text = text;
164                         this.refresh = refresh;
165                 }
166
167                 /**
168                  * Returns the formatted time.
169                  *
170                  * @return The formatted time
171                  */
172                 public String getText() {
173                         return text;
174                 }
175
176                 /**
177                  * Returns the time after which to refresh the time.
178                  *
179                  * @return The time after which to refresh the time (in milliseconds)
180                  */
181                 public long getRefresh() {
182                         return refresh;
183                 }
184
185                 /**
186                  * {@inheritDoc}
187                  */
188                 @Override
189                 public String toString() {
190                         return text;
191                 }
192
193         }
194
195 }