🚧 Add Loaders to all template-using pages
[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.Template
10 import net.pterodactylus.util.template.TemplateContext
11 import java.util.logging.Level
12 import java.util.logging.Logger
13 import javax.inject.Inject
14
15 /**
16  * The “create Sone” page lets the user create a new Sone.
17  */
18 @MenuName("CreateSone")
19 class CreateSonePage @Inject constructor(template: Template, webInterface: WebInterface, loaders: Loaders):
20                 SoneTemplatePage("createSone.html", webInterface, loaders, template = template, 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 }