Clean up SoneTemplatePage’s constructors
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / CreateSonePage.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.utils.isPOST
6 import net.pterodactylus.sone.web.WebInterface
7 import net.pterodactylus.sone.web.page.FreenetRequest
8 import net.pterodactylus.util.template.Template
9 import net.pterodactylus.util.template.TemplateContext
10 import java.util.logging.Level
11 import java.util.logging.Logger
12
13 /**
14  * The “create Sone” page lets the user create a new Sone.
15  */
16 class CreateSonePage(template: Template, webInterface: WebInterface):
17                 SoneTemplatePage("createSone.html", webInterface, template, "Page.CreateSone.Title") {
18
19         private val logger = Logger.getLogger(CreateSonePage::class.java.name)
20
21         override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
22                 templateContext["sones"] = webInterface.core.localSones.sortedWith(Sone.NICE_NAME_COMPARATOR)
23                 templateContext["identitiesWithoutSone"] = webInterface.core.identityManager.allOwnIdentities.filterNot { "Sone" in it.contexts }.sortedBy { "${it.nickname}@${it.id}".toLowerCase() }
24                 if (freenetRequest.isPOST) {
25                         val identity = freenetRequest.httpRequest.getPartAsStringFailsafe("identity", 43)
26                         webInterface.core.identityManager.allOwnIdentities.firstOrNull { it.id == identity }?.let { ownIdentity ->
27                                 val sone = webInterface.core.createSone(ownIdentity)
28                                 if (sone == null) {
29                                         logger.log(Level.SEVERE, "Could not create Sone for OwnIdentity: $ownIdentity")
30                                 }
31                                 setCurrentSone(freenetRequest.toadletContext, sone)
32                                 throw RedirectException("index.html")
33                         }
34                         templateContext["errorNoIdentity"] = true
35                 }
36         }
37
38         override fun isEnabled(toadletContext: ToadletContext) =
39                         if (webInterface.core.preferences.isRequireFullAccess && !toadletContext.isAllowedFullAccess) {
40                                 false
41                         } else {
42                                 (getCurrentSone(toadletContext) == null) || (webInterface.core.localSones.size == 1)
43                         }
44
45 }