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