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