From 616d826774d0ac43e1152ec6a4f5c3c198b95483 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 24 Jul 2015 23:07:21 +0200 Subject: [PATCH] Parse album:// links --- .../pterodactylus/sone/template/ParserFilter.java | 11 ++++++- .../net/pterodactylus/sone/text/AlbumPart.java | 27 ++++++++++++++++ .../pterodactylus/sone/text/SoneTextParser.java | 30 ++++++++++-------- .../net/pterodactylus/sone/web/WebInterface.java | 2 +- .../sone/text/SoneTextParserTest.java | 36 +++++++++++++++++++++- 5 files changed, 90 insertions(+), 16 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/text/AlbumPart.java diff --git a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java index 81af149..2aafd1f 100644 --- a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java @@ -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); } @@ -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 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 index 0000000..3f38443 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/text/AlbumPart.java @@ -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 David ‘Bombe’ Roden + */ +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(); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java index 1573d1c..9fcce7a 100644 --- a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java +++ b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java @@ -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 { 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 { } - /** 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 { line = line.substring(link.length()); continue; } + if (linkType == LinkType.ALBUM) { + Optional 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; diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 95c8a3f..0b7ec52 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -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()); diff --git a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java index 8dc1d4a..ef98e36 100644 --- a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java +++ b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java @@ -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 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 albumTitles = new HashMap(); + + public void addAlbumTitle(String albumId, String albumTitle) { + albumTitles.put(albumId, albumTitle); + } + + @Override + public Optional 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(); + } + + } + } -- 2.7.4