Replace like page with Kotlin version
[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.text.TextFilter
4 import net.pterodactylus.sone.utils.isPOST
5 import net.pterodactylus.sone.web.pages.SoneTemplatePage
6 import net.pterodactylus.sone.web.WebInterface
7 import net.pterodactylus.sone.web.page.FreenetRequest
8 import net.pterodactylus.util.template.Template
9 import net.pterodactylus.util.template.TemplateContext
10
11 /**
12  * This page lets the user post a reply to a post.
13  */
14 class CreateReplyPage(template: Template, webInterface: WebInterface):
15                 SoneTemplatePage("createReply.html", template, "Page.CreateReply.Title", webInterface, true) {
16
17         override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
18                 val postId = request.httpRequest.getPartAsStringFailsafe("post", 36).apply { templateContext["postId"] = this }
19                 val text = request.httpRequest.getPartAsStringFailsafe("text", 65536).trim().apply { templateContext["text"] = this }
20                 val returnPage = request.httpRequest.getPartAsStringFailsafe("returnPage", 256).apply { templateContext["returnPage"] = this }
21                 if (request.isPOST) {
22                         if (text == "") {
23                                 templateContext["errorTextEmpty"] = true
24                                 return
25                         }
26                         val post = webInterface.core.getPost(postId).orNull() ?: throw RedirectException("noPermission.html")
27                         val sender = webInterface.core.getLocalSone(request.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: getCurrentSone(request.toadletContext)
28                         webInterface.core.createReply(sender, post, TextFilter.filter(request.httpRequest.getHeader("Host"), text))
29                         throw RedirectException(returnPage)
30                 }
31         }
32
33 }