2 * Sone - GetTimesAjaxPage.java - Copyright © 2010–2013 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.Date;
25 import java.util.concurrent.TimeUnit;
27 import net.pterodactylus.sone.data.Post;
28 import net.pterodactylus.sone.data.PostReply;
29 import net.pterodactylus.sone.web.WebInterface;
30 import net.pterodactylus.sone.web.page.FreenetRequest;
32 import com.fasterxml.jackson.databind.node.ObjectNode;
33 import com.google.common.base.Optional;
36 * Ajax page that returns a formatted, relative timestamp for replies or posts.
38 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class GetTimesAjaxPage extends JsonPage {
42 /** Formatter for tooltips. */
43 private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
46 * Creates a new get times AJAX page.
49 * The Sone web interface
51 public GetTimesAjaxPage(WebInterface webInterface) {
52 super("getTimes.ajax", webInterface);
59 protected JsonReturnObject createJsonObject(FreenetRequest request) {
60 String allIds = request.getHttpRequest().getParam("posts");
61 ObjectNode postTimes = new ObjectNode(instance);
62 if (allIds.length() > 0) {
63 String[] ids = allIds.split(",");
64 for (String id : ids) {
65 Optional<Post> post = webInterface.getCore().getPost(id);
66 if (!post.isPresent()) {
69 ObjectNode postTime = new ObjectNode(instance);
70 Time time = getTime(post.get().getTime());
71 postTime.put("timeText", time.getText());
72 postTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh()));
73 synchronized (dateFormat) {
74 postTime.put("tooltip", dateFormat.format(new Date(post.get().getTime())));
76 postTimes.put(id, postTime);
79 ObjectNode replyTimes = new ObjectNode(instance);
80 allIds = request.getHttpRequest().getParam("replies");
81 if (allIds.length() > 0) {
82 String[] ids = allIds.split(",");
83 for (String id : ids) {
84 Optional<PostReply> reply = webInterface.getCore().getPostReply(id);
85 if (!reply.isPresent()) {
88 ObjectNode replyTime = new ObjectNode(instance);
89 Time time = getTime(reply.get().getTime());
90 replyTime.put("timeText", time.getText());
91 replyTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh()));
92 synchronized (dateFormat) {
93 replyTime.put("tooltip", dateFormat.format(new Date(reply.get().getTime())));
95 replyTimes.put(id, replyTime);
98 return createSuccessJsonObject().put("postTimes", postTimes).put("replyTimes", replyTimes);
105 protected boolean needsFormPassword() {
113 protected boolean requiresLogin() {
122 * Returns the formatted relative time for a given time.
125 * The time to format the difference from (in milliseconds)
126 * @return The formatted age
128 private Time getTime(long time) {
129 return getTime(webInterface, time);
137 * Returns the formatted relative time for a given time.
139 * @param webInterface
140 * The Sone web interface (for l10n access)
142 * The time to format the difference from (in milliseconds)
143 * @return The formatted age
145 public static Time getTime(WebInterface webInterface, long time) {
147 return new Time(webInterface.getL10n().getString("View.Sone.Text.UnknownDate"), TimeUnit.HOURS.toMillis(12));
149 long age = System.currentTimeMillis() - time;
153 text = webInterface.getL10n().getDefaultString("View.Time.InTheFuture");
154 refresh = TimeUnit.MINUTES.toMillis(5);
155 } else if (age < TimeUnit.SECONDS.toMillis(20)) {
156 text = webInterface.getL10n().getDefaultString("View.Time.AFewSecondsAgo");
157 refresh = TimeUnit.SECONDS.toMillis(10);
158 } else if (age < TimeUnit.SECONDS.toMillis(45)) {
159 text = webInterface.getL10n().getString("View.Time.HalfAMinuteAgo");
160 refresh = TimeUnit.SECONDS.toMillis(20);
161 } else if (age < TimeUnit.SECONDS.toMillis(90)) {
162 text = webInterface.getL10n().getString("View.Time.AMinuteAgo");
163 refresh = TimeUnit.MINUTES.toMillis(1);
164 } else if (age < TimeUnit.MINUTES.toMillis(30)) {
165 text = webInterface.getL10n().getString("View.Time.XMinutesAgo", "min", String.valueOf(TimeUnit.MILLISECONDS.toMinutes(age + TimeUnit.SECONDS.toMillis(30))));
166 refresh = TimeUnit.MINUTES.toMillis(1);
167 } else if (age < TimeUnit.MINUTES.toMillis(45)) {
168 text = webInterface.getL10n().getString("View.Time.HalfAnHourAgo");
169 refresh = TimeUnit.MINUTES.toMillis(10);
170 } else if (age < TimeUnit.MINUTES.toMillis(90)) {
171 text = webInterface.getL10n().getString("View.Time.AnHourAgo");
172 refresh = TimeUnit.HOURS.toMillis(1);
173 } else if (age < TimeUnit.HOURS.toMillis(21)) {
174 text = webInterface.getL10n().getString("View.Time.XHoursAgo", "hour", String.valueOf(TimeUnit.MILLISECONDS.toHours(age + TimeUnit.MINUTES.toMillis(30))));
175 refresh = TimeUnit.HOURS.toMillis(1);
176 } else if (age < TimeUnit.HOURS.toMillis(42)) {
177 text = webInterface.getL10n().getString("View.Time.ADayAgo");
178 refresh = TimeUnit.DAYS.toMillis(1);
179 } else if (age < TimeUnit.DAYS.toMillis(6)) {
180 text = webInterface.getL10n().getString("View.Time.XDaysAgo", "day", String.valueOf(TimeUnit.MILLISECONDS.toDays(age + TimeUnit.HOURS.toMillis(12))));
181 refresh = TimeUnit.DAYS.toMillis(1);
182 } else if (age < TimeUnit.DAYS.toMillis(11)) {
183 text = webInterface.getL10n().getString("View.Time.AWeekAgo");
184 refresh = TimeUnit.DAYS.toMillis(1);
185 } else if (age < TimeUnit.DAYS.toMillis(28)) {
186 text = webInterface.getL10n().getString("View.Time.XWeeksAgo", "week", String.valueOf((TimeUnit.MILLISECONDS.toHours(age) + 84) / (7 * 24)));
187 refresh = TimeUnit.DAYS.toMillis(1);
188 } else if (age < TimeUnit.DAYS.toMillis(42)) {
189 text = webInterface.getL10n().getString("View.Time.AMonthAgo");
190 refresh = TimeUnit.DAYS.toMillis(1);
191 } else if (age < TimeUnit.DAYS.toMillis(330)) {
192 text = webInterface.getL10n().getString("View.Time.XMonthsAgo", "month", String.valueOf((TimeUnit.MILLISECONDS.toDays(age) + 15) / 30));
193 refresh = TimeUnit.DAYS.toMillis(1);
194 } else if (age < TimeUnit.DAYS.toMillis(540)) {
195 text = webInterface.getL10n().getString("View.Time.AYearAgo");
196 refresh = TimeUnit.DAYS.toMillis(7);
198 text = webInterface.getL10n().getString("View.Time.XYearsAgo", "year", String.valueOf((long) ((TimeUnit.MILLISECONDS.toDays(age) + 182.64) / 365.28)));
199 refresh = TimeUnit.DAYS.toMillis(7);
201 return new Time(text, refresh);
205 * Container for a formatted time.
207 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
209 public static class Time {
211 /** The formatted time. */
212 private final String text;
214 /** The time after which to refresh the time. */
215 private final long refresh;
218 * Creates a new formatted time container.
223 * The time after which to refresh the time (in milliseconds)
225 public Time(String text, long refresh) {
227 this.refresh = refresh;
231 * Returns the formatted time.
233 * @return The formatted time
235 public String getText() {
240 * Returns the time after which to refresh the time.
242 * @return The time after which to refresh the time (in milliseconds)
244 public long getRefresh() {
252 public String toString() {