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=97706a988194339a1ce78ced914d4dc2a97c22e9;hp=13cf424ba7bfcc802b6e663fa3aa7de8d1d09bbc;hb=419098bcd6215125408b29e60bd888e60979d37b;hpb=d678616d36fce087fe89cdbc27f0169e7a612790 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 13cf424..97706a9 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–2015 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,6 +17,8 @@ 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.Date; @@ -26,7 +28,9 @@ import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; 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; /** * Ajax page that returns a formatted, relative timestamp for replies or posts. @@ -52,41 +56,41 @@ public class GetTimesAjaxPage extends JsonPage { * {@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); - if (post == null) { + Optional 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", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh())); synchronized (dateFormat) { - postTime.put("tooltip", dateFormat.format(new Date(post.getTime()))); + 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) { - PostReply reply = webInterface.getCore().getPostReply(id); - if (reply == null) { + Optional 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", TimeUnit.MILLISECONDS.toSeconds(time.getRefresh())); synchronized (dateFormat) { - replyTime.put("tooltip", dateFormat.format(new Date(reply.getTime()))); + replyTime.put("tooltip", dateFormat.format(new Date(reply.get().getTime()))); } replyTimes.put(id, replyTime); } @@ -179,7 +183,7 @@ public class GetTimesAjaxPage extends JsonPage { 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) / 24)); + 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");