2 * Sone - GetTimesAjaxPage.java - Copyright © 2010–2016 David Roden
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
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
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/>.
18 package net.pterodactylus.sone.web.ajax;
20 import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance;
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;
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;
36 import com.fasterxml.jackson.databind.node.ObjectNode;
37 import com.google.common.base.Optional;
40 * Ajax page that returns a formatted, relative timestamp for replies or posts.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class GetTimesAjaxPage extends JsonPage {
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;
52 * Creates a new get times AJAX page.
55 * The Sone web interface
57 public GetTimesAjaxPage(WebInterface webInterface, TimeTextConverter timeTextConverter, L10nFilter l10nFilter) {
58 super("getTimes.ajax", webInterface);
59 this.timeTextConverter = timeTextConverter;
60 this.l10nFilter = l10nFilter;
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()) {
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())));
84 postTimes.put(id, postTime);
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()) {
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())));
103 replyTimes.put(id, replyTime);
106 return createSuccessJsonObject().put("postTimes", postTimes).put("replyTimes", replyTimes);
113 protected boolean needsFormPassword() {
121 protected boolean requiresLogin() {
130 * Returns the formatted relative time for a given time.
133 * The time to format the difference from (in milliseconds)
134 * @return The formatted age
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());
142 * Container for a formatted time.
144 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
146 public static class Time {
148 /** The formatted time. */
149 private final String text;
151 /** The time after which to refresh the time. */
152 private final long refresh;
155 * Creates a new formatted time container.
160 * The time after which to refresh the time (in milliseconds)
162 public Time(String text, long refresh) {
164 this.refresh = refresh;
168 * Returns the formatted time.
170 * @return The formatted time
172 public String getText() {
177 * Returns the time after which to refresh the time.
179 * @return The time after which to refresh the time (in milliseconds)
181 public long getRefresh() {
189 public String toString() {