Parse album:// links
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 24 Jul 2015 21:07:21 +0000 (23:07 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 25 Jul 2015 08:56:41 +0000 (10:56 +0200)
src/main/java/net/pterodactylus/sone/template/ParserFilter.java
src/main/java/net/pterodactylus/sone/text/AlbumPart.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/text/SoneTextParser.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java

index 81af149..2aafd1f 100644 (file)
@@ -31,7 +31,9 @@ import java.util.List;
 import java.util.Map;
 
 import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.text.AlbumPart;
 import net.pterodactylus.sone.text.FreenetLinkPart;
 import net.pterodactylus.sone.text.LinkPart;
 import net.pterodactylus.sone.text.Part;
@@ -177,6 +179,8 @@ public class ParserFilter implements Filter {
                        render(writer, (SonePart) part);
                } else if (part instanceof PostPart) {
                        render(writer, (PostPart) part);
+               } else if (part instanceof AlbumPart) {
+                       render(writer, (AlbumPart) part);
                } else if (part instanceof Iterable<?>) {
                        render(writer, (Iterable<Part>) part);
                }
@@ -250,7 +254,7 @@ public class ParserFilter implements Filter {
         *            The part to render
         */
        private void render(Writer writer, PostPart postPart) {
-               SoneTextParser parser = new SoneTextParser(core, core);
+               SoneTextParser parser = new SoneTextParser(core, core, core);
                SoneTextParserContext parserContext = new SoneTextParserContext(null, postPart.getPost().getSone());
                try {
                        Iterable<Part> parts = parser.parse(parserContext, new StringReader(postPart.getPost().getText()));
@@ -275,6 +279,11 @@ public class ParserFilter implements Filter {
                }
        }
 
+       private void render(Writer writer, AlbumPart albumPart) {
+               Album album = albumPart.getAlbum();
+               renderLink(writer, String.format("imageBrowser.html?album=%s", album.getId()), album.getTitle(), album.getDescription(), "in-sone");
+       }
+
        /**
         * Renders the given link.
         *
diff --git a/src/main/java/net/pterodactylus/sone/text/AlbumPart.java b/src/main/java/net/pterodactylus/sone/text/AlbumPart.java
new file mode 100644 (file)
index 0000000..3f38443
--- /dev/null
@@ -0,0 +1,27 @@
+package net.pterodactylus.sone.text;
+
+import net.pterodactylus.sone.data.Album;
+
+/**
+ * {@link Part} implementation that contains information about the linked {@link Album}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class AlbumPart implements Part {
+
+       private final Album album;
+
+       public AlbumPart(Album album) {
+               this.album = album;
+       }
+
+       public Album getAlbum() {
+               return album;
+       }
+
+       @Override
+       public String getText() {
+               return album.getTitle();
+       }
+
+}
index 1573d1c..9fcce7a 100644 (file)
@@ -28,9 +28,11 @@ import java.util.logging.Logger;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.impl.IdOnlySone;
+import net.pterodactylus.sone.database.AlbumProvider;
 import net.pterodactylus.sone.database.PostProvider;
 import net.pterodactylus.sone.database.SoneProvider;
 import net.pterodactylus.util.io.Closer;
@@ -66,7 +68,8 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                HTTP("http://", false),
                HTTPS("https://", false),
                SONE("sone://", false),
-               POST("post://", false);
+               POST("post://", false),
+               ALBUM("album://", false);
 
                private final String scheme;
                private final boolean freenetLink;
@@ -91,23 +94,14 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
 
        }
 
-       /** The Sone provider. */
        private final SoneProvider soneProvider;
-
-       /** The post provider. */
        private final PostProvider postProvider;
+       private final AlbumProvider albumProvider;
 
-       /**
-        * Creates a new freenet link parser.
-        *
-        * @param soneProvider
-        *            The Sone provider
-        * @param postProvider
-        *            The post provider
-        */
-       public SoneTextParser(SoneProvider soneProvider, PostProvider postProvider) {
+       public SoneTextParser(SoneProvider soneProvider, PostProvider postProvider, AlbumProvider albumProvider) {
                this.soneProvider = soneProvider;
                this.postProvider = postProvider;
+               this.albumProvider = albumProvider;
        }
 
        //
@@ -213,6 +207,16 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                                line = line.substring(link.length());
                                                continue;
                                        }
+                                       if (linkType == LinkType.ALBUM) {
+                                               Optional<Album> album = albumProvider.getAlbum(link.substring(linkType.getScheme().length()));
+                                               if (album.isPresent()) {
+                                                       parts.add(new AlbumPart(album.get()));
+                                               } else {
+                                                       parts.add(new PlainTextPart(link));
+                                               }
+                                               line = line.substring(link.length());
+                                               continue;
+                                       }
 
                                        if (linkType.isFreenetLink()) {
                                                FreenetURI uri;
index 95c8a3f..0b7ec52 100644 (file)
@@ -250,7 +250,7 @@ public class WebInterface {
        public WebInterface(SonePlugin sonePlugin) {
                this.sonePlugin = sonePlugin;
                formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
-               soneTextParser = new SoneTextParser(getCore(), getCore());
+               soneTextParser = new SoneTextParser(getCore(), getCore(), getCore());
 
                templateContextFactory = new TemplateContextFactory();
                templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
index 8dc1d4a..ef98e36 100644 (file)
@@ -31,9 +31,11 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
+import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.impl.IdOnlySone;
+import net.pterodactylus.sone.database.AlbumProvider;
 import net.pterodactylus.sone.database.PostProvider;
 import net.pterodactylus.sone.database.SoneProvider;
 
@@ -50,7 +52,8 @@ public class SoneTextParserTest {
 
        private final SoneProvider soneProvider = new TestSoneProvider();
        private final TestPostProvider postProvider = new TestPostProvider();
-       private final SoneTextParser soneTextParser = new SoneTextParser(soneProvider, postProvider);
+       private final TestAlbumProvider albumProvider = new TestAlbumProvider();
+       private final SoneTextParser soneTextParser = new SoneTextParser(soneProvider, postProvider, albumProvider);
 
        /**
         * Tests basic plain-text operation of the parser.
@@ -153,6 +156,13 @@ public class SoneTextParserTest {
                assertThat(convertText(parts, PlainTextPart.class, PostPart.class), is("This [post|old|foo|Post about foo...] is awesome."));
        }
 
+       @Test
+       public void linksToAlbumIsParsedCorrectly() throws IOException {
+               albumProvider.addAlbumTitle("album-id", "Super Album");
+               Iterable<Part> parts = soneTextParser.parse(null, new StringReader("This album://album-id rocks!"));
+               assertThat(convertText(parts, PlainTextPart.class, AlbumPart.class), is("This [album|album-id|Super Album] rocks!"));
+       }
+
        //
        // PRIVATE METHODS
        //
@@ -213,6 +223,9 @@ public class SoneTextParserTest {
                                        .append('|')
                                        .append(postPart.getPost().getText())
                                        .append(']');
+                       } else if (part instanceof AlbumPart) {
+                               Album album = ((AlbumPart) part).getAlbum();
+                               text.append(String.format("[album|%s|%s]", album.getId(), album.getTitle()));
                        }
                }
                return text.toString();
@@ -311,4 +324,25 @@ public class SoneTextParserTest {
 
        }
 
+       private static class TestAlbumProvider implements AlbumProvider {
+
+               private final Map<String, String> albumTitles = new HashMap<String, String>();
+
+               public void addAlbumTitle(String albumId, String albumTitle) {
+                       albumTitles.put(albumId, albumTitle);
+               }
+
+               @Override
+               public Optional<Album> getAlbum(String albumId) {
+                       if (albumTitles.containsKey(albumId)) {
+                               Album album = mock(Album.class);
+                               when(album.getId()).thenReturn(albumId);
+                               when(album.getTitle()).thenReturn(albumTitles.get(albumId));
+                               return Optional.of(album);
+                       }
+                       return Optional.absent();
+               }
+
+       }
+
 }