Add page to edit albums with Javascript.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 23 Sep 2011 22:25:03 +0000 (00:25 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 23 Sep 2011 22:25:03 +0000 (00:25 +0200)
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java [new file with mode: 0644]

index 9dd9954..d72cec2 100644 (file)
@@ -75,6 +75,7 @@ import net.pterodactylus.sone.web.ajax.DeleteProfileFieldAjaxPage;
 import net.pterodactylus.sone.web.ajax.DeleteReplyAjaxPage;
 import net.pterodactylus.sone.web.ajax.DismissNotificationAjaxPage;
 import net.pterodactylus.sone.web.ajax.DistrustAjaxPage;
+import net.pterodactylus.sone.web.ajax.EditAlbumAjaxPage;
 import net.pterodactylus.sone.web.ajax.EditImageAjaxPage;
 import net.pterodactylus.sone.web.ajax.EditProfileFieldAjaxPage;
 import net.pterodactylus.sone.web.ajax.FollowSoneAjaxPage;
@@ -665,6 +666,7 @@ public class WebInterface implements CoreListener {
                pageToadlets.add(pageToadletFactory.createPageToadlet(new UnlockSoneAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new FollowSoneAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new UnfollowSoneAjaxPage(this)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java
new file mode 100644 (file)
index 0000000..b530526
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Sone - EditAlbumAjaxPage.java - Copyright © 2011 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 <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.web.ajax;
+
+import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.web.WebInterface;
+import net.pterodactylus.sone.web.page.FreenetRequest;
+import net.pterodactylus.util.json.JsonObject;
+
+/**
+ * Page that stores a user’s album modifications.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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 JsonObject createJsonObject(FreenetRequest request) {
+               String albumId = request.getHttpRequest().getParam("album");
+               Album album = webInterface.getCore().getAlbum(albumId, false);
+               if (album == null) {
+                       return createErrorJsonObject("invalid-album-id");
+               }
+               if (!webInterface.getCore().isLocalSone(album.getSone())) {
+                       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();
+               album.setTitle(title).setDescription(description);
+               webInterface.getCore().touchConfiguration();
+               return createSuccessJsonObject().put("albumId", album.getId()).put("title", album.getTitle()).put("description", album.getDescription());
+       }
+
+}