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.data.Post
8 import net.pterodactylus.sone.data.PostReply
9 import net.pterodactylus.sone.data.Sone
10 import net.pterodactylus.sone.data.SoneOptions
11 import net.pterodactylus.sone.freenet.L10nFilter
12 import net.pterodactylus.sone.template.SoneAccessor
13 import net.pterodactylus.sone.text.TimeTextConverter
14 import net.pterodactylus.sone.utils.jsonObject
15 import net.pterodactylus.sone.utils.toArray
16 import net.pterodactylus.sone.web.WebInterface
17 import net.pterodactylus.sone.web.page.FreenetRequest
18 import java.text.SimpleDateFormat
19 import java.util.TimeZone
22 * The “get status” AJAX handler returns all information that is necessary to
23 * update the web interface in real-time.
25 class GetStatusAjaxPage(webInterface: WebInterface, private val elementLoader: ElementLoader, private val timeTextConverter: TimeTextConverter, private val l10nFilter: L10nFilter, timeZone: TimeZone = TimeZone.getDefault()):
26 JsonPage("getStatus.ajax", webInterface) {
28 private val dateFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss").apply {
29 this.timeZone = timeZone
32 override fun createJsonObject(request: FreenetRequest) =
33 (webInterface.getCurrentSoneWithoutCreatingSession(request.toadletContext) as Sone?).let { currentSone ->
34 createSuccessJsonObject().apply {
35 this["loggedIn"] = currentSone != null
36 this["options"] = currentSone?.options?.toJsonOptions() ?: jsonObject {}
37 this["notificationHash"] = webInterface.getNotifications(currentSone).sortedBy { it.createdTime }.hashCode()
38 this["sones"] = request.httpRequest.getParam("soneIds").split(',').map { webInterface.core.getSone(it).orNull() }.plus(currentSone).filterNotNull().toJsonSones()
39 this["newPosts"] = webInterface.getNewPosts(currentSone).toJsonPosts()
40 this["newReplies"] = webInterface.getNewReplies(currentSone).toJsonReplies()
41 this["linkedElements"] = request.httpRequest.getParam("elements", "[]").asJson().map(JsonNode::asText).map(elementLoader::loadElement).toJsonElements()
45 private operator fun JsonReturnObject.set(key: String, value: JsonNode) = put(key, value)
46 private operator fun JsonReturnObject.set(key: String, value: Int) = put(key, value)
47 private operator fun JsonReturnObject.set(key: String, value: Boolean) = put(key, value)
49 private fun String.asJson() = ObjectMapper().readTree(this).asIterable()
51 override fun needsFormPassword() = false
52 override fun requiresLogin() = false
54 private fun SoneOptions.toJsonOptions() = jsonObject {
55 put("ShowNotification/NewSones", isShowNewSoneNotifications)
56 put("ShowNotification/NewPosts", isShowNewPostNotifications)
57 put("ShowNotification/NewReplies", isShowNewReplyNotifications)
60 private fun Iterable<Sone>.toJsonSones() = map { sone ->
63 put("name", SoneAccessor.getNiceName(sone))
64 put("local", sone.isLocal)
65 put("status", sone.status.name)
66 put("modified", webInterface.core.isModifiedSone(sone))
67 put("locked", webInterface.core.isLocked(sone))
68 put("lastUpdatedUnknown", sone.time == 0L)
69 synchronized(dateFormatter) {
70 put("lastUpdated", dateFormatter.format(sone.time))
72 put("lastUpdatedText", timeTextConverter.getTimeText(sone.time).l10nText.let { l10nFilter.format(null, it, emptyMap()) })
76 private fun Iterable<Post>.toJsonPosts() = map { post ->
79 put("sone", post.sone.id)
80 put("time", post.time)
81 put("recipient", post.recipientId.orNull())
85 private fun Iterable<PostReply>.toJsonReplies() = map { reply ->
88 put("sone", reply.sone.id)
89 put("post", reply.postId)
90 put("postSone", reply.post.get().sone.id)
94 private fun Iterable<LinkedElement>.toJsonElements() = map { (link, failed, loading) ->
97 put("loading", loading)