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