aa5b94c7d6c7d885db74a74117349485e3aa944c
[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.text.TextFilter
5 import net.pterodactylus.sone.utils.asOptional
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 javax.inject.Inject
12
13 /**
14  * This page lets the user create a new [Post].
15  */
16 class CreatePostPage @Inject constructor(template: Template, webInterface: WebInterface):
17                 LoggedInPage("createPost.html", template, "Page.CreatePost.Title", webInterface) {
18
19         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
20                 val returnPage = soneRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256)
21                 templateContext["returnPage"] = returnPage
22                 if (soneRequest.isPOST) {
23                         val text = soneRequest.httpRequest.getPartAsStringFailsafe("text", 65536).trim()
24                         if (text == "") {
25                                 templateContext["errorTextEmpty"] = true
26                                 return
27                         }
28                         val sender = soneRequest.core.getLocalSone(soneRequest.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: currentSone
29                         val recipient = soneRequest.core.getSone(soneRequest.httpRequest.getPartAsStringFailsafe("recipient", 43))
30                         soneRequest.core.createPost(sender, recipient.asOptional(), TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), text))
31                         throw RedirectException(returnPage)
32                 }
33         }
34
35 }