X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FGetStatusAjaxPage.kt;h=d7884bf31f70c60ffea48786af590ecba0f5624b;hp=21d296a4e57d3566817f8dbe16bf5d795d9e5157;hb=dd96e781b592c3bae9b0f66f85ba05a4e4cc18ce;hpb=787aa8a7839dcb32c07a8e2b6d982c7e2cb67244 diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt index 21d296a..d7884bf 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.kt @@ -1,6 +1,9 @@ package net.pterodactylus.sone.web.ajax import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.ObjectMapper +import net.pterodactylus.sone.core.ElementLoader +import net.pterodactylus.sone.core.LinkedElement import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.data.PostReply import net.pterodactylus.sone.data.Sone @@ -18,7 +21,7 @@ import java.text.SimpleDateFormat * The “get status” AJAX handler returns all information that is necessary to * update the web interface in real-time. */ -class GetStatusAjaxPage(webInterface: WebInterface, private val timeTextConverter: TimeTextConverter, private val l10nFilter: L10nFilter): +class GetStatusAjaxPage(webInterface: WebInterface, private val elementLoader: ElementLoader, private val timeTextConverter: TimeTextConverter, private val l10nFilter: L10nFilter): JsonPage("getStatus.ajax", webInterface) { private val dateFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss") @@ -32,6 +35,7 @@ class GetStatusAjaxPage(webInterface: WebInterface, private val timeTextConverte this["sones"] = request.httpRequest.getParam("soneIds").split(',').map { webInterface.core.getSone(it).orNull() }.plus(currentSone).filterNotNull().toJsonSones() this["newPosts"] = webInterface.getNewPosts(currentSone).toJsonPosts() this["newReplies"] = webInterface.getNewReplies(currentSone).toJsonReplies() + this["loadedElements"] = request.httpRequest.getParam("elements", "[]").asJson().map(JsonNode::asText).map(elementLoader::loadElement).toJsonElements() } } @@ -39,6 +43,8 @@ class GetStatusAjaxPage(webInterface: WebInterface, private val timeTextConverte private operator fun JsonReturnObject.set(key: String, value: Int) = put(key, value) private operator fun JsonReturnObject.set(key: String, value: Boolean) = put(key, value) + private fun String.asJson() = ObjectMapper().readTree(this).asIterable() + override fun needsFormPassword() = false override fun requiresLogin() = false @@ -82,4 +88,12 @@ class GetStatusAjaxPage(webInterface: WebInterface, private val timeTextConverte } }.toArray() + private fun Iterable.toJsonElements() = map { (link, failed, loading) -> + jsonObject { + put("link", link) + put("loading", loading) + put("failed", failed) + } + }.toArray() + }