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