X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FEditImageAjaxPage.kt;fp=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FEditImageAjaxPage.kt;h=ee2cf19665fa3cbd5eaca814bb76127b52119293;hb=e1a0aa2cd922f84804f039f90611e7ad6a7699d0;hp=0000000000000000000000000000000000000000;hpb=fecc40a37c481ea7c8d0aa5d13331834d165de6a;p=Sone.git diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.kt new file mode 100644 index 0000000..ee2cf19 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.kt @@ -0,0 +1,65 @@ +package net.pterodactylus.sone.web.ajax + +import net.pterodactylus.sone.template.ParserFilter +import net.pterodactylus.sone.template.RenderFilter +import net.pterodactylus.sone.template.ShortenFilter +import net.pterodactylus.sone.text.TextFilter +import net.pterodactylus.sone.utils.headers +import net.pterodactylus.sone.utils.ifTrue +import net.pterodactylus.sone.utils.parameters +import net.pterodactylus.sone.web.WebInterface +import net.pterodactylus.sone.web.page.FreenetRequest +import net.pterodactylus.util.template.TemplateContext + +/** + * Page that stores a user’s image modifications. + */ +class EditImageAjaxPage(webInterface: WebInterface, + private val parserFilter: ParserFilter, + private val shortenFilter: ShortenFilter, + private val renderFilter: RenderFilter) : JsonPage("editImage.ajax", webInterface) { + + override fun createJsonObject(request: FreenetRequest) = + request.parameters["image"] + .let(webInterface.core::getImage) + ?.let { image -> + image.sone.isLocal.ifTrue { + when { + request.parameters["moveLeft"] == "true" -> createSuccessJsonObject().apply { + put("sourceImageId", image.id) + put("destinationImageId", image.album.moveImageUp(image).id) + webInterface.core.touchConfiguration() + } + request.parameters["moveRight"] == "true" -> createSuccessJsonObject().apply { + put("sourceImageId", image.id) + put("destinationImageId", image.album.moveImageDown(image).id) + webInterface.core.touchConfiguration() + } + else -> request.parameters["title"]!!.let { title -> + title.trim().isNotBlank().ifTrue { + request.parameters["description"]!!.let { description -> + image.modify() + .setTitle(title) + .setDescription(TextFilter.filter(request.headers["Host"], description)) + .update().let { newImage -> + createSuccessJsonObject().apply { + put("title", newImage.title) + put("description", newImage.description) + put("parsedDescription", newImage.description.let { + parserFilter.format(TemplateContext(), it, mutableMapOf("sone" to image.sone)).let { + shortenFilter.format(TemplateContext(), it, mutableMapOf()).let { + renderFilter.format(TemplateContext(), it, mutableMapOf()) as String + } + } + }) + webInterface.core.touchConfiguration() + } + } + } + } ?: createErrorJsonObject("invalid-image-title") + } + } + } ?: createErrorJsonObject("not-authorized") + } ?: createErrorJsonObject("invalid-image-id") + +}