✨ Annotate CreateSonePage with MenuName
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / CreateSonePage.kt
1 package net.pterodactylus.sone.web.pages
2
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
13
14 /**
15  * The “create Sone” page lets the user create a new Sone.
16  */
17 @MenuName("CreateSone")
18 class CreateSonePage @Inject constructor(template: Template, webInterface: WebInterface):
19                 SoneTemplatePage("createSone.html", webInterface, template, "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                                 throw RedirectException("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 }