From 00a434a23c9ea1e57c63d8a3c0fc4b09277af431 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 24 Jul 2015 22:45:36 +0200 Subject: [PATCH] Let core be an album provider --- .../java/net/pterodactylus/sone/core/Core.java | 7 ++++--- .../net/pterodactylus/sone/core/SoneParser.java | 9 +++++---- .../pterodactylus/sone/web/CreateAlbumPage.java | 13 ++++++++++--- .../pterodactylus/sone/web/DeleteAlbumPage.java | 22 ++++++++++++---------- .../net/pterodactylus/sone/web/EditAlbumPage.java | 22 ++++++++++++---------- .../pterodactylus/sone/web/ImageBrowserPage.java | 4 ++-- .../net/pterodactylus/sone/web/SearchPage.java | 3 ++- .../pterodactylus/sone/web/UploadImagePage.java | 11 ++++++----- .../sone/web/ajax/EditAlbumAjaxPage.java | 20 +++++++++++--------- .../pterodactylus/sone/core/SoneParserTest.java | 6 +++--- 10 files changed, 67 insertions(+), 50 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index b4a94e4..140d80c 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -73,6 +73,7 @@ import net.pterodactylus.sone.data.Sone.ShowCustomAvatars; import net.pterodactylus.sone.data.Sone.SoneStatus; import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.sone.database.AlbumBuilder; +import net.pterodactylus.sone.database.AlbumProvider; import net.pterodactylus.sone.database.Database; import net.pterodactylus.sone.database.DatabaseException; import net.pterodactylus.sone.database.ImageBuilder; @@ -115,7 +116,7 @@ import com.google.inject.Singleton; * @author David ‘Bombe’ Roden */ @Singleton -public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider { +public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider, AlbumProvider { /** The logger. */ private static final Logger logger = getLogger(Core.class.getName()); @@ -559,8 +560,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * @return The album with the given ID, or {@code null} if no album with the * given ID exists */ - public Album getAlbum(String albumId) { - return database.getAlbum(albumId).orNull(); + public Optional getAlbum(String albumId) { + return database.getAlbum(albumId); } public ImageBuilder imageBuilder() { diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index 2712bd3..24b856f 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -29,6 +29,7 @@ import net.pterodactylus.sone.database.SoneBuilder; import net.pterodactylus.util.xml.SimpleXML; import net.pterodactylus.util.xml.XML; +import com.google.common.base.Optional; import org.w3c.dom.Document; /** @@ -266,10 +267,10 @@ public class SoneParser { logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid album!", sone)); return null; } - Album parent = null; + Optional parent = Optional.absent(); if (parentId != null) { parent = core.getAlbum(parentId); - if (parent == null) { + if (!parent.isPresent()) { logger.log(Level.WARNING, String.format("Downloaded Sone %s has album with invalid parent!", sone)); return null; } @@ -282,8 +283,8 @@ public class SoneParser { .setTitle(title) .setDescription(description) .update(); - if (parent != null) { - parent.addAlbum(album); + if (parent.isPresent()) { + parent.get().addAlbum(album); } else { topLevelAlbums.add(album); } diff --git a/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java b/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java index e0f0725..338056a 100644 --- a/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java +++ b/src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java @@ -27,6 +27,8 @@ import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.web.Method; +import com.google.common.base.Optional; + /** * Page that lets the user create a new album. * @@ -65,11 +67,16 @@ public class CreateAlbumPage extends SoneTemplatePage { String description = request.getHttpRequest().getPartAsStringFailsafe("description", 256).trim(); Sone currentSone = getCurrentSone(request.getToadletContext()); String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", IdBuilder.ID_STRING_LENGTH); - Album parent = webInterface.getCore().getAlbum(parentId); + Optional parent; if (parentId.equals("")) { - parent = currentSone.getRootAlbum(); + parent = Optional.of(currentSone.getRootAlbum()); + } else { + parent = webInterface.getCore().getAlbum(parentId); + if (!parent.isPresent()) { + throw new RedirectException("noPermission.html"); + } } - Album album = webInterface.getCore().createAlbum(currentSone, parent); + Album album = webInterface.getCore().createAlbum(currentSone, parent.get()); try { album.modify().setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update(); } catch (AlbumTitleMustNotBeEmpty atmnbe) { diff --git a/src/main/java/net/pterodactylus/sone/web/DeleteAlbumPage.java b/src/main/java/net/pterodactylus/sone/web/DeleteAlbumPage.java index e588976..94fbaf1 100644 --- a/src/main/java/net/pterodactylus/sone/web/DeleteAlbumPage.java +++ b/src/main/java/net/pterodactylus/sone/web/DeleteAlbumPage.java @@ -24,6 +24,8 @@ import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.web.Method; +import com.google.common.base.Optional; + /** * Page that lets the user delete an {@link Album}. * @@ -51,26 +53,26 @@ public class DeleteAlbumPage extends SoneTemplatePage { super.processTemplate(request, templateContext); if (request.getMethod() == Method.POST) { String albumId = request.getHttpRequest().getPartAsStringFailsafe("album", IdBuilder.ID_STRING_LENGTH); - Album album = webInterface.getCore().getAlbum(albumId); - if (album == null) { + Optional album = webInterface.getCore().getAlbum(albumId); + if (!album.isPresent()) { throw new RedirectException("invalid.html"); } - if (!album.getSone().isLocal()) { + if (!album.get().getSone().isLocal()) { throw new RedirectException("noPermission.html"); } if (request.getHttpRequest().isPartSet("abortDelete")) { - throw new RedirectException("imageBrowser.html?album=" + album.getId()); + throw new RedirectException("imageBrowser.html?album=" + album.get().getId()); } - Album parentAlbum = album.getParent(); - webInterface.getCore().deleteAlbum(album); - if (parentAlbum.equals(album.getSone().getRootAlbum())) { - throw new RedirectException("imageBrowser.html?sone=" + album.getSone().getId()); + Album parentAlbum = album.get().getParent(); + webInterface.getCore().deleteAlbum(album.get()); + if (parentAlbum.equals(album.get().getSone().getRootAlbum())) { + throw new RedirectException("imageBrowser.html?sone=" + album.get().getSone().getId()); } throw new RedirectException("imageBrowser.html?album=" + parentAlbum.getId()); } String albumId = request.getHttpRequest().getParam("album"); - Album album = webInterface.getCore().getAlbum(albumId); - if (album == null) { + Optional album = webInterface.getCore().getAlbum(albumId); + if (!album.isPresent()) { throw new RedirectException("invalid.html"); } templateContext.set("album", album); diff --git a/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java b/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java index aab4614..f54f887 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java @@ -26,6 +26,8 @@ import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.web.Method; +import com.google.common.base.Optional; + /** * Page that lets the user edit the name and description of an album. * @@ -53,36 +55,36 @@ public class EditAlbumPage extends SoneTemplatePage { super.processTemplate(request, templateContext); if (request.getMethod() == Method.POST) { String albumId = request.getHttpRequest().getPartAsStringFailsafe("album", IdBuilder.ID_STRING_LENGTH); - Album album = webInterface.getCore().getAlbum(albumId); - if (album == null) { + Optional album = webInterface.getCore().getAlbum(albumId); + if (!album.isPresent()) { throw new RedirectException("invalid.html"); } - if (!album.getSone().isLocal()) { + if (!album.get().getSone().isLocal()) { throw new RedirectException("noPermission.html"); } if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("moveLeft", 4))) { - album.getParent().moveAlbumUp(album); + album.get().getParent().moveAlbumUp(album.get()); webInterface.getCore().touchConfiguration(); - throw new RedirectException("imageBrowser.html?album=" + album.getParent().getId()); + throw new RedirectException("imageBrowser.html?album=" + album.get().getParent().getId()); } else if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("moveRight", 4))) { - album.getParent().moveAlbumDown(album); + album.get().getParent().moveAlbumDown(album.get()); webInterface.getCore().touchConfiguration(); - throw new RedirectException("imageBrowser.html?album=" + album.getParent().getId()); + throw new RedirectException("imageBrowser.html?album=" + album.get().getParent().getId()); } String albumImageId = request.getHttpRequest().getPartAsStringFailsafe("album-image", IdBuilder.ID_STRING_LENGTH); if (webInterface.getCore().getImage(albumImageId, false) == null) { albumImageId = null; } - album.modify().setAlbumImage(albumImageId).update(); + album.get().modify().setAlbumImage(albumImageId).update(); String title = request.getHttpRequest().getPartAsStringFailsafe("title", 100).trim(); String description = request.getHttpRequest().getPartAsStringFailsafe("description", 1000).trim(); try { - album.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update(); + album.get().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()); + throw new RedirectException("imageBrowser.html?album=" + album.get().getId()); } } diff --git a/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java b/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java index 60b22d5..36805b3 100644 --- a/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java @@ -69,9 +69,9 @@ public class ImageBrowserPage extends SoneTemplatePage { super.processTemplate(request, templateContext); String albumId = request.getHttpRequest().getParam("album", null); if (albumId != null) { - Album album = webInterface.getCore().getAlbum(albumId); + Optional album = webInterface.getCore().getAlbum(albumId); templateContext.set("albumRequested", true); - templateContext.set("album", album); + templateContext.set("album", album.orNull()); templateContext.set("page", request.getHttpRequest().getParam("page")); return; } diff --git a/src/main/java/net/pterodactylus/sone/web/SearchPage.java b/src/main/java/net/pterodactylus/sone/web/SearchPage.java index 95b0bba..fd26f44 100644 --- a/src/main/java/net/pterodactylus/sone/web/SearchPage.java +++ b/src/main/java/net/pterodactylus/sone/web/SearchPage.java @@ -32,6 +32,7 @@ import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; +import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Profile; @@ -356,7 +357,7 @@ public class SearchPage extends SoneTemplatePage { */ private String getAlbumId(String phrase) { String albumId = phrase.startsWith("album://") ? phrase.substring(8) : phrase; - return (webInterface.getCore().getAlbum(albumId) != null) ? albumId : null; + return webInterface.getCore().getAlbum(albumId).isPresent() ? albumId : null; } /** diff --git a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java index 2c7b0a9..673e439 100644 --- a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java +++ b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java @@ -45,6 +45,7 @@ import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.web.Method; +import com.google.common.base.Optional; import com.google.common.io.ByteStreams; import freenet.support.api.Bucket; @@ -85,11 +86,11 @@ public class UploadImagePage extends SoneTemplatePage { if (request.getMethod() == Method.POST) { Sone currentSone = getCurrentSone(request.getToadletContext()); String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", IdBuilder.ID_STRING_LENGTH); - Album parent = webInterface.getCore().getAlbum(parentId); - if (parent == null) { + Optional parent = webInterface.getCore().getAlbum(parentId); + if (!parent.isPresent()) { throw new RedirectException("noPermission.html"); } - if (!currentSone.equals(parent.getSone())) { + if (!currentSone.equals(parent.get().getSone())) { throw new RedirectException("noPermission.html"); } String name = request.getHttpRequest().getPartAsStringFailsafe("title", 200); @@ -123,7 +124,7 @@ public class UploadImagePage extends SoneTemplatePage { } String mimeType = getMimeType(imageData); TemporaryImage temporaryImage = webInterface.getCore().createTemporaryImage(mimeType, imageData); - net.pterodactylus.sone.data.Image image = webInterface.getCore().createImage(currentSone, parent, temporaryImage); + net.pterodactylus.sone.data.Image image = webInterface.getCore().createImage(currentSone, parent.get(), temporaryImage); image.modify().setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).setWidth(uploadedImage.getWidth(null)).setHeight(uploadedImage.getHeight(null)).update(); } catch (IOException ioe1) { logger.log(Level.WARNING, "Could not read uploaded image!", ioe1); @@ -134,7 +135,7 @@ public class UploadImagePage extends SoneTemplatePage { Closer.close(imageDataInputStream); Closer.flush(uploadedImage); } - throw new RedirectException("imageBrowser.html?album=" + parent.getId()); + throw new RedirectException("imageBrowser.html?album=" + parent.get().getId()); } } 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 238206a..570d027 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java @@ -22,6 +22,8 @@ import net.pterodactylus.sone.text.TextFilter; import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.sone.web.page.FreenetRequest; +import com.google.common.base.Optional; + /** * Page that stores a user’s album modifications. * @@ -49,29 +51,29 @@ public class EditAlbumAjaxPage extends JsonPage { @Override protected JsonReturnObject createJsonObject(FreenetRequest request) { String albumId = request.getHttpRequest().getParam("album"); - Album album = webInterface.getCore().getAlbum(albumId); - if (album == null) { + Optional album = webInterface.getCore().getAlbum(albumId); + if (!album.isPresent()) { return createErrorJsonObject("invalid-album-id"); } - if (!album.getSone().isLocal()) { + if (!album.get().getSone().isLocal()) { return createErrorJsonObject("not-authorized"); } if ("true".equals(request.getHttpRequest().getParam("moveLeft"))) { - Album swappedAlbum = album.getParent().moveAlbumUp(album); + Album swappedAlbum = album.get().getParent().moveAlbumUp(album.get()); webInterface.getCore().touchConfiguration(); - return createSuccessJsonObject().put("sourceAlbumId", album.getId()).put("destinationAlbumId", swappedAlbum.getId()); + return createSuccessJsonObject().put("sourceAlbumId", album.get().getId()).put("destinationAlbumId", swappedAlbum.getId()); } if ("true".equals(request.getHttpRequest().getParam("moveRight"))) { - Album swappedAlbum = album.getParent().moveAlbumDown(album); + Album swappedAlbum = album.get().getParent().moveAlbumDown(album.get()); webInterface.getCore().touchConfiguration(); - return createSuccessJsonObject().put("sourceAlbumId", album.getId()).put("destinationAlbumId", swappedAlbum.getId()); + return createSuccessJsonObject().put("sourceAlbumId", album.get().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(); + album.get().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()); + return createSuccessJsonObject().put("albumId", album.get().getId()).put("title", album.get().getTitle()).put("description", album.get().getDescription()); } catch (IllegalStateException e) { return createErrorJsonObject("invalid-album-title"); } diff --git a/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java b/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java index 6986e36..3fb2a34 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneParserTest.java @@ -345,11 +345,11 @@ public class SoneParserTest { @Before public void setupAlbums() { - when(core.getAlbum(anyString())).thenAnswer(new Answer() { + when(core.getAlbum(anyString())).thenAnswer(new Answer>() { @Override - public Album answer(InvocationOnMock invocation) + public Optional answer(InvocationOnMock invocation) throws Throwable { - return albums.get(invocation.getArguments()[0]); + return Optional.fromNullable(albums.get(invocation.getArguments()[0])); } }); } -- 2.7.4