Remove obsolete loading animation
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / CreateSonePage.kt
1 package net.pterodactylus.sone.web
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.page.FreenetRequest
7 import net.pterodactylus.util.template.Template
8 import net.pterodactylus.util.template.TemplateContext
9 import java.util.logging.Level
10 import java.util.logging.Logger
11
12 /**
13  * The “create Sone” page lets the user create a new Sone.
14  */
15 class CreateSonePage(template: Template, webInterface: WebInterface):
16                 SoneTemplatePage("createSone.html", template, "Page.CreateSone.Title", webInterface, false) {
17
18         private val logger = Logger.getLogger(CreateSonePage::class.java.name)
19
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.isPOST) {
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)
27                                 if (sone == null) {
28                                         logger.log(Level.SEVERE, "Could not create Sone for OwnIdentity: $ownIdentity")
29                                 }
30                                 setCurrentSone(request.toadletContext, sone)
31                                 throw RedirectException("index.html")
32                         }
33                         templateContext["errorNoIdentity"] = true
34                 }
35         }
36
37         override fun isEnabled(toadletContext: ToadletContext) =
38                         if (webInterface.core.preferences.isRequireFullAccess && !toadletContext.isAllowedFullAccess) {
39                                 false
40                         } else {
41                                 (getCurrentSone(toadletContext) == null) || (webInterface.core.localSones.size == 1)
42                         }
43
44 }