Detect if a Sone has been changed back to the previous insert state.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 841f560..60101b4 100644 (file)
@@ -113,6 +113,9 @@ public class Core implements IdentityListener {
        /* synchronize access on {@link #newSones}. */
        private Set<String> knownSones = new HashSet<String>();
 
+       /** All posts. */
+       private Map<String, Post> posts = new HashMap<String, Post>();
+
        /** All new posts. */
        private Set<String> newPosts = new HashSet<String>();
 
@@ -120,12 +123,15 @@ public class Core implements IdentityListener {
        /* synchronize access on {@link #newPosts}. */
        private Set<String> knownPosts = new HashSet<String>();
 
-       /** All posts. */
-       private Map<String, Post> posts = new HashMap<String, Post>();
-
        /** 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.
         *
@@ -394,6 +400,18 @@ public class Core implements IdentityListener {
        }
 
        /**
+        * Returns whether the given Sone has been modified.
+        *
+        * @param sone
+        *            The Sone to check for modifications
+        * @return {@code true} if a modification has been detected in the Sone,
+        *         {@code false} otherwise
+        */
+       public boolean isModifiedSone(Sone sone) {
+               return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
+       }
+
+       /**
         * Returns the post with the given ID.
         *
         * @param postId
@@ -468,6 +486,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
@@ -553,9 +587,9 @@ public class Core implements IdentityListener {
                        localSones.put(ownIdentity.getId(), sone);
                        SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
                        soneInserters.put(sone, soneInserter);
-                       soneInserter.start();
                        setSoneStatus(sone, SoneStatus.idle);
                        loadSone(sone);
+                       soneInserter.start();
                        new Thread(new Runnable() {
 
                                @Override
@@ -579,10 +613,6 @@ public class Core implements IdentityListener {
        public Sone createSone(OwnIdentity ownIdentity) {
                identityManager.addContext(ownIdentity, "Sone");
                Sone sone = addLocalSone(ownIdentity);
-               synchronized (sone) {
-                       /* mark as modified so that it gets inserted immediately. */
-                       sone.setModificationCounter(sone.getModificationCounter() + 1);
-               }
                return sone;
        }
 
@@ -654,8 +684,13 @@ public class Core implements IdentityListener {
                                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) {
@@ -666,7 +701,6 @@ public class Core implements IdentityListener {
                                storedSone.setLikePostIds(sone.getLikedPostIds());
                                storedSone.setLikeReplyIds(sone.getLikedReplyIds());
                                storedSone.setLatestEdition(sone.getRequestUri().getEdition());
-                               storedSone.setModificationCounter(0);
                        }
                }
        }
@@ -721,7 +755,7 @@ public class Core implements IdentityListener {
                        logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
                        return;
                }
-               long soneModificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0);
+               String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
 
                /* load profile. */
                Profile profile = new Profile();
@@ -806,7 +840,7 @@ public class Core implements IdentityListener {
                        sone.setLikePostIds(likedPostIds);
                        sone.setLikeReplyIds(likedReplyIds);
                        sone.setFriends(friends);
-                       sone.setModificationCounter(soneModificationCounter);
+                       soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
                }
                synchronized (newSones) {
                        for (String friend : friends) {
@@ -818,6 +852,11 @@ public class Core implements IdentityListener {
                                knownPosts.add(post.getId());
                        }
                }
+               synchronized (newReplies) {
+                       for (Reply reply : replies) {
+                               knownReplies.add(reply.getId());
+                       }
+               }
        }
 
        /**
@@ -843,7 +882,7 @@ public class Core implements IdentityListener {
                        /* save Sone into configuration. */
                        String sonePrefix = "Sone/" + sone.getId();
                        configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
-                       configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter());
+                       configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
 
                        /* save profile. */
                        Profile profile = sone.getProfile();
@@ -933,6 +972,9 @@ public class Core implements IdentityListener {
                synchronized (posts) {
                        posts.put(post.getId(), post);
                }
+               synchronized (newPosts) {
+                       knownPosts.add(post.getId());
+               }
                sone.addPost(post);
                saveSone(sone);
        }
@@ -990,6 +1032,9 @@ public class Core implements IdentityListener {
                synchronized (replies) {
                        replies.put(reply.getId(), reply);
                }
+               synchronized (newReplies) {
+                       knownReplies.add(reply.getId());
+               }
                sone.addReply(reply);
                saveSone(sone);
        }
@@ -1090,6 +1135,18 @@ public class Core implements IdentityListener {
                        }
                }
 
+               /* 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);
+                       }
+               }
+
        }
 
        /**
@@ -1120,6 +1177,15 @@ public class Core implements IdentityListener {
                                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);
                }