From: David ‘Bombe’ Roden Date: Thu, 14 Sep 2017 05:04:05 +0000 (+0200) Subject: Replace get times ajax page with Kotlin version X-Git-Tag: 0.9.7^2~64 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=69af5a8e6bc69347e3979fd2f351ead5fcfb98fb;p=Sone.git Replace get times ajax page with Kotlin version --- diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java deleted file mode 100644 index 429a275..0000000 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * 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 - * Foundation, either version 3 of the License, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License along with - * this program. If not, see . - */ - -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 com.fasterxml.jackson.databind.node.ObjectNode; -import com.google.common.base.Optional; - -/** - * Ajax page that returns a formatted, relative timestamp for replies or posts. - * - * @author David ‘Bombe’ Roden - */ -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. - * - * @param webInterface - * The Sone web interface - */ - public GetTimesAjaxPage(WebInterface webInterface, TimeTextConverter timeTextConverter, L10nFilter l10nFilter) { - super("getTimes.ajax", webInterface); - this.timeTextConverter = timeTextConverter; - this.l10nFilter = l10nFilter; - } - - /** - * {@inheritDoc} - */ - @Override - protected JsonReturnObject createJsonObject(FreenetRequest request) { - String allIds = request.getHttpRequest().getParam("posts"); - ObjectNode postTimes = new ObjectNode(instance); - if (allIds.length() > 0) { - String[] ids = allIds.split(","); - for (String id : ids) { - Optional post = webInterface.getCore().getPost(id); - if (!post.isPresent()) { - continue; - } - 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.get().getTime()))); - } - postTimes.put(id, postTime); - } - } - ObjectNode replyTimes = new ObjectNode(instance); - allIds = request.getHttpRequest().getParam("replies"); - if (allIds.length() > 0) { - String[] ids = allIds.split(","); - for (String id : ids) { - Optional reply = webInterface.getCore().getPostReply(id); - if (!reply.isPresent()) { - continue; - } - 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.get().getTime()))); - } - replyTimes.put(id, replyTime); - } - } - return createSuccessJsonObject().put("postTimes", postTimes).put("replyTimes", replyTimes); - } - - /** - * {@inheritDoc} - */ - @Override - protected boolean needsFormPassword() { - return false; - } - - /** - * {@inheritDoc} - */ - @Override - protected boolean requiresLogin() { - return false; - } - - // - // PRIVATE METHODS - // - - /** - * Returns the formatted relative time for a given time. - * - * @param time - * The time to format the difference from (in milliseconds) - * @return The formatted age - */ - private Time getTime(long time) { - TimeText timeText = timeTextConverter.getTimeText(time); - return new Time(l10nFilter.format(null, timeText.getL10nText(), Collections.emptyMap()), timeText.getRefreshTime()); - } - - /** - * Container for a formatted time. - * - * @author David ‘Bombe’ Roden - */ - public static class Time { - - /** The formatted time. */ - private final String text; - - /** The time after which to refresh the time. */ - private final long refresh; - - /** - * Creates a new formatted time container. - * - * @param text - * The formatted time - * @param refresh - * The time after which to refresh the time (in milliseconds) - */ - public Time(String text, long refresh) { - this.text = text; - this.refresh = refresh; - } - - /** - * Returns the formatted time. - * - * @return The formatted time - */ - public String getText() { - return text; - } - - /** - * Returns the time after which to refresh the time. - * - * @return The time after which to refresh the time (in milliseconds) - */ - public long getRefresh() { - return refresh; - } - - /** - * {@inheritDoc} - */ - @Override - public String toString() { - return text; - } - - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.kt new file mode 100644 index 0000000..358003a --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetTimesAjaxPage.kt @@ -0,0 +1,43 @@ +package net.pterodactylus.sone.web.ajax + +import net.pterodactylus.sone.freenet.L10nFilter +import net.pterodactylus.sone.text.TimeTextConverter +import net.pterodactylus.sone.utils.jsonObject +import net.pterodactylus.sone.utils.let +import net.pterodactylus.sone.utils.parameters +import net.pterodactylus.sone.web.WebInterface +import net.pterodactylus.sone.web.page.FreenetRequest +import java.text.SimpleDateFormat + +/** + * Ajax page that returns a formatted, relative timestamp for replies or posts. + */ +class GetTimesAjaxPage(webInterface: WebInterface, + private val timeTextConverter: TimeTextConverter, + private val l10nFilter: L10nFilter) : JsonPage("getTimes.ajax", webInterface) { + + private val dateTimeFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss") + + override fun needsFormPassword() = false + override fun requiresLogin() = false + + override fun createJsonObject(request: FreenetRequest) = + createSuccessJsonObject().apply { + put("postTimes", request.parameters["posts"]!!.idsToJson { webInterface.core.getPost(it)?.let { it.id to it.time } }) + put("replyTimes", request.parameters["replies"]!!.idsToJson { webInterface.core.getPostReply(it)?.let { it.id to it.time } }) + } + + private fun String.idsToJson(transform: (String) -> Pair?) = + split(",").mapNotNull(transform).toJson() + + private fun List>.toJson() = jsonObject { + this@toJson.map { (id, time) -> + val timeText = timeTextConverter.getTimeText(time) + id to jsonObject( + "timeText" to l10nFilter.format(null, timeText.l10nText, emptyMap()), + "refreshTime" to timeText.refreshTime / 1000, + "tooltip" to dateTimeFormatter.format(time)) + }.forEach { this@jsonObject.put(it.first, it.second) } + } + +}