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