🚧 Add handler for startup event
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / SoneTemplatePage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import freenet.clients.http.*
4 import net.pterodactylus.sone.data.*
5 import net.pterodactylus.sone.freenet.*
6 import net.pterodactylus.sone.main.*
7 import net.pterodactylus.sone.utils.*
8 import net.pterodactylus.sone.web.*
9 import net.pterodactylus.sone.web.page.*
10 import net.pterodactylus.util.notify.*
11 import net.pterodactylus.util.template.*
12 import net.pterodactylus.util.web.*
13 import java.net.*
14
15 /**
16  * Base page for the Sone web interface.
17  */
18 open class SoneTemplatePage(
19                 private val webInterface: WebInterface,
20                 loaders: Loaders,
21                 templateRenderer: TemplateRenderer,
22                 private val pageTitleKey: String? = null,
23                 private val requiresLogin: Boolean = false,
24                 private val pageTitle: (FreenetRequest) -> String = { pageTitleKey?.let(webInterface.translation::translate) ?: "" }
25 ) : FreenetTemplatePage(templateRenderer, loaders, "noPermission.html") {
26
27         private val core = webInterface.core
28         private val sessionProvider: SessionProvider = webInterface
29         protected val translation: Translation = webInterface.translation
30
31         protected fun getCurrentSone(toadletContext: ToadletContext, createSession: Boolean = true) =
32                         sessionProvider.getCurrentSone(toadletContext, createSession)
33
34         protected fun setCurrentSone(toadletContext: ToadletContext, sone: Sone?) =
35                         sessionProvider.setCurrentSone(toadletContext, sone)
36
37         fun requiresLogin() = requiresLogin
38
39         override fun getPageTitle(request: FreenetRequest) = getPageTitle(request.toSoneRequest(core, webInterface))
40
41         open fun getPageTitle(soneRequest: SoneRequest) = pageTitle(soneRequest)
42
43         override val styleSheets = listOf("css/sone.css")
44
45         override val shortcutIcon = "images/icon.png"
46
47         override fun getAdditionalLinkNodes(request: FreenetRequest) =
48                         listOf(mapOf(
49                                         "rel" to "search",
50                                         "type" to "application/opensearchdescription+xml",
51                                         "title" to "Sone",
52                                         "href" to "http://${request.httpRequest.getHeader("host")}/Sone/OpenSearch.xml"
53                         ))
54
55         final override fun processTemplate(request: FreenetRequest, templateContext: TemplateContext) {
56                 super.processTemplate(request, templateContext)
57                 templateContext["preferences"] = core.preferences
58                 templateContext["currentSone"] = getCurrentSone(request.toadletContext)
59                 templateContext["localSones"] = core.localSones
60                 templateContext["request"] = request
61                 templateContext["currentVersion"] = SonePlugin.getPluginVersion()
62                 templateContext["hasLatestVersion"] = core.updateChecker.hasLatestVersion()
63                 templateContext["latestEdition"] = core.updateChecker.latestEdition
64                 templateContext["latestVersion"] = core.updateChecker.latestVersion
65                 templateContext["latestVersionTime"] = core.updateChecker.latestVersionDate
66                 webInterface.getNotifications(getCurrentSone(request.toadletContext)).sortedBy(Notification::getCreatedTime).run {
67                         templateContext["notifications"] = this
68                         templateContext["notificationHash"] = this.hashCode()
69                 }
70                 handleRequest(request, templateContext)
71         }
72
73         open fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
74                 handleRequest(freenetRequest.toSoneRequest(core, webInterface), templateContext)
75         }
76
77         open fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) {
78         }
79
80         override fun getRedirectTarget(request: FreenetRequest): String? {
81                 if (requiresLogin && getCurrentSone(request.toadletContext) == null) {
82                         val parameters = request.httpRequest.parameterNames
83                                         .flatMap { name -> request.httpRequest.getMultipleParam(name).map { name to it } }
84                                         .joinToString("&") { "${it.first.urlEncode}=${it.second.urlEncode}" }
85                                         .emptyToNull
86                         return "login.html?target=${request.httpRequest.path}${parameters?.let { ("?" + it).urlEncode } ?: ""}"
87                 }
88                 return null
89         }
90
91         private val String.urlEncode: String get() = URLEncoder.encode(this, "UTF-8")
92
93         override fun isEnabled(toadletContext: ToadletContext) =
94                         isEnabled(SoneRequest(toadletContext.uri, Method.GET, HTTPRequestImpl(toadletContext.uri, "GET"), toadletContext, webInterface.sessionManager, core, webInterface))
95
96         open fun isEnabled(soneRequest: SoneRequest) = when {
97                 requiresLogin && getCurrentSone(soneRequest.toadletContext) == null -> false
98                 core.preferences.requireFullAccess && !soneRequest.toadletContext.isAllowedFullAccess -> false
99                 else -> true
100         }
101
102 }