Make post returned by post reply optional.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Jan 2013 06:42:59 +0000 (07:42 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 24 Jan 2013 06:42:59 +0000 (07:42 +0100)
src/main/java/net/pterodactylus/sone/data/PostReply.java
src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java
src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java
src/main/java/net/pterodactylus/sone/template/ReplyGroupFilter.java
src/main/java/net/pterodactylus/sone/web/NewPage.java
src/main/java/net/pterodactylus/sone/web/ViewSonePage.java
src/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java

index 8dc6f93..36921dd 100644 (file)
@@ -37,7 +37,7 @@ public interface PostReply extends Reply<PostReply> {
         *
         * @return The post this reply refers to
         */
-       public Post getPost();
+       public Optional<Post> getPost();
 
        /**
         * Sets the post this reply refers to.
index fa6629e..fdedaa0 100644 (file)
@@ -21,6 +21,8 @@ import net.pterodactylus.sone.core.PostProvider;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 
+import com.google.common.base.Optional;
+
 /**
  * Simple {@link PostReply} implementation.
  *
@@ -64,8 +66,8 @@ public class PostReplyImpl extends ReplyImpl<PostReply> implements PostReply {
         * {@inheritDoc}
         */
        @Override
-       public Post getPost() {
-               return postProvider.getPost(postId).get();
+       public Optional<Post> getPost() {
+               return postProvider.getPost(postId);
        }
 
        /**
index 1881a1b..dbf09a3 100644 (file)
@@ -31,6 +31,8 @@ import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 import net.pterodactylus.sone.freenet.wot.Trust;
 import net.pterodactylus.util.notify.Notification;
 
+import com.google.common.base.Optional;
+
 /**
  * Filter for {@link ListNotification}s.
  *
@@ -282,11 +284,11 @@ public class ListNotificationFilters {
         */
        public static boolean isReplyVisible(Sone sone, PostReply reply) {
                checkNotNull(reply, "reply must not be null");
-               Post post = reply.getPost();
-               if (post == null) {
+               Optional<Post> post = reply.getPost();
+               if (!post.isPresent()) {
                        return false;
                }
-               if (!isPostVisible(sone, post)) {
+               if (!isPostVisible(sone, post.get())) {
                        return false;
                }
                if (reply.getTime() > System.currentTimeMillis()) {
index 4806cee..bcf73d8 100644 (file)
@@ -29,6 +29,8 @@ import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.util.template.Filter;
 import net.pterodactylus.util.template.TemplateContext;
 
+import com.google.common.base.Optional;
+
 /**
  * {@link Filter} implementation that groups replies by the post the are in
  * reply to, returning a map with the post as key and the list of replies as
@@ -48,17 +50,21 @@ public class ReplyGroupFilter implements Filter {
                Map<Post, Set<Sone>> postSones = new HashMap<Post, Set<Sone>>();
                Map<Post, Set<PostReply>> postReplies = new HashMap<Post, Set<PostReply>>();
                for (PostReply reply : allReplies) {
-                       Post post = reply.getPost();
-                       Set<Sone> sones = postSones.get(post);
+                       /*
+                        * All replies from a new-reply notification have posts,
+                        * ListNotificationFilters takes care of that.
+                        */
+                       Optional<Post> post = reply.getPost();
+                       Set<Sone> sones = postSones.get(post.get());
                        if (sones == null) {
                                sones = new HashSet<Sone>();
-                               postSones.put(post, sones);
+                               postSones.put(post.get(), sones);
                        }
                        sones.add(reply.getSone());
-                       Set<PostReply> replies = postReplies.get(post);
+                       Set<PostReply> replies = postReplies.get(post.get());
                        if (replies == null) {
                                replies = new HashSet<PostReply>();
-                               postReplies.put(post, replies);
+                               postReplies.put(post.get(), replies);
                        }
                        replies.add(reply);
                }
index 2d4ec55..22d27c8 100644 (file)
@@ -67,7 +67,7 @@ public class NewPage extends SoneTemplatePage {
                /* collect new elements from notifications. */
                Set<Post> posts = new HashSet<Post>(webInterface.getNewPosts());
                for (PostReply reply : webInterface.getNewReplies()) {
-                       posts.add(reply.getPost());
+                       posts.add(reply.getPost().get());
                }
 
                /* filter and sort them. */
index dec4fe2..5572bca 100644 (file)
@@ -36,6 +36,8 @@ import net.pterodactylus.util.number.Numbers;
 import net.pterodactylus.util.template.Template;
 import net.pterodactylus.util.template.TemplateContext;
 
+import com.google.common.base.Optional;
+
 /**
  * Lets the user browser another Sone.
  *
@@ -95,11 +97,11 @@ public class ViewSonePage extends SoneTemplatePage {
                Set<PostReply> replies = sone.getReplies();
                final Map<Post, List<PostReply>> repliedPosts = new HashMap<Post, List<PostReply>>();
                for (PostReply reply : replies) {
-                       Post post = reply.getPost();
-                       if (repliedPosts.containsKey(post) || sone.equals(post.getSone()) || (sone.equals(post.getRecipient()))) {
+                       Optional<Post> post = reply.getPost();
+                       if (!post.isPresent() || repliedPosts.containsKey(post.get()) || sone.equals(post.get().getSone()) || (sone.equals(post.get().getRecipient()))) {
                                continue;
                        }
-                       repliedPosts.put(post, webInterface.getCore().getReplies(post));
+                       repliedPosts.put(post.get(), webInterface.getCore().getReplies(post.get()));
                }
                List<Post> posts = new ArrayList<Post>(repliedPosts.keySet());
                Collections.sort(posts, new Comparator<Post>() {
index d9a9dfb..3bb11d5 100644 (file)
@@ -857,8 +857,8 @@ public class WebInterface {
                }
                if (!hasFirstStartNotification()) {
                        notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
-                       if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && (reply.getPost().getSone() != null) && (reply.getTime() <= System.currentTimeMillis())) {
-                               mentionNotification.add(reply.getPost());
+                       if (!getMentionedSones(reply.getText()).isEmpty() && !isLocal && reply.getPost().isPresent() && (reply.getTime() <= System.currentTimeMillis())) {
+                               mentionNotification.add(reply.getPost().get());
                                notificationManager.addNotification(mentionNotification);
                        }
                } else {
@@ -900,7 +900,7 @@ public class WebInterface {
        public void markReplyKnown(MarkPostReplyKnownEvent markPostReplyKnownEvent) {
                newReplyNotification.remove(markPostReplyKnownEvent.postReply());
                localReplyNotification.remove(markPostReplyKnownEvent.postReply());
-               mentionNotification.remove(markPostReplyKnownEvent.postReply().getPost());
+               mentionNotification.remove(markPostReplyKnownEvent.postReply().getPost().get());
        }
 
        /**
@@ -940,11 +940,11 @@ public class WebInterface {
                localReplyNotification.remove(reply);
                if (!getMentionedSones(reply.getText()).isEmpty()) {
                        boolean isMentioned = false;
-                       for (PostReply existingReply : getCore().getReplies(reply.getPost())) {
+                       for (PostReply existingReply : getCore().getReplies(reply.getPost().get())) {
                                isMentioned |= !reply.isKnown() && !getMentionedSones(existingReply.getText()).isEmpty();
                        }
                        if (!isMentioned) {
-                               mentionNotification.remove(reply.getPost());
+                               mentionNotification.remove(reply.getPost().get());
                        }
                }
        }
index 9c4c76f..e69aa12 100644 (file)
@@ -135,7 +135,7 @@ public class GetStatusAjaxPage extends JsonPage {
                        jsonReply.put("id", reply.getId());
                        jsonReply.put("sone", reply.getSone().getId());
                        jsonReply.put("post", reply.getPostId());
-                       jsonReply.put("postSone", reply.getPost().getSone().getId());
+                       jsonReply.put("postSone", reply.getPost().get().getSone().getId());
                        jsonReplies.add(jsonReply);
                }
                return createSuccessJsonObject().put("loggedIn", currentSone != null).put("options", createJsonOptions(currentSone)).put("sones", jsonSones).put("notificationHash", notifications.hashCode()).put("newPosts", jsonPosts).put("newReplies", jsonReplies);