3103898d31d98298dbe42963c79dab397b4fbbe3
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / GetStatusAjaxPage.kt
1 package net.pterodactylus.sone.web.ajax
2
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
20
21 /**
22  * The “get status” AJAX handler returns all information that is necessary to
23  * update the web interface in real-time.
24  */
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) {
27
28         private val dateFormatter = SimpleDateFormat("MMM d, yyyy, HH:mm:ss").apply {
29                 this.timeZone = timeZone
30         }
31
32         override fun createJsonObject(request: FreenetRequest) =
33                         getCurrentSone(request.toadletContext, false).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()
42                                 }
43                         }
44
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)
48
49         private fun String.asJson() = ObjectMapper().readTree(this).asIterable()
50
51         override fun needsFormPassword() = false
52         override fun requiresLogin() = false
53
54         private fun SoneOptions.toJsonOptions() = jsonObject {
55                 put("ShowNotification/NewSones", isShowNewSoneNotifications)
56                 put("ShowNotification/NewPosts", isShowNewPostNotifications)
57                 put("ShowNotification/NewReplies", isShowNewReplyNotifications)
58         }
59
60         private fun Iterable<Sone>.toJsonSones() = map { sone ->
61                 jsonObject {
62                         put("id", sone.id)
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))
71                         }
72                         put("lastUpdatedText", timeTextConverter.getTimeText(sone.time).l10nText.let { l10nFilter.format(null, it, emptyMap()) })
73                 }
74         }.toArray()
75
76         private fun Iterable<Post>.toJsonPosts() = map { post ->
77                 jsonObject {
78                         put("id", post.id)
79                         put("sone", post.sone.id)
80                         put("time", post.time)
81                         put("recipient", post.recipientId.orNull())
82                 }
83         }.toArray()
84
85         private fun Iterable<PostReply>.toJsonReplies() = map { reply ->
86                 jsonObject {
87                         put("id", reply.id)
88                         put("sone", reply.sone.id)
89                         put("post", reply.postId)
90                         put("postSone", reply.post.get().sone.id)
91                 }
92         }.toArray()
93
94         private fun Iterable<LinkedElement>.toJsonElements() = map { (link, failed, loading) ->
95                 jsonObject {
96                         put("link", link)
97                         put("loading", loading)
98                         put("failed", failed)
99                 }
100         }.toArray()
101
102 }