b1d62b317a910069503f3496c35da5acdfd92fd4
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / CreatePostPage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.Sone
4 import net.pterodactylus.sone.main.*
5 import net.pterodactylus.sone.text.TextFilter
6 import net.pterodactylus.sone.utils.asOptional
7 import net.pterodactylus.sone.utils.isPOST
8 import net.pterodactylus.sone.web.WebInterface
9 import net.pterodactylus.sone.web.page.*
10 import net.pterodactylus.util.template.Template
11 import net.pterodactylus.util.template.TemplateContext
12 import javax.inject.Inject
13
14 /**
15  * This page lets the user create a new [Post].
16  */
17 @TemplatePath("/templates/createPost.html")
18 class CreatePostPage @Inject constructor(template: Template, webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer):
19                 LoggedInPage("createPost.html", template, "Page.CreatePost.Title", webInterface, loaders, templateRenderer) {
20
21         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
22                 val returnPage = soneRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256)
23                 templateContext["returnPage"] = returnPage
24                 if (soneRequest.isPOST) {
25                         val text = soneRequest.httpRequest.getPartAsStringFailsafe("text", 65536).trim()
26                         if (text == "") {
27                                 templateContext["errorTextEmpty"] = true
28                                 return
29                         }
30                         val sender = soneRequest.core.getLocalSone(soneRequest.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: currentSone
31                         val recipient = soneRequest.core.getSone(soneRequest.httpRequest.getPartAsStringFailsafe("recipient", 43))
32                         soneRequest.core.createPost(sender, recipient.asOptional(), TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), text))
33                         throw RedirectException(returnPage)
34                 }
35         }
36
37 }