From 4c1684d486dc19aacc09a297f75d6fd2228007f1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 17 Sep 2014 18:56:22 +0200 Subject: [PATCH] Disallow empty album titles, too. --- .../java/net/pterodactylus/sone/data/AlbumImpl.java | 5 +++++ .../net/pterodactylus/sone/web/CreateAlbumPage.java | 7 ++++++- .../net/pterodactylus/sone/web/EditAlbumPage.java | 11 ++++++----- .../net/pterodactylus/sone/web/WebInterface.java | 2 ++ .../sone/web/ajax/EditAlbumAjaxPage.java | 10 +++++++--- src/main/resources/i18n/sone.en.properties | 4 ++++ src/main/resources/templates/emptyAlbumTitle.html | 7 +++++++ src/main/resources/templates/imageBrowser.html | 21 ++++++++++++++------- 8 files changed, 51 insertions(+), 16 deletions(-) create mode 100644 src/main/resources/templates/emptyAlbumTitle.html diff --git a/src/main/java/net/pterodactylus/sone/data/AlbumImpl.java b/src/main/java/net/pterodactylus/sone/data/AlbumImpl.java index b60e99e..bb32d75 100644 --- a/src/main/java/net/pterodactylus/sone/data/AlbumImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/AlbumImpl.java @@ -303,6 +303,9 @@ public class AlbumImpl implements Album { @Override public Album update() throws IllegalStateException { + if (title.isPresent() && title.get().trim().isEmpty()) { + throw new AlbumTitleMustNotBeEmpty(); + } if (title.isPresent()) { AlbumImpl.this.title = title.get(); } @@ -317,6 +320,8 @@ public class AlbumImpl implements Album { }; } + public static class AlbumTitleMustNotBeEmpty extends IllegalStateException { } + // // FINGERPRINTABLE METHODS // diff --git a/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java b/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java index 76a71d6..9f9f795 100644 --- a/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java +++ b/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java @@ -18,6 +18,7 @@ package net.pterodactylus.sone.web; import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.AlbumImpl.AlbumTitleMustNotBeEmpty; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.text.TextFilter; import net.pterodactylus.sone.web.page.FreenetRequest; @@ -68,7 +69,11 @@ public class CreateAlbumPage extends SoneTemplatePage { parent = currentSone.getRootAlbum(); } Album album = webInterface.getCore().createAlbum(currentSone, parent); - album.modify().setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update(); + try { + album.modify().setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update(); + } catch (AlbumTitleMustNotBeEmpty atmnbe) { + throw new RedirectException("emptyAlbumTitle.html"); + } webInterface.getCore().touchConfiguration(); throw new RedirectException("imageBrowser.html?album=" + album.getId()); } diff --git a/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java b/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java index 7debd3d..e09ff11 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java @@ -18,6 +18,7 @@ package net.pterodactylus.sone.web; import net.pterodactylus.sone.data.Album; +import net.pterodactylus.sone.data.AlbumImpl.AlbumTitleMustNotBeEmpty; import net.pterodactylus.sone.text.TextFilter; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.template.Template; @@ -73,12 +74,12 @@ public class EditAlbumPage extends SoneTemplatePage { } album.modify().setAlbumImage(albumImageId).update(); String title = request.getHttpRequest().getPartAsStringFailsafe("title", 100).trim(); - if (title.length() == 0) { - templateContext.set("titleMissing", true); - return; - } String description = request.getHttpRequest().getPartAsStringFailsafe("description", 1000).trim(); - album.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update(); + try { + album.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update(); + } catch (AlbumTitleMustNotBeEmpty atmnbe) { + throw new RedirectException("emptyAlbumTitle.html"); + } webInterface.getCore().touchConfiguration(); throw new RedirectException("imageBrowser.html?album=" + album.getId()); } diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index f3b77e9..96c76f7 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -642,6 +642,7 @@ public class WebInterface { Template deleteImageTemplate = parseTemplate("/templates/deleteImage.html"); Template noPermissionTemplate = parseTemplate("/templates/noPermission.html"); Template emptyImageTitleTemplate = parseTemplate("/templates/emptyImageTitle.html"); + Template emptyAlbumTitleTemplate = parseTemplate("/templates/emptyAlbumTitle.html"); Template optionsTemplate = parseTemplate("/templates/options.html"); Template rescueTemplate = parseTemplate("/templates/rescue.html"); Template aboutTemplate = parseTemplate("/templates/about.html"); @@ -694,6 +695,7 @@ public class WebInterface { pageToadlets.add(pageToadletFactory.createPageToadlet(new AboutPage(aboutTemplate, this, SonePlugin.VERSION), "About")); pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("noPermission.html", noPermissionTemplate, "Page.NoPermission.Title", this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyImageTitle.html", emptyImageTitleTemplate, "Page.EmptyImageTitle.Title", this))); + pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("emptyAlbumTitle.html", emptyAlbumTitleTemplate, "Page.EmptyAlbumTitle.Title", this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new DismissNotificationPage(emptyTemplate, this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new SoneTemplatePage("invalid.html", invalidTemplate, "Page.Invalid.Title", this))); pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage("css/", "/static/css/", "text/css"))); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java index 4c3d2ba..238206a 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java @@ -68,9 +68,13 @@ public class EditAlbumAjaxPage extends JsonPage { } String title = request.getHttpRequest().getParam("title").trim(); String description = request.getHttpRequest().getParam("description").trim(); - 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()); + 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/resources/i18n/sone.en.properties b/src/main/resources/i18n/sone.en.properties index ac9a578..8f89a08 100644 --- a/src/main/resources/i18n/sone.en.properties +++ b/src/main/resources/i18n/sone.en.properties @@ -309,6 +309,10 @@ Page.EmptyImageTitle.Title=Title Must Not Be Empty - Sone Page.EmptyImageTitle.Page.Title=Title Must Not Be Empty Page.EmptyImageTitle.Text.EmptyImageTitle=You have to give your image a title. Please go back to the previous page and enter a title. +Page.EmptyAlbumTitle.Title=Title Must Not Be Empty - Sone +Page.EmptyAlbumTitle.Page.Title=Title Must Not Be Empty +Page.EmptyAlbumTitle.Text.EmptyAlbumTitle=You have to give your album a title. Please go back to the previous page and enter a title. + Page.DismissNotification.Title=Dismiss Notification - Sone Page.WotPluginMissing.Text.WotRequired=Because the Web of Trust is an integral part of Sone, the Web of Trust plugin has to be loaded in order to run Sone. diff --git a/src/main/resources/templates/emptyAlbumTitle.html b/src/main/resources/templates/emptyAlbumTitle.html new file mode 100644 index 0000000..0df7c51 --- /dev/null +++ b/src/main/resources/templates/emptyAlbumTitle.html @@ -0,0 +1,7 @@ +<%include include/head.html> + +

<%= Page.EmptyAlbumTitle.Page.Title|l10n|html>

+ +

<%= Page.EmptyAlbumTitle.Text.EmptyAlbumTitle|l10n|html>

+ +<%include include/tail.html> diff --git a/src/main/resources/templates/imageBrowser.html b/src/main/resources/templates/imageBrowser.html index fd0296d..7dd4c29 100644 --- a/src/main/resources/templates/imageBrowser.html +++ b/src/main/resources/templates/imageBrowser.html @@ -239,13 +239,20 @@ title = $(":input[name='title']:enabled", this.form).val(); description = $(":input[name='description']:enabled", this.form).val(); ajaxGet("editAlbum.ajax", { "formPassword": getFormPassword(), "album": albumId, "title": title, "description": description }, function(data) { - if (data && data.success) { - getAlbum(data.albumId).find(".album-title").text(data.title); - getAlbum(data.albumId).find(".album-description").text(data.description); - getAlbum(data.albumId).find(":input[name='title']").attr("defaultValue", title); - getAlbum(data.albumId).find(":input[name='description']").attr("defaultValue", description); - cancelAlbumEditing(); - } + if (data) { + var albumTitleField = getAlbum(data.albumId).find(".album-title"); + var albumDescriptionField = getAlbum(data.albumId).find(".album-description"); + if (data.success) { + albumTitleField.text(data.title); + albumDescriptionField.text(data.description); + getAlbum(data.albumId).find(":input[name='title']").attr("defaultValue", title); + getAlbum(data.albumId).find(":input[name='description']").attr("defaultValue", description); + } else { + albumTitleField.attr("value", albumTitleField.attr("defaultValue")); + albumDescriptionField.attr("value", albumDescriptionField.attr("defaultValue")); + } + cancelAlbumEditing(); + } }); return false; }); -- 2.7.4