19cdd2404db3ada396584c876e1bb736f5036733
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / GetNotificationsAjaxPage.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.data.SoneOptions
5 import net.pterodactylus.sone.main.SonePlugin
6 import net.pterodactylus.sone.utils.jsonArray
7 import net.pterodactylus.sone.utils.jsonObject
8 import net.pterodactylus.sone.web.WebInterface
9 import net.pterodactylus.sone.web.page.FreenetRequest
10 import net.pterodactylus.util.notify.Notification
11 import net.pterodactylus.util.notify.TemplateNotification
12 import java.io.StringWriter
13
14 /**
15  * AJAX handler to return all current notifications.
16  */
17 class GetNotificationsAjaxPage(webInterface: WebInterface) : JsonPage("getNotifications.ajax", webInterface) {
18
19         override val needsFormPassword = false
20         override val requiresLogin = false
21
22         override fun createJsonObject(request: FreenetRequest) =
23                         getCurrentSone(request.toadletContext, false).let { currentSone ->
24                                 webInterface.getNotifications(currentSone)
25                                                 .sortedBy(Notification::getCreatedTime)
26                                                 .let { notifications ->
27                                                         createSuccessJsonObject().apply {
28                                                                 put("notificationHash", notifications.hashCode())
29                                                                 put("options", currentSone?.options.asJsonObject)
30                                                                 put("notifications", notifications.asJsonObject(currentSone, request))
31                                                         }
32                                                 }
33                         }
34
35         private fun Collection<Notification>.asJsonObject(currentSone: Sone?, freenetRequest: FreenetRequest) = jsonArray(
36                         *map { notification ->
37                                 jsonObject(
38                                                 "id" to notification.id,
39                                                 "createdTime" to notification.createdTime,
40                                                 "lastUpdatedTime" to notification.lastUpdatedTime,
41                                                 "dismissable" to notification.isDismissable,
42                                                 "text" to if (notification is TemplateNotification) notification.render(currentSone, freenetRequest) else notification.render()
43                                 )
44                         }.toTypedArray()
45         )
46
47         private fun TemplateNotification.render(currentSone: Sone?, freenetRequest: FreenetRequest) = StringWriter().use {
48                 val mergedTemplateContext = webInterface.templateContextFactory.createTemplateContext()
49                                 .mergeContext(templateContext)
50                                 .apply {
51                                         this["core"] = core
52                                         this["currentSone"] = currentSone
53                                         this["localSones"] = core.localSones
54                                         this["request"] = freenetRequest
55                                         this["currentVersion"] = SonePlugin.getPluginVersion()
56                                         this["hasLatestVersion"] = core.updateChecker.hasLatestVersion()
57                                         this["latestEdition"] = core.updateChecker.latestEdition
58                                         this["latestVersion"] = core.updateChecker.latestVersion
59                                         this["latestVersionTime"] = core.updateChecker.latestVersionDate
60                                         this["notification"] = this@render
61                                 }
62                 it.also { render(mergedTemplateContext, it) }
63         }.toString()
64
65 }
66
67 private val SoneOptions?.asJsonObject
68         get() = this?.let { options ->
69                 jsonObject(
70                                 "ShowNotification/NewSones" to options.isShowNewSoneNotifications,
71                                 "ShowNotification/NewPosts" to options.isShowNewPostNotifications,
72                                 "ShowNotification/NewReplies" to options.isShowNewReplyNotifications
73                 )
74         } ?: jsonObject {}
75
76 private fun Notification.render() = StringWriter().use { it.also { render(it) } }.toString()