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=0be0c465a3eb7b868e7c55d5fe3739986aeea8a7;hb=d91e48af542fef4c0064c5a6641b473240ef4273;hpb=b72db16cd16e8bfbaadc44604bdff3f49a1aff51 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 0be0c46..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–2011 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,15 +17,24 @@ 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.Reply; +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.util.json.JsonObject; -import net.pterodactylus.util.number.Digits; +import net.pterodactylus.sone.web.page.FreenetRequest; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.base.Optional; /** * Ajax page that returns a formatted, relative timestamp for replies or posts. @@ -36,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. @@ -43,49 +54,52 @@ 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(Request request) { - long now = System.currentTimeMillis(); + 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) { - Post post = webInterface.getCore().getPost(id, false); - if (post == null) { + Optional post = webInterface.getCore().getPost(id); + if (!post.isPresent()) { continue; } - long age = now - post.getTime(); - JsonObject postTime = new JsonObject(); - Time time = getTime(age); + ObjectNode postTime = new ObjectNode(instance); + Time time = getTime(post.get().getTime()); postTime.put("timeText", time.getText()); - postTime.put("refreshTime", time.getRefresh() / Time.SECOND); - postTime.put("tooltip", dateFormat.format(new Date(post.getTime()))); + postTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh())); + synchronized (dateFormat) { + postTime.put("tooltip", dateFormat.format(new Date(post.get().getTime()))); + } 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(","); for (String id : ids) { - Reply reply = webInterface.getCore().getReply(id, false); - if (reply == null) { + Optional reply = webInterface.getCore().getPostReply(id); + if (!reply.isPresent()) { continue; } - long age = now - reply.getTime(); - JsonObject replyTime = new JsonObject(); - Time time = getTime(age); + ObjectNode replyTime = new ObjectNode(instance); + Time time = getTime(reply.get().getTime()); replyTime.put("timeText", time.getText()); - replyTime.put("refreshTime", time.getRefresh() / Time.SECOND); - replyTime.put("tooltip", dateFormat.format(new Date(reply.getTime()))); + replyTime.put("refreshTime", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh())); + synchronized (dateFormat) { + replyTime.put("tooltip", dateFormat.format(new Date(reply.get().getTime()))); + } replyTimes.put(id, replyTime); } } @@ -113,82 +127,15 @@ public class GetTimesAjaxPage extends JsonPage { // /** - * Returns the formatted relative time for a given age. - * - * @param age - * The age to format (in milliseconds) - * @return The formatted age - */ - private Time getTime(long age) { - return getTime(webInterface, age); - } - - // - // STATIC METHODS - // - - /** - * Returns the formatted relative time for a given age. + * Returns the formatted relative time for a given time. * - * @param webInterface - * The Sone web interface (for l10n access) - * @param age - * The age to format (in milliseconds) + * @param time + * The time to format the difference from (in milliseconds) * @return The formatted age */ - public static Time getTime(WebInterface webInterface, long age) { - String text; - long refresh; - if (age < 0) { - text = webInterface.getL10n().getDefaultString("View.Time.InTheFuture"); - refresh = 5 * Time.MINUTE; - } else if (age < 20 * Time.SECOND) { - text = webInterface.getL10n().getDefaultString("View.Time.AFewSecondsAgo"); - refresh = 10 * Time.SECOND; - } else if (age < 45 * Time.SECOND) { - text = webInterface.getL10n().getString("View.Time.HalfAMinuteAgo"); - refresh = 20 * Time.SECOND; - } else if (age < 90 * Time.SECOND) { - text = webInterface.getL10n().getString("View.Time.AMinuteAgo"); - refresh = Time.MINUTE; - } else if (age < 30 * Time.MINUTE) { - text = webInterface.getL10n().getString("View.Time.XMinutesAgo", "min", String.valueOf((int) (Digits.round(age, Time.MINUTE) / Time.MINUTE))); - refresh = 1 * Time.MINUTE; - } else if (age < 45 * Time.MINUTE) { - text = webInterface.getL10n().getString("View.Time.HalfAnHourAgo"); - refresh = 10 * Time.MINUTE; - } else if (age < 90 * Time.MINUTE) { - text = webInterface.getL10n().getString("View.Time.AnHourAgo"); - refresh = Time.HOUR; - } else if (age < 21 * Time.HOUR) { - text = webInterface.getL10n().getString("View.Time.XHoursAgo", "hour", String.valueOf((int) (Digits.round(age, Time.HOUR) / Time.HOUR))); - refresh = Time.HOUR; - } else if (age < 42 * Time.HOUR) { - text = webInterface.getL10n().getString("View.Time.ADayAgo"); - refresh = Time.DAY; - } else if (age < 6 * Time.DAY) { - text = webInterface.getL10n().getString("View.Time.XDaysAgo", "day", String.valueOf((int) (Digits.round(age, Time.DAY) / Time.DAY))); - refresh = Time.DAY; - } else if (age < 11 * Time.DAY) { - text = webInterface.getL10n().getString("View.Time.AWeekAgo"); - refresh = Time.DAY; - } else if (age < 4 * Time.WEEK) { - text = webInterface.getL10n().getString("View.Time.XWeeksAgo", "week", String.valueOf((int) (Digits.round(age, Time.WEEK) / Time.WEEK))); - refresh = Time.DAY; - } else if (age < 6 * Time.WEEK) { - text = webInterface.getL10n().getString("View.Time.AMonthAgo"); - refresh = Time.DAY; - } else if (age < 11 * Time.MONTH) { - text = webInterface.getL10n().getString("View.Time.XMonthsAgo", "month", String.valueOf((int) (Digits.round(age, Time.MONTH) / Time.MONTH))); - refresh = Time.DAY; - } else if (age < 18 * Time.MONTH) { - text = webInterface.getL10n().getString("View.Time.AYearAgo"); - refresh = Time.WEEK; - } else { - text = webInterface.getL10n().getString("View.Time.XYearsAgo", "year", String.valueOf((int) (Digits.round(age, Time.YEAR) / Time.YEAR))); - refresh = Time.WEEK; - } - return new Time(text, refresh); + private Time getTime(long time) { + TimeText timeText = timeTextConverter.getTimeText(time); + return new Time(l10nFilter.format(null, timeText.getL10nText(), Collections.emptyMap()), timeText.getRefreshTime()); } /** @@ -198,27 +145,6 @@ public class GetTimesAjaxPage extends JsonPage { */ public static class Time { - /** Number of milliseconds in a second. */ - private static final long SECOND = 1000; - - /** Number of milliseconds in a minute. */ - private static final long MINUTE = 60 * SECOND; - - /** Number of milliseconds in an hour. */ - private static final long HOUR = 60 * MINUTE; - - /** Number of milliseconds in a day. */ - private static final long DAY = 24 * HOUR; - - /** Number of milliseconds in a week. */ - private static final long WEEK = 7 * DAY; - - /** Number of milliseconds in a 30-day month. */ - private static final long MONTH = 30 * DAY; - - /** Number of milliseconds in a year. */ - private static final long YEAR = 365 * DAY; - /** The formatted time. */ private final String text;