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 java.text.DateFormat;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23 import java.util.concurrent.TimeUnit;
25 import net.pterodactylus.sone.data.Post;
26 import net.pterodactylus.sone.data.PostReply;
27 import net.pterodactylus.sone.web.WebInterface;
28 import net.pterodactylus.sone.web.page.FreenetRequest;
29 import net.pterodactylus.util.json.JsonObject;
32 * Ajax page that returns a formatted, relative timestamp for replies or posts.
34 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36 public class GetTimesAjaxPage extends JsonPage {
38 /** Formatter for tooltips. */
39 private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss");
42 * Creates a new get times AJAX page.
45 * The Sone web interface
47 public GetTimesAjaxPage(WebInterface webInterface) {
48 super("getTimes.ajax", webInterface);
55 protected JsonObject createJsonObject(FreenetRequest request) {
56 String allIds = request.getHttpRequest().getParam("posts");
57 JsonObject postTimes = new JsonObject();
58 if (allIds.length() > 0) {
59 String[] ids = allIds.split(",");
60 for (String id : ids) {
61 Post post = webInterface.getCore().getPost(id);
65 JsonObject postTime = new JsonObject();
66 Time time = getTime(post.getTime());
67 postTime.put("timeText", time.getText());
68 postTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh()));
69 synchronized (dateFormat) {
70 postTime.put("tooltip", dateFormat.format(new Date(post.getTime())));
72 postTimes.put(id, postTime);
75 JsonObject replyTimes = new JsonObject();
76 allIds = request.getHttpRequest().getParam("replies");
77 if (allIds.length() > 0) {
78 String[] ids = allIds.split(",");
79 for (String id : ids) {
80 PostReply reply = webInterface.getCore().getPostReply(id);
84 JsonObject replyTime = new JsonObject();
85 Time time = getTime(reply.getTime());
86 replyTime.put("timeText", time.getText());
87 replyTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh()));
88 synchronized (dateFormat) {
89 replyTime.put("tooltip", dateFormat.format(new Date(reply.getTime())));
91 replyTimes.put(id, replyTime);
94 return createSuccessJsonObject().put("postTimes", postTimes).put("replyTimes", replyTimes);
101 protected boolean needsFormPassword() {
109 protected boolean requiresLogin() {
118 * Returns the formatted relative time for a given time.
121 * The time to format the difference from (in milliseconds)
122 * @return The formatted age
124 private Time getTime(long time) {
125 return getTime(webInterface, time);
133 * Returns the formatted relative time for a given time.
135 * @param webInterface
136 * The Sone web interface (for l10n access)
138 * The time to format the difference from (in milliseconds)
139 * @return The formatted age
141 public static Time getTime(WebInterface webInterface, long time) {
143 return new Time(webInterface.getL10n().getString("View.Sone.Text.UnknownDate"), TimeUnit.HOURS.toMillis(12));
145 long age = System.currentTimeMillis() - time;
149 text = webInterface.getL10n().getDefaultString("View.Time.InTheFuture");
150 refresh = TimeUnit.MINUTES.toMillis(5);
151 } else if (age < TimeUnit.SECONDS.toMillis(20)) {
152 text = webInterface.getL10n().getDefaultString("View.Time.AFewSecondsAgo");
153 refresh = TimeUnit.SECONDS.toMillis(10);
154 } else if (age < TimeUnit.SECONDS.toMillis(45)) {
155 text = webInterface.getL10n().getString("View.Time.HalfAMinuteAgo");
156 refresh = TimeUnit.SECONDS.toMillis(20);
157 } else if (age < TimeUnit.SECONDS.toMillis(90)) {
158 text = webInterface.getL10n().getString("View.Time.AMinuteAgo");
159 refresh = TimeUnit.MINUTES.toMillis(1);
160 } else if (age < TimeUnit.MINUTES.toMillis(30)) {
161 text = webInterface.getL10n().getString("View.Time.XMinutesAgo", "min", String.valueOf(TimeUnit.MILLISECONDS.toMinutes(age + TimeUnit.SECONDS.toMillis(30))));
162 refresh = TimeUnit.MINUTES.toMillis(1);
163 } else if (age < TimeUnit.MINUTES.toMillis(45)) {
164 text = webInterface.getL10n().getString("View.Time.HalfAnHourAgo");
165 refresh = TimeUnit.MINUTES.toMillis(10);
166 } else if (age < TimeUnit.MINUTES.toMillis(90)) {
167 text = webInterface.getL10n().getString("View.Time.AnHourAgo");
168 refresh = TimeUnit.HOURS.toMillis(1);
169 } else if (age < TimeUnit.HOURS.toMillis(21)) {
170 text = webInterface.getL10n().getString("View.Time.XHoursAgo", "hour", String.valueOf(TimeUnit.MILLISECONDS.toHours(age + TimeUnit.MINUTES.toMillis(30))));
171 refresh = TimeUnit.HOURS.toMillis(1);
172 } else if (age < TimeUnit.HOURS.toMillis(42)) {
173 text = webInterface.getL10n().getString("View.Time.ADayAgo");
174 refresh = TimeUnit.DAYS.toMillis(1);
175 } else if (age < TimeUnit.DAYS.toMillis(6)) {
176 text = webInterface.getL10n().getString("View.Time.XDaysAgo", "day", String.valueOf(TimeUnit.MILLISECONDS.toDays(age + TimeUnit.HOURS.toMillis(12))));
177 refresh = TimeUnit.DAYS.toMillis(1);
178 } else if (age < TimeUnit.DAYS.toMillis(11)) {
179 text = webInterface.getL10n().getString("View.Time.AWeekAgo");
180 refresh = TimeUnit.DAYS.toMillis(1);
181 } else if (age < TimeUnit.DAYS.toMillis(28)) {
182 text = webInterface.getL10n().getString("View.Time.XWeeksAgo", "week", String.valueOf((TimeUnit.MILLISECONDS.toHours(age) + 84) / 24));
183 refresh = TimeUnit.DAYS.toMillis(1);
184 } else if (age < TimeUnit.DAYS.toMillis(42)) {
185 text = webInterface.getL10n().getString("View.Time.AMonthAgo");
186 refresh = TimeUnit.DAYS.toMillis(1);
187 } else if (age < TimeUnit.DAYS.toMillis(330)) {
188 text = webInterface.getL10n().getString("View.Time.XMonthsAgo", "month", String.valueOf((TimeUnit.MILLISECONDS.toDays(age) + 15) / 30));
189 refresh = TimeUnit.DAYS.toMillis(1);
190 } else if (age < TimeUnit.DAYS.toMillis(540)) {
191 text = webInterface.getL10n().getString("View.Time.AYearAgo");
192 refresh = TimeUnit.DAYS.toMillis(7);
194 text = webInterface.getL10n().getString("View.Time.XYearsAgo", "year", String.valueOf((long) ((TimeUnit.MILLISECONDS.toDays(age) + 182.64) / 365.28)));
195 refresh = TimeUnit.DAYS.toMillis(7);
197 return new Time(text, refresh);
201 * Container for a formatted time.
203 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
205 public static class Time {
207 /** The formatted time. */
208 private final String text;
210 /** The time after which to refresh the time. */
211 private final long refresh;
214 * Creates a new formatted time container.
219 * The time after which to refresh the time (in milliseconds)
221 public Time(String text, long refresh) {
223 this.refresh = refresh;
227 * Returns the formatted time.
229 * @return The formatted time
231 public String getText() {
236 * Returns the time after which to refresh the time.
238 * @return The time after which to refresh the time (in milliseconds)
240 public long getRefresh() {
248 public String toString() {