Use mapPresent() instead of more complex construct
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / GetStatusAjaxPage.kt
index 21d296a..e2642c9 100644 (file)
@@ -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
@@ -9,29 +12,34 @@ import net.pterodactylus.sone.freenet.L10nFilter
 import net.pterodactylus.sone.template.SoneAccessor
 import net.pterodactylus.sone.text.TimeTextConverter
 import net.pterodactylus.sone.utils.jsonObject
+import net.pterodactylus.sone.utils.mapPresent
 import net.pterodactylus.sone.utils.toArray
 import net.pterodactylus.sone.web.WebInterface
 import net.pterodactylus.sone.web.page.FreenetRequest
 import java.text.SimpleDateFormat
+import java.util.TimeZone
 
 /**
  * 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, timeZone: TimeZone = TimeZone.getDefault()):
                JsonPage("getStatus.ajax", webInterface) {
 
-       private val dateFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss")
+       private val dateFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss").apply {
+               this.timeZone = timeZone
+       }
 
        override fun createJsonObject(request: FreenetRequest) =
-                       (webInterface.getCurrentSoneWithoutCreatingSession(request.toadletContext) as Sone?).let { currentSone ->
+                       getCurrentSone(request.toadletContext, false).let { currentSone ->
                                createSuccessJsonObject().apply {
                                        this["loggedIn"] = currentSone != null
                                        this["options"] = currentSone?.options?.toJsonOptions() ?: jsonObject {}
                                        this["notificationHash"] = webInterface.getNotifications(currentSone).sortedBy { it.createdTime }.hashCode()
-                                       this["sones"] = request.httpRequest.getParam("soneIds").split(',').map { webInterface.core.getSone(it).orNull() }.plus(currentSone).filterNotNull().toJsonSones()
+                                       this["sones"] = request.httpRequest.getParam("soneIds").split(',').mapPresent(core::getSone).plus(currentSone).filterNotNull().toJsonSones()
                                        this["newPosts"] = webInterface.getNewPosts(currentSone).toJsonPosts()
                                        this["newReplies"] = webInterface.getNewReplies(currentSone).toJsonReplies()
+                                       this["linkedElements"] = request.httpRequest.getParam("elements", "[]").asJson().map(JsonNode::asText).map(elementLoader::loadElement).toJsonElements()
                                }
                        }
 
@@ -39,6 +47,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 +92,12 @@ class GetStatusAjaxPage(webInterface: WebInterface, private val timeTextConverte
                }
        }.toArray()
 
+       private fun Iterable<LinkedElement>.toJsonElements() = map { (link, failed, loading) ->
+               jsonObject {
+                       put("link", link)
+                       put("loading", loading)
+                       put("failed", failed)
+               }
+       }.toArray()
+
 }