Merge branch 'next' into feature/album-and-image-links
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 27 Jul 2015 18:30:21 +0000 (20:30 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 27 Jul 2015 18:30:21 +0000 (20:30 +0200)
70 files changed:
src/main/java/net/pterodactylus/sone/core/CompatibilityMode.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/core/ConfigurationSoneParser.java
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/SoneParser.java
src/main/java/net/pterodactylus/sone/data/Album.java
src/main/java/net/pterodactylus/sone/data/IdBuilder.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/data/Image.java
src/main/java/net/pterodactylus/sone/data/Post.java
src/main/java/net/pterodactylus/sone/data/Profile.java
src/main/java/net/pterodactylus/sone/data/Reply.java
src/main/java/net/pterodactylus/sone/data/Sone.java
src/main/java/net/pterodactylus/sone/data/impl/AlbumImpl.java
src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java
src/main/java/net/pterodactylus/sone/data/impl/ImageImpl.java
src/main/java/net/pterodactylus/sone/data/impl/PostImpl.java
src/main/java/net/pterodactylus/sone/data/impl/ReplyImpl.java
src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryPost.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryPostReply.java
src/main/java/net/pterodactylus/sone/template/BuildIdFilter.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/template/ParserFilter.java
src/main/java/net/pterodactylus/sone/template/ProfileAccessor.java
src/main/java/net/pterodactylus/sone/template/RenderFilter.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/text/AlbumPart.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/text/PostPart.java
src/main/java/net/pterodactylus/sone/text/SoneTextParser.java
src/main/java/net/pterodactylus/sone/web/BookmarkPage.java
src/main/java/net/pterodactylus/sone/web/CreateAlbumPage.java
src/main/java/net/pterodactylus/sone/web/CreateReplyPage.java
src/main/java/net/pterodactylus/sone/web/DeleteAlbumPage.java
src/main/java/net/pterodactylus/sone/web/DeleteImagePage.java
src/main/java/net/pterodactylus/sone/web/DeletePostPage.java
src/main/java/net/pterodactylus/sone/web/DeleteReplyPage.java
src/main/java/net/pterodactylus/sone/web/EditAlbumPage.java
src/main/java/net/pterodactylus/sone/web/EditImagePage.java
src/main/java/net/pterodactylus/sone/web/EditProfilePage.java
src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java
src/main/java/net/pterodactylus/sone/web/LikePage.java
src/main/java/net/pterodactylus/sone/web/OptionsPage.java
src/main/java/net/pterodactylus/sone/web/SearchPage.java
src/main/java/net/pterodactylus/sone/web/UnbookmarkPage.java
src/main/java/net/pterodactylus/sone/web/UnlikePage.java
src/main/java/net/pterodactylus/sone/web/UploadImagePage.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/java/net/pterodactylus/sone/web/ajax/EditAlbumAjaxPage.java
src/main/java/net/pterodactylus/sone/web/ajax/EditImageAjaxPage.java
src/main/resources/i18n/sone.en.properties
src/main/resources/static/css/sone.css
src/main/resources/templates/editProfile.html
src/main/resources/templates/imageBrowser.html
src/main/resources/templates/include/browseAlbums.html
src/main/resources/templates/include/head.html
src/main/resources/templates/include/soneMenu.html
src/main/resources/templates/include/viewAlbum.html [new file with mode: 0644]
src/main/resources/templates/include/viewPost.html
src/main/resources/templates/include/viewReply.html
src/main/resources/templates/insert/sone.xml
src/main/resources/templates/invalid.html
src/main/resources/templates/notify/newVersionNotification.html
src/main/resources/templates/notify/soneInsertNotification.html
src/main/resources/templates/options.html
src/main/resources/templates/viewSone.html
src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java
src/test/java/net/pterodactylus/sone/core/SoneParserTest.java
src/test/java/net/pterodactylus/sone/data/IdBuilderTest.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java
src/test/java/net/pterodactylus/sone/template/BuildIdFilterTest.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/template/ParserFilterTest.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/template/RenderFilterTest.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java

diff --git a/src/main/java/net/pterodactylus/sone/core/CompatibilityMode.java b/src/main/java/net/pterodactylus/sone/core/CompatibilityMode.java
new file mode 100644 (file)
index 0000000..6983632
--- /dev/null
@@ -0,0 +1,17 @@
+package net.pterodactylus.sone.core;
+
+import net.pterodactylus.sone.data.Post;
+
+/**
+ * Sone compatibility modes.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public enum CompatibilityMode {
+
+       /**
+        * This mode causes Sone to use a post’s {@link Post#getInternalId() internal ID} to locate posts when parsing links.
+        */
+       oldElementIds,
+
+}
index a29856b..02c10db 100644 (file)
@@ -271,7 +271,7 @@ public class ConfigurationSoneParser {
                                        .setHeight(height)
                                        .update();
                        album.addImage(image);
-                       images.put(image.getId(), image);
+                       images.put(imageId, image);
                }
        }
 
index da209d8..140d80c 100644 (file)
@@ -27,6 +27,7 @@ import static java.util.logging.Logger.getLogger;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -72,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;
@@ -98,6 +100,7 @@ import net.pterodactylus.util.thread.NamedThreadFactory;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.Multimap;
@@ -113,7 +116,7 @@ import com.google.inject.Singleton;
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 @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());
@@ -154,6 +157,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        /** The trust updater. */
        private final WebOfTrustUpdater webOfTrustUpdater;
 
+       private final Set<CompatibilityMode> compatibilityModes = EnumSet.noneOf(CompatibilityMode.class);
+
        /** The times Sones were followed. */
        private final Map<String, Long> soneFollowingTimes = new HashMap<String, Long>();
 
@@ -273,6 +278,18 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                return updateChecker;
        }
 
+       public boolean isCompatibilityMode(CompatibilityMode compatibilityMode) {
+               return compatibilityModes.contains(compatibilityMode);
+       }
+
+       public void setCompatibilityMode(CompatibilityMode compatibilityMode) {
+               compatibilityModes.add(compatibilityMode);
+       }
+
+       public void clearCompatibilityMod(CompatibilityMode compatibilityMode) {
+               compatibilityModes.remove(compatibilityMode);
+       }
+
        /**
         * Returns the Sone rescuer for the given local Sone.
         *
@@ -420,8 +437,17 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * {@inheritDoc}
         */
        @Override
-       public Optional<Post> getPost(String postId) {
-               return database.getPost(postId);
+       public Optional<Post> getPost(final String postId) {
+               Optional<Post> post = database.getPost(postId);
+               if (post.isPresent() || !isCompatibilityMode(CompatibilityMode.oldElementIds)) {
+                       return post;
+               }
+               return FluentIterable.from(getSones()).transformAndConcat(Sone.toAllPosts).filter(new Predicate<Post>() {
+                       @Override
+                       public boolean apply(Post input) {
+                               return (input != null) && input.getInternalId().equals(postId);
+                       }
+               }).first();
        }
 
        /**
@@ -534,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<Album> getAlbum(String albumId) {
+               return database.getAlbum(albumId);
        }
 
        public ImageBuilder imageBuilder() {
@@ -1476,7 +1502,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        int postCounter = 0;
                        for (Post post : sone.getPosts()) {
                                String postPrefix = sonePrefix + "/Posts/" + postCounter++;
-                               configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
+                               configuration.getStringValue(postPrefix + "/ID").setValue(post.getInternalId());
                                configuration.getStringValue(postPrefix + "/Recipient").setValue(post.getRecipientId().orNull());
                                configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
                                configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
@@ -1487,7 +1513,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        int replyCounter = 0;
                        for (PostReply reply : sone.getReplies()) {
                                String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
-                               configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
+                               configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getInternalId());
                                configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPostId());
                                configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
                                configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
@@ -1514,7 +1540,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        int albumCounter = 0;
                        for (Album album : albums) {
                                String albumPrefix = sonePrefix + "/Albums/" + albumCounter++;
-                               configuration.getStringValue(albumPrefix + "/ID").setValue(album.getId());
+                               configuration.getStringValue(albumPrefix + "/ID").setValue(album.getInternalId());
                                configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
                                configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
                                configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId());
@@ -1530,8 +1556,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                                continue;
                                        }
                                        String imagePrefix = sonePrefix + "/Images/" + imageCounter++;
-                                       configuration.getStringValue(imagePrefix + "/ID").setValue(image.getId());
-                                       configuration.getStringValue(imagePrefix + "/Album").setValue(album.getId());
+                                       configuration.getStringValue(imagePrefix + "/ID").setValue(image.getInternalId());
+                                       configuration.getStringValue(imagePrefix + "/Album").setValue(album.getInternalId());
                                        configuration.getStringValue(imagePrefix + "/Key").setValue(image.getKey());
                                        configuration.getStringValue(imagePrefix + "/Title").setValue(image.getTitle());
                                        configuration.getStringValue(imagePrefix + "/Description").setValue(image.getDescription());
@@ -1596,6 +1622,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null);
                        }
 
+                       /* save compatibility modes. */
+                       configuration.getBooleanValue("CompatibilityModes/OldElementIds").setValue(compatibilityModes.contains(CompatibilityMode.oldElementIds));
+
                        /* save known posts. */
                        database.save();
 
@@ -1644,6 +1673,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        }
                        ++soneCounter;
                }
+
+               /* load compatibility modes. */
+               if (configuration.getBooleanValue("CompatibilityModes/OldElementIds").getValue(false)) {
+                       setCompatibilityMode(CompatibilityMode.oldElementIds);
+               }
        }
 
        /**
index ae1e2a5..9122852 100644 (file)
@@ -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<Album> 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);
                                }
index c75088f..b4c6b0f 100644 (file)
@@ -107,6 +107,7 @@ public interface Album extends Identified, Fingerprintable {
         * @return The ID of this album
         */
        String getId();
+       String getInternalId();
 
        /**
         * Returns the Sone this album belongs to.
diff --git a/src/main/java/net/pterodactylus/sone/data/IdBuilder.java b/src/main/java/net/pterodactylus/sone/data/IdBuilder.java
new file mode 100644 (file)
index 0000000..f9accc1
--- /dev/null
@@ -0,0 +1,31 @@
+package net.pterodactylus.sone.data;
+
+import javax.annotation.Nonnull;
+import javax.annotation.concurrent.ThreadSafe;
+
+import com.google.common.base.Charsets;
+import com.google.common.hash.HashFunction;
+import com.google.common.hash.Hashing;
+
+/**
+ * Builds (practically) unique IDs by combining Sone and element IDs.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+@ThreadSafe
+public class IdBuilder {
+
+       private static final HashFunction HASH_FUNCTION = Hashing.sha256();
+       public static final int ID_STRING_LENGTH = HASH_FUNCTION.bits() / 4;
+
+       private final HashFunction sha256 = HASH_FUNCTION;
+
+       @Nonnull
+       public String buildId(@Nonnull String soneId, @Nonnull String id) {
+               return sha256.newHasher()
+                               .putBytes(soneId.getBytes(Charsets.UTF_8))
+                               .putBytes(id.getBytes(Charsets.UTF_8))
+                               .hash().toString();
+       }
+
+}
index 22ddc29..f2ef88d 100644 (file)
@@ -30,6 +30,7 @@ public interface Image extends Identified, Fingerprintable {
         * @return The ID of this image
         */
        String getId();
+       String getInternalId();
 
        /**
         * Returns the Sone this image belongs to.
index 95abae6..d27d243 100644 (file)
@@ -62,6 +62,7 @@ public interface Post extends Identified {
         * @return The ID of the post
         */
        public String getId();
+       String getInternalId();
 
        /**
         * Returns whether this post has already been loaded.
@@ -144,6 +145,11 @@ public interface Post extends Identified {
                }
 
                @Override
+               public String getInternalId() {
+                       return id;
+               }
+
+               @Override
                public boolean isLoaded() {
                        return false;
                }
index 4970cf9..679196b 100644 (file)
@@ -243,7 +243,7 @@ public class Profile implements Fingerprintable {
                        return this;
                }
                checkArgument(avatar.getSone().equals(sone), "avatar must belong to Sone");
-               this.avatar = avatar.getId();
+               this.avatar = avatar.getInternalId();
                return this;
        }
 
index e9b7a1d..69769eb 100644 (file)
@@ -62,6 +62,7 @@ public interface Reply<T extends Reply<T>> extends Identified {
         * @return The ID of the reply
         */
        public String getId();
+       String getInternalId();
 
        /**
         * Returns the Sone that posted this reply.
index e64a388..7acdb7f 100644 (file)
@@ -35,6 +35,7 @@ import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 import net.pterodactylus.sone.template.SoneAccessor;
 
+import com.google.common.base.Optional;
 import freenet.keys.FreenetURI;
 
 import com.google.common.base.Function;
@@ -188,6 +189,13 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
                }
        };
 
+       Function<Sone, Collection<Post>> toAllPosts = new Function<Sone, Collection<Post>>() {
+               @Override
+               public Collection<Post> apply(@Nullable Sone sone) {
+                       return (sone != null) ? sone.getPosts() : Collections.<Post>emptyList();
+               }
+       };
+
        /**
         * Returns the identity of this Sone.
         *
@@ -502,6 +510,7 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
         * @return The root album of this Sone
         */
        Album getRootAlbum();
