Add test for get times ajax page (test is timezone-dependent for now)
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / GetTimesAjaxPage.java
index 1711a5b..429a275 100644 (file)
@@ -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
 
 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.sone.web.page.FreenetRequest;
-import net.pterodactylus.util.json.JsonObject;
-import net.pterodactylus.util.number.Digits;
+
+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.
@@ -37,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.
@@ -44,46 +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(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) {
-                               Post post = webInterface.getCore().getPost(id, false);
-                               if (post == null) {
+                               Optional<Post> post = webInterface.getCore().getPost(id);
+                               if (!post.isPresent()) {
                                        continue;
                                }
-                               JsonObject postTime = new JsonObject();
-                               Time time = getTime(post.getTime());
+                               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<PostReply> reply = webInterface.getCore().getPostReply(id);
+                               if (!reply.isPresent()) {
                                        continue;
                                }
-                               JsonObject replyTime = new JsonObject();
-                               Time time = getTime(reply.getTime());
+                               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);
                        }
                }
@@ -118,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"), 12 * Time.HOUR);
-               }
-               long age = System.currentTimeMillis() - time;
-               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);
+               TimeText timeText = timeTextConverter.getTimeText(time);
+               return new Time(l10nFilter.format(null, timeText.getL10nText(), Collections.<String, Object>emptyMap()), timeText.getRefreshTime());
        }
 
        /**
@@ -200,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;