X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FGetTimesAjaxPage.java;h=429a275bdf90f4ced710cfbfae033a10f5e9c17b;hp=b0d9dacc2e4e5b5e767dc5f3ff52591b515f1ce9;hb=d91e48af542fef4c0064c5a6641b473240ef4273;hpb=6e9a43ccd93ae125720547c0fe421dc81a54ba90 diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java index b0d9dac..429a275 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java @@ -1,5 +1,5 @@ /* - * Sone - GetTimesAjaxPage.java - Copyright © 2010–2013 David Roden + * Sone - GetTimesAjaxPage.java - Copyright © 2010–2016 David Roden * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software @@ -17,17 +17,23 @@ package net.pterodactylus.sone.web.ajax; +import static com.fasterxml.jackson.databind.node.JsonNodeFactory.instance; + import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Collections; import java.util.Date; import java.util.concurrent.TimeUnit; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; +import net.pterodactylus.sone.freenet.L10nFilter; +import net.pterodactylus.sone.text.TimeText; +import net.pterodactylus.sone.text.TimeTextConverter; import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetRequest; -import net.pterodactylus.util.json.JsonObject; +import com.fasterxml.jackson.databind.node.ObjectNode; import com.google.common.base.Optional; /** @@ -39,6 +45,8 @@ public class GetTimesAjaxPage extends JsonPage { /** Formatter for tooltips. */ private static final DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy, HH:mm:ss"); + private final TimeTextConverter timeTextConverter; + private final L10nFilter l10nFilter; /** * Creates a new get times AJAX page. @@ -46,17 +54,19 @@ public class GetTimesAjaxPage extends JsonPage { * @param webInterface * The Sone web interface */ - public GetTimesAjaxPage(WebInterface webInterface) { + public GetTimesAjaxPage(WebInterface webInterface, TimeTextConverter timeTextConverter, L10nFilter l10nFilter) { super("getTimes.ajax", webInterface); + this.timeTextConverter = timeTextConverter; + this.l10nFilter = l10nFilter; } /** * {@inheritDoc} */ @Override - protected JsonObject createJsonObject(FreenetRequest request) { + protected JsonReturnObject createJsonObject(FreenetRequest request) { String allIds = request.getHttpRequest().getParam("posts"); - JsonObject postTimes = new JsonObject(); + ObjectNode postTimes = new ObjectNode(instance); if (allIds.length() > 0) { String[] ids = allIds.split(","); for (String id : ids) { @@ -64,7 +74,7 @@ public class GetTimesAjaxPage extends JsonPage { if (!post.isPresent()) { continue; } - JsonObject postTime = new JsonObject(); + ObjectNode postTime = new ObjectNode(instance); Time time = getTime(post.get().getTime()); postTime.put("timeText", time.getText()); postTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh())); @@ -74,7 +84,7 @@ public class GetTimesAjaxPage extends JsonPage { postTimes.put(id, postTime); } } - JsonObject replyTimes = new JsonObject(); + ObjectNode replyTimes = new ObjectNode(instance); allIds = request.getHttpRequest().getParam("replies"); if (allIds.length() > 0) { String[] ids = allIds.split(","); @@ -83,7 +93,7 @@ public class GetTimesAjaxPage extends JsonPage { if (!reply.isPresent()) { continue; } - JsonObject replyTime = new JsonObject(); + ObjectNode replyTime = new ObjectNode(instance); Time time = getTime(reply.get().getTime()); replyTime.put("timeText", time.getText()); replyTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh())); @@ -124,79 +134,8 @@ public class GetTimesAjaxPage extends JsonPage { * @return The formatted age */ private Time getTime(long time) { - return getTime(webInterface, time); - } - - // - // STATIC METHODS - // - - /** - * Returns the formatted relative time for a given time. - * - * @param webInterface - * The Sone web interface (for l10n access) - * @param time - * The time to format the difference from (in milliseconds) - * @return The formatted age - */ - public static Time getTime(WebInterface webInterface, long time) { - if (time == 0) { - return new Time(webInterface.getL10n().getString("View.Sone.Text.UnknownDate"), TimeUnit.HOURS.toMillis(12)); - } - long age = System.currentTimeMillis() - time; - String text; - long refresh; - if (age < 0) { - text = webInterface.getL10n().getDefaultString("View.Time.InTheFuture"); - refresh = TimeUnit.MINUTES.toMillis(5); - } else if (age < TimeUnit.SECONDS.toMillis(20)) { - text = webInterface.getL10n().getDefaultString("View.Time.AFewSecondsAgo"); - refresh = TimeUnit.SECONDS.toMillis(10); - } else if (age < TimeUnit.SECONDS.toMillis(45)) { - text = webInterface.getL10n().getString("View.Time.HalfAMinuteAgo"); - refresh = TimeUnit.SECONDS.toMillis(20); - } else if (age < TimeUnit.SECONDS.toMillis(90)) { - text = webInterface.getL10n().getString("View.Time.AMinuteAgo"); - refresh = TimeUnit.MINUTES.toMillis(1); - } else if (age < TimeUnit.MINUTES.toMillis(30)) { - text = webInterface.getL10n().getString("View.Time.XMinutesAgo", "min", String.valueOf(TimeUnit.MILLISECONDS.toMinutes(age + TimeUnit.SECONDS.toMillis(30)))); - refresh = TimeUnit.MINUTES.toMillis(1); - } else if (age < TimeUnit.MINUTES.toMillis(45)) { - text = webInterface.getL10n().getString("View.Time.HalfAnHourAgo"); - refresh = TimeUnit.MINUTES.toMillis(10); - } else if (age < TimeUnit.MINUTES.toMillis(90)) { - text = webInterface.getL10n().getString("View.Time.AnHourAgo"); - refresh = TimeUnit.HOURS.toMillis(1); - } else if (age < TimeUnit.HOURS.toMillis(21)) { - text = webInterface.getL10n().getString("View.Time.XHoursAgo", "hour", String.valueOf(TimeUnit.MILLISECONDS.toHours(age + TimeUnit.MINUTES.toMillis(30)))); - refresh = TimeUnit.HOURS.toMillis(1); - } else if (age < TimeUnit.HOURS.toMillis(42)) { - text = webInterface.getL10n().getString("View.Time.ADayAgo"); - refresh = TimeUnit.DAYS.toMillis(1); - } else if (age < TimeUnit.DAYS.toMillis(6)) { - text = webInterface.getL10n().getString("View.Time.XDaysAgo", "day", String.valueOf(TimeUnit.MILLISECONDS.toDays(age + TimeUnit.HOURS.toMillis(12)))); - refresh = TimeUnit.DAYS.toMillis(1); - } else if (age < TimeUnit.DAYS.toMillis(11)) { - text = webInterface.getL10n().getString("View.Time.AWeekAgo"); - refresh = TimeUnit.DAYS.toMillis(1); - } else if (age < TimeUnit.DAYS.toMillis(28)) { - text = webInterface.getL10n().getString("View.Time.XWeeksAgo", "week", String.valueOf((TimeUnit.MILLISECONDS.toHours(age) + 84) / (7 * 24))); - refresh = TimeUnit.DAYS.toMillis(1); - } else if (age < TimeUnit.DAYS.toMillis(42)) { - text = webInterface.getL10n().getString("View.Time.AMonthAgo"); - refresh = TimeUnit.DAYS.toMillis(1); - } else if (age < TimeUnit.DAYS.toMillis(330)) { - text = webInterface.getL10n().getString("View.Time.XMonthsAgo", "month", String.valueOf((TimeUnit.MILLISECONDS.toDays(age) + 15) / 30)); - refresh = TimeUnit.DAYS.toMillis(1); - } else if (age < TimeUnit.DAYS.toMillis(540)) { - text = webInterface.getL10n().getString("View.Time.AYearAgo"); - refresh = TimeUnit.DAYS.toMillis(7); - } else { - text = webInterface.getL10n().getString("View.Time.XYearsAgo", "year", String.valueOf((long) ((TimeUnit.MILLISECONDS.toDays(age) + 182.64) / 365.28))); - refresh = TimeUnit.DAYS.toMillis(7); - } - return new Time(text, refresh); + TimeText timeText = timeTextConverter.getTimeText(time); + return new Time(l10nFilter.format(null, timeText.getL10nText(), Collections.emptyMap()), timeText.getRefreshTime()); } /**