92a015a2689715db00f3be5edf364edd58507792
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / CreateReplyPage.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 post a reply to a post.
14  */
15 @TemplatePath("/templates/createReply.html")
16 class CreateReplyPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer):
17                 LoggedInPage("createReply.html", "Page.CreateReply.Title", webInterface, loaders, templateRenderer) {
18
19         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
20                 val postId = soneRequest.httpRequest.getPartAsStringFailsafe("post", 36).apply { templateContext["postId"] = this }
21                 val text = soneRequest.httpRequest.getPartAsStringFailsafe("text", 65536).trim().apply { templateContext["text"] = this }
22                 val returnPage = soneRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256).apply { templateContext["returnPage"] = this }
23                 if (soneRequest.isPOST) {
24                         if (text == "") {
25                                 templateContext["errorTextEmpty"] = true
26                                 return
27                         }
28                         val post = soneRequest.core.getPost(postId) ?: throw RedirectException("noPermission.html")
29                         val sender = soneRequest.core.getLocalSone(soneRequest.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: currentSone
30                         soneRequest.core.createReply(sender, post, TextFilter.filter(soneRequest.httpRequest.getHeader("Host"), text))
31                         throw RedirectException(returnPage)
32                 }
33         }
34
35 }