From f814ad7a876bea03db77b30532eab3e7e03a05d8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 19 Nov 2016 16:32:58 +0100 Subject: [PATCH] Incrase maximum size for content to 2 MiB --- .../pterodactylus/sone/core/FreenetInterface.java | 2 +- .../kotlin/net/pterodactylus/sone/text/TimeText.kt | 8 ++ .../pterodactylus/sone/text/TimeTextConverter.kt | 50 +++++++++ .../sone/text/TimeTextConverterTest.kt | 122 +++++++++++++++++++++ 4 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/text/TimeText.kt create mode 100644 src/main/kotlin/net/pterodactylus/sone/text/TimeTextConverter.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/text/TimeTextConverterTest.kt diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index 82cc94b..4b33139 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -187,7 +187,7 @@ public class FreenetInterface { }; FetchContext fetchContext = client.getFetchContext(); try { - ClientGetter clientGetter = client.fetch(uri, 1048576, callback, fetchContext, RequestStarter.INTERACTIVE_PRIORITY_CLASS); + ClientGetter clientGetter = client.fetch(uri, 2097152, callback, fetchContext, RequestStarter.INTERACTIVE_PRIORITY_CLASS); clientGetter.setMetaSnoop(snoop); clientGetter.restart(uri, fetchContext.filterData, node.clientCore.clientContext); } catch (FetchException fe) { diff --git a/src/main/kotlin/net/pterodactylus/sone/text/TimeText.kt b/src/main/kotlin/net/pterodactylus/sone/text/TimeText.kt new file mode 100644 index 0000000..740e03e --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/text/TimeText.kt @@ -0,0 +1,8 @@ +package net.pterodactylus.sone.text + +import net.pterodactylus.sone.freenet.L10nText + +/** + * Container for an l10n key and a refresh time. + */ +data class TimeText(val l10nText: L10nText, val refreshTime: Long) diff --git a/src/main/kotlin/net/pterodactylus/sone/text/TimeTextConverter.kt b/src/main/kotlin/net/pterodactylus/sone/text/TimeTextConverter.kt new file mode 100644 index 0000000..ae79fe5 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/text/TimeTextConverter.kt @@ -0,0 +1,50 @@ +package net.pterodactylus.sone.text + +import net.pterodactylus.sone.freenet.L10nText +import java.util.concurrent.TimeUnit.DAYS +import java.util.concurrent.TimeUnit.HOURS +import java.util.concurrent.TimeUnit.MILLISECONDS +import java.util.concurrent.TimeUnit.MINUTES +import java.util.concurrent.TimeUnit.SECONDS + +/** + * Converts a time (in Java milliseconds) to an L10n key and a refresh time. + */ +class TimeTextConverter(private val timeSuppler: () -> Long = { System.currentTimeMillis() }) { + + fun getTimeText(time: Long): TimeText { + val age = timeSuppler.invoke() - time + return when { + time == 0L -> TimeText(L10nText("View.Sone.Text.UnknownDate"), 12.hours()) + age < 0 -> TimeText(L10nText("View.Time.InTheFuture"), 5.minutes()) + age < 20.seconds() -> TimeText(L10nText("View.Time.AFewSecondsAgo"), 10.seconds()) + age < 45.seconds() -> TimeText(L10nText("View.Time.HalfAMinuteAgo"), 20.seconds()) + age < 90.seconds() -> TimeText(L10nText("View.Time.AMinuteAgo"), 1.minutes()) + age < 30.minutes() -> TimeText(L10nText("View.Time.XMinutesAgo", listOf((age + 30.seconds()).toMinutes())), 1.minutes()) + age < 45.minutes() -> TimeText(L10nText("View.Time.HalfAnHourAgo"), 10.minutes()) + age < 90.minutes() -> TimeText(L10nText("View.Time.AnHourAgo"), 1.hours()) + age < 21.hours() -> TimeText(L10nText("View.Time.XHoursAgo", listOf((age + 30.minutes()).toHours())), 1.hours()) + age < 42.hours() -> TimeText(L10nText("View.Time.ADayAgo"), 1.days()) + age < 6.days() -> TimeText(L10nText("View.Time.XDaysAgo", listOf((age + 12.hours()).toDays())), 1.days()) + age < 11.days() -> TimeText(L10nText("View.Time.AWeekAgo"), 1.days()) + age < 28.days() -> TimeText(L10nText("View.Time.XWeeksAgo", listOf((age + 3.days() + 12.hours()).toWeeks())), 1.days()) + age < 42.days() -> TimeText(L10nText("View.Time.AMonthAgo"), 1.days()) + age < 330.days() -> TimeText(L10nText("View.Time.XMonthsAgo", listOf((age + 15.days()).toMonths())), 1.days()) + age < 540.days() -> TimeText(L10nText("View.Time.AYearAgo"), 7.days()) + else -> TimeText(L10nText("View.Time.XYearsAgo", listOf((age + 182.days() + 12.hours()).toYears())), 7.days()) + } + } + + private fun Long.toMinutes() = MILLISECONDS.toMinutes(this) + private fun Long.toHours() = MILLISECONDS.toHours(this) + private fun Long.toDays() = MILLISECONDS.toDays(this) + private fun Long.toWeeks() = MILLISECONDS.toDays(this) / 7 + private fun Long.toMonths() = MILLISECONDS.toDays(this) / 30 + private fun Long.toYears() = MILLISECONDS.toDays(this) / 365 + private fun Int.seconds() = SECONDS.toMillis(this.toLong()) + private fun Int.minutes() = MINUTES.toMillis(this.toLong()) + private fun Int.hours() = HOURS.toMillis(this.toLong()) + private fun Int.days() = DAYS.toMillis(this.toLong()) + +} + diff --git a/src/test/kotlin/net/pterodactylus/sone/text/TimeTextConverterTest.kt b/src/test/kotlin/net/pterodactylus/sone/text/TimeTextConverterTest.kt new file mode 100644 index 0000000..cd8b3ed --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/text/TimeTextConverterTest.kt @@ -0,0 +1,122 @@ +package net.pterodactylus.sone.text + +import net.pterodactylus.sone.freenet.L10nText +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.Test +import java.util.concurrent.TimeUnit.DAYS +import java.util.concurrent.TimeUnit.HOURS +import java.util.concurrent.TimeUnit.MINUTES +import java.util.concurrent.TimeUnit.SECONDS + +/** + * Unit test for [TimeTextConverter]. + */ +class TimeTextConverterTest { + + val now = System.currentTimeMillis() + val converter = TimeTextConverter { now } + + private fun verifyInterval(startTime: Long, end: Long, vararg expectedTimeTexts: TimeText) { + assertThat(converter.getTimeText(now - startTime), equalTo(expectedTimeTexts[0])) + assertThat(converter.getTimeText(now - (end - 1)), equalTo(expectedTimeTexts[1 % expectedTimeTexts.size])) + } + + @Test + fun `time of zero returns the l10n key for "unknown" and a refresh time of 12 hours`() { + assertThat(converter.getTimeText(0), equalTo(TimeText(L10nText("View.Sone.Text.UnknownDate"), HOURS.toMillis(12)))) + } + + @Test + fun `time in the future returns the correct l10n key and refresh of 5 minutes`() { + assertThat(converter.getTimeText(now + 1), equalTo(TimeText(L10nText("View.Time.InTheFuture"), MINUTES.toMillis(5)))) + } + + @Test + fun `time of zero to twenty seconds ago returns l10n key for "a few seconds ago" and refresh of 10 seconds`() { + verifyInterval(0, SECONDS.toMillis(20), TimeText(L10nText("View.Time.AFewSecondsAgo"), SECONDS.toMillis(10))) + } + + @Test + fun `time of twenty to forty-five seconds ago returns l10n key for "half a minute ago" and refresh of 20 seconds`() { + verifyInterval(SECONDS.toMillis(20), SECONDS.toMillis(45), TimeText(L10nText("View.Time.HalfAMinuteAgo"), SECONDS.toMillis(20))) + } + + @Test + fun `time of forty-five to ninety seconds ago returns l10n key for "a minute ago" and a refresh time of 1 minute`() { + verifyInterval(SECONDS.toMillis(45), SECONDS.toMillis(90), TimeText(L10nText("View.Time.AMinuteAgo"), MINUTES.toMillis(1))) + } + + @Test + fun `time of ninety seconds to thirty minutes ago returns l10n key for "x minutes ago," the number of minutes, and a refresh time of 1 minute`() { + verifyInterval(SECONDS.toMillis(90), MINUTES.toMillis(30), + TimeText(L10nText("View.Time.XMinutesAgo", listOf(2L)), MINUTES.toMillis(1)), + TimeText(L10nText("View.Time.XMinutesAgo", listOf(30L)), MINUTES.toMillis(1))) + } + + @Test + fun `time of thirty to forty-five minutes ago returns l10n key for "half an hour ago" and a refresh time of 10 minutes`() { + verifyInterval(MINUTES.toMillis(30), MINUTES.toMillis(45), TimeText(L10nText("View.Time.HalfAnHourAgo"), MINUTES.toMillis(10))) + } + + @Test + fun `time of forty-five to ninety minutes ago returns l10n key for "an hour ago" and a refresh time of 1 hour`() { + verifyInterval(MINUTES.toMillis(45), MINUTES.toMillis(90), TimeText(L10nText("View.Time.AnHourAgo"), HOURS.toMillis(1))) + } + + @Test + fun `time of ninety minutes to twenty-one hours ago returns l10n key for "x hours ago," the number of hours, and a refresh time of 1 hour`() { + verifyInterval(MINUTES.toMillis(90), HOURS.toMillis(21), + TimeText(L10nText("View.Time.XHoursAgo", listOf(2L)), HOURS.toMillis(1)), + TimeText(L10nText("View.Time.XHoursAgo", listOf(21L)), HOURS.toMillis(1))) + } + + @Test + fun `time of twenty-one to forty-two hours ago returns l10n key for "a day ago" and a refresh time of 1 day`() { + verifyInterval(HOURS.toMillis(21), HOURS.toMillis(42), TimeText(L10nText("View.Time.ADayAgo"), DAYS.toMillis(1))) + } + + @Test + fun `time of forty-two hours to six days ago returns l10n key for "x days ago," the number of days, and a refresh time of 1 day`() { + verifyInterval(HOURS.toMillis(42), DAYS.toMillis(6), + TimeText(L10nText("View.Time.XDaysAgo", listOf(2L)), DAYS.toMillis(1)), + TimeText(L10nText("View.Time.XDaysAgo", listOf(6L)), DAYS.toMillis(1))) + } + + @Test + fun `time of six to eleven days ago returns l10n key for "a week ago" and a refresh time of 1 day`() { + verifyInterval(DAYS.toMillis(6), DAYS.toMillis(11), TimeText(L10nText("View.Time.AWeekAgo"), DAYS.toMillis(1))) + } + + @Test + fun `time of eleven to twenty-eight days ago returns l10n key for "x weeks ago," the number of weeks, and a refresh time of 1 day`() { + verifyInterval(DAYS.toMillis(11), DAYS.toMillis(28), + TimeText(L10nText("View.Time.XWeeksAgo", listOf(2L)), DAYS.toMillis(1)), + TimeText(L10nText("View.Time.XWeeksAgo", listOf(4L)), DAYS.toMillis(1))) + } + + @Test + fun `time of twenty-eight to forty-two days ago returns l10n key for "a month ago" and a refresh time of 1 day`() { + verifyInterval(DAYS.toMillis(28), DAYS.toMillis(42), TimeText(L10nText("View.Time.AMonthAgo"), DAYS.toMillis(1))) + } + + @Test + fun `time of forty-two to three hundred and thirty days ago returns l10n key for "x months ago," the number of months, and a refresh time of 1 day`() { + verifyInterval(DAYS.toMillis(42), DAYS.toMillis(330), + TimeText(L10nText("View.Time.XMonthsAgo", listOf(1L)), DAYS.toMillis(1)), + TimeText(L10nText("View.Time.XMonthsAgo", listOf(11L)), DAYS.toMillis(1))) + } + + @Test + fun `time of three hundred and thirty to five hundred and forty days days ago returns l10n key for "a year ago" and a refresh time of 7 days`() { + verifyInterval(DAYS.toMillis(330), DAYS.toMillis(540), TimeText(L10nText("View.Time.AYearAgo"), DAYS.toMillis(7))) + } + + @Test + fun `time of five hunder and forty to infinity days ago returns l10n key for "x years ago," the number of years, and a refresh time of 7 days`() { + verifyInterval(DAYS.toMillis(540), DAYS.toMillis(6000), + TimeText(L10nText("View.Time.XYearsAgo", listOf(1L)), DAYS.toMillis(7)), + TimeText(L10nText("View.Time.XYearsAgo", listOf(16L)), DAYS.toMillis(7))) + } + +} -- 2.7.4