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