+       Optional<Image> getImageByInternalId(final String internalId);
 
        /**
         * Returns Sone-specific options.
index 3488577..6a45187 100644 (file)
@@ -29,6 +29,7 @@ import java.util.Map;
 import java.util.UUID;
 
 import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Sone;
 
@@ -46,6 +47,8 @@ import com.google.common.hash.Hashing;
  */
 public class AlbumImpl implements Album {
 
+       private final IdBuilder idBuilder = new IdBuilder();
+
        /** The ID of this album. */
        private final String id;
 
@@ -95,6 +98,11 @@ public class AlbumImpl implements Album {
 
        @Override
        public String getId() {
+               return idBuilder.buildId(sone.getId(), id);
+       }
+
+       @Override
+       public String getInternalId() {
                return id;
        }
 
index e9a0c57..85402ce 100644 (file)
@@ -9,6 +9,7 @@ import java.util.Set;
 
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Client;
+import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
@@ -16,6 +17,7 @@ import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.SoneOptions;
 import net.pterodactylus.sone.freenet.wot.Identity;
 
+import com.google.common.base.Optional;
 import freenet.keys.FreenetURI;
 
 import com.google.common.base.Objects;
@@ -219,6 +221,11 @@ public class IdOnlySone implements Sone {
        }
 
        @Override
+       public Optional<Image> getImageByInternalId(String internalId) {
+               return Optional.absent();
+       }
+
+       @Override
        public SoneOptions getOptions() {
                return null;
        }
index 2df98b1..b385cfd 100644 (file)
@@ -25,6 +25,7 @@ import static com.google.common.base.Preconditions.checkState;
 import java.util.UUID;
 
 import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Sone;
 
@@ -39,6 +40,8 @@ import com.google.common.hash.Hashing;
  */
 public class ImageImpl implements Image {
 
+       private final IdBuilder idBuilder = new IdBuilder();
+
        /** The ID of the image. */
        private final String id;
 
@@ -88,6 +91,11 @@ public class ImageImpl implements Image {
 
        @Override
        public String getId() {
+               return idBuilder.buildId(sone.getId(), id);
+       }
+
+       @Override
+       public String getInternalId() {
                return id;
        }
 
index 9dcd7d0..4442b3b 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.data.impl;
 
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.SoneProvider;
@@ -31,6 +32,8 @@ import com.google.common.base.Optional;
  */
 public class PostImpl implements Post {
 
+       private final IdBuilder idBuilder = new IdBuilder();
+
        /** The Sone provider. */
        private final SoneProvider soneProvider;
 
@@ -86,6 +89,11 @@ public class PostImpl implements Post {
         */
        @Override
        public String getId() {
+               return idBuilder.buildId(soneId, id);
+       }
+
+       @Override
+       public String getInternalId() {
                return id;
        }
 
index a67081f..b3681d7 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.data.impl;
 
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.SoneProvider;
@@ -30,6 +31,8 @@ import net.pterodactylus.sone.database.SoneProvider;
  */
 public abstract class ReplyImpl<T extends Reply<T>> implements Reply<T> {
 
+       private final IdBuilder idBuilder = new IdBuilder();
+
        /** The Sone provider. */
        private final SoneProvider soneProvider;
 
@@ -75,6 +78,11 @@ public abstract class ReplyImpl<T extends Reply<T>> implements Reply<T> {
         */
        @Override
        public String getId() {
+               return idBuilder.buildId(soneId, id);
+       }
+
+       @Override
+       public String getInternalId() {
                return id;
        }
 
index f7ebfb1..97fe8eb 100644 (file)
@@ -30,9 +30,11 @@ import java.util.Set;
 import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import javax.annotation.Nullable;
 
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Client;
+import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
@@ -44,6 +46,9 @@ import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 
+import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
+import com.google.common.collect.FluentIterable;
 import freenet.keys.FreenetURI;
 
 import com.google.common.hash.Hasher;
@@ -584,6 +589,16 @@ public class SoneImpl implements Sone {
                return rootAlbum;
        }
 
+       @Override
+       public Optional<Image> getImageByInternalId(final String internalId) {
+               return FluentIterable.from(toAllImages.apply(this)).filter(new Predicate<Image>() {
+                       @Override
+                       public boolean apply(@Nullable Image input) {
+                               return (input != null) && input.getInternalId().equals(internalId);
+                       }
+               }).first();
+       }
+
        /**
         * Returns Sone-specific options.
         *
index 180cf6c..34e90f8 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.database.memory;
 
 import java.util.UUID;
 
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.database.SoneProvider;
@@ -33,6 +34,8 @@ import com.google.common.base.Optional;
  */
 class MemoryPost implements Post {
 
+       private final IdBuilder idBuilder = new IdBuilder();
+
        /** The post database. */
        private final MemoryDatabase postDatabase;
 
@@ -91,6 +94,11 @@ class MemoryPost implements Post {
         */
        @Override
        public String getId() {
+               return idBuilder.buildId(soneId, id.toString());
+       }
+
+       @Override
+       public String getInternalId() {
                return id.toString();
        }
 
index a6686ca..eb98a1d 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.database.memory;
 
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Sone;
@@ -31,6 +32,8 @@ import com.google.common.base.Optional;
  */
 class MemoryPostReply implements PostReply {
 
+       private final IdBuilder idBuilder = new IdBuilder();
+
        /** The database. */
        private final MemoryDatabase database;
 
@@ -89,6 +92,11 @@ class MemoryPostReply implements PostReply {
         */
        @Override
        public String getId() {
+               return idBuilder.buildId(soneId, id);
+       }
+
+       @Override
+       public String getInternalId() {
                return id;
        }
 
diff --git a/src/main/java/net/pterodactylus/sone/template/BuildIdFilter.java b/src/main/java/net/pterodactylus/sone/template/BuildIdFilter.java
new file mode 100644 (file)
index 0000000..321743a
--- /dev/null
@@ -0,0 +1,57 @@
+package net.pterodactylus.sone.template;
+
+import java.util.Map;
+
+import javax.annotation.Nullable;
+
+import net.pterodactylus.sone.data.IdBuilder;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.util.template.Filter;
+import net.pterodactylus.util.template.TemplateContext;
+
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+
+/**
+ * Filter that {@link IdBuilder builds IDs} from a piped-in element ID and a Sone or Sone ID given as parameter “sone.”
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class BuildIdFilter implements Filter {
+
+       private final IdBuilder idBuilder = new IdBuilder();
+
+       @Override
+       public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
+               Optional<String> soneId = getSoneId(parameters);
+               if (!soneId.isPresent()) {
+                       return null;
+               }
+               Optional<String> elementId = Optional.fromNullable(data).transform(getStringValue());
+               if (!elementId.isPresent()) {
+                       return null;
+               }
+               return idBuilder.buildId(soneId.get(), elementId.get());
+       }
+
+       private Optional<String> getSoneId(Map<String, Object> parameters) {
+               Object soneObject = parameters.get("sone");
+               if (soneObject instanceof String) {
+                       return Optional.of((String) soneObject);
+               } else if (soneObject instanceof Sone) {
+                       return Optional.of(((Sone) soneObject).getId());
+               }
+               return Optional.absent();
+       }
+
+       private Function<? super Object, String> getStringValue() {
+               return new Function<Object, String>() {
+                       @Nullable
+                       @Override
+                       public String apply(Object input) {
+                               return (input != null) ? input.toString() : null;
+                       }
+               };
+       }
+
+}
index ec35e2e..2625441 100644 (file)
 package net.pterodactylus.sone.template;
 
 import static java.lang.String.valueOf;
-import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
 
 import java.io.IOException;
 import java.io.StringReader;
-import java.io.StringWriter;
-import java.io.UnsupportedEncodingException;
-import java.io.Writer;
-import java.net.URLEncoder;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.Collections;
 import java.util.Map;
 
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.text.FreenetLinkPart;
-import net.pterodactylus.sone.text.LinkPart;
 import net.pterodactylus.sone.text.Part;
-import net.pterodactylus.sone.text.PlainTextPart;
-import net.pterodactylus.sone.text.PostPart;
-import net.pterodactylus.sone.text.SonePart;
 import net.pterodactylus.sone.text.SoneTextParser;
 import net.pterodactylus.sone.text.SoneTextParserContext;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.template.Filter;
-import net.pterodactylus.util.template.Template;
 import net.pterodactylus.util.template.TemplateContext;
-import net.pterodactylus.util.template.TemplateContextFactory;
-import net.pterodactylus.util.template.TemplateParser;
 
 /**
- * Filter that filters a given text through a {@link SoneTextParser}.
+ * Filter that filters a given text through a {@link SoneTextParser} and returns the parsed {@link Part}s.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class ParserFilter implements Filter {
 
-       /** The core. */
        private final Core core;
-
-       /** The link parser. */
        private final SoneTextParser soneTextParser;
 
-       /** The template context factory. */
-       private final TemplateContextFactory templateContextFactory;
-
-       /** The template for {@link PlainTextPart}s. */
-       private static final Template plainTextTemplate = TemplateParser.parse(new StringReader("<%text|html>"));
-
-       /** The template for {@link FreenetLinkPart}s. */
-       private static final Template linkTemplate = TemplateParser.parse(new StringReader("<a class=\"<%cssClass|html>\" href=\"<%link|html>\" title=\"<%title|html>\"><%text|html></a>"));
-
-       /**
-        * Creates a new filter that runs its input through a {@link SoneTextParser}
-        * .
-        *
-        * @param core
-        *            The core
-        * @param templateContextFactory
-        *            The context factory for rendering the parts
-        * @param soneTextParser
-        *            The Sone text parser
-        */
-       public ParserFilter(Core core, TemplateContextFactory templateContextFactory, SoneTextParser soneTextParser) {
+       public ParserFilter(Core core, SoneTextParser soneTextParser) {
                this.core = core;
-               this.templateContextFactory = templateContextFactory;
                this.soneTextParser = soneTextParser;
        }
 
@@ -92,209 +54,18 @@ public class ParserFilter implements Filter {
        @Override
        public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
                String text = valueOf(data);
-               int length = parseInt(valueOf(parameters.get("length")), -1);
-               int cutOffLength = parseInt(valueOf(parameters.get("cut-off-length")), length);
                Object sone = parameters.get("sone");
                if (sone instanceof String) {
                        sone = core.getSone((String) sone).orNull();
                }
                FreenetRequest request = (FreenetRequest) templateContext.get("request");
                SoneTextParserContext context = new SoneTextParserContext(request, (Sone) sone);
-               StringWriter parsedTextWriter = new StringWriter();
                try {
-                       Iterable<Part> parts = soneTextParser.parse(context, new StringReader(text));
-                       if (length > -1) {
-                               int allPartsLength = 0;
-                               List<Part> shortenedParts = new ArrayList<Part>();
-                               for (Part part : parts) {
-                                       if (part instanceof PlainTextPart) {
-                                               String longText = ((PlainTextPart) part).getText();
-                                               if (allPartsLength < cutOffLength) {
-                                                       if ((allPartsLength + longText.length()) > cutOffLength) {
-                                                               shortenedParts.add(new PlainTextPart(longText.substring(0, cutOffLength - allPartsLength) + "…"));
-                                                       } else {
-                                                               shortenedParts.add(part);
-                                                       }
-                                               }
-                                               allPartsLength += longText.length();
-                                       } else if (part instanceof LinkPart) {
-                                               if (allPartsLength < cutOffLength) {
-                                                       shortenedParts.add(part);
-                                               }
-                                               allPartsLength += ((LinkPart) part).getText().length();
-                                       } else {
-                                               if (allPartsLength < cutOffLength) {
-                                                       shortenedParts.add(part);
-                                               }
-                                       }
-                               }
-                               if (allPartsLength >= length) {
-                                       parts = shortenedParts;
-                               }
-                       }
-                       render(parsedTextWriter, parts);
+                       return soneTextParser.parse(context, new StringReader(text));
                } catch (IOException ioe1) {
-                       /* no exceptions in a StringReader or StringWriter, ignore. */
-               }
-               return parsedTextWriter.toString();
-       }
-
-       //
-       // PRIVATE METHODS
-       //
-
-       /**
-        * Renders the given parts.
-        *
-        * @param writer
-        *            The writer to render the parts to
-        * @param parts
-        *            The parts to render
-        */
-       private void render(Writer writer, Iterable<Part> parts) {
-               for (Part part : parts) {
-                       render(writer, part);
+                       /* no exceptions in a StringReader, ignore. */
+                       return Collections.<Part>emptyList();
                }
        }
 
-       /**
-        * Renders the given part.
-        *
-        * @param writer
-        *            The writer to render the part to
-        * @param part
-        *            The part to render
-        */
-       @SuppressWarnings("unchecked")
-       private void render(Writer writer, Part part) {
-               if (part instanceof PlainTextPart) {
-                       render(writer, (PlainTextPart) part);
-               } else if (part instanceof FreenetLinkPart) {
-                       render(writer, (FreenetLinkPart) part);
-               } else if (part instanceof LinkPart) {
-                       render(writer, (LinkPart) part);
-               } else if (part instanceof SonePart) {
-                       render(writer, (SonePart) part);
-               } else if (part instanceof PostPart) {
-                       render(writer, (PostPart) part);
-               } else if (part instanceof Iterable<?>) {
-                       render(writer, (Iterable<Part>) part);
-               }
-       }
-
-       /**
-        * Renders the given plain-text part.
-        *
-        * @param writer
-        *            The writer to render the part to
-        * @param plainTextPart
-        *            The part to render
-        */
-       private void render(Writer writer, PlainTextPart plainTextPart) {
-               TemplateContext templateContext = templateContextFactory.createTemplateContext();
-               templateContext.set("text", plainTextPart.getText());
-               plainTextTemplate.render(templateContext, writer);
-       }
-
-       /**
-        * Renders the given freenet link part.
-        *
-        * @param writer
-        *            The writer to render the part to
-        * @param freenetLinkPart
-        *            The part to render
-        */
-       private void render(Writer writer, FreenetLinkPart freenetLinkPart) {
-               renderLink(writer, "/" + freenetLinkPart.getLink(), freenetLinkPart.getText(), freenetLinkPart.getTitle(), freenetLinkPart.isTrusted() ? "freenet-trusted" : "freenet");
-       }
-
-       /**
-        * Renders the given link part.
-        *
-        * @param writer
-        *            The writer to render the part to
-        * @param linkPart
-        *            The part to render
-        */
-       private void render(Writer writer, LinkPart linkPart) {
-               try {
-                       renderLink(writer, "/external-link/?_CHECKED_HTTP_=" + URLEncoder.encode(linkPart.getLink(), "UTF-8"), linkPart.getText(), linkPart.getTitle(), "internet");
-               } catch (UnsupportedEncodingException uee1) {
-                       /* not possible for UTF-8. */
-                       throw new RuntimeException("The JVM does not support UTF-8 encoding!", uee1);
-               }
-       }
-
-       /**
-        * Renders the given Sone part.
-        *
-        * @param writer
-        *            The writer to render the part to
-        * @param sonePart
-        *            The part to render
-        */
-       private void render(Writer writer, SonePart sonePart) {
-               if ((sonePart.getSone() != null) && (sonePart.getSone().getName() != null)) {
-                       renderLink(writer, "viewSone.html?sone=" + sonePart.getSone().getId(), SoneAccessor.getNiceName(sonePart.getSone()), SoneAccessor.getNiceName(sonePart.getSone()), "in-sone");
-               } else {
-                       renderLink(writer, "/WebOfTrust/ShowIdentity?id=" + sonePart.getSone().getId(), sonePart.getSone().getId(), sonePart.getSone().getId(), "in-sone");
-               }
-       }
-
-       /**
-        * Renders the given post part.
-        *
-        * @param writer
-        *            The writer to render the part to
-        * @param postPart
-        *            The part to render
-        */
-       private void render(Writer writer, PostPart postPart) {
-               SoneTextParser parser = new SoneTextParser(core, core);
-               SoneTextParserContext parserContext = new SoneTextParserContext(null, postPart.getPost().getSone());
-               try {
-                       Iterable<Part> parts = parser.parse(parserContext, new StringReader(postPart.getPost().getText()));
-                       StringBuilder excerpt = new StringBuilder();
-                       for (Part part : parts) {
-                               excerpt.append(part.getText());
-                               if (excerpt.length() > 20) {
-                                       int lastSpace = excerpt.lastIndexOf(" ", 20);
-                                       if (lastSpace > -1) {
-                                               excerpt.setLength(lastSpace);
-                                       } else {
-                                               excerpt.setLength(20);
-                                       }
-                                       excerpt.append("…");
-                                       break;
-                               }
-                       }
-                       renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), excerpt.toString(), SoneAccessor.getNiceName(postPart.getPost().getSone()), "in-sone");
-               } catch (IOException ioe1) {
-                       /* StringReader shouldn’t throw. */
-               }
-       }
-
-       /**
-        * Renders the given link.
-        *
-        * @param writer
-        *            The writer to render the link to
-        * @param link
-        *            The link to render
-        * @param text
-        *            The text of the link
-        * @param title
-        *            The title of the link
-        * @param cssClass
-        *            The CSS class of the link
-        */
-       private void renderLink(Writer writer, String link, String text, String title, String cssClass) {
-               TemplateContext templateContext = templateContextFactory.createTemplateContext();
-               templateContext.set("cssClass", cssClass);
-               templateContext.set("link", link);
-               templateContext.set("text", text);
-               templateContext.set("title", title);
-               linkTemplate.render(templateContext, writer);
-       }
-
 }
index 762bc14..eb280e1 100644 (file)
@@ -65,7 +65,7 @@ public class ProfileAccessor extends ReflectionAccessor {
                        if (avatarId == null) {
                                return null;
                        }
-                       if (core.getImage(avatarId, false) == null) {
+                       if (!profile.getSone().getImageByInternalId(avatarId).isPresent()) {
                                /* avatar ID but no matching image? show nothing. */
                                return null;
                        }
diff --git a/src/main/java/net/pterodactylus/sone/template/RenderFilter.java b/src/main/java/net/pterodactylus/sone/template/RenderFilter.java
new file mode 100644 (file)
index 0000000..1ab0d9e
--- /dev/null
@@ -0,0 +1,190 @@
+package net.pterodactylus.sone.template;
+
+import static java.lang.String.valueOf;
+import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
+import java.io.Writer;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+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;
+import net.pterodactylus.sone.text.PartContainer;
+import net.pterodactylus.sone.text.PlainTextPart;
+import net.pterodactylus.sone.text.PostPart;
+import net.pterodactylus.sone.text.SonePart;
+import net.pterodactylus.sone.text.SoneTextParser;
+import net.pterodactylus.sone.text.SoneTextParserContext;
+import net.pterodactylus.util.template.Filter;
+import net.pterodactylus.util.template.Template;
+import net.pterodactylus.util.template.TemplateContext;
+import net.pterodactylus.util.template.TemplateContextFactory;
+import net.pterodactylus.util.template.TemplateParser;
+
+/**
+ * {@link Filter} implementation that renders an {@link Iterable} (such as a {@link PartContainer}) of {@link Part}s to HTML.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class RenderFilter implements Filter {
+
+       private static final Template plainTextTemplate = TemplateParser.parse(new StringReader("<%text|html>"));
+       private static final Template linkTemplate =
+                       TemplateParser.parse(new StringReader("<a class=\"<%cssClass|html>\" href=\"<%link|html>\" title=\"<%title|html>\"><%text|html></a>"));
+       private final Core core;
+       private final TemplateContextFactory templateContextFactory;
+
+       public RenderFilter(Core core, TemplateContextFactory templateContextFactory) {
+               this.core = core;
+               this.templateContextFactory = templateContextFactory;
+       }
+
+       @Override
+       public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
+               if (!(data instanceof Iterable<?>)) {
+                       return null;
+               }
+               Iterable<Part> parts = (Iterable<Part>) data;
+               int length = parseInt(valueOf(parameters.get("length")), -1);
+               int cutOffLength = parseInt(valueOf(parameters.get("cut-off-length")), length);
+               StringWriter parsedTextWriter = new StringWriter();
+               if (length > -1) {
+                       int allPartsLength = 0;
+                       List<Part> shortenedParts = new ArrayList<Part>();
+                       for (Part part : parts) {
+                               if (part instanceof PlainTextPart) {
+                                       String longText = part.getText();
+                                       if (allPartsLength < cutOffLength) {
+                                               if ((allPartsLength + longText.length()) > cutOffLength) {
+                                                       shortenedParts.add(new PlainTextPart(longText.substring(0, cutOffLength - allPartsLength) + "…"));
+                                               } else {
+                                                       shortenedParts.add(part);
+                                               }
+                                       }
+                                       allPartsLength += longText.length();
+                               } else if (part instanceof LinkPart) {
+                                       if (allPartsLength < cutOffLength) {
+                                               shortenedParts.add(part);
+                                       }
+                                       allPartsLength += part.getText().length();
+                               } else {
+                                       if (allPartsLength < cutOffLength) {
+                                               shortenedParts.add(part);
+                                       }
+                               }
+                       }
+                       if (allPartsLength >= length) {
+                               parts = shortenedParts;
+                       }
+               }
+               render(parsedTextWriter, parts);
+               return parsedTextWriter.toString();
+       }
+
+       private void render(Writer writer, Iterable<Part> parts) {
+               for (Part part : parts) {
+                       render(writer, part);
+               }
+       }
+
+       private void render(Writer writer, Part part) {
+               if (part instanceof PlainTextPart) {
+                       render(writer, (PlainTextPart) part);
+               } else if (part instanceof FreenetLinkPart) {
+                       render(writer, (FreenetLinkPart) part);
+               } else if (part instanceof LinkPart) {
+                       render(writer, (LinkPart) part);
+               } else if (part instanceof SonePart) {
+                       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);
+               }
+       }
+
+       private void render(Writer writer, PlainTextPart plainTextPart) {
+               TemplateContext templateContext = templateContextFactory.createTemplateContext();
+               templateContext.set("text", plainTextPart.getText());
+               plainTextTemplate.render(templateContext, writer);
+       }
+
+       private void render(Writer writer, FreenetLinkPart freenetLinkPart) {
+               renderLink(writer, "/" + freenetLinkPart.getLink(), freenetLinkPart.getText(), freenetLinkPart.getTitle(),
+                               freenetLinkPart.isTrusted() ? "freenet-trusted" : "freenet");
+       }
+
+       private void render(Writer writer, LinkPart linkPart) {
+               try {
+                       renderLink(writer, "/external-link/?_CHECKED_HTTP_=" + URLEncoder.encode(linkPart.getLink(), "UTF-8"), linkPart.getText(),
+                                       linkPart.getTitle(), "internet");
+               } catch (UnsupportedEncodingException uee1) {
+                       /* not possible for UTF-8. */
+                       throw new RuntimeException("The JVM does not support UTF-8 encoding!", uee1);
+               }
+       }
+
+       private void render(Writer writer, SonePart sonePart) {
+               Sone sone = sonePart.getSone();
+               if ((sone != null) && (sone.getName() != null)) {
+                       String niceName = SoneAccessor.getNiceName(sone);
+                       renderLink(writer, "viewSone.html?sone=" + sone.getId(), niceName, niceName, "in-sone");
+               } else {
+                       renderLink(writer, "/WebOfTrust/ShowIdentity?id=" + sone.getId(), sone.getId(), sone.getId(), "in-sone");
+               }
+       }
+
+       private void render(Writer writer, PostPart postPart) {
+               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()));
+                       StringBuilder excerpt = new StringBuilder();
+                       for (Part part : parts) {
+                               excerpt.append(part.getText());
+                               if (excerpt.length() > 20) {
+                                       int lastSpace = excerpt.lastIndexOf(" ", 20);
+                                       if (lastSpace > -1) {
+                                               excerpt.setLength(lastSpace);
+                                       } else {
+                                               excerpt.setLength(20);
+                                       }
+                                       excerpt.append("…");
+                                       break;
+                               }
+                       }
+                       renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), excerpt.toString(),
+                                       SoneAccessor.getNiceName(postPart.getPost().getSone()), postPart.usesDeprecatedLink() ? "internet" : "in-sone");
+               } catch (IOException ioe1) {
+                       /* StringReader shouldn’t throw. */
+               }
+       }
+
+       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");
+       }
+
+       private void renderLink(Writer writer, String link, String text, String title, String cssClass) {
+               TemplateContext templateContext = templateContextFactory.createTemplateContext();
+               templateContext.set("cssClass", cssClass);
+               templateContext.set("link", link);
+               templateContext.set("text", text);
+               templateContext.set("title", title);
+               linkTemplate.render(templateContext, writer);
+       }
+
+}
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 f7177c9..c6ab534 100644 (file)
@@ -26,39 +26,26 @@ import net.pterodactylus.sone.data.Post;
  */
 public class PostPart implements Part {
 
-       /** The post this part refers to. */
        private final Post post;
+       private final boolean usesDeprecatedLink;
 
-       /**
-        * Creates a new post part.
-        *
-        * @param post
-        *            The referenced post
-        */
        public PostPart(Post post) {
-               this.post = post;
+               this(post, false);
        }
 
-       //
-       // ACCESSORS
-       //
+       public PostPart(Post post, boolean usesDeprecatedLink) {
+               this.post = post;
+               this.usesDeprecatedLink = usesDeprecatedLink;
+       }
 
-       /**
-        * Returns the post referenced by this part.
-        *
-        * @return The post referenced by this part
-        */
        public Post getPost() {
                return post;
        }
 
-       //
-       // PART METHODS
-       //
+       public boolean usesDeprecatedLink() {
+               return usesDeprecatedLink;
+       }
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        public String getText() {
                return post.getText();
index a1675d7..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;
        }
 
        //
@@ -204,19 +198,23 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                                continue;
                                        }
                                        if (linkType == LinkType.POST) {
-                                               if (line.length() >= (7 + 36)) {
-                                                       String postId = line.substring(7, 43);
-                                                       Optional<Post> post = postProvider.getPost(postId);
-                                                       if (post.isPresent()) {
-                                                               parts.add(new PostPart(post.get()));
-                                                       } else {
-                                                               parts.add(new PlainTextPart(line.substring(0, 43)));
-                                                       }
-                                                       line = line.substring(43);
+                                               Optional<Post> post = postProvider.getPost(link.substring(7));
+                                               if (post.isPresent()) {
+                                                       parts.add(new PostPart(post.get(), link.substring(7).equals(post.get().getInternalId())));
                                                } else {
-                                                       parts.add(new PlainTextPart(line));
-                                                       line = "";
+                                                       parts.add(new PlainTextPart(link));
+                                               }
+                                               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;
                                        }
 
index c0a8907..b053b5e 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.web;
 
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.template.Template;
@@ -53,7 +54,7 @@ public class BookmarkPage extends SoneTemplatePage {
        protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
                super.processTemplate(request, templateContext);
                if (request.getMethod() == Method.POST) {
-                       String id = request.getHttpRequest().getPartAsStringFailsafe("post", 36);
+                       String id = request.getHttpRequest().getPartAsStringFailsafe("post", IdBuilder.ID_STRING_LENGTH);
                        String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
                        Optional<Post> post = webInterface.getCore().getPost(id);
                        if (post.isPresent()) {
index 1c599e4..338056a 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.web;
 
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty;
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.text.TextFilter;
 import net.pterodactylus.sone.web.page.FreenetRequest;
@@ -26,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.
  *
@@ -63,12 +66,17 @@ public class CreateAlbumPage extends SoneTemplatePage {
                        }
                        String description = request.getHttpRequest().getPartAsStringFailsafe("description", 256).trim();
                        Sone currentSone = getCurrentSone(request.getToadletContext());
-                       String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", 36);
-                       Album parent = webInterface.getCore().getAlbum(parentId);
+                       String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", IdBuilder.ID_STRING_LENGTH);
+                       Optional<Album> 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) {
index 55903d8..2e59de7 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.web;
 
 import com.google.common.base.Optional;
 
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.text.TextFilter;
@@ -56,7 +57,7 @@ public class CreateReplyPage extends SoneTemplatePage {
        @Override
        protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
                super.processTemplate(request, templateContext);
-               String postId = request.getHttpRequest().getPartAsStringFailsafe("post", 36);
+               String postId = request.getHttpRequest().getPartAsStringFailsafe("post", IdBuilder.ID_STRING_LENGTH);
                String text = request.getHttpRequest().getPartAsStringFailsafe("text", 65536).trim();
                String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
                if (request.getMethod() == Method.POST) {
index 98e8909..94fbaf1 100644 (file)
 package net.pterodactylus.sone.web;
 
 import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 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}.
  *
@@ -49,27 +52,27 @@ public class DeleteAlbumPage extends SoneTemplatePage {
        protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
                super.processTemplate(request, templateContext);
                if (request.getMethod() == Method.POST) {
-                       String albumId = request.getHttpRequest().getPartAsStringFailsafe("album", 36);
-                       Album album = webInterface.getCore().getAlbum(albumId);
-                       if (album == null) {
+                       String albumId = request.getHttpRequest().getPartAsStringFailsafe("album", IdBuilder.ID_STRING_LENGTH);
+                       Optional<Album> 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> album = webInterface.getCore().getAlbum(albumId);
+               if (!album.isPresent()) {
                        throw new RedirectException("invalid.html");
                }
                templateContext.set("album", album);
index 77f3ab8..9d50bfb 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.web;
 
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.template.Template;
@@ -52,7 +53,8 @@ public class DeleteImagePage extends SoneTemplatePage {
        @Override
        protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
                super.processTemplate(request, templateContext);
-               String imageId = (request.getMethod() == Method.POST) ? request.getHttpRequest().getPartAsStringFailsafe("image", 36) : request.getHttpRequest().getParam("image");
+               String imageId = (request.getMethod() == Method.POST) ? request.getHttpRequest().getPartAsStringFailsafe("image", IdBuilder.ID_STRING_LENGTH)
+                       : request.getHttpRequest().getParam("image");
                Image image = webInterface.getCore().getImage(imageId, false);
                if (image == null) {
                        throw new RedirectException("invalid.html");
index 5a309fa..c8e0af0 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.web;
 
 import com.google.common.base.Optional;
 
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.template.Template;
@@ -64,7 +65,7 @@ public class DeletePostPage extends SoneTemplatePage {
                        templateContext.set("post", post.get());
                        templateContext.set("returnPage", returnPage);
                } else if (request.getMethod() == Method.POST) {
-                       String postId = request.getHttpRequest().getPartAsStringFailsafe("post", 36);
+                       String postId = request.getHttpRequest().getPartAsStringFailsafe("post", IdBuilder.ID_STRING_LENGTH);
                        String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
                        Optional<Post> post = webInterface.getCore().getPost(postId);
                        if (!post.isPresent() || !post.get().getSone().isLocal()) {
index f7ca132..8e48dd3 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.web;
 
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.template.Template;
@@ -54,7 +55,7 @@ public class DeleteReplyPage extends SoneTemplatePage {
        @Override
        protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
                super.processTemplate(request, templateContext);
-               String replyId = request.getHttpRequest().getPartAsStringFailsafe("reply", 36);
+               String replyId = request.getHttpRequest().getPartAsStringFailsafe("reply", IdBuilder.ID_STRING_LENGTH);
                Optional<PostReply> reply = webInterface.getCore().getPostReply(replyId);
                String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
                if (request.getMethod() == Method.POST) {
index 66434e5..f54f887 100644 (file)
@@ -19,12 +19,15 @@ package net.pterodactylus.sone.web;
 
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Album.Modifier.AlbumTitleMustNotBeEmpty;
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.text.TextFilter;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 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.
  *
@@ -51,37 +54,37 @@ public class EditAlbumPage extends SoneTemplatePage {
        protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
                super.processTemplate(request, templateContext);
                if (request.getMethod() == Method.POST) {
-                       String albumId = request.getHttpRequest().getPartAsStringFailsafe("album", 36);
-                       Album album = webInterface.getCore().getAlbum(albumId);
-                       if (album == null) {
+                       String albumId = request.getHttpRequest().getPartAsStringFailsafe("album", IdBuilder.ID_STRING_LENGTH);
+                       Optional<Album> 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", 36);
+                       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());
                }
        }
 
index 178add1..304b19c 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.web;
 
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.text.TextFilter;
 import net.pterodactylus.sone.web.page.FreenetRequest;
@@ -54,7 +55,7 @@ public class EditImagePage extends SoneTemplatePage {
        protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
                super.processTemplate(request, templateContext);
                if (request.getMethod() == Method.POST) {
-                       String imageId = request.getHttpRequest().getPartAsStringFailsafe("image", 36);
+                       String imageId = request.getHttpRequest().getPartAsStringFailsafe("image", IdBuilder.ID_STRING_LENGTH);
                        String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
                        Image image = webInterface.getCore().getImage(imageId, false);
                        if (image == null) {
index 162d637..47fd8c3 100644 (file)
@@ -22,6 +22,7 @@ import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
 
 import java.util.List;
 
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Profile.DuplicateField;
 import net.pterodactylus.sone.data.Profile.Field;
@@ -80,12 +81,12 @@ public class EditProfilePage extends SoneTemplatePage {
                                birthDay = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim(), null);
                                birthMonth = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim(), null);
                                birthYear = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim(), null);
-                               avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", 36);
+                               avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", IdBuilder.ID_STRING_LENGTH);
                                profile.setFirstName(firstName.length() > 0 ? firstName : null);
                                profile.setMiddleName(middleName.length() > 0 ? middleName : null);
                                profile.setLastName(lastName.length() > 0 ? lastName : null);
                                profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear);
-                               profile.setAvatar(webInterface.getCore().getImage(avatarId, false));
+                               profile.setAvatar(currentSone.getImageByInternalId(avatarId).orNull());
                                for (Field field : fields) {
                                        String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + field.getId(), 400);
                                        String filteredValue = filter(request.getHttpRequest().getHeader("Host"), value);
index 60b22d5..36805b3 100644 (file)
@@ -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> 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;
                }
index 7f08e65..30bf4f0 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.web;
 
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.web.page.FreenetRequest;
@@ -55,7 +56,7 @@ public class LikePage extends SoneTemplatePage {
                super.processTemplate(request, templateContext);
                if (request.getMethod() == Method.POST) {
                        String type = request.getHttpRequest().getPartAsStringFailsafe("type", 16);
-                       String id = request.getHttpRequest().getPartAsStringFailsafe(type, 36);
+                       String id = request.getHttpRequest().getPartAsStringFailsafe(type, IdBuilder.ID_STRING_LENGTH);
                        String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
                        Sone currentSone = getCurrentSone(request.getToadletContext());
                        if ("post".equals(type)) {
index c38f97b..1332638 100644 (file)
@@ -22,6 +22,7 @@ import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
 import java.util.ArrayList;
 import java.util.List;
 
+import net.pterodactylus.sone.core.CompatibilityMode;
 import net.pterodactylus.sone.core.Preferences;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
@@ -133,6 +134,14 @@ public class OptionsPage extends SoneTemplatePage {
                        Integer fcpFullAccessRequiredInteger = parseInt(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1), preferences.getFcpFullAccessRequired().ordinal());
                        FullAccessRequired fcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequiredInteger];
                        preferences.setFcpFullAccessRequired(fcpFullAccessRequired);
+
+                       boolean compatOldElementIds = request.getHttpRequest().isPartSet("compat-old-element-ids");
+                       if (compatOldElementIds) {
+                               webInterface.getCore().setCompatibilityMode(CompatibilityMode.oldElementIds);
+                       } else {
+                               webInterface.getCore().clearCompatibilityMod(CompatibilityMode.oldElementIds);
+                       }
+
                        webInterface.getCore().touchConfiguration();
                        if (fieldErrors.isEmpty()) {
                                throw new RedirectException(getPath());
@@ -158,6 +167,7 @@ public class OptionsPage extends SoneTemplatePage {
                templateContext.set("trust-comment", preferences.getTrustComment());
                templateContext.set("fcp-interface-active", preferences.isFcpInterfaceActive());
                templateContext.set("fcp-full-access-required", preferences.getFcpFullAccessRequired().ordinal());
+               templateContext.set("compat-old-element-ids", webInterface.getCore().isCompatibilityMode(CompatibilityMode.oldElementIds));
        }
 
 }
index 95b0bba..fd26f44 100644 (file)
@@ -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;
        }
 
        /**
index 72ff2fc..3be06ae 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.web;
 
 import java.util.Set;
 
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.template.Template;
@@ -55,7 +56,7 @@ public class UnbookmarkPage extends SoneTemplatePage {
        protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
                super.processTemplate(request, templateContext);
                if (request.getMethod() == Method.POST) {
-                       String id = request.getHttpRequest().getPartAsStringFailsafe("post", 36);
+                       String id = request.getHttpRequest().getPartAsStringFailsafe("post", IdBuilder.ID_STRING_LENGTH);
                        String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
                        Optional<Post> post = webInterface.getCore().getPost(id);
                        if (post.isPresent()) {
index 9254a42..16291bf 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.web;
 
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.web.page.FreenetRequest;
@@ -55,7 +56,7 @@ public class UnlikePage extends SoneTemplatePage {
                super.processTemplate(request, templateContext);
                if (request.getMethod() == Method.POST) {
                        String type = request.getHttpRequest().getPartAsStringFailsafe("type", 16);
-                       String id = request.getHttpRequest().getPartAsStringFailsafe(type, 36);
+                       String id = request.getHttpRequest().getPartAsStringFailsafe(type, IdBuilder.ID_STRING_LENGTH);
                        String returnPage = request.getHttpRequest().getPartAsStringFailsafe("returnPage", 256);
                        Sone currentSone = getCurrentSone(request.getToadletContext());
                        if ("post".equals(type)) {
index a1e987a..673e439 100644 (file)
@@ -34,6 +34,7 @@ import javax.imageio.ImageReader;
 import javax.imageio.stream.ImageInputStream;
 
 import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Image.Modifier.ImageTitleMustNotBeEmpty;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.TemporaryImage;
@@ -44,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;
@@ -83,12 +85,12 @@ public class UploadImagePage extends SoneTemplatePage {
                super.processTemplate(request, templateContext);
                if (request.getMethod() == Method.POST) {
                        Sone currentSone = getCurrentSone(request.getToadletContext());
-                       String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", 36);
-                       Album parent = webInterface.getCore().getAlbum(parentId);
-                       if (parent == null) {
+                       String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", IdBuilder.ID_STRING_LENGTH);
+                       Optional<Album> 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);
@@ -122,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);
@@ -133,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());
                }
        }
 
index beba513..06ebf8d 100644 (file)
@@ -73,6 +73,7 @@ import net.pterodactylus.sone.main.ReparseFilter;
 import net.pterodactylus.sone.main.SonePlugin;
 import net.pterodactylus.sone.notify.ListNotification;
 import net.pterodactylus.sone.template.AlbumAccessor;
+import net.pterodactylus.sone.template.BuildIdFilter;
 import net.pterodactylus.sone.template.CollectionAccessor;
 import net.pterodactylus.sone.template.CssClassNameFilter;
 import net.pterodactylus.sone.template.HttpRequestAccessor;
@@ -83,6 +84,7 @@ import net.pterodactylus.sone.template.JavascriptFilter;
 import net.pterodactylus.sone.template.ParserFilter;
 import net.pterodactylus.sone.template.PostAccessor;
 import net.pterodactylus.sone.template.ProfileAccessor;
+import net.pterodactylus.sone.template.RenderFilter;
 import net.pterodactylus.sone.template.ReplyAccessor;
 import net.pterodactylus.sone.template.ReplyGroupFilter;
 import net.pterodactylus.sone.template.RequestChangeFilter;
@@ -193,6 +195,7 @@ public class WebInterface {
 
        /** The parser filter. */
        private final ParserFilter parserFilter;
+       private final RenderFilter renderFilter;
 
        /** The “new Sone” notification. */
        private final ListNotification<Sone> newSoneNotification;
@@ -247,7 +250,7 @@ public class WebInterface {
                this.sonePlugin = sonePlugin;
                this.loaders = loaders;
                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());
@@ -272,8 +275,9 @@ public class WebInterface {
                templateContextFactory.addFilter("match", new MatchFilter());
                templateContextFactory.addFilter("css", new CssClassNameFilter());
                templateContextFactory.addFilter("js", new JavascriptFilter());
-               templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), templateContextFactory, soneTextParser));
+               templateContextFactory.addFilter("parse", parserFilter = new ParserFilter(getCore(), soneTextParser));
                templateContextFactory.addFilter("reparse", new ReparseFilter());
+               templateContextFactory.addFilter("render", renderFilter = new RenderFilter(getCore(), templateContextFactory));
                templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
                templateContextFactory.addFilter("format", new FormatFilter());
                templateContextFactory.addFilter("sort", new CollectionSortFilter());
@@ -283,6 +287,7 @@ public class WebInterface {
                templateContextFactory.addFilter("unique", new UniqueElementFilter());
                templateContextFactory.addFilter("mod", new ModFilter());
                templateContextFactory.addFilter("paginate", new PaginationFilter());
+               templateContextFactory.addFilter("build-id", new BuildIdFilter());
                templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER);
                templateContextFactory.addProvider(loaders.getTemplateProvider());
                templateContextFactory.addTemplateObject("webInterface", this);
@@ -704,8 +709,8 @@ public class WebInterface {
                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, parserFilter)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new EditAlbumAjaxPage(this, parserFilter, renderFilter)));
+               pageToadlets.add(pageToadletFactory.createPageToadlet(new EditImageAjaxPage(this, parserFilter, renderFilter)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new TrustAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new DistrustAjaxPage(this)));
                pageToadlets.add(pageToadletFactory.createPageToadlet(new UntrustAjaxPage(this)));
index 238206a..6fe06bc 100644 (file)
 
 package net.pterodactylus.sone.web.ajax;
 
+import java.util.Collections;
+
 import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.Image;
+import net.pterodactylus.sone.template.ParserFilter;
+import net.pterodactylus.sone.template.RenderFilter;
+import net.pterodactylus.sone.text.Part;
 import net.pterodactylus.sone.text.TextFilter;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.sone.web.page.FreenetRequest;
+import net.pterodactylus.util.template.TemplateContext;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableMap;
 
 /**
  * Page that stores a user’s album modifications.
@@ -29,14 +39,13 @@ import net.pterodactylus.sone.web.page.FreenetRequest;
  */
 public class EditAlbumAjaxPage extends JsonPage {
 
-       /**
-        * Creates a new edit album AJAX page.
-        *
-        * @param webInterface
-        *            The Sone web interface
-        */
-       public EditAlbumAjaxPage(WebInterface webInterface) {
+       private final ParserFilter parserFilter;
+       private final RenderFilter renderFilter;
+
+       public EditAlbumAjaxPage(WebInterface webInterface, ParserFilter parserFilter, RenderFilter renderFilter) {
                super("editAlbum.ajax", webInterface);
+               this.parserFilter = parserFilter;
+               this.renderFilter = renderFilter;
        }
 
        //
@@ -49,32 +58,38 @@ 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> 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", parseDescription(album.get()));
                } catch (IllegalStateException e) {
                        return createErrorJsonObject("invalid-album-title");
                }
        }
 
+       private String parseDescription(Album album) {
+               Iterable<Part> parts = (Iterable<Part>) parserFilter.format(new TemplateContext(), album.getDescription(),
+                               ImmutableMap.<String, Object>builder().put("sone", album.getSone()).build());
+               return (String) renderFilter.format(new TemplateContext(), parts, Collections.<String, Object>emptyMap());
+       }
+
 }
index 79de200..a40a955 100644 (file)
 
 package net.pterodactylus.sone.web.ajax;
 
+import java.util.Collections;
+
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.template.ParserFilter;
+import net.pterodactylus.sone.template.RenderFilter;
+import net.pterodactylus.sone.text.Part;
 import net.pterodactylus.sone.text.TextFilter;
 import net.pterodactylus.sone.web.WebInterface;
 import net.pterodactylus.sone.web.page.FreenetRequest;
@@ -33,8 +37,8 @@ import com.google.common.collect.ImmutableMap;
  */
 public class EditImageAjaxPage extends JsonPage {
 
-       /** Parser for image descriptions. */
        private final ParserFilter parserFilter;
+       private final RenderFilter renderFilter;
 
        /**
         * Creates a new edit image AJAX page.
@@ -44,9 +48,10 @@ public class EditImageAjaxPage extends JsonPage {
         * @param parserFilter
         *            The parser filter for image descriptions
         */
-       public EditImageAjaxPage(WebInterface webInterface, ParserFilter parserFilter) {
+       public EditImageAjaxPage(WebInterface webInterface, ParserFilter parserFilter, RenderFilter renderFilter) {
                super("editImage.ajax", webInterface);
                this.parserFilter = parserFilter;
+               this.renderFilter = renderFilter;
        }
 
        //
@@ -83,7 +88,16 @@ public class EditImageAjaxPage extends JsonPage {
                String description = request.getHttpRequest().getParam("description").trim();
                image.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
                webInterface.getCore().touchConfiguration();
-               return createSuccessJsonObject().put("imageId", image.getId()).put("title", image.getTitle()).put("description", image.getDescription()).put("parsedDescription", (String) parserFilter.format(new TemplateContext(), image.getDescription(), ImmutableMap.<String, Object>builder().put("sone", image.getSone()).build()));
+               return createSuccessJsonObject().put("imageId", image.getId())
+                               .put("title", image.getTitle())
+                               .put("description", image.getDescription())
+                               .put("parsedDescription", parseDescription(image));
+       }
+
+       private String parseDescription(Image image) {
+               Iterable<Part> parts = (Iterable<Part>) parserFilter.format(new TemplateContext(), image.getDescription(),
+                               ImmutableMap.<String, Object>builder().put("sone", image.getSone()).build());
+               return (String) renderFilter.format(new TemplateContext(), parts, Collections.<String, Object>emptyMap());
        }
 
 }
index 40a12b1..12927a5 100644 (file)
@@ -69,6 +69,9 @@ Page.Options.Option.FcpFullAccessRequired.Description=Require FCP connection fro
 Page.Options.Option.FcpFullAccessRequired.Value.No=No
 Page.Options.Option.FcpFullAccessRequired.Value.Writing=For Write Access
 Page.Options.Option.FcpFullAccessRequired.Value.Always=Always
+Page.Options.Section.CompatibilityOptions.Title=Compatibility Options
+Page.Options.Section.CompatibilityOptions.Description=These options control how much deprecated functionality Sone will support.
+Page.Options.Option.CompatibilityOptions.OldElementIds.Description=Support old post IDs in links. Activating this will try to locate a post linked to by an old ID even though it can not be guaranteed that it is the post that was originally linked to.
 Page.Options.Section.Cleaning.Title=Clean Up
 Page.Options.Option.ClearOnNextRestart.Description=Resets the configuration of the Sone plugin at the next restart. Warning! {strong}This will destroy all of your Sones{/strong} so make sure you have backed up everyhing you still need! Also, you need to set the next option to true to actually do it.
 Page.Options.Option.ReallyClearOnNextRestart.Description=This option needs to be set to “yes” if you really, {strong}really{/strong} want to clear the plugin configuration on the next restart.
@@ -367,6 +370,7 @@ View.Post.ShowSource=Toggle Parser
 View.Post.NotDownloaded=This post has not yet been downloaded, or it has been deleted.
 View.Post.ShowMore=show more
 View.Post.ShowLess=show less
+View.Post.LinkedAlbum.SizeAndAuthor={1,number} {1,choice,0#images|1#image|1<images} by [link]{0}[/link]
 
 View.UpdateStatus.Text.ChooseSenderIdentity=Choose the sender identity
 
@@ -463,3 +467,4 @@ Notification.Mention.Text=You have been mentioned in the following posts:
 Notification.SoneIsInserting.Text=Your Sone sone://{0} is now being inserted.
 Notification.SoneIsInserted.Text=Your Sone sone://{0} has been inserted in {1,number} {1,choice,0#seconds|1#second|1<seconds}.
 Notification.SoneInsertAborted.Text=Your Sone sone://{0} could not be inserted.
+# 72-74
index 1767090..557ea46 100644 (file)
@@ -353,6 +353,54 @@ textarea {
        cursor: pointer;
 }
 
+#sone .post .linked-album {
+       display: table;
+       margin-top: 1ex;
+       border-collapse: collapse;
+}
+
+#sone .post .linked-album .album-header {
+       display: table-caption;
+       margin-bottom: 1ex;
+}
+
+#sone .post .linked-album .album-header .album-title {
+       font-size: 150%;
+       font-weight: bold;
+}
+
+#sone .post .linked-image {
+       display: table-row;
+}
+
+#sone .post .linked-image .image-left {
+       display: table-cell;
+       padding-bottom: 1ex;
+}
+
+#sone .post .linked-image .image {
+       width: 160px;
+       height: 90px;
+       overflow: hidden;
+       border: solid 1px black;
+       display: inline-block;
+       padding: 0px;
+}
+
+#sone .post .linked-image .about-image {
+       display: table-cell;
+       padding-left: 1ex;
+}
+
+#sone .post .linked-image .about-image .title {
+       font-size: 110%;
+       font-weight: bold;
+}
+
+#sone .post .linked-image .about-image .description {
+       margin-top: 0.5ex;
+}
+
 #sone .post .status-line {
        margin-top: 0.5ex;
        font-size: 85%;
@@ -690,6 +738,9 @@ textarea {
        height: 250px;
        overflow: hidden;
        padding: -1px;
+}
+
+#sone .image-container {
        border: solid 1px #000;
 }
 
@@ -713,6 +764,26 @@ textarea {
        width: 95%;
 }
 
+#sone .album-container .image-container {
+       position: absolute;
+}
+
+#sone .album-container .link-to-album {
+       display: none;
+}
+
+#sone .album-container:hover .link-to-album {
+       display: block;
+       float: right;
+       z-index: 1;
+       padding: 0.5ex;
+       background-color: #eee;
+       position: relative;
+       top: 250px;
+       right: 0.5ex;
+       margin-top: -2em;
+}
+
 #sone .image .album-sone {
        font-size: 80%;
 }
