Fix logging.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index a4fca35..811950d 100644 (file)
@@ -24,8 +24,8 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.Map.Entry;
+import java.util.Set;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.logging.Level;
@@ -40,12 +40,12 @@ import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
+import net.pterodactylus.sone.data.Profile.Field;
 import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.data.TemporaryImage;
-import net.pterodactylus.sone.data.Profile.Field;
 import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
 import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.data.TemporaryImage;
 import net.pterodactylus.sone.fcp.FcpInterface;
 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
 import net.pterodactylus.sone.freenet.wot.Identity;
@@ -149,9 +149,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        /** All replies. */
        private Map<String, PostReply> replies = new HashMap<String, PostReply>();
 
-       /** All new replies. */
-       private Set<String> newReplies = new HashSet<String>();
-
        /** All known replies. */
        private Set<String> knownReplies = new HashSet<String>();
 
@@ -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
@@ -880,6 +863,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        }
                        sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
                        sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
+                       sone.setKnown(true);
                        /* TODO - load posts ’n stuff */
                        localSones.put(ownIdentity.getId(), sone);
                        final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
@@ -1184,15 +1168,13 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                List<Post> storedPosts = storedSone.getPosts();
                                synchronized (knownPosts) {
                                        for (Post post : sone.getPosts()) {
-                                               post.setSone(storedSone);
+                                               post.setSone(storedSone).setKnown(knownPosts.contains(post.getId()));
                                                if (!storedPosts.contains(post)) {
                                                        if (post.getTime() < getSoneFollowingTime(sone)) {
-                                                               sone.setKnown(true);
                                                                knownPosts.add(post.getId());
                                                        } else if (!knownPosts.contains(post.getId())) {
+                                                               sone.setKnown(false);
                                                                coreListenerManager.fireNewPostFound(post);
-                                                       } else {
-                                                               sone.setKnown(true);
                                                        }
                                                }
                                                posts.put(post.getId(), post);
@@ -1209,14 +1191,14 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                        }
                                }
                                Set<PostReply> storedReplies = storedSone.getReplies();
-                               synchronized (newReplies) {
+                               synchronized (knownReplies) {
                                        for (PostReply reply : sone.getReplies()) {
-                                               reply.setSone(storedSone);
+                                               reply.setSone(storedSone).setKnown(knownReplies.contains(reply.getId()));
                                                if (!storedReplies.contains(reply)) {
                                                        if (reply.getTime() < getSoneFollowingTime(sone)) {
                                                                knownReplies.add(reply.getId());
                                                        } else if (!knownReplies.contains(reply.getId())) {
-                                                               newReplies.add(reply.getId());
+                                                               reply.setKnown(false);
                                                                coreListenerManager.fireNewReplyFound(reply);
                                                        }
                                                }
@@ -1311,8 +1293,8 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        }
 
        /**
-        * Marks the given Sone as known. If the Sone was {@link #isNewPost(String)
-        * new} before, a {@link CoreListener#markSoneKnown(Sone)} event is fired.
+        * Marks the given Sone as known. If the Sone was not {@link Post#isKnown()
+        * known} before, a {@link CoreListener#markSoneKnown(Sone)} event is fired.
         *
         * @param sone
         *            The Sone to mark as known
@@ -1545,7 +1527,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());
                        }
@@ -1659,8 +1641,8 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        }
 
        /**
-        * Marks the given post as known, if it is currently a new post (according
-        * to {@link #isNewPost(String)}).
+        * Marks the given post as known, if it is currently not a known post
+        * (according to {@link Post#isKnown()}).
         *
         * @param post
         *            The post to mark as known
@@ -1668,11 +1650,14 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        public void markPostKnown(Post post) {
                post.setKnown(true);
                synchronized (knownPosts) {
+                       coreListenerManager.fireMarkPostKnown(post);
                        if (knownPosts.add(post.getId())) {
-                               coreListenerManager.fireMarkPostKnown(post);
                                touchConfiguration();
                        }
                }
+               for (PostReply reply : getReplies(post)) {
+                       markReplyKnown(reply);
+               }
        }
 
        /**
@@ -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());
                }
@@ -1799,17 +1783,17 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        }
 
        /**
-        * Marks the given reply as known, if it is currently a new reply (according
-        * to {@link #isNewReply(String)}).
+        * Marks the given reply as known, if it is currently not a known reply
+        * (according to {@link Reply#isKnown()}).
         *
         * @param reply
         *            The reply to mark as known
         */
        public void markReplyKnown(PostReply reply) {
-               synchronized (newReplies) {
-                       newReplies.remove(reply.getId());
+               reply.setKnown(true);
+               synchronized (knownReplies) {
+                       coreListenerManager.fireMarkReplyKnown(reply);
                        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);
                        }
                }
@@ -2424,7 +2408,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         */
        @Override
        public void ownIdentityAdded(OwnIdentity ownIdentity) {
-               logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
+               logger.log(Level.FINEST, "Adding OwnIdentity: %s", ownIdentity);
                if (ownIdentity.hasContext("Sone")) {
                        trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet<Identity>()));
                        addLocalSone(ownIdentity);
@@ -2445,7 +2429,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         */
        @Override
        public void identityAdded(OwnIdentity ownIdentity, Identity identity) {
-               logger.log(Level.FINEST, "Adding Identity: " + identity);
+               logger.log(Level.FINEST, "Adding Identity: %s", identity);
                trustedIdentities.get(ownIdentity).add(identity);
                addRemoteSone(identity);
        }
@@ -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);
                                }
                        }