From e1a0aa2cd922f84804f039f90611e7ad6a7699d0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 8 Sep 2017 20:18:01 +0200 Subject: [PATCH] Replace edit image ajax page with Kotlin version --- .../sone/web/ajax/EditImageAjaxPage.java | 101 --------------------- .../sone/web/ajax/EditImageAjaxPage.kt | 65 +++++++++++++ 2 files changed, 65 insertions(+), 101 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.kt diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java deleted file mode 100644 index d6256c1..0000000 --- a/src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Sone - EditImageAjaxPage.java - Copyright © 2011–2016 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.web.ajax; - -import com.google.common.collect.ImmutableMap; -import net.pterodactylus.sone.data.Image; -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.web.WebInterface; -import net.pterodactylus.sone.web.page.FreenetRequest; -import net.pterodactylus.util.template.TemplateContext; - -/** - * Page that stores a user’s image modifications. - * - * @author David ‘Bombe’ Roden - */ -public class EditImageAjaxPage extends JsonPage { - - private final ParserFilter parserFilter; - private final ShortenFilter shortenFilter; - private final RenderFilter renderFilter; - - /** - * Creates a new edit image AJAX page. - * - * @param webInterface - * The Sone web interface - * @param parserFilter - * The parser filter for image descriptions - */ - public EditImageAjaxPage(WebInterface webInterface, ParserFilter parserFilter, ShortenFilter shortenFilter, RenderFilter renderFilter) { - super("editImage.ajax", webInterface); - this.parserFilter = parserFilter; - this.shortenFilter = shortenFilter; - this.renderFilter = renderFilter; - } - - // - // JSONPAGE METHODS - // - - /** - * {@inheritDoc} - */ - @Override - protected JsonReturnObject createJsonObject(FreenetRequest request) { - String imageId = request.getHttpRequest().getParam("image"); - Image image = webInterface.getCore().getImage(imageId, false); - if (image == null) { - return createErrorJsonObject("invalid-image-id"); - } - if (!image.getSone().isLocal()) { - return createErrorJsonObject("not-authorized"); - } - if ("true".equals(request.getHttpRequest().getParam("moveLeft"))) { - Image swappedImage = image.getAlbum().moveImageUp(image); - webInterface.getCore().touchConfiguration(); - return createSuccessJsonObject().put("sourceImageId", image.getId()).put("destinationImageId", swappedImage.getId()); - } - if ("true".equals(request.getHttpRequest().getParam("moveRight"))) { - Image swappedImage = image.getAlbum().moveImageDown(image); - webInterface.getCore().touchConfiguration(); - return createSuccessJsonObject().put("sourceImageId", image.getId()).put("destinationImageId", swappedImage.getId()); - } - String title = request.getHttpRequest().getParam("title").trim(); - if (title.isEmpty()) { - return createErrorJsonObject("invalid-image-title"); - } - String description = request.getHttpRequest().getParam("description").trim(); - image.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update(); - webInterface.getCore().touchConfiguration(); - return createSuccessJsonObject().put("imageId", image.getId()).put("title", image.getTitle()).put("description", image.getDescription()).put("parsedDescription", renderImageDescription(image)); - } - - private String renderImageDescription(Image image) { - TemplateContext templateContext = new TemplateContext(); - ImmutableMap parameters = ImmutableMap.builder().put("sone", image.getSone()).build(); - Object parts = parserFilter.format(templateContext, image.getDescription(), parameters); - Object shortenedParts = shortenFilter.format(templateContext, parts, parameters); - return (String) renderFilter.format(templateContext, shortenedParts, parameters); - } - -} 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") + +} -- 2.7.4