🔀 Merge branch 'release/v82'
[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.*
4 import net.pterodactylus.sone.main.*
5 import net.pterodactylus.sone.text.*
6 import net.pterodactylus.sone.utils.*
7 import net.pterodactylus.sone.web.*
8 import net.pterodactylus.sone.web.page.*
9 import net.pterodactylus.util.template.*
10 import javax.inject.*
11
12 /**
13  * This page lets the user create a new [Post].
14  */
15 @TemplatePath("/templates/createPost.html")
16 @ToadletPath("createPost.html")
17 class CreatePostPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
18                 LoggedInPage("Page.CreatePost.Title", webInterface, loaders, templateRenderer) {
19
20         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
21                 val returnPage = soneRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256)
22                 templateContext["returnPage"] = returnPage
23                 if (soneRequest.isPOST) {
24                         val text = soneRequest.httpRequest.getPartAsStringFailsafe("text", 65536).trim()
25                         if (text == "") {
26                                 templateContext["errorTextEmpty"] = true
27                                 return
28                         }
29                         val sender = soneRequest.core.getLocalSone(soneRequest.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: currentSone
30                         val recipient = soneRequest.core.getSone(soneRequest.httpRequest.getPartAsStringFailsafe("recipient", 43))
31                         soneRequest.core.createPost(sender, recipient, TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), text))
32                         redirectTo(returnPage)
33                 }
34         }
35
36 }