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