877a1fc746a9a5853c12ba990e18ab8841fddbb7
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / DeletePostPage.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.utils.*
6 import net.pterodactylus.sone.web.*
7 import net.pterodactylus.sone.web.page.*
8 import net.pterodactylus.util.template.*
9 import javax.inject.*
10
11 /**
12  * Lets the user delete a post they made.
13  */
14 @TemplatePath("/templates/deletePost.html")
15 class DeletePostPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
16                 LoggedInPage("deletePost.html", "Page.DeletePost.Title", webInterface, loaders, templateRenderer) {
17
18         override fun handleRequest(soneRequest: SoneRequest, currentSone: Sone, templateContext: TemplateContext) {
19                 if (soneRequest.isPOST) {
20                         val post = soneRequest.core.getPost(soneRequest.httpRequest.getPartAsStringFailsafe("post", 36)) ?: throw RedirectException("noPermission.html")
21                         val returnPage = soneRequest.httpRequest.getPartAsStringFailsafe("returnPage", 256)
22                         if (!post.sone.isLocal) {
23                                 throw RedirectException("noPermission.html")
24                         }
25                         if (soneRequest.httpRequest.isPartSet("confirmDelete")) {
26                                 soneRequest.core.deletePost(post)
27                                 throw RedirectException(returnPage)
28                         } else if (soneRequest.httpRequest.isPartSet("abortDelete")) {
29                                 throw RedirectException(returnPage)
30                         }
31                         templateContext["post"] = post
32                         templateContext["returnPage"] = returnPage
33                         return
34                 }
35                 templateContext["post"] = soneRequest.core.getPost(soneRequest.httpRequest.getParam("post")) ?: throw RedirectException("noPermission.html")
36                 templateContext["returnPage"] = soneRequest.httpRequest.getParam("returnPage")
37         }
38
39 }