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