X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FSoneTemplatePage.kt;h=9734a033ed2d68c8c87e3744b3d1d0be313ff311;hp=3c3e98a1aca5306bfb0790b67aa84e94f63a68f8;hb=HEAD;hpb=e3da46d8947ab8a542a156069b3b24c13fd011df diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt index 3c3e98a..86131c3 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt @@ -1,44 +1,42 @@ package net.pterodactylus.sone.web.pages import freenet.clients.http.* -import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.data.* +import net.pterodactylus.sone.freenet.* import net.pterodactylus.sone.main.* -import net.pterodactylus.sone.utils.emptyToNull -import net.pterodactylus.sone.web.SessionProvider -import net.pterodactylus.sone.web.WebInterface +import net.pterodactylus.sone.utils.* +import net.pterodactylus.sone.web.* import net.pterodactylus.sone.web.page.* -import net.pterodactylus.util.notify.Notification -import net.pterodactylus.util.template.Template -import net.pterodactylus.util.template.TemplateContext +import net.pterodactylus.util.notify.* +import net.pterodactylus.util.template.* import net.pterodactylus.util.web.* -import java.net.URLEncoder +import java.net.* /** * Base page for the Sone web interface. */ -open class SoneTemplatePage @JvmOverloads constructor( - path: String, +open class SoneTemplatePage( private val webInterface: WebInterface, loaders: Loaders, - template: Template, templateRenderer: TemplateRenderer, private val pageTitleKey: String? = null, private val requiresLogin: Boolean = false, - private val pageTitle: (FreenetRequest) -> String = { pageTitleKey?.let(webInterface.l10n::getString) ?: "" } -) : FreenetTemplatePage(path, templateRenderer, loaders, template, "noPermission.html") { + private val pageTitle: (FreenetRequest) -> String = { pageTitleKey?.let(webInterface.translation::translate) ?: "" } +) : FreenetTemplatePage(templateRenderer, loaders, "noPermission.html") { private val core = webInterface.core private val sessionProvider: SessionProvider = webInterface + protected val translation: Translation = webInterface.translation protected fun getCurrentSone(toadletContext: ToadletContext, createSession: Boolean = true) = - sessionProvider.getCurrentSone(toadletContext, createSession) + sessionProvider.getCurrentSone(toadletContext) protected fun setCurrentSone(toadletContext: ToadletContext, sone: Sone?) = sessionProvider.setCurrentSone(toadletContext, sone) fun requiresLogin() = requiresLogin - override public fun getPageTitle(freenetRequest: FreenetRequest) = getPageTitle(freenetRequest.toSoneRequest(core, webInterface)) + override fun getPageTitle(request: FreenetRequest) = getPageTitle(request.toSoneRequest(core, webInterface)) open fun getPageTitle(soneRequest: SoneRequest) = pageTitle(soneRequest) @@ -46,7 +44,7 @@ open class SoneTemplatePage @JvmOverloads constructor( override val shortcutIcon = "images/icon.png" - override public fun getAdditionalLinkNodes(request: FreenetRequest) = + override fun getAdditionalLinkNodes(request: FreenetRequest) = listOf(mapOf( "rel" to "search", "type" to "application/opensearchdescription+xml", @@ -54,22 +52,22 @@ open class SoneTemplatePage @JvmOverloads constructor( "href" to "http://${request.httpRequest.getHeader("host")}/Sone/OpenSearch.xml" )) - final override public fun processTemplate(freenetRequest: FreenetRequest, templateContext: TemplateContext) { - super.processTemplate(freenetRequest, templateContext) + final override fun processTemplate(request: FreenetRequest, templateContext: TemplateContext) { + super.processTemplate(request, templateContext) templateContext["preferences"] = core.preferences - templateContext["currentSone"] = getCurrentSone(freenetRequest.toadletContext) + templateContext["currentSone"] = getCurrentSone(request.toadletContext) templateContext["localSones"] = core.localSones - templateContext["request"] = freenetRequest + templateContext["request"] = request templateContext["currentVersion"] = SonePlugin.getPluginVersion() templateContext["hasLatestVersion"] = core.updateChecker.hasLatestVersion() templateContext["latestEdition"] = core.updateChecker.latestEdition templateContext["latestVersion"] = core.updateChecker.latestVersion templateContext["latestVersionTime"] = core.updateChecker.latestVersionDate - webInterface.getNotifications(getCurrentSone(freenetRequest.toadletContext)).sortedBy(Notification::getCreatedTime).run { + webInterface.getNotifications(getCurrentSone(request.toadletContext)).sortedBy(Notification::getCreatedTime).run { templateContext["notifications"] = this templateContext["notificationHash"] = this.hashCode() } - handleRequest(freenetRequest, templateContext) + handleRequest(request, templateContext) } open fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { @@ -79,13 +77,13 @@ open class SoneTemplatePage @JvmOverloads constructor( open fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) { } - override public fun getRedirectTarget(freenetRequest: FreenetRequest): String? { - if (requiresLogin && getCurrentSone(freenetRequest.toadletContext) == null) { - val parameters = freenetRequest.httpRequest.parameterNames - .flatMap { name -> freenetRequest.httpRequest.getMultipleParam(name).map { name to it } } + override fun getRedirectTarget(request: FreenetRequest): String? { + if (requiresLogin && getCurrentSone(request.toadletContext) == null) { + val parameters = request.httpRequest.parameterNames + .flatMap { name -> request.httpRequest.getMultipleParam(name).map { name to it } } .joinToString("&") { "${it.first.urlEncode}=${it.second.urlEncode}" } .emptyToNull - return "login.html?target=${freenetRequest.httpRequest.path}${parameters?.let { ("?" + it).urlEncode } ?: ""}" + return "login.html?target=${request.httpRequest.path}${parameters?.let { ("?" + it).urlEncode } ?: ""}" } return null } @@ -93,7 +91,7 @@ open class SoneTemplatePage @JvmOverloads constructor( private val String.urlEncode: String get() = URLEncoder.encode(this, "UTF-8") override fun isEnabled(toadletContext: ToadletContext) = - isEnabled(SoneRequest(toadletContext.uri, Method.GET, HTTPRequestImpl(toadletContext.uri, "GET"), toadletContext, webInterface.l10n, webInterface.sessionManager, core, webInterface)) + isEnabled(SoneRequest(toadletContext.uri, Method.GET, HTTPRequestImpl(toadletContext.uri, "GET"), toadletContext, core, webInterface)) open fun isEnabled(soneRequest: SoneRequest) = when { requiresLogin && getCurrentSone(soneRequest.toadletContext) == null -> false