1 package net.pterodactylus.sone.web.ajax
3 import com.fasterxml.jackson.databind.JsonNode
4 import com.fasterxml.jackson.databind.ObjectMapper
5 import net.pterodactylus.sone.core.ElementLoader
6 import net.pterodactylus.sone.core.LinkedElement
7 import net.pterodactylus.sone.template.LinkedElementRenderFilter
8 import net.pterodactylus.sone.utils.jsonArray
9 import net.pterodactylus.sone.utils.jsonObject
10 import net.pterodactylus.sone.web.WebInterface
11 import net.pterodactylus.sone.web.page.FreenetRequest
14 * Renders linked elements after they have been loaded.
16 class GetLinkedElementAjaxPage(webInterface: WebInterface, private val elementLoader: ElementLoader, private val linkedElementRenderFilter: LinkedElementRenderFilter):
17 JsonPage("getLinkedElement.ajax", webInterface) {
19 override fun createJsonObject(request: FreenetRequest): JsonReturnObject =
20 request.httpRequest.getParam("elements", "[]").asJson()
21 .map(JsonNode::asText)
22 .map(elementLoader::loadElement)
23 .filterNot { it.loading }
24 .map { it to renderLinkedElement(it) }
27 *(elements.map { element ->
29 put("link", element.first.link)
30 put("html", element.second)
34 }.let { linkedElements ->
35 createSuccessJsonObject().apply {
36 put("linkedElements", linkedElements)
40 override fun needsFormPassword() = false
41 override fun requiresLogin() = false
43 private fun String.asJson() = ObjectMapper().readTree(this).asIterable()
45 private fun renderLinkedElement(linkedElement: LinkedElement) =
46 linkedElementRenderFilter.format(null, linkedElement, emptyMap())