From 47dc11af1a7bbca09c04239edee33f83d6539b81 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 6 Sep 2017 07:49:21 +0200 Subject: [PATCH] Replace edit album ajax page with Kotlin version --- .../sone/web/ajax/EditAlbumAjaxPage.java | 80 ---------------------- .../sone/web/ajax/EditAlbumAjaxPage.kt | 45 ++++++++++++ 2 files changed, 45 insertions(+), 80 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.kt diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java deleted file mode 100644 index 9817fd8..0000000 --- a/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Sone - EditAlbumAjaxPage.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 net.pterodactylus.sone.data.Album; -import net.pterodactylus.sone.text.TextFilter; -import net.pterodactylus.sone.web.WebInterface; -import net.pterodactylus.sone.web.page.FreenetRequest; - -/** - * Page that stores a user’s album modifications. - * - * @author David ‘Bombe’ Roden - */ -public class EditAlbumAjaxPage extends JsonPage { - - /** - * Creates a new edit album AJAX page. - * - * @param webInterface - * The Sone web interface - */ - public EditAlbumAjaxPage(WebInterface webInterface) { - super("editAlbum.ajax", webInterface); - } - - // - // JSONPAGE METHODS - // - - /** - * {@inheritDoc} - */ - @Override - protected JsonReturnObject createJsonObject(FreenetRequest request) { - String albumId = request.getHttpRequest().getParam("album"); - Album album = webInterface.getCore().getAlbum(albumId); - if (album == null) { - return createErrorJsonObject("invalid-album-id"); - } - if (!album.getSone().isLocal()) { - return createErrorJsonObject("not-authorized"); - } - if ("true".equals(request.getHttpRequest().getParam("moveLeft"))) { - Album swappedAlbum = album.getParent().moveAlbumUp(album); - webInterface.getCore().touchConfiguration(); - return createSuccessJsonObject().put("sourceAlbumId", album.getId()).put("destinationAlbumId", swappedAlbum.getId()); - } - if ("true".equals(request.getHttpRequest().getParam("moveRight"))) { - Album swappedAlbum = album.getParent().moveAlbumDown(album); - webInterface.getCore().touchConfiguration(); - return createSuccessJsonObject().put("sourceAlbumId", album.getId()).put("destinationAlbumId", swappedAlbum.getId()); - } - String title = request.getHttpRequest().getParam("title").trim(); - String description = request.getHttpRequest().getParam("description").trim(); - try { - album.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update(); - webInterface.getCore().touchConfiguration(); - return createSuccessJsonObject().put("albumId", album.getId()).put("title", album.getTitle()).put("description", album.getDescription()); - } catch (IllegalStateException e) { - return createErrorJsonObject("invalid-album-title"); - } - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.kt new file mode 100644 index 0000000..cf12b33 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.kt @@ -0,0 +1,45 @@ +package net.pterodactylus.sone.web.ajax + +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 + +/** + * Page that stores a user’s album modifications. + */ +class EditAlbumAjaxPage(webInterface: WebInterface) : JsonPage("editAlbum.ajax", webInterface) { + + override fun createJsonObject(request: FreenetRequest) = + request.parameters["album"]!! + .let(webInterface.core::getAlbum) + ?.let { album -> + album.sone.isLocal.ifTrue { + when { + request.parameters["moveLeft"] == "true" -> createSuccessJsonObject().apply { + put("sourceAlbumId", album.id) + put("destinationAlbumId", album.parent.moveAlbumUp(album).id) + } + request.parameters["moveRight"] == "true" -> createSuccessJsonObject().apply { + put("sourceAlbumId", album.id) + put("destinationAlbumId", album.parent.moveAlbumDown(album).id) + } + else -> try { + album.modify() + .setTitle(request.parameters["title"]) + .setDescription(TextFilter.filter(request.headers["Host"], request.parameters["description"])) + .update() + createSuccessJsonObject() + .put("albumId", album.id) + .put("title", album.title) + .put("description", album.description) + } catch (e: IllegalStateException) { + createErrorJsonObject("invalid-album-title") + } + } + } ?: createErrorJsonObject("not-authorized") + } ?: createErrorJsonObject("invalid-album-id") + +} -- 2.7.4