From: David ‘Bombe’ Roden Date: Thu, 24 Jan 2013 06:42:59 +0000 (+0100) Subject: Make post returned by post reply optional. X-Git-Tag: 0.8.5^2~3^2~58^2~1 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=9e8b1de83b5791778788406edbba9ed6d256a63f;p=Sone.git Make post returned by post reply optional. --- diff --git a/src/main/java/net/pterodactylus/sone/data/PostReply.java b/src/main/java/net/pterodactylus/sone/data/PostReply.java index 8dc6f93..36921dd 100644 --- a/src/main/java/net/pterodactylus/sone/data/PostReply.java +++ b/src/main/java/net/pterodactylus/sone/data/PostReply.java @@ -37,7 +37,7 @@ public interface PostReply extends Reply { * * @return The post this reply refers to */ - public Post getPost(); + public Optional getPost(); /** * Sets the post this reply refers to. diff --git a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java index fa6629e..fdedaa0 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/PostReplyImpl.java @@ -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 implements PostReply { * {@inheritDoc} */ @Override - public Post getPost() { - return postProvider.getPost(postId).get(); + public Optional getPost() { + return postProvider.getPost(postId); } /** diff --git a/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java b/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java index 1881a1b..dbf09a3 100644 --- a/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java +++ b/src/main/java/net/pterodactylus/sone/notify/ListNotificationFilters.java @@ -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 = reply.getPost(); + if (!post.isPresent()) { return false; } - if (!isPostVisible(sone, post)) { + if (!isPostVisible(sone, post.get())) { return false; } if (reply.getTime() > System.currentTimeMillis()) { diff --git a/src/main/java/net/pterodactylus/sone/template/ReplyGroupFilter.java b/src/main/java/net/pterodactylus/sone/template/ReplyGroupFilter.java index 4806cee..bcf73d8 100644 --- a/src/main/java/net/pterodactylus/sone/template/ReplyGroupFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ReplyGroupFilter.java @@ -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> postSones = new HashMap>(); Map> postReplies = new HashMap>(); for (PostReply reply : allReplies) { - Post post = reply.getPost(); - Set sones = postSones.get(post); + /* + * All replies from a new-reply notification have posts, + * ListNotificationFilters takes care of that. + */ + Optional post = reply.getPost(); + Set sones = postSones.get(post.get()); if (sones == null) { sones = new HashSet(); - postSones.put(post, sones); + postSones.put(post.get(), sones); } sones.add(reply.getSone()); - Set replies = postReplies.get(post); + Set replies = postReplies.get(post.get()); if (replies == null) { replies = new HashSet(); - postReplies.put(post, replies); + postReplies.put(post.get(), replies); } replies.add(reply); } diff --git a/src/main/java/net/pterodactylus/sone/web/NewPage.java b/src/main/java/net/pterodactylus/sone/web/NewPage.java index 2d4ec55..22d27c8 100644 --- a/src/main/java/net/pterodactylus/sone/web/NewPage.java +++ b/src/main/java/net/pterodactylus/sone/web/NewPage.java @@ -67,7 +67,7 @@ public class NewPage extends SoneTemplatePage { /* collect new elements from notifications. */ Set posts = new HashSet(webInterface.getNewPosts()); for (PostReply reply : webInterface.getNewReplies()) { - posts.add(reply.getPost()); + posts.add(reply.getPost().get()); } /* filter and sort them. */ diff --git a/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java b/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java index dec4fe2..5572bca 100644 --- a/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java +++ b/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java @@ -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 replies = sone.getReplies(); final Map> repliedPosts = new HashMap>(); for (PostReply reply : replies) { - Post post = reply.getPost(); - if (repliedPosts.containsKey(post) || sone.equals(post.getSone()) || (sone.equals(post.getRecipient()))) { + Optional 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 posts = new ArrayList(repliedPosts.keySet()); Collections.sort(posts, new Comparator() { diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index d9a9dfb..3bb11d5 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -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()); } } } diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java index 9c4c76f..e69aa12 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetStatusAjaxPage.java @@ -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);