index ddc08a9..af0bb8d 100644 (file)
                        </li>
                        <%foreach currentSone.allImages image>
                                <li>
-                                       <input type="radio" name="avatarId" value="<%image.id|html>"<%if avatarId|match value=image.id> checked="checked"<%/if>/>
+                                       <input type="radio" name="avatarId" value="<%image.internalId|html>"<%if avatarId|match value=image.internalId> checked="checked"<%/if>/>
                                        <div class="post-avatar"><% image|image-link max-width==48 max-height==48 mode==enlarge title=image.title></div>
                                </li>
                        <%/foreach>
index 7dd4c29..a237c7f 100644 (file)
                                 var albumDescriptionField = getAlbum(data.albumId).find(".album-description");
                                 if (data.success) {
                                     albumTitleField.text(data.title);
-                                    albumDescriptionField.text(data.description);
+                                    albumDescriptionField.html(data.description);
                                     getAlbum(data.albumId).find(":input[name='title']").attr("defaultValue", title);
                                     getAlbum(data.albumId).find(":input[name='description']").attr("defaultValue", description);
                                 } else {
                                <%/foreach>
                        </div>
 
-                       <p id="description"><% album.description|parse sone=album.sone></p>
+                       <p id="description"><% album.description|parse sone=album.sone|render></p>
 
                        <%if album.sone.local>
                                <div class="show-edit-album hidden toggle-link"><a class="small-link">» <%= Page.ImageBrowser.Album.Edit.Title|l10n|html></a></div>
                                        </div>
                                        <div class="show-data">
                                                <div class="image-title"><% image.title|html></div>
-                                               <div class="image-description"><% image.description|parse sone=image.sone></div>
+                                               <div class="image-description"><% image.description|parse sone=image.sone|render></div>
                                        </div>
                                        <%if album.sone.local>
                                                <form class="edit-image" action="editImage.html" method="post">
                                <%/if>
                        </div>
 
-                       <p class="parsed"><%image.description|parse sone=image.sone></p>
+                       <p class="parsed"><%image.description|parse sone=image.sone|render></p>
 
                        <%if image.sone.local>
 
                                <div class="show-data">
                                        <div class="album-sone"><a href="imageBrowser.html?sone=<%album.sone.id|html>"><%album.sone.niceName|html></a></div>
                                        <div class="album-title"><% album.title|html> (<%= View.Sone.Stats.Images|l10n 0=album.images.size>)</div>
-                                       <div class="album-description"><% album.description|parse sone=album.sone></div>
+                                       <div class="album-description"><% album.description|parse sone=album.sone|render></div>
                                </div>
                        </div>
                        <%= false|store key==endRow>
index 9aacad3..74bf714 100644 (file)
@@ -1,48 +1,7 @@
 <%foreach albums album>
        <%first><h2><%= Page.ImageBrowser.Header.Albums|l10n|html></h2><%/first>
        <%if loop.count|mod divisor==3><div class="album-row"><%/if>
-       <div id="album-<% album.id|html>" class="album">
-               <div class="album-id hidden"><% album.id|html></div>
-               <div class="album-container">
-                       <a href="imageBrowser.html?album=<% album.id|html>" title="<% album.title|html>">
-                               <%ifnull album.albumImage>
-                                       <img src="images/unknown-image-0.png" width="333" height="250" alt="<% album.title|html>" title="<% album.title|html>" style="position: relative; top: 0px; left: -41px;" />
-                               <%else><!-- TODO -->
-                                       <% album.albumImage|image-link max-width==250 max-height==250 mode==enlarge title=album.title>
-                               <%/if>
-                       </a>
-               </div>
-               <div class="show-data">
-                       <div class="album-title"><% album.title|html> (<%= View.Sone.Stats.Images|l10n 0=album.images.size>)</div>
-                       <div class="album-description"><% album.description|parse sone=album.sone></div>
-               </div>
-               <%if album.sone.local>
-                       <form class="edit-album" action="editAlbum.html" method="post">
-                               <input type="hidden" name="formPassword" value="<%formPassword|html>" />
-                               <input type="hidden" name="returnPage" value="<%request.uri|html>" />
-                               <input type="hidden" name="album" value="<%album.id|html>" />
-
-                               <div class="move-buttons">
-                                               <button <%first>class="hidden" <%/first>type="submit" name="moveLeft" value="true"><%= Page.ImageBrowser.Image.Button.MoveLeft|l10n|html></button>
-                                               <button <%last>class="hidden" <%/last>type="submit" name="moveRight" value="true"><%= Page.ImageBrowser.Image.Button.MoveRight|l10n|html></button>
-                               </div>
-
-                               <div class="edit-data hidden">
-                                       <div>
-                                               <input type="text" name="title" value="<%album.title|html>" />
-                                       </div>
-                                       <div>
-                                               <textarea name="description"><%album.description|html></textarea>
-                                       </div>
-                                       <div>
-                                               <button <%first>class="hidden" <%/first>type="submit" name="moveLeft" value="true"><%= Page.ImageBrowser.Image.Button.MoveLeft|l10n|html></button>
-                                               <button type="submit" name="submit"><%= Page.ImageBrowser.Album.Button.Save|l10n|html></button>
-                                               <button <%last>class="hidden" <%/last>type="submit" name="moveRight" value="true"><%= Page.ImageBrowser.Image.Button.MoveRight|l10n|html></button>
-                                       </div>
-                               </div>
-                       </form>
-               <%/if>
-       </div>
+       <%include include/viewAlbum.html>
        <%= false|store key==endRow>
        <%if loop.count|mod divisor==3 offset==1><%= true|store key==endRow><%/if>
        <%last><%= true|store key==endRow><%/last>
index 13b564d..74091ca 100644 (file)
@@ -44,7 +44,7 @@
                                <a class="picture" href="index.html">
                                        <%ifnull !currentSone>
                                                <%ifnull !currentSone.profile.avatar>
-                                                       <%currentSone.profile.avatar|image-link max-width==80 max-height==80 mode==enlarge title=="Profile Avatar">
+                                                       <%currentSone.profile.avatar|build-id sone=currentSone|image-link max-width==80 max-height==80 mode==enlarge title=="Profile Avatar">
                                                <%else>
                                                        <img src="/WebOfTrust/GetIdenticon?identity=<% currentSone.id|html>&amp;width=80&amp;height=80" width="80" height="80" alt="Profile Avatar" />
                                                <%/if>
index fc51e79..a93b0a7 100644 (file)
@@ -2,7 +2,7 @@
        <div class="sone-menu-id hidden"><%sone.id|html></div>
        <div class="avatar menu-avatar">
                <%ifnull !sone.profile.avatar>
-                       <%sone.profile.avatar|image-link max-width==64 max-height==64 mode==enlarge title=sone.niceName>
+                       <%sone.profile.avatar|build-id sone=sone|image-link max-width==64 max-height==64 mode==enlarge title=sone.niceName>
                <%else>
                        <img src="/WebOfTrust/GetIdenticon?identity=<%sone.id|html>&amp;width=128&amp;height=128" width="64" height="64" alt="Avatar Image" />
                <%/if>
diff --git a/src/main/resources/templates/include/viewAlbum.html b/src/main/resources/templates/include/viewAlbum.html
new file mode 100644 (file)
index 0000000..0821909
--- /dev/null
@@ -0,0 +1,45 @@
+<div id="album-<% album.id|html>" class="album">
+       <div class="album-id hidden"><% album.id|html></div>
+       <div class="album-container">
+               <a href="imageBrowser.html?album=<% album.id|html>" title="<% album.title|html>">
+                       <%ifnull album.albumImage>
+                       <img src="images/unknown-image-0.png" width="333" height="250" alt="<% album.title|html>" title="<% album.title|html>" style="position: relative; top: 0px; left: -41px;" />
+                       <%else><!-- TODO -->
+                       <div class="image-container">
+                               <% album.albumImage|image-link class==album-image max-width==250 max-height==250 mode==enlarge title=album.title>
+                       </div>
+                       <div class="link-to-album"><a href="album://<% album.id|html>">[link album]</a></div>
+                       <%/if>
+               </a>
+       </div>
+       <div class="show-data">
+               <div class="album-title"><% album.title|html> (<%= View.Sone.Stats.Images|l10n 0=album.images.size>)</div>
+               <div class="album-description"><% album.description|parse sone=album.sone|render></div>
+       </div>
+       <%if album.sone.local>
+       <form class="edit-album" action="editAlbum.html" method="post">
+               <input type="hidden" name="formPassword" value="<%formPassword|html>" />
+               <input type="hidden" name="returnPage" value="<%request.uri|html>" />
+               <input type="hidden" name="album" value="<%album.id|html>" />
+
+               <div class="move-buttons">
+                       <button <%if loop.first>class="hidden" <%/if>type="submit" name="moveLeft" value="true"><%= Page.ImageBrowser.Image.Button.MoveLeft|l10n|html></button>
+                       <button <%if loop.last>class="hidden" <%/if>type="submit" name="moveRight" value="true"><%= Page.ImageBrowser.Image.Button.MoveRight|l10n|html></button>
+               </div>
+
+               <div class="edit-data hidden">
+                       <div>
+                               <input type="text" name="title" value="<%album.title|html>" />
+                       </div>
+                       <div>
+                               <textarea name="description"><%album.description|html></textarea>
+                       </div>
+                       <div>
+                               <button <%if loop.first>class="hidden" <%/if>type="submit" name="moveLeft" value="true"><%= Page.ImageBrowser.Image.Button.MoveLeft|l10n|html></button>
+                               <button type="submit" name="submit"><%= Page.ImageBrowser.Album.Button.Save|l10n|html></button>
+                               <button <%if loop.last>class="hidden" <%/if>type="submit" name="moveRight" value="true"><%= Page.ImageBrowser.Image.Button.MoveRight|l10n|html></button>
+                       </div>
+               </div>
+       </form>
+       <%/if>
+</div>
index 1c53d42..d150d4f 100644 (file)
@@ -7,7 +7,7 @@
        <div class="avatar post-avatar" >
                <%if post.loaded>
                        <%ifnull !post.sone.profile.avatar>
-                               <%post.sone.profile.avatar|image-link max-width==48 max-height==48 mode==enlarge title=="Avatar Image">
+                               <%post.sone.profile.avatar|build-id sone=post.sone|image-link max-width==48 max-height==48 mode==enlarge title=="Avatar Image">
                        <%else>
                                <img src="/WebOfTrust/GetIdenticon?identity=<% post.sone.id|html>&amp;width=96&amp;height=96" width="48" height="48" alt="Avatar Image" />
                        <%/if>
                                <%/if>
                        <%/if>
                        <% post.text|html|store key==originalText text==true>
-                       <% post.text|parse sone=post.sone|store key==parsedText text==true>
-                       <% post.text|parse sone=post.sone length=core.preferences.charactersPerPost cut-off-length=core.preferences.postCutOffLength|store key==shortText text==true>
+                       <% post.text|parse sone=post.sone|store key==parts>
+                       <% parts|render|store key==parsedText text==true>
+                       <% parts|render length=core.preferences.charactersPerPost cut-off-length=core.preferences.postCutOffLength|store key==shortText text==true>
                        <div class="post-text raw-text<%if !raw> hidden<%/if>"><% originalText></div>
                        <div class="post-text text<%if raw> hidden<%/if><%if !shortText|match key=parsedText> hidden<%/if>"><% parsedText></div>
                        <div class="post-text short-text<%if raw> hidden<%/if><%if shortText|match key=parsedText> hidden<%/if>"><% shortText></div>
                        <%if !shortText|match value=parsedText><%if !raw><a class="expand-post-text" href="viewPost.html?post=<% post.id|html>&amp;raw=true"><%= View.Post.ShowMore|l10n|html></a><%/if><%/if>
                        <%if !shortText|match value=parsedText><%if !raw><a class="shrink-post-text hidden"><%= View.Post.ShowLess|l10n|html></a><%/if><%/if>
                </div>
+               <%foreach parts part>
+                       <%if part.class.simpleName|match value==AlbumPart><!-- ← this is so ugly. -->
+                               <div class="linked-album">
+                                       <div class="linked-image">
+                                               <div class="image-left">
+                                                       <div class="image">
+                                                               <a href="imageBrowser.html?album=<% part.album.id|html>"><% part.album.albumImage|image-link max-width==160 max-height==90 mode==enlarge title=part.album.title></a>
+                                                       </div>
+                                               </div>
+                                               <div class="about-image">
+                                                       <div class="title"><% part.album.title|html></div>
+                                                       <div><% =View.Post.LinkedAlbum.SizeAndAuthor|l10n 0=part.album.sone.niceName 1=part.album.images.size|html|replace needle=="[link]" replacement=='<a href="viewSone.html?sone=<sone-id>">'|replace needle=='[/link]' replacement=='</a>'|replace needle=='<sone-id>' replacement=part.album.sone.id></div>
+                                                       <div class="description"><% part.album.description|parse sone=part.album.sone|render></div>
+                                               </div>
+                                       </div>
+                               </div>
+                       <%/if>
+               <%/foreach>
                <div class="post-status-line status-line<%if !post.loaded> hidden<%/if>">
                        <div class="bookmarks">
                                <form class="unbookmark<%if !post.bookmarked> hidden<%/if>" action="unbookmark.html" method="post">
index 5e2ed7d..ec8b447 100644 (file)
@@ -6,7 +6,7 @@
        <%include include/soneMenu.html class=="sone-reply-menu" sone=reply.sone>
        <div class="avatar reply-avatar">
                <%ifnull !reply.sone.profile.avatar>
-                       <% reply.sone.profile.avatar|image-link max-width==36 max-height==36 mode==enlarge title=="Avatar Image">
+                       <% reply.sone.profile.avatar|build-id sone=reply.sone|image-link max-width==36 max-height==36 mode==enlarge title=="Avatar Image">
                <%else>
                        <img src="/WebOfTrust/GetIdenticon?identity=<% reply.sone.id|html>&amp;width=72&height=72" width="36" height="36" alt="Avatar Image" />
                <%/if>
@@ -15,8 +15,9 @@
                <div>
                        <div class="author profile-link"><a href="viewSone.html?sone=<% reply.sone.id|html>"><% reply.sone.niceName|html></a></div>
                        <% reply.text|html|store key==originalText text==true>
-                       <% reply.text|parse sone=reply.sone|store key==parsedText text==true>
-                       <% reply.text|parse sone=reply.sone length=core.preferences.charactersPerPost cut-off-length=core.preferences.postCutOffLength|store key==shortText text==true>
+                       <% reply.text|parse sone=reply.sone|store key==parts>
+                       <% parts|render|store key==parsedText text==true>
+                       <% parts|render length=core.preferences.charactersPerPost cut-off-length=core.preferences.postCutOffLength|store key==shortText text==true>
                        <div class="reply-text raw-text<%if !raw> hidden<%/if>"><% originalText></div>
                        <div class="reply-text text<%if raw> hidden<%/if><%if !shortText|match key=parsedText> hidden<%/if>"><% parsedText></div>
                        <div class="reply-text short-text<%if raw> hidden<%/if><%if shortText|match key=parsedText> hidden<%/if>"><% shortText></div>
index 3d456d4..9d29d6b 100644 (file)
@@ -30,7 +30,7 @@
        <posts>
                <%foreach currentSone.posts post>
                <post>
-                       <id><% post.id|xml></id>
+                       <id><% post.internalId|xml></id>
                        <recipient><%if post.recipientId.present><% post.recipientId.get|xml><%/if></recipient>
                        <time><% post.time></time>
                        <text><% post.text|xml></text>
@@ -41,7 +41,7 @@
        <replies>
                <%foreach currentSone.replies reply>
                <reply>
-                       <id><% reply.id></id>
+                       <id><% reply.internalId></id>
                        <post-id><% reply.postId|xml></post-id>
                        <time><% reply.time></time>
                        <text><% reply.text|xml></text>
        <albums>
                <%/first>
                <album>
-                       <id><%album.id|xml></id>
+                       <id><%album.internalId|xml></id>
                        <%if !album.parent.root>
-                       <parent><%album.parent.id|xml></parent>
+                       <parent><%album.parent.internalId|xml></parent>
                        <%/if>
                        <title><%album.title|xml></title>
                        <description><%album.description|xml></description>
-                       <album-image><%album.albumImage.id|xml></album-image>
+                       <album-image><%album.albumImage.internalId|xml></album-image>
                        <%foreach album.images image>
                        <%first>
                        <images>
                                <%/first>
                                <image>
-                                       <id><%image.id|xml></id>
+                                       <id><%image.internalId|xml></id>
                                        <creation-time><%image.creationTime|xml></creation-time>
                                        <key><%image.key|xml></key>
                                        <title><%image.title|xml></title>
index 3ac37cf..922829c 100644 (file)
@@ -4,9 +4,9 @@
 
        <%foreach messages message>
                <%if message|substring start==0 length==1|match value=='!'>
-                       <p class="error"><% message|substring start==1|parse></p>
+                       <p class="error"><% message|substring start==1|parse|render></p>
                <%else>
-                       <p><% message|parse></p>
+                       <p><% message|parse|render></p>
                <%/if>
        <%foreachelse>
                <p><%= Page.Invalid.Text|l10n|html|replace needle=="{link}" replacement=='<a href="index.html">'|replace needle=="{/link}" replacement=='</a>'></p>
index 599a7e6..4cee9ce 100644 (file)
@@ -1 +1 @@
-<div class="text"><%= Notification.NewVersion.Text|l10n|replace needle=="{version}" replacement=latestVersion|replace needle=="{edition}" replacement=latestEdition|parse sone=="nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"></div>
+<div class="text"><%= Notification.NewVersion.Text|l10n|replace needle=="{version}" replacement=latestVersion|replace needle=="{edition}" replacement=latestEdition|parse sone=="nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"|render></div>
index c15864f..8965e36 100644 (file)
@@ -1,7 +1,7 @@
 <%if soneStatus|match value=="inserting">
-       <%= Notification.SoneIsInserting.Text|l10n 0=insertSone.id|parse>
+       <%= Notification.SoneIsInserting.Text|l10n 0=insertSone.id|parse|render>
 <%elseif soneStatus|match value=="inserted">
-       <%= Notification.SoneIsInserted.Text|l10n 0=insertSone.id 1=insertDuration|parse>
+       <%= Notification.SoneIsInserted.Text|l10n 0=insertSone.id 1=insertDuration|parse|render>
 <%elseif soneStatus|match value=="insert-aborted">
-       <%= Notification.SoneInsertAborted.Text|l10n 0=insertSone.id|parse>
+       <%= Notification.SoneInsertAborted.Text|l10n 0=insertSone.id|parse|render>
 <%/if>
index 4ec88cb..0879ed1 100644 (file)
                        </select>
                </p>
 
+        <h2><%= Page.Options.Section.CompatibilityOptions.Title|l10n|html></h2>
+
+        <p><%= Page.Options.Section.CompatibilityOptions.Description|l10n|html></p>
+
+        <p>
+            <input type="checkbox" name="compat-old-element-ids"<%if compat-old-element-ids> checked="checked"<%/if> />
+            <%= Page.Options.Option.CompatibilityOptions.OldElementIds.Description|l10n|html>
+        </p>
+
                <p><button type="submit"><%= Page.Options.Button.Save|l10n|html></button></p>
 
        </form>
index 4b88474..dd994ea 100644 (file)
@@ -52,7 +52,7 @@
                        <%foreach sone.profile.fields field>
                                <div class="profile-field">
                                        <div class="name"><% field.name|html></div>
-                                       <div class="value"><% field.value|parse sone=sone></div>
+                                       <div class="value"><% field.value|parse sone=sone|render></div>
                                </div>
                        <%/foreach>
 
index 091c93f..3caeb79 100644 (file)
@@ -159,6 +159,7 @@ public class FreenetInterfaceTest {
                byte[] imageData = new byte[] { 1, 2, 3, 4 };
                temporaryImage.setImageData(imageData);
                Image image = new ImageImpl("image-id");
+               image.modify().setSone(sone).update();
                InsertToken insertToken = freenetInterface.new InsertToken(image);
                InsertContext insertContext = mock(InsertContext.class);
                when(highLevelSimpleClient.getInsertContext(anyBoolean())).thenReturn(insertContext);
@@ -178,6 +179,7 @@ public class FreenetInterfaceTest {
                byte[] imageData = new byte[] { 1, 2, 3, 4 };
                temporaryImage.setImageData(imageData);
                Image image = new ImageImpl("image-id");
+               image.modify().setSone(sone).update();
                InsertToken insertToken = freenetInterface.new InsertToken(image);
                InsertContext insertContext = mock(InsertContext.class);
                when(highLevelSimpleClient.getInsertContext(anyBoolean())).thenReturn(insertContext);
index dc19195..3fb2a34 100644 (file)
@@ -30,6 +30,7 @@ import java.util.Set;
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Album.Modifier;
 import net.pterodactylus.sone.data.Client;
+import net.pterodactylus.sone.data.IdBuilder;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
@@ -344,11 +345,11 @@ public class SoneParserTest {
 
        @Before
        public void setupAlbums() {
-               when(core.getAlbum(anyString())).thenAnswer(new Answer<Album>() {
+               when(core.getAlbum(anyString())).thenAnswer(new Answer<Optional<Album>>() {
                        @Override
-                       public Album answer(InvocationOnMock invocation)
+                       public Optional<Album> answer(InvocationOnMock invocation)
                        throws Throwable {
-                               return albums.get(invocation.getArguments()[0]);
+                               return Optional.fromNullable(albums.get(invocation.getArguments()[0]));
                        }
                });
        }
@@ -435,8 +436,8 @@ public class SoneParserTest {
                when(imageBuilder.withId(anyString())).thenAnswer(new Answer<ImageBuilder>() {
                        @Override
                        public ImageBuilder answer(InvocationOnMock invocation) {
-                               when(image.getId()).thenReturn(
-                                               (String) invocation.getArguments()[0]);
+                               when(image.getId()).thenReturn(new IdBuilder().buildId("identity", (String) invocation.getArguments()[0]));
+                               when(image.getInternalId()).thenReturn((String) invocation.getArguments()[0]);
                                return imageBuilder;
                        }
                });
@@ -818,7 +819,8 @@ public class SoneParserTest {
                assertThat(sone.getRootAlbum().getAlbums(), hasSize(1));
                assertThat(sone.getRootAlbum().getAlbums().get(0).getImages(), hasSize(1));
                Image image = sone.getRootAlbum().getAlbums().get(0).getImages().get(0);
-               assertThat(image.getId(), is("image-id"));
+               assertThat(image.getId(), is(new IdBuilder().buildId("identity", "image-id")));
+               assertThat(image.getInternalId(), is("image-id"));
                assertThat(image.getCreationTime(), is(1407197508000L));
                assertThat(image.getKey(), is("KSK@GPLv3.txt"));
                assertThat(image.getTitle(), is("image-title"));
diff --git a/src/test/java/net/pterodactylus/sone/data/IdBuilderTest.java b/src/test/java/net/pterodactylus/sone/data/IdBuilderTest.java
new file mode 100644 (file)
index 0000000..bc8da11
--- /dev/null
@@ -0,0 +1,26 @@
+package net.pterodactylus.sone.data;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link IdBuilderTest}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class IdBuilderTest {
+
+       private static final String SONE_ID = "~Yp72VX0c6FLDvgIzip5wIvaGIIrjKcKvnX~pTaMKXs";
+       private static final String ELEMENT_ID = "88CC70AE-E853-4EEE-B245-E4C55F40DDDF";
+       private static final String EXPECTED_ID = "139a629a13f6a2c4191fb19ecead7e57335ea3deb2a971b88d5e004378c4daad";
+
+       private final IdBuilder idBuilder = new IdBuilder();
+
+       @Test
+       public void idBuilderBuildsCorrectIds() {
+               assertThat(idBuilder.buildId(SONE_ID, ELEMENT_ID), is(EXPECTED_ID));
+       }
+
+}
index cc5babb..c00f25a 100644 (file)
@@ -268,7 +268,7 @@ public class MemoryDatabaseTest {
 
        @Test
        public void testBasicAlbumFunctionality() {
-               Album newAlbum = new AlbumImpl(mock(Sone.class));
+               Album newAlbum = new AlbumImpl(when(mock(Sone.class).getId()).thenReturn(SONE_ID).<Sone>getMock());
                assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));
                memoryDatabase.storeAlbum(newAlbum);
                assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(of(newAlbum)));
diff --git a/src/test/java/net/pterodactylus/sone/template/BuildIdFilterTest.java b/src/test/java/net/pterodactylus/sone/template/BuildIdFilterTest.java
new file mode 100644 (file)
index 0000000..d8d7bac
--- /dev/null
@@ -0,0 +1,51 @@
+package net.pterodactylus.sone.template;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.mockito.Mockito.when;
+
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.util.template.TemplateContext;
+
+import com.google.common.collect.ImmutableMap;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+/**
+ * Unit test for {@link BuildIdFilter}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class BuildIdFilterTest {
+
+       private static final String SONE_ID = "~Yp72VX0c6FLDvgIzip5wIvaGIIrjKcKvnX~pTaMKXs";
+       private static final String ELEMENT_ID = "88CC70AE-E853-4EEE-B245-E4C55F40DDDF";
+       private static final Object EXPECTED_ID = "139a629a13f6a2c4191fb19ecead7e57335ea3deb2a971b88d5e004378c4daad";
+
+       private final BuildIdFilter buildIdFilter = new BuildIdFilter();
+       private final TemplateContext templateContext = null;
+
+       @Test
+       public void filterBuildsCorrectIdsWithSoneAsString() {
+               assertThat(buildIdFilter.format(templateContext, ELEMENT_ID, ImmutableMap.<String, Object>of("sone", SONE_ID)), is(EXPECTED_ID));
+       }
+
+       @Test
+       public void filterBuildsCorrectIdsWithSoneAsSone() {
+               Sone sone = Mockito.mock(Sone.class);
+               when(sone.getId()).thenReturn(SONE_ID);
+               assertThat(buildIdFilter.format(templateContext, ELEMENT_ID, ImmutableMap.<String, Object>of("sone", sone)), is(EXPECTED_ID));
+       }
+
+       @Test
+       public void filterReturnsNullIfSoneNotPresent() {
+               assertThat(buildIdFilter.format(templateContext, ELEMENT_ID, ImmutableMap.<String, Object>of()), nullValue());
+       }
+
+       @Test
+       public void filterReturnsNullIfElementNotPresent() {
+               assertThat(buildIdFilter.format(templateContext, null, ImmutableMap.<String, Object>of("sone", SONE_ID)), nullValue());
+       }
+
+}
diff --git a/src/test/java/net/pterodactylus/sone/template/ParserFilterTest.java b/src/test/java/net/pterodactylus/sone/template/ParserFilterTest.java
new file mode 100644 (file)
index 0000000..268a3fa
--- /dev/null
@@ -0,0 +1,83 @@
+package net.pterodactylus.sone.template;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.empty;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.text.Part;
+import net.pterodactylus.sone.text.PlainTextPart;
+import net.pterodactylus.sone.text.SoneTextParser;
+import net.pterodactylus.sone.text.SoneTextParserContext;
+import net.pterodactylus.util.template.TemplateContext;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableMap;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+
+/**
+ * Unit test for {@link ParserFilter}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ParserFilterTest {
+
+       private final Core core = mock(Core.class);
+       private final SoneTextParser soneTextParser = mock(SoneTextParser.class);
+       private final ParserFilter parserFilter = new ParserFilter(core, soneTextParser);
+       private final TemplateContext templateContext = new TemplateContext();
+       private final Sone sone = mock(Sone.class);
+
+       @Test
+       public void filterReturnsPartsReturnedByParser() throws IOException {
+               List<Part> parts = setupSoneTextParser();
+               assertThat(parserFilter.format(templateContext, "Text", Collections.<String, Object>emptyMap()), is((Object) parts));
+       }
+
+       private List<Part> setupSoneTextParser() throws IOException {
+               List<Part> parts = Arrays.<Part>asList(new PlainTextPart("Text"));
+               when(soneTextParser.parse(any(SoneTextParserContext.class), any(StringReader.class))).thenReturn(parts);
+               return parts;
+       }
+
+       @Test
+       public void filterUsesGivenSone() throws IOException {
+               List<Part> parts = setupSoneTextParser();
+               assertThat(parserFilter.format(templateContext, "Text", ImmutableMap.<String, Object>of("sone", sone)), is((Object) parts));
+               verifyThatContextContainsCorrectSone();
+       }
+
+       @Test
+       public void filterGetsCorrectSoneFromCore() throws IOException {
+               when(core.getSone("sone-id")).thenReturn(Optional.of(sone));
+               List<Part> parts = setupSoneTextParser();
+               assertThat(parserFilter.format(templateContext, "Text", ImmutableMap.<String, Object>of("sone", "sone-id")), is((Object) parts));
+               verifyThatContextContainsCorrectSone();
+       }
+
+       private void verifyThatContextContainsCorrectSone() throws IOException {
+               ArgumentCaptor<SoneTextParserContext> contextArgumentCaptor = ArgumentCaptor.forClass(SoneTextParserContext.class);
+               verify(soneTextParser).parse(contextArgumentCaptor.capture(), any(StringReader.class));
+               assertThat(contextArgumentCaptor.getValue().getPostingSone(), is(sone));
+       }
+
+       @Test
+       public void filterReturnsEmptyCollectionOnExceptionInParser() throws IOException {
+               when(soneTextParser.parse(any(SoneTextParserContext.class), any(StringReader.class))).thenThrow(IOException.class);
+               assertThat((Collection<Part>) parserFilter.format(templateContext, "Text", Collections.<String, Object>emptyMap()), empty());
+       }
+
+}
diff --git a/src/test/java/net/pterodactylus/sone/template/RenderFilterTest.java b/src/test/java/net/pterodactylus/sone/template/RenderFilterTest.java
new file mode 100644 (file)
index 0000000..a600cdd
--- /dev/null
@@ -0,0 +1,172 @@
+package net.pterodactylus.sone.template;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Arrays;
+import java.util.Collections;
+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.Post;
+import net.pterodactylus.sone.data.Profile;
+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;
+import net.pterodactylus.sone.text.PlainTextPart;
+import net.pterodactylus.sone.text.PostPart;
+import net.pterodactylus.sone.text.SonePart;
+import net.pterodactylus.util.template.HtmlFilter;
+import net.pterodactylus.util.template.TemplateContext;
+import net.pterodactylus.util.template.TemplateContextFactory;
+
+import com.google.common.collect.ImmutableMap;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link RenderFilter}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class RenderFilterTest {
+
+       private static final Map<String, Object> EMPTY_MAP = Collections.emptyMap();
+       private final Core core = mock(Core.class);
+       private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
+       private final RenderFilter renderFilter = new RenderFilter(core, templateContextFactory);
+       private final TemplateContext templateContext = new TemplateContext();
+       private final Sone sone = mock(Sone.class);
+       private final Post post = mock(Post.class);
+
+       @Before
+       public void setupTemplateContextFactory() {
+               templateContextFactory.addFilter("html", new HtmlFilter());
+       }
+
+       @Before
+       public void setupSone() {
+               when(sone.getId()).thenReturn("sone-id");
+               when(sone.getName()).thenReturn("SoneName");
+               when(sone.getProfile()).thenReturn(new Profile(sone));
+       }
+
+       @Before
+       public void setupPost() {
+               when(post.getId()).thenReturn("post-id");
+               when(post.getSone()).thenReturn(sone);
+       }
+
+       @Test
+       public void filterCanRenderPlainText() {
+               List<Part> parts = Arrays.<Part>asList(new PlainTextPart("<Text>"));
+               assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP), is((Object) "&lt;Text&gt;"));
+       }
+
+       @Test
+       public void filterCanRenderMultiplePlainTextParts() {
+               List<Part> parts = Arrays.<Part>asList(new PlainTextPart("<Text>"), new PlainTextPart("<Foo>"));
+               assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP), is((Object) "&lt;Text&gt;&lt;Foo&gt;"));
+       }
+
+       @Test
+       public void filterCanRenderUntrustedFreenetLinks() {
+               List<Part> parts = Arrays.<Part>asList(new FreenetLinkPart("SSK@foo,bar/baz", "foo/baz", false));
+               assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+                               is((Object) "<a class=\"freenet\" href=\"/SSK@foo,bar/baz\" title=\"foo/baz\">foo/baz</a>"));
+       }
+
+       @Test
+       public void filterCanRenderTrustedFreenetLinks() {
+               List<Part> parts = Arrays.<Part>asList(new FreenetLinkPart("SSK@foo,bar/baz", "foo/baz", true));
+               assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+                               is((Object) "<a class=\"freenet-trusted\" href=\"/SSK@foo,bar/baz\" title=\"foo/baz\">foo/baz</a>"));
+       }
+
+       @Test
+       public void filterCanRenderInternetLinks() {
+               List<Part> parts = Arrays.<Part>asList(new LinkPart("http://link.sone", "link.sone"));
+               assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+                               is((Object) "<a class=\"internet\" href=\"/external-link/?_CHECKED_HTTP_=http%3A%2F%2Flink.sone\" title=\"link.sone\">link.sone</a>"));
+       }
+
+       @Test
+       public void filterCanRenderSonePartsForUnknownSones() {
+               Sone unknownSone = mock(Sone.class);
+               when(unknownSone.getId()).thenReturn("sone-id");
+               List<Part> parts = Arrays.<Part>asList(new SonePart(unknownSone));
+               assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+                               is((Object) "<a class=\"in-sone\" href=\"/WebOfTrust/ShowIdentity?id=sone-id\" title=\"sone-id\">sone-id</a>"));
+       }
+
+       @Test
+       public void filterCanRenderSonePartsForKnownSones() {
+               List<Part> parts = Arrays.<Part>asList(new SonePart(sone));
+               assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+                               is((Object) "<a class=\"in-sone\" href=\"viewSone.html?sone=sone-id\" title=\"SoneName\">SoneName</a>"));
+       }
+
+       @Test
+       public void filterCanRenderPostParts() {
+               when(post.getText()).thenReturn("123456789012345678901234567890");
+               List<Part> parts = Arrays.<Part>asList(new PostPart(post));
+               assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+                               is((Object) "<a class=\"in-sone\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345678901234567890&hellip;</a>"));
+       }
+
+       @Test
+       public void filterCanRenderPostPartsWithoutBreakingWords() {
+               when(post.getText()).thenReturn("12345 12345 12345 12345 12345 12345");
+               List<Part> parts = Arrays.<Part>asList(new PostPart(post));
+               assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+                               is((Object) "<a class=\"in-sone\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345 12345 12345&hellip;</a>"));
+       }
+
+       @Test
+       public void filterCanRenderPostPartsWithShortText() {
+               when(post.getText()).thenReturn("12345 12345");
+               List<Part> parts = Arrays.<Part>asList(new PostPart(post));
+               assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+                               is((Object) "<a class=\"in-sone\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345 12345</a>"));
+       }
+
+       @Test
+       public void filterCanRenderPostPartsWithOldPostIds() {
+               when(post.getText()).thenReturn("12345 12345");
+               List<Part> parts = Arrays.<Part>asList(new PostPart(post, true));
+               assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+                               is((Object) "<a class=\"internet\" href=\"viewPost.html?post=post-id\" title=\"SoneName\">12345 12345</a>"));
+       }
+
+       @Test
+       public void filterCanRenderAlbumParts() {
+               Album album = mock(Album.class);
+               when(album.getId()).thenReturn("album-id");
+               when(album.getTitle()).thenReturn("Title");
+               when(album.getDescription()).thenReturn("Description");
+               List<Part> parts = Arrays.<Part>asList(new AlbumPart(album));
+               assertThat(renderFilter.format(templateContext, parts, EMPTY_MAP),
+                               is((Object) "<a class=\"in-sone\" href=\"imageBrowser.html?album=album-id\" title=\"Description\">Title</a>"));
+       }
+
+       @Test
+       public void filterHonorsLength() {
+               List<Part> parts = Arrays.<Part>asList(new PlainTextPart("12345678901234567890"));
+               assertThat(renderFilter.format(templateContext, parts, ImmutableMap.<String, Object>of("length", "10")),
+                               is((Object) "1234567890&hellip;"));
+       }
+
+       @Test
+       public void filterHonorsCutOffLength() {
+               List<Part> parts = Arrays.<Part>asList(new PlainTextPart("12345678901234567890"));
+               assertThat(renderFilter.format(templateContext, parts, ImmutableMap.<String, Object>of("length", "10", "cut-off-length", "5")),
+                               is((Object) "12345&hellip;"));
+       }
+
+}
index 2ac6db7..ef98e36 100644 (file)
 
 package net.pterodactylus.sone.text;
 
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
 import java.io.IOException;
 import java.io.StringReader;
 import java.util.Arrays;
 import java.util.Collection;
+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;
 
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
-import junit.framework.TestCase;
+import org.junit.Test;
 
 /**
  * JUnit test case for {@link SoneTextParser}.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public class SoneTextParserTest extends TestCase {
+public class SoneTextParserTest {
 
-       //
-       // ACTIONS
-       //
+       private final SoneProvider soneProvider = new TestSoneProvider();
+       private final TestPostProvider postProvider = new TestPostProvider();
+       private final TestAlbumProvider albumProvider = new TestAlbumProvider();
+       private final SoneTextParser soneTextParser = new SoneTextParser(soneProvider, postProvider, albumProvider);
 
        /**
         * Tests basic plain-text operation of the parser.
@@ -47,25 +61,24 @@ public class SoneTextParserTest extends TestCase {
         * @throws IOException
         *             if an I/O error occurs
         */
-       @SuppressWarnings("static-method")
+       @Test
        public void testPlainText() throws IOException {
-               SoneTextParser soneTextParser = new SoneTextParser(null, null);
                Iterable<Part> parts;
 
                /* check basic operation. */
                parts = soneTextParser.parse(null, new StringReader("Test."));
-               assertNotNull("Parts", parts);
-               assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
+               assertThat(parts, notNullValue());
+               assertThat(convertText(parts, PlainTextPart.class), is("Test."));
 
                /* check empty lines at start and end. */
                parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n"));
-               assertNotNull("Parts", parts);
-               assertEquals("Part Text", "Test.", convertText(parts, PlainTextPart.class));
+               assertThat(parts, notNullValue());
+               assertThat(convertText(parts, PlainTextPart.class), is("Test."));
 
                /* check duplicate empty lines in the text. */
                parts = soneTextParser.parse(null, new StringReader("\nTest.\n\n\nTest."));
-               assertNotNull("Parts", parts);
-               assertEquals("Part Text", "Test.\n\nTest.", convertText(parts, PlainTextPart.class));
+               assertThat(parts, notNullValue());
+               assertThat(convertText(parts, PlainTextPart.class), is("Test.\n\nTest."));
        }
 
        /**
@@ -74,25 +87,25 @@ public class SoneTextParserTest extends TestCase {
         * @throws IOException
         *             if an I/O error occurs
         */
-       @SuppressWarnings("static-method")
+       @Test
        public void testKSKLinks() throws IOException {
-               SoneTextParser soneTextParser = new SoneTextParser(null, null);
                Iterable<Part> parts;
 
                /* check basic links. */
                parts = soneTextParser.parse(null, new StringReader("KSK@gpl.txt"));
-               assertNotNull("Parts", parts);
-               assertEquals("Part Text", "[KSK@gpl.txt|gpl.txt|gpl.txt]", convertText(parts, FreenetLinkPart.class));
+               assertThat(parts, notNullValue());
+               assertThat(convertText(parts, FreenetLinkPart.class), is("[KSK@gpl.txt|gpl.txt|gpl.txt]"));
 
                /* check embedded links. */
                parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\u200b."));
-               assertNotNull("Parts", parts);
-               assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b.", convertText(parts, PlainTextPart.class, FreenetLinkPart.class));
+               assertThat(parts, notNullValue());
+               assertThat(convertText(parts, PlainTextPart.class, FreenetLinkPart.class), is("Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\u200b."));
 
                /* check embedded links and line breaks. */
                parts = soneTextParser.parse(null, new StringReader("Link is KSK@gpl.txt\nKSK@test.dat\n"));
-               assertNotNull("Parts", parts);
-               assertEquals("Part Text", "Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\n[KSK@test.dat|test.dat|test.dat]", convertText(parts, PlainTextPart.class, FreenetLinkPart.class));
+               assertThat(parts, notNullValue());
+               assertThat(convertText(parts, PlainTextPart.class, FreenetLinkPart.class),
+                       is("Link is [KSK@gpl.txt|gpl.txt|gpl.txt]\n[KSK@test.dat|test.dat|test.dat]"));
        }
 
        /**
@@ -101,15 +114,15 @@ public class SoneTextParserTest extends TestCase {
         * @throws IOException
         *             if an I/O error occurs
         */
-       @SuppressWarnings({ "synthetic-access", "static-method" })
+       @Test
        public void testEmptyLinesAndSoneLinks() throws IOException {
-               SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
                Iterable<Part> parts;
 
                /* check basic links. */
                parts = soneTextParser.parse(null, new StringReader("Some text.\n\nLink to sone://DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU and stuff."));
-               assertNotNull("Parts", parts);
-               assertEquals("Part Text", "Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff.", convertText(parts, PlainTextPart.class, SonePart.class));
+               assertThat(parts, notNullValue());
+               assertThat(convertText(parts, PlainTextPart.class, SonePart.class),
+                       is("Some text.\n\nLink to [Sone|DAxKQzS48mtaQc7sUVHIgx3fnWZPQBz0EueBreUVWrU] and stuff."));
        }
 
        /**
@@ -119,15 +132,35 @@ public class SoneTextParserTest extends TestCase {
         * @throws IOException
         *             if an I/O error occurs
         */
-       @SuppressWarnings({ "synthetic-access", "static-method" })
+       @Test
        public void testEmpyHttpLinks() throws IOException {
-               SoneTextParser soneTextParser = new SoneTextParser(new TestSoneProvider(), null);
                Iterable<Part> parts;
 
                /* check empty http links. */
                parts = soneTextParser.parse(null, new StringReader("Some text. Empty link: http:// – nice!"));
-               assertNotNull("Parts", parts);
-               assertEquals("Part Text", "Some text. Empty link: http:// – nice!", convertText(parts, PlainTextPart.class));
+               assertThat(parts, notNullValue());
+               assertThat(convertText(parts, PlainTextPart.class), is("Some text. Empty link: http:// – nice!"));
+       }
+
+       @Test
+       public void linksToPostAreParsedCorrectly() throws IOException {
+               postProvider.addValidPostId("foo", "internal", "Post about foo...");
+               Iterable<Part> parts = soneTextParser.parse(null, new StringReader("This post://foo is awesome."));
+               assertThat(convertText(parts, PlainTextPart.class, PostPart.class), is("This [post|new|foo|Post about foo...] is awesome."));
+       }
+
+       @Test
+       public void linksToPostsWithOldIdsAreParsedCorrectly() throws IOException {
+               postProvider.addValidPostId("foo", "internal", "Post about foo...");
+               Iterable<Part> parts = soneTextParser.parse(null, new StringReader("This post://internal is awesome."));
+               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!"));
        }
 
        //
@@ -148,7 +181,7 @@ public class SoneTextParserTest extends TestCase {
        private static String convertText(Iterable<Part> parts, Class<?>... validClasses) {
                StringBuilder text = new StringBuilder();
                for (Part part : parts) {
-                       assertNotNull("Part", part);
+                       assertThat(part, notNullValue());
                        boolean classValid = validClasses.length == 0;
                        for (Class<?> validClass : validClasses) {
                                if (validClass.isAssignableFrom(part.getClass())) {
@@ -156,20 +189,43 @@ public class SoneTextParserTest extends TestCase {
                                        break;
                                }
                        }
-                       if (!classValid) {
-                               fail("Part’s Class (" + part.getClass() + ") is not one of " + Arrays.toString(validClasses));
-                       }
+                       assertThat("Part’s Class (" + part.getClass() + ") is not one of " + Arrays.toString(validClasses), classValid, is(true));
                        if (part instanceof PlainTextPart) {
-                               text.append(((PlainTextPart) part).getText());
+                               text.append(part.getText());
                        } else if (part instanceof FreenetLinkPart) {
                                FreenetLinkPart freenetLinkPart = (FreenetLinkPart) part;
-                               text.append('[').append(freenetLinkPart.getLink()).append('|').append(freenetLinkPart.isTrusted() ? "trusted|" : "").append(freenetLinkPart.getTitle()).append('|').append(freenetLinkPart.getText()).append(']');
+                               text.append('[')
+                                       .append(freenetLinkPart.getLink())
+                                       .append('|')
+                                       .append(freenetLinkPart.isTrusted() ? "trusted|" : "")
+                                       .append(freenetLinkPart.getTitle())
+                                       .append('|')
+                                       .append(freenetLinkPart.getText())
+                                       .append(']');
                        } else if (part instanceof LinkPart) {
                                LinkPart linkPart = (LinkPart) part;
-                               text.append('[').append(linkPart.getLink()).append('|').append(linkPart.getTitle()).append('|').append(linkPart.getText()).append(']');
+                               text.append('[')
+                                       .append(linkPart.getLink())
+                                       .append('|')
+                                       .append(linkPart.getTitle())
+                                       .append('|')
+                                       .append(linkPart.getText())
+                                       .append(']');
                        } else if (part instanceof SonePart) {
                                SonePart sonePart = (SonePart) part;
                                text.append("[Sone|").append(sonePart.getSone().getId()).append(']');
+                       } else if (part instanceof PostPart) {
+                               PostPart postPart = (PostPart) part;
+                               text.append("[post|")
+                                       .append(postPart.usesDeprecatedLink() ? "old" : "new")
+                                       .append('|')
+                                       .append(postPart.getPost().getId())
+                                       .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();
@@ -226,4 +282,67 @@ public class SoneTextParserTest extends TestCase {
 
        }
 
+       private static class TestPostProvider implements PostProvider {
+
+               private final Map<String, String> postTexts = new HashMap<String, String>();
+               private final Map<String, String> postInternalIds = new HashMap<String, String>();
+               private final Map<String, String> internalIdPosts = new HashMap<String, String>();
+
+               private void addValidPostId(String validPostId, String internalId, String text) {
+                       postTexts.put(validPostId, text);
+                       postInternalIds.put(validPostId, internalId);
+                       internalIdPosts.put(internalId, validPostId);
+               }
+
+               @Override
+               public Collection<Post> getDirectedPosts(String recipientId) {
+                       return Collections.emptyList();
+               }
+
+               @Override
+               public Collection<Post> getPosts(String soneId) {
+                       return Collections.emptyList();
+               }
+
+               @Override
+               public Optional<Post> getPost(String postId) {
+                       if (postTexts.containsKey(postId)) {
+                               Post post = mock(Post.class);
+                               when(post.getId()).thenReturn(postId);
+                               when(post.getInternalId()).thenReturn(postInternalIds.get(postId));
+                               when(post.getText()).thenReturn(postTexts.get(postId));
+                               return Optional.of(post);
+                       } else if (internalIdPosts.containsKey(postId)) {
+                               Post post = mock(Post.class);
+                               when(post.getId()).thenReturn(internalIdPosts.get(postId));
+                               when(post.getInternalId()).thenReturn(postId);
+                               when(post.getText()).thenReturn(postTexts.get(internalIdPosts.get(postId)));
+                               return Optional.of(post);
+                       }
+                       return Optional.absent();
+               }
+
+       }
+
+       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();
+               }
+
+       }
+
 }