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.utils.isPOST
6 import net.pterodactylus.sone.web.WebInterface
7 import net.pterodactylus.sone.web.page.*
8 import net.pterodactylus.util.template.Template
9 import net.pterodactylus.util.template.TemplateContext
10 import java.util.logging.Level
11 import java.util.logging.Logger
12 import javax.inject.Inject
15 * The “create Sone” page lets the user create a new Sone.
17 @MenuName("CreateSone")
18 class CreateSonePage @Inject constructor(template: Template, webInterface: WebInterface):
19 SoneTemplatePage("createSone.html", webInterface, template, "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 throw RedirectException("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)