Clean up SoneTemplatePage’s constructors
[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 @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) = pageTitle(freenetRequest)
40
41         override public fun getStyleSheets() =
42                         listOf("css/sone.css")
43
44         override public fun getShortcutIcon() = "images/icon.png"
45
46         override public fun getAdditionalLinkNodes(request: FreenetRequest) =
47                         listOf(mapOf(
48                                         "rel" to "search",
49                                         "type" to "application/opensearchdescription+xml",
50                                         "title" to "Sone",
51                                         "href" to "http://${request.httpRequest.getHeader("host")}/Sone/OpenSearch.xml"
52                         ))
53
54         final override public fun processTemplate(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
55                 super.processTemplate(freenetRequest, templateContext)
56                 templateContext["preferences"] = core.preferences
57                 templateContext["currentSone"] = getCurrentSone(freenetRequest.toadletContext)
58                 templateContext["localSones"] = core.localSones
59                 templateContext["request"] = freenetRequest
60                 templateContext["currentVersion"] = SonePlugin.getPluginVersion()
61                 templateContext["hasLatestVersion"] = core.updateChecker.hasLatestVersion()
62                 templateContext["latestEdition"] = core.updateChecker.latestEdition
63                 templateContext["latestVersion"] = core.updateChecker.latestVersion
64                 templateContext["latestVersionTime"] = core.updateChecker.latestVersionDate
65                 webInterface.getNotifications(getCurrentSone(freenetRequest.toadletContext)).sortedBy(Notification::getCreatedTime).run {
66                         templateContext["notifications"] = this
67                         templateContext["notificationHash"] = this.hashCode()
68                 }
69                 handleRequest(freenetRequest, templateContext)
70         }
71
72         internal open fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
73         }
74
75         override public fun getRedirectTarget(freenetRequest: FreenetRequest): String? {
76                 if (requiresLogin && getCurrentSone(freenetRequest.toadletContext) == null) {
77                         val parameters = freenetRequest.httpRequest.parameterNames
78                                         .flatMap { name -> freenetRequest.httpRequest.getMultipleParam(name).map { name to it } }
79                                         .joinToString("&") { "${it.first.urlEncode}=${it.second.urlEncode}" }
80                                         .emptyToNull
81                         return "login.html?target=${freenetRequest.httpRequest.path}${parameters?.let { ("?" + it).urlEncode } ?: ""}"
82                 }
83                 return null
84         }
85
86         private val String.urlEncode: String get() = URLEncoder.encode(this, "UTF-8")
87
88         override fun isEnabled(toadletContext: ToadletContext) = when {
89                 requiresLogin && getCurrentSone(toadletContext) == null -> false
90                 core.preferences.isRequireFullAccess && !toadletContext.isAllowedFullAccess -> false
91                 else -> true
92         }
93
94 }