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