2 * Sone - GetTimesAjaxPage.java - Copyright © 2010–2011 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 java.text.DateFormat;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
24 import net.pterodactylus.sone.data.Post;
25 import net.pterodactylus.sone.data.Reply;
26 import net.pterodactylus.sone.web.WebInterface;
27 import net.pterodactylus.util.json.JsonObject;
28 import net.pterodactylus.util.number.Digits;
31 * Ajax page that returns a formatted, relative timestamp for replies or posts.
33 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35 public class GetTimesAjaxPage extends JsonPage {
37 /** Formatter for tooltips. */
38 private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
41 * Creates a new get times AJAX page.
44 * The Sone web interface
46 public GetTimesAjaxPage(WebInterface webInterface) {
47 super("getTimes.ajax", webInterface);
54 protected JsonObject createJsonObject(Request request) {
55 String allIds = request.getHttpRequest().getParam("posts");
56 JsonObject postTimes = new JsonObject();
57 if (allIds.length() > 0) {
58 String[] ids = allIds.split(",");
59 for (String id : ids) {
60 Post post = webInterface.getCore().getPost(id, false);
64 JsonObject postTime = new JsonObject();
65 Time time = getTime(post.getTime());
66 postTime.put("timeText", time.getText());
67 postTime.put("refreshTime", time.getRefresh() / Time.SECOND);
68 postTime.put("tooltip", dateFormat.format(new Date(post.getTime())));
69 postTimes.put(id, postTime);
72 JsonObject replyTimes = new JsonObject();
73 allIds = request.getHttpRequest().getParam("replies");
74 if (allIds.length() > 0) {
75 String[] ids = allIds.split(",");
76 for (String id : ids) {
77 Reply reply = webInterface.getCore().getReply(id, false);
81 JsonObject replyTime = new JsonObject();
82 Time time = getTime(reply.getTime());
83 replyTime.put("timeText", time.getText());
84 replyTime.put("refreshTime", time.getRefresh() / Time.SECOND);
85 replyTime.put("tooltip", dateFormat.format(new Date(reply.getTime())));
86 replyTimes.put(id, replyTime);
89 return createSuccessJsonObject().put("postTimes", postTimes).put("replyTimes", replyTimes);
96 protected boolean needsFormPassword() {
104 protected boolean requiresLogin() {
113 * Returns the formatted relative time for a given time.
116 * The time to format the difference from (in milliseconds)
117 * @return The formatted age
119 private Time getTime(long time) {
120 return getTime(webInterface, time);
128 * Returns the formatted relative time for a given time.
130 * @param webInterface
131 * The Sone web interface (for l10n access)
133 * The time to format the difference from (in milliseconds)
134 * @return The formatted age
136 public static Time getTime(WebInterface webInterface, long time) {
138 return new Time(webInterface.getL10n().getString("View.Sone.Text.UnknownDate"), 12 * Time.HOUR);
140 long age = System.currentTimeMillis() - time;
144 text = webInterface.getL10n().getDefaultString("View.Time.InTheFuture");
145 refresh = 5 * Time.MINUTE;
146 } else if (age < 20 * Time.SECOND) {
147 text = webInterface.getL10n().getDefaultString("View.Time.AFewSecondsAgo");
148 refresh = 10 * Time.SECOND;
149 } else if (age < 45 * Time.SECOND) {
150 text = webInterface.getL10n().getString("View.Time.HalfAMinuteAgo");
151 refresh = 20 * Time.SECOND;
152 } else if (age < 90 * Time.SECOND) {
153 text = webInterface.getL10n().getString("View.Time.AMinuteAgo");
154 refresh = Time.MINUTE;
155 } else if (age < 30 * Time.MINUTE) {
156 text = webInterface.getL10n().getString("View.Time.XMinutesAgo", "min", String.valueOf((int) (Digits.round(age, Time.MINUTE) / Time.MINUTE)));
157 refresh = 1 * Time.MINUTE;
158 } else if (age < 45 * Time.MINUTE) {
159 text = webInterface.getL10n().getString("View.Time.HalfAnHourAgo");
160 refresh = 10 * Time.MINUTE;
161 } else if (age < 90 * Time.MINUTE) {
162 text = webInterface.getL10n().getString("View.Time.AnHourAgo");
164 } else if (age < 21 * Time.HOUR) {
165 text = webInterface.getL10n().getString("View.Time.XHoursAgo", "hour", String.valueOf((int) (Digits.round(age, Time.HOUR) / Time.HOUR)));
167 } else if (age < 42 * Time.HOUR) {
168 text = webInterface.getL10n().getString("View.Time.ADayAgo");
170 } else if (age < 6 * Time.DAY) {
171 text = webInterface.getL10n().getString("View.Time.XDaysAgo", "day", String.valueOf((int) (Digits.round(age, Time.DAY) / Time.DAY)));
173 } else if (age < 11 * Time.DAY) {
174 text = webInterface.getL10n().getString("View.Time.AWeekAgo");
176 } else if (age < 4 * Time.WEEK) {
177 text = webInterface.getL10n().getString("View.Time.XWeeksAgo", "week", String.valueOf((int) (Digits.round(age, Time.WEEK) / Time.WEEK)));
179 } else if (age < 6 * Time.WEEK) {
180 text = webInterface.getL10n().getString("View.Time.AMonthAgo");
182 } else if (age < 11 * Time.MONTH) {
183 text = webInterface.getL10n().getString("View.Time.XMonthsAgo", "month", String.valueOf((int) (Digits.round(age, Time.MONTH) / Time.MONTH)));
185 } else if (age < 18 * Time.MONTH) {
186 text = webInterface.getL10n().getString("View.Time.AYearAgo");
189 text = webInterface.getL10n().getString("View.Time.XYearsAgo", "year", String.valueOf((int) (Digits.round(age, Time.YEAR) / Time.YEAR)));
192 return new Time(text, refresh);
196 * Container for a formatted time.
198 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
200 public static class Time {
202 /** Number of milliseconds in a second. */
203 private static final long SECOND = 1000;
205 /** Number of milliseconds in a minute. */
206 private static final long MINUTE = 60 * SECOND;
208 /** Number of milliseconds in an hour. */
209 private static final long HOUR = 60 * MINUTE;
211 /** Number of milliseconds in a day. */
212 private static final long DAY = 24 * HOUR;
214 /** Number of milliseconds in a week. */
215 private static final long WEEK = 7 * DAY;
217 /** Number of milliseconds in a 30-day month. */
218 private static final long MONTH = 30 * DAY;
220 /** Number of milliseconds in a year. */
221 private static final long YEAR = 365 * DAY;
223 /** The formatted time. */
224 private final String text;
226 /** The time after which to refresh the time. */
227 private final long refresh;
230 * Creates a new formatted time container.
235 * The time after which to refresh the time (in milliseconds)
237 public Time(String text, long refresh) {
239 this.refresh = refresh;
243 * Returns the formatted time.
245 * @return The formatted time
247 public String getText() {
252 * Returns the time after which to refresh the time.
254 * @return The time after which to refresh the time (in milliseconds)
256 public long getRefresh() {
264 public String toString() {