1 package net.pterodactylus.sone.web.pages
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.*
13 * The “create Sone” page lets the user create a new Sone.
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") {
21 private val logger = Logger.getLogger(CreateSonePage::class.java.name)
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)
31 logger.log(Level.SEVERE, "Could not create Sone for OwnIdentity: $ownIdentity")
33 setCurrentSone(soneRequest.toadletContext, sone)
34 redirectTo("index.html")
36 templateContext["errorNoIdentity"] = true
40 override fun isEnabled(soneRequest: SoneRequest) =
41 if (soneRequest.core.preferences.requireFullAccess && !soneRequest.toadletContext.isAllowedFullAccess) {
44 (getCurrentSone(soneRequest.toadletContext) == null) || (soneRequest.core.localSones.size == 1)