X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FGetTimesAjaxPage.kt;fp=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FGetTimesAjaxPage.kt;h=358003a0c80a3362b35606194e5136d6f6f3f55a;hp=0000000000000000000000000000000000000000;hb=69af5a8e6bc69347e3979fd2f351ead5fcfb98fb;hpb=d91e48af542fef4c0064c5a6641b473240ef4273 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) } + } + +}