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