From cb6325fa54d93612c5aad307204d30600b27af81 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 27 Jan 2012 10:10:12 +0100 Subject: [PATCH] =?utf8?q?Store=20a=20reply=E2=80=99s=20known=20status=20i?= =?utf8?q?n=20the=20reply=20itself.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/sone/core/Core.java | 41 +++++++--------------- .../java/net/pterodactylus/sone/data/Reply.java | 25 +++++++++++++ .../pterodactylus/sone/template/ReplyAccessor.java | 2 +- .../net/pterodactylus/sone/web/WebInterface.java | 2 +- 4 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index a4fca35..f979284 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -149,9 +149,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis /** All replies. */ private Map replies = new HashMap(); - /** All new replies. */ - private Set newReplies = new HashSet(); - /** All known replies. */ private Set knownReplies = new HashSet(); @@ -649,20 +646,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } /** - * Returns whether the reply with the given ID is new. - * - * @param replyId - * The ID of the reply to check - * @return {@code true} if the reply is considered to be new, {@code false} - * otherwise - */ - public boolean isNewReply(String replyId) { - synchronized (newReplies) { - return !knownReplies.contains(replyId) && newReplies.contains(replyId); - } - } - - /** * Returns all Sones that have liked the given post. * * @param post @@ -1209,15 +1192,17 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } } Set storedReplies = storedSone.getReplies(); - synchronized (newReplies) { + synchronized (knownReplies) { for (PostReply reply : sone.getReplies()) { reply.setSone(storedSone); if (!storedReplies.contains(reply)) { if (reply.getTime() < getSoneFollowingTime(sone)) { + reply.setKnown(true); knownReplies.add(reply.getId()); } else if (!knownReplies.contains(reply.getId())) { - newReplies.add(reply.getId()); coreListenerManager.fireNewReplyFound(reply); + } else { + reply.setKnown(true); } } replies.put(reply.getId(), reply); @@ -1545,7 +1530,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis knownPosts.add(post.getId()); } } - synchronized (newReplies) { + synchronized (knownReplies) { for (PostReply reply : replies) { knownReplies.add(reply.getId()); } @@ -1756,8 +1741,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis synchronized (replies) { replies.put(reply.getId(), reply); } - synchronized (newReplies) { - newReplies.add(reply.getId()); + synchronized (knownReplies) { coreListenerManager.fireNewReplyFound(reply); } sone.addReply(reply); @@ -1790,7 +1774,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis synchronized (replies) { replies.remove(reply.getId()); } - synchronized (newReplies) { + synchronized (knownReplies) { markReplyKnown(reply); knownReplies.remove(reply.getId()); } @@ -1806,8 +1790,8 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis * The reply to mark as known */ public void markReplyKnown(PostReply reply) { - synchronized (newReplies) { - newReplies.remove(reply.getId()); + reply.setKnown(true); + synchronized (knownReplies) { if (knownReplies.add(reply.getId())) { coreListenerManager.fireMarkReplyKnown(reply); touchConfiguration(); @@ -2221,7 +2205,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis /* save known replies. */ int replyCounter = 0; - synchronized (newReplies) { + synchronized (knownReplies) { for (String knownReplyId : knownReplies) { configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId); } @@ -2364,7 +2348,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis if (knownReplyId == null) { break; } - synchronized (newReplies) { + synchronized (knownReplies) { knownReplies.add(knownReplyId); } } @@ -2502,10 +2486,9 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } } synchronized (replies) { - synchronized (newReplies) { + synchronized (knownReplies) { for (PostReply reply : sone.getReplies()) { replies.remove(reply.getId()); - newReplies.remove(reply.getId()); coreListenerManager.fireReplyRemoved(reply); } } diff --git a/src/main/java/net/pterodactylus/sone/data/Reply.java b/src/main/java/net/pterodactylus/sone/data/Reply.java index 780fe69..15d8287 100644 --- a/src/main/java/net/pterodactylus/sone/data/Reply.java +++ b/src/main/java/net/pterodactylus/sone/data/Reply.java @@ -69,6 +69,9 @@ public abstract class Reply> { /** The text of the reply. */ private volatile String text; + /** Whether the reply is known. */ + private volatile boolean known; + /** * Creates a new reply with the given ID. * @@ -187,6 +190,28 @@ public abstract class Reply> { return (T) this; } + /** + * Returns whether this reply is known. + * + * @return {@code true} if this reply is known, {@code false} otherwise + */ + public boolean isKnown() { + return known; + } + + /** + * Sets whether this reply is known. + * + * @param known + * {@code true} if this reply is known, {@code false} otherwise + * @return This reply + */ + @SuppressWarnings("unchecked") + public T setKnown(boolean known) { + this.known = known; + return (T) this; + } + // // OBJECT METHODS // diff --git a/src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java b/src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java index 24bcfd6..7b89713 100644 --- a/src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/ReplyAccessor.java @@ -58,7 +58,7 @@ public class ReplyAccessor extends ReflectionAccessor { Sone currentSone = (Sone) templateContext.get("currentSone"); return (currentSone != null) && (currentSone.isLikedReplyId(reply.getId())); } else if (member.equals("new")) { - return core.isNewReply(reply.getId()); + return !reply.isKnown(); } else if (member.equals("loaded")) { return reply.getSone() != null; } diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index a007222..ba12765 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -888,7 +888,7 @@ public class WebInterface implements CoreListener { if (!getMentionedSones(reply.getText()).isEmpty()) { boolean isMentioned = false; for (PostReply existingReply : getCore().getReplies(reply.getPost())) { - isMentioned |= getCore().isNewReply(reply.getId()) && !getMentionedSones(existingReply.getText()).isEmpty(); + isMentioned |= !reply.isKnown() && !getMentionedSones(existingReply.getText()).isEmpty(); } if (!isMentioned) { mentionNotification.remove(reply.getPost()); -- 2.7.4