1 package net.pterodactylus.sone.web
3 import freenet.clients.http.ToadletContext
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.web.page.FreenetRequest
6 import net.pterodactylus.util.template.Template
7 import net.pterodactylus.util.template.TemplateContext
8 import net.pterodactylus.util.web.Method.POST
9 import java.util.logging.Level
10 import java.util.logging.Logger
13 * The “create Sone” page lets the user create a new Sone.
15 class CreateSonePage(template: Template, webInterface: WebInterface):
16 SoneTemplatePage("createSone.html", template, "Page.CreateSone.Title", webInterface, false) {
18 private val logger = Logger.getLogger(CreateSonePage::class.java.name)
20 override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
21 templateContext["sones"] = webInterface.core.localSones.sortedWith(Sone.NICE_NAME_COMPARATOR)
22 templateContext["identitiesWithoutSone"] = webInterface.core.identityManager.allOwnIdentities.filterNot { "Sone" in it.contexts }.sortedBy { "${it.nickname}@${it.id}".toLowerCase() }
23 if (request.method == POST) {
24 val identity = request.httpRequest.getPartAsStringFailsafe("identity", 43)
25 webInterface.core.identityManager.allOwnIdentities.firstOrNull { it.id == identity }?.let { ownIdentity ->
26 val sone = webInterface.core.createSone(ownIdentity)
28 logger.log(Level.SEVERE, "Could not create Sone for OwnIdentity: $ownIdentity")
30 setCurrentSone(request.toadletContext, sone)
31 throw RedirectException("index.html")
33 templateContext["errorNoIdentity"] = true
37 override fun isEnabled(toadletContext: ToadletContext) =
38 if (webInterface.core.preferences.isRequireFullAccess && !toadletContext.isAllowedFullAccess) {
41 (getCurrentSone(toadletContext) == null) || (webInterface.core.localSones.size == 1)