🔀 Merge 'feature/sone-locked-notification' into 'next'
[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.*
12 import net.pterodactylus.util.template.TemplateContext
13 import javax.inject.Inject
14
15 /**
16  * Page that stores a user’s image modifications.
17  */
18 @ToadletPath("editImage.ajax")
19 class EditImageAjaxPage @Inject constructor(webInterface: WebInterface,
20                 private val parserFilter: ParserFilter,
21                 private val shortenFilter: ShortenFilter,
22                 private val renderFilter: RenderFilter) : JsonPage(webInterface) {
23
24         override fun createJsonObject(request: FreenetRequest) =
25                         request.parameters["image"]
26                                         .let(core::getImage)
27                                         ?.let { image ->
28                                                 image.sone.isLocal.ifTrue {
29                                                         when {
30                                                                 request.parameters["moveLeft"] == "true" -> createSuccessJsonObject().apply {
31                                                                         put("sourceImageId", image.id)
32                                                                         put("destinationImageId", image.album.moveImageUp(image).id)
33                                                                         core.touchConfiguration()
34                                                                 }
35                                                                 request.parameters["moveRight"] == "true" -> createSuccessJsonObject().apply {
36                                                                         put("sourceImageId", image.id)
37                                                                         put("destinationImageId", image.album.moveImageDown(image).id)
38                                                                         core.touchConfiguration()
39                                                                 }
40                                                                 else -> request.parameters["title"]!!.let { title ->
41                                                                         title.trim().isNotBlank().ifTrue {
42                                                                                 request.parameters["description"]!!.let { description ->
43                                                                                         image.modify()
44                                                                                                         .setTitle(title)
45                                                                                                         .setDescription(TextFilter.filter(request.headers["Host"], description))
46                                                                                                         .update().let { newImage ->
47                                                                                                 createSuccessJsonObject().apply {
48                                                                                                         put("title", newImage.title)
49                                                                                                         put("description", newImage.description)
50                                                                                                         put("parsedDescription", newImage.description.let {
51                                                                                                                 parserFilter.format(TemplateContext(), it, mutableMapOf("sone" to image.sone)).let {
52                                                                                                                         shortenFilter.format(TemplateContext(), it, mutableMapOf()).let {
53                                                                                                                                 renderFilter.format(TemplateContext(), it, mutableMapOf()) as String
54                                                                                                                         }
55                                                                                                                 }
56                                                                                                         })
57                                                                                                         core.touchConfiguration()
58                                                                                                 }
59                                                                                         }
60                                                                                 }
61                                                                         } ?: createErrorJsonObject("invalid-image-title")
62                                                                 }
63                                                         }
64                                                 } ?: createErrorJsonObject("not-authorized")
65                                         } ?: createErrorJsonObject("invalid-image-id")
66
67 }