Filter posts and replies from the future.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 7 Apr 2011 11:39:02 +0000 (13:39 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 7 Apr 2011 11:39:02 +0000 (13:39 +0200)
This fixes #122.

src/main/java/net/pterodactylus/sone/data/Post.java
src/main/java/net/pterodactylus/sone/data/Reply.java
src/main/java/net/pterodactylus/sone/template/PostAccessor.java
src/main/java/net/pterodactylus/sone/web/IndexPage.java
src/main/java/net/pterodactylus/sone/web/SearchPage.java

index 964a054..bb431d0 100644 (file)
@@ -20,6 +20,8 @@ package net.pterodactylus.sone.data;
 import java.util.Comparator;
 import java.util.UUID;
 
+import net.pterodactylus.util.filter.Filter;
+
 /**
  * A post is a short message that a user writes in his Sone to let other users
  * know what is going on.
@@ -38,6 +40,16 @@ public class Post {
 
        };
 
+       /** Filter for posts with timestamps from the future. */
+       public static final Filter<Post> FUTURE_POSTS_FILTER = new Filter<Post>() {
+
+               @Override
+               public boolean filterObject(Post post) {
+                       return post.getTime() <= System.currentTimeMillis();
+               }
+
+       };
+
        /** The GUID of the post. */
        private final UUID id;
 
index a106391..2dacfbe 100644 (file)
@@ -20,6 +20,8 @@ package net.pterodactylus.sone.data;
 import java.util.Comparator;
 import java.util.UUID;
 
+import net.pterodactylus.util.filter.Filter;
+
 /**
  * A reply is like a {@link Post} but can never be posted on its own, it always
  * refers to another {@link Post}.
@@ -38,6 +40,16 @@ public class Reply {
 
        };
 
+       /** Filter for replies with timestamps from the future. */
+       public static final Filter<Reply> FUTURE_REPLIES_FILTER = new Filter<Reply>() {
+
+               @Override
+               public boolean filterObject(Reply reply) {
+                       return reply.getTime() <= System.currentTimeMillis();
+               }
+
+       };
+
        /** The ID of the reply. */
        private final UUID id;
 
index ca0c84a..99d6845 100644 (file)
@@ -19,7 +19,9 @@ package net.pterodactylus.sone.template;
 
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.util.filter.Filters;
 import net.pterodactylus.util.template.ReflectionAccessor;
 import net.pterodactylus.util.template.TemplateContext;
 
@@ -54,7 +56,7 @@ public class PostAccessor extends ReflectionAccessor {
        public Object get(TemplateContext templateContext, Object object, String member) {
                Post post = (Post) object;
                if ("replies".equals(member)) {
-                       return core.getReplies(post);
+                       return Filters.filteredList(core.getReplies(post), Reply.FUTURE_REPLIES_FILTER);
                } else if (member.equals("likes")) {
                        return core.getLikes(post);
                } else if (member.equals("liked")) {
index db48314..5f4a4a4 100644 (file)
@@ -25,6 +25,7 @@ import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.util.collection.Pagination;
+import net.pterodactylus.util.filter.Filters;
 import net.pterodactylus.util.number.Numbers;
 import net.pterodactylus.util.template.Template;
 import net.pterodactylus.util.template.TemplateContext;
@@ -73,6 +74,7 @@ public class IndexPage extends SoneTemplatePage {
                                }
                        }
                }
+               allPosts = Filters.filteredList(allPosts, Post.FUTURE_POSTS_FILTER);
                Collections.sort(allPosts, Post.TIME_COMPARATOR);
                Pagination<Post> pagination = new Pagination<Post>(allPosts, 25).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0));
                templateContext.set("pagination", pagination);
index 73673ab..cf79b4d 100644 (file)
@@ -86,7 +86,7 @@ public class SearchPage extends SoneTemplatePage {
                        posts.addAll(sone.getPosts());
                }
                @SuppressWarnings("synthetic-access")
-               Set<Hit<Post>> postHits = getHits(posts, phrases, new PostStringGenerator());
+               Set<Hit<Post>> postHits = getHits(Filters.filteredSet(posts, Post.FUTURE_POSTS_FILTER), phrases, new PostStringGenerator());
 
                /* now filter. */
                soneHits = Filters.filteredSet(soneHits, Hit.POSITIVE_FILTER);
@@ -329,7 +329,7 @@ public class SearchPage extends SoneTemplatePage {
                        if (post.getRecipient() != null) {
                                postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient()));
                        }
-                       for (Reply reply : webInterface.getCore().getReplies(post)) {
+                       for (Reply reply : Filters.filteredList(webInterface.getCore().getReplies(post), Reply.FUTURE_REPLIES_FILTER)) {
                                postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone()));
                                postString.append(' ').append(reply.getText());
                        }