1 package net.pterodactylus.sone.web.ajax
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
15 * Page that stores a user’s image modifications.
17 class EditImageAjaxPage(webInterface: WebInterface,
18 private val parserFilter: ParserFilter,
19 private val shortenFilter: ShortenFilter,
20 private val renderFilter: RenderFilter) : JsonPage("editImage.ajax", webInterface) {
22 override fun createJsonObject(request: FreenetRequest) =
23 request.parameters["image"]
24 .let(webInterface.core::getImage)
26 image.sone.isLocal.ifTrue {
28 request.parameters["moveLeft"] == "true" -> createSuccessJsonObject().apply {
29 put("sourceImageId", image.id)
30 put("destinationImageId", image.album.moveImageUp(image).id)
31 webInterface.core.touchConfiguration()
33 request.parameters["moveRight"] == "true" -> createSuccessJsonObject().apply {
34 put("sourceImageId", image.id)
35 put("destinationImageId", image.album.moveImageDown(image).id)
36 webInterface.core.touchConfiguration()
38 else -> request.parameters["title"]!!.let { title ->
39 title.trim().isNotBlank().ifTrue {
40 request.parameters["description"]!!.let { description ->
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
55 webInterface.core.touchConfiguration()
59 } ?: createErrorJsonObject("invalid-image-title")
62 } ?: createErrorJsonObject("not-authorized")
63 } ?: createErrorJsonObject("invalid-image-id")