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