Mark new replies.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 450e996..895f45e 100644 (file)
@@ -116,9 +116,22 @@ public class Core implements IdentityListener {
        /** All posts. */
        private Map<String, Post> posts = new HashMap<String, Post>();
 
+       /** All new posts. */
+       private Set<String> newPosts = new HashSet<String>();
+
+       /** All known posts. */
+       /* synchronize access on {@link #newPosts}. */
+       private Set<String> knownPosts = new HashSet<String>();
+
        /** All replies. */
        private Map<String, Reply> replies = new HashMap<String, Reply>();
 
+       /** All new replies. */
+       private Set<String> newReplies = new HashSet<String>();
+
+       /** All known replies. */
+       private Set<String> knownReplies = new HashSet<String>();
+
        /**
         * Creates a new core.
         *
@@ -405,6 +418,23 @@ public class Core implements IdentityListener {
        }
 
        /**
+        * Returns whether the given post ID is new. After this method returns it is
+        * marked a known post ID.
+        *
+        * @param postId
+        *            The post ID
+        * @return {@code true} if the post is considered to be new, {@code false}
+        *         otherwise
+        */
+       public boolean isNewPost(String postId) {
+               synchronized (newPosts) {
+                       boolean isNew = !knownPosts.contains(postId) && newPosts.remove(postId);
+                       knownPosts.add(postId);
+                       return isNew;
+               }
+       }
+
+       /**
         * Returns the reply with the given ID.
         *
         * @param replyId
@@ -444,6 +474,22 @@ public class Core implements IdentityListener {
        }
 
        /**
+        * 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) {
+                       boolean isNew = !knownReplies.contains(replyId) && newReplies.remove(replyId);
+                       knownReplies.add(replyId);
+                       return isNew;
+               }
+       }
+
+       /**
         * Returns all Sones that have liked the given post.
         *
         * @param post
@@ -607,8 +653,8 @@ public class Core implements IdentityListener {
         *            The updated Sone
         */
        public void updateSone(Sone sone) {
-               if (isRemoteSone(sone)) {
-                       Sone storedSone = getRemoteSone(sone.getId());
+               if (hasSone(sone.getId())) {
+                       Sone storedSone = getSone(sone.getId());
                        if (!(sone.getTime() > storedSone.getTime())) {
                                logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
                                return;
@@ -617,16 +663,26 @@ public class Core implements IdentityListener {
                                for (Post post : storedSone.getPosts()) {
                                        posts.remove(post.getId());
                                }
-                               for (Post post : sone.getPosts()) {
-                                       posts.put(post.getId(), post);
+                               synchronized (newPosts) {
+                                       for (Post post : sone.getPosts()) {
+                                               if (!storedSone.getPosts().contains(post) && !knownSones.contains(post.getId())) {
+                                                       newPosts.add(post.getId());
+                                               }
+                                               posts.put(post.getId(), post);
+                                       }
                                }
                        }
                        synchronized (replies) {
                                for (Reply reply : storedSone.getReplies()) {
                                        replies.remove(reply.getId());
                                }
-                               for (Reply reply : sone.getReplies()) {
-                                       replies.put(reply.getId(), reply);
+                               synchronized (newReplies) {
+                                       for (Reply reply : sone.getReplies()) {
+                                               if (!storedSone.getReplies().contains(reply) && !knownSones.contains(reply.getId())) {
+                                                       newReplies.add(reply.getId());
+                                               }
+                                               replies.put(reply.getId(), reply);
+                                       }
                                }
                        }
                        synchronized (storedSone) {
@@ -784,6 +840,16 @@ public class Core implements IdentityListener {
                                knownSones.add(friend);
                        }
                }
+               synchronized (newPosts) {
+                       for (Post post : posts) {
+                               knownPosts.add(post.getId());
+                       }
+               }
+               synchronized (newReplies) {
+                       for (Reply reply : replies) {
+                               knownReplies.add(reply.getId());
+                       }
+               }
        }
 
        /**
@@ -857,8 +923,8 @@ public class Core implements IdentityListener {
 
                        /* save friends. */
                        int friendCounter = 0;
-                       for (String friend : sone.getFriends()) {
-                               configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(friend);
+                       for (String friendId : sone.getFriends()) {
+                               configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
                        }
                        configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
 
@@ -899,6 +965,9 @@ public class Core implements IdentityListener {
                synchronized (posts) {
                        posts.put(post.getId(), post);
                }
+               synchronized (newPosts) {
+                       knownPosts.add(post.getId());
+               }
                sone.addPost(post);
                saveSone(sone);
        }
@@ -956,6 +1025,9 @@ public class Core implements IdentityListener {
                synchronized (replies) {
                        replies.put(reply.getId(), reply);
                }
+               synchronized (newReplies) {
+                       knownReplies.add(reply.getId());
+               }
                sone.addReply(reply);
                saveSone(sone);
        }
@@ -1043,6 +1115,31 @@ public class Core implements IdentityListener {
                                knownSones.add(knownSoneId);
                        }
                }
+
+               /* load known posts. */
+               int postCounter = 0;
+               while (true) {
+                       String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
+                       if (knownPostId == null) {
+                               break;
+                       }
+                       synchronized (newPosts) {
+                               knownPosts.add(knownPostId);
+                       }
+               }
+
+               /* load known replies. */
+               int replyCounter = 0;
+               while (true) {
+                       String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
+                       if (knownReplyId == null) {
+                               break;
+                       }
+                       synchronized (newReplies) {
+                               knownReplies.add(knownReplyId);
+                       }
+               }
+
        }
 
        /**
@@ -1064,6 +1161,24 @@ public class Core implements IdentityListener {
                                configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
                        }
 
+                       /* save known posts. */
+                       int postCounter = 0;
+                       synchronized (newPosts) {
+                               for (String knownPostId : knownPosts) {
+                                       configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
+                               }
+                               configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
+                       }
+
+                       /* save known replies. */
+                       int replyCounter = 0;
+                       synchronized (newReplies) {
+                               for (String knownReplyId : knownReplies) {
+                                       configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
+                               }
+                               configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
+                       }
+
                } catch (ConfigurationException ce1) {
                        logger.log(Level.SEVERE, "Could not store configuration!", ce1);
                }