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 @TemplatePath("/templates/createSone.html")
20 class CreateSonePage @Inject constructor(template: Template, webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer):
21 SoneTemplatePage("createSone.html", webInterface, loaders, template, templateRenderer, pageTitleKey = "Page.CreateSone.Title") {
23 private val logger = Logger.getLogger(CreateSonePage::class.java.name)
25 override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) {
26 templateContext["sones"] = soneRequest.core.localSones.sortedWith(Sone.NICE_NAME_COMPARATOR)
27 templateContext["identitiesWithoutSone"] = soneRequest.core.identityManager.allOwnIdentities.filterNot { "Sone" in it.contexts }.sortedBy { "${it.nickname}@${it.id}".toLowerCase() }
28 if (soneRequest.isPOST) {
29 val identity = soneRequest.httpRequest.getPartAsStringFailsafe("identity", 43)
30 soneRequest.core.identityManager.allOwnIdentities.firstOrNull { it.id == identity }?.let { ownIdentity ->
31 val sone = soneRequest.core.createSone(ownIdentity)
33 logger.log(Level.SEVERE, "Could not create Sone for OwnIdentity: $ownIdentity")
35 setCurrentSone(soneRequest.toadletContext, sone)
36 throw RedirectException("index.html")
38 templateContext["errorNoIdentity"] = true
42 override fun isEnabled(soneRequest: SoneRequest) =
43 if (soneRequest.core.preferences.requireFullAccess && !soneRequest.toadletContext.isAllowedFullAccess) {
46 (getCurrentSone(soneRequest.toadletContext) == null) || (soneRequest.core.localSones.size == 1)