Use compatibility mode when getting posts
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 24 Jul 2015 14:47:58 +0000 (16:47 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 24 Jul 2015 18:48:00 +0000 (20:48 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/data/Sone.java

index 90fc314..c1d51a3 100644 (file)
@@ -99,6 +99,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;
@@ -435,8 +436,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();
        }
 
        /**
index 4d0905f..7acdb7f 100644 (file)
@@ -189,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.
         *