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.*
12 import net.pterodactylus.util.template.TemplateContext
13 import javax.inject.Inject
16 * Page that stores a user’s image modifications.
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) {
24 override fun createJsonObject(request: FreenetRequest) =
25 request.parameters["image"]
28 image.sone.isLocal.ifTrue {
30 request.parameters["moveLeft"] == "true" -> createSuccessJsonObject().apply {
31 put("sourceImageId", image.id)
32 put("destinationImageId", image.album.moveImageUp(image).id)
33 core.touchConfiguration()
35 request.parameters["moveRight"] == "true" -> createSuccessJsonObject().apply {
36 put("sourceImageId", image.id)
37 put("destinationImageId", image.album.moveImageDown(image).id)
38 core.touchConfiguration()
40 else -> request.parameters["title"]!!.let { title ->
41 title.trim().isNotBlank().ifTrue {
42 request.parameters["description"]!!.let { description ->
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
57 core.touchConfiguration()
61 } ?: createErrorJsonObject("invalid-image-title")
64 } ?: createErrorJsonObject("not-authorized")
65 } ?: createErrorJsonObject("invalid-image-id")