Replace edit image ajax page with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / ajax / EditImageAjaxPage.kt
1 package net.pterodactylus.sone.web.ajax
2
3 import net.pterodactylus.sone.template.ParserFilter
4 import net.pterodactylus.sone.template.RenderFilter
5 import net.pterodactylus.sone.template.ShortenFilter
6 import net.pterodactylus.sone.text.TextFilter
7 import net.pterodactylus.sone.utils.headers
8 import net.pterodactylus.sone.utils.ifTrue
9 import net.pterodactylus.sone.utils.parameters
10 import net.pterodactylus.sone.web.WebInterface
11 import net.pterodactylus.sone.web.page.FreenetRequest
12 import net.pterodactylus.util.template.TemplateContext
13
14 /**
15  * Page that stores a user’s image modifications.
16  */
17 class EditImageAjaxPage(webInterface: WebInterface,
18                 private val parserFilter: ParserFilter,
19                 private val shortenFilter: ShortenFilter,
20                 private val renderFilter: RenderFilter) : JsonPage("editImage.ajax", webInterface) {
21
22         override fun createJsonObject(request: FreenetRequest) =
23                         request.parameters["image"]
24                                         .let(webInterface.core::getImage)
25                                         ?.let { image ->
26                                                 image.sone.isLocal.ifTrue {
27                                                         when {
28                                                                 request.parameters["moveLeft"] == "true" -> createSuccessJsonObject().apply {
29                                                                         put("sourceImageId", image.id)
30                                                                         put("destinationImageId", image.album.moveImageUp(image).id)
31                                                                         webInterface.core.touchConfiguration()
32                                                                 }
33                                                                 request.parameters["moveRight"] == "true" -> createSuccessJsonObject().apply {
34                                                                         put("sourceImageId", image.id)
35                                                                         put("destinationImageId", image.album.moveImageDown(image).id)
36                                                                         webInterface.core.touchConfiguration()
37                                                                 }
38                                                                 else -> request.parameters["title"]!!.let { title ->
39                                                                         title.trim().isNotBlank().ifTrue {
40                                                                                 request.parameters["description"]!!.let { description ->
41                                                                                         image.modify()
42                                                                                                         .setTitle(title)
43                                                                                                         .setDescription(TextFilter.filter(request.headers["Host"], description))
44                                                                                                         .update().let { newImage ->
45                                                                                                 createSuccessJsonObject().apply {
46                                                                                                         put("title", newImage.title)
47                                                                                                         put("description", newImage.description)
48                                                                                                         put("parsedDescription", newImage.description.let {
49                                                                                                                 parserFilter.format(TemplateContext(), it, mutableMapOf("sone" to image.sone)).let {
50                                                                                                                         shortenFilter.format(TemplateContext(), it, mutableMapOf()).let {
51                                                                                                                                 renderFilter.format(TemplateContext(), it, mutableMapOf()) as String
52                                                                                                                         }
53                                                                                                                 }
54                                                                                                         })
55                                                                                                         webInterface.core.touchConfiguration()
56                                                                                                 }
57                                                                                         }
58                                                                                 }
59                                                                         } ?: createErrorJsonObject("invalid-image-title")
60                                                                 }
61                                                         }
62                                                 } ?: createErrorJsonObject("not-authorized")
63                                         } ?: createErrorJsonObject("invalid-image-id")
64
65 }