Disallow empty album titles, too.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 17 Sep 2014 16:56:22 +0000 (18:56 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 17 Sep 2014 16:56:22 +0000 (18:56 +0200)
src/main/java/net/pterodactylus/sone/data/AlbumImpl.java
src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java
src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java
src/main/resources/i18n/sone.en.properties
src/main/resources/templates/emptyAlbumTitle.html [new file with mode: 0644]
src/main/resources/templates/imageBrowser.html

index b60e99e..bb32d75 100644 (file)
@@ -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
        //
index 76a71d6..9f9f795 100644 (file)
@@ -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());
                }
index 7debd3d..e09ff11 100644 (file)
@@ -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());
                }
index f3b77e9..96c76f7 100644 (file)
@@ -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<FreenetRequest>("css/", "/static/css/", "text/css")));
index 4c3d2ba..238206a 100644 (file)
@@ -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");
+               }
        }
 
 }
index ac9a578..8f89a08 100644 (file)
@@ -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 (file)
index 0000000..0df7c51
--- /dev/null
@@ -0,0 +1,7 @@
+<%include include/head.html>
+
+       <h1><%= Page.EmptyAlbumTitle.Page.Title|l10n|html></h1>
+
+       <p><%= Page.EmptyAlbumTitle.Text.EmptyAlbumTitle|l10n|html></p>
+
+<%include include/tail.html>
index fd0296d..7dd4c29 100644 (file)
                                                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;
                                        });