Refactoring.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 4 Nov 2013 05:25:10 +0000 (06:25 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:51 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/fcp/GetPostFeedCommand.java

index 60f6e41..970e301 100644 (file)
 
 package net.pterodactylus.sone.fcp;
 
-import java.util.ArrayList;
+import static com.google.common.base.Optional.presentInstances;
+import static com.google.common.collect.FluentIterable.from;
+import static net.pterodactylus.sone.data.Sone.TO_POSTS;
+
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
@@ -31,9 +34,6 @@ import net.pterodactylus.sone.freenet.fcp.FcpException;
 import freenet.support.SimpleFieldSet;
 import freenet.support.api.Bucket;
 
-import com.google.common.base.Optional;
-import com.google.common.collect.Collections2;
-
 /**
  * Implementation of an FCP interface for other clients or plugins to
  * communicate with Sone.
@@ -58,26 +58,21 @@ public class GetPostFeedCommand extends AbstractSoneCommand {
                int startPost = getInt(parameters, "StartPost", 0);
                int maxPosts = getInt(parameters, "MaxPosts", -1);
 
-               Collection<Post> allPosts = new HashSet<Post>();
-               allPosts.addAll(sone.getPosts());
-               for (String friendSoneId : sone.getFriends()) {
-                       Optional<Sone> friendSone = getCore().getSone(friendSoneId);
-                       if (!friendSone.isPresent()) {
-                               continue;
-                       }
-                       allPosts.addAll(friendSone.get().getPosts());
-               }
-               allPosts.addAll(getCore().getDatabase().getDirectedPosts(sone.getId()));
-               allPosts = Collections2.filter(allPosts, Post.FUTURE_POSTS_FILTER);
-
-               List<Post> sortedPosts = new ArrayList<Post>(allPosts);
-               Collections.sort(sortedPosts, Post.TIME_COMPARATOR);
+               List<Post> sortedPosts = from(collectAllPostsForSone(sone)).filter(Post.FUTURE_POSTS_FILTER).toSortedList(Post.TIME_COMPARATOR);
 
                if (sortedPosts.size() < startPost) {
-                       return new Response("PostFeed", encodePosts(Collections.<Post> emptyList(), "Posts."));
+                       return new Response("PostFeed", encodePosts(Collections.<Post>emptyList(), "Posts."));
                }
 
                return new Response("PostFeed", encodePostsWithReplies(sortedPosts.subList(startPost, (maxPosts == -1) ? sortedPosts.size() : Math.min(startPost + maxPosts, sortedPosts.size())), "Posts."));
        }
 
+       private Collection<Post> collectAllPostsForSone(Sone sone) {
+               Collection<Post> allPosts = new HashSet<Post>();
+               allPosts.addAll(sone.getPosts());
+               allPosts.addAll(from(presentInstances(from(sone.getFriends()).transform(getCore().getDatabase().getSone()))).transformAndConcat(TO_POSTS).toList());
+               allPosts.addAll(getCore().getDatabase().getDirectedPosts(sone.getId()));
+               return allPosts;
+       }
+
 }