Rename object for linked elements in JSON reply
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / CreatePostPage.kt
1 package net.pterodactylus.sone.web
2
3 import net.pterodactylus.sone.text.TextFilter
4 import net.pterodactylus.sone.utils.isPOST
5 import net.pterodactylus.sone.web.page.FreenetRequest
6 import net.pterodactylus.util.template.Template
7 import net.pterodactylus.util.template.TemplateContext
8
9 /**
10  * This page lets the user create a new [Post].
11  */
12 class CreatePostPage(template: Template, webInterface: WebInterface):
13                 SoneTemplatePage("createPost.html", template, "Page.CreatePost.Title", webInterface, true) {
14
15         override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
16                 val returnPage = request.httpRequest.getPartAsStringFailsafe("returnPage", 256)
17                 templateContext["returnPage"] = returnPage
18                 if (request.isPOST) {
19                         val text = request.httpRequest.getPartAsStringFailsafe("text", 65536).trim()
20                         if (text == "") {
21                                 templateContext["errorTextEmpty"] = true
22                                 return
23                         }
24                         val sender = webInterface.core.getLocalSone(request.httpRequest.getPartAsStringFailsafe("sender", 43)) ?: getCurrentSone(request.toadletContext)
25                         val recipient = webInterface.core.getSone(request.httpRequest.getPartAsStringFailsafe("recipient", 43))
26                         webInterface.core.createPost(sender, recipient, TextFilter.filter(request.httpRequest.getHeader("Host"), text))
27                         throw RedirectException(returnPage)
28                 }
29         }
30
31 }