Return the created reply from createReply().
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 895f45e..90d3696 100644 (file)
@@ -400,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
@@ -575,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
@@ -601,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;
        }
 
@@ -693,7 +701,6 @@ public class Core implements IdentityListener {
                                storedSone.setLikePostIds(sone.getLikedPostIds());
                                storedSone.setLikeReplyIds(sone.getLikedReplyIds());
                                storedSone.setLatestEdition(sone.getRequestUri().getEdition());
-                               storedSone.setModificationCounter(0);
                        }
                }
        }
@@ -748,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();
@@ -833,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) {
@@ -875,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();
@@ -999,9 +1006,10 @@ public class Core implements IdentityListener {
         *            The post that this reply refers to
         * @param text
         *            The text of the reply
+        * @return The created reply
         */
-       public void createReply(Sone sone, Post post, String text) {
-               createReply(sone, post, System.currentTimeMillis(), text);
+       public Reply createReply(Sone sone, Post post, String text) {
+               return createReply(sone, post, System.currentTimeMillis(), text);
        }
 
        /**
@@ -1015,11 +1023,12 @@ public class Core implements IdentityListener {
         *            The time of the reply
         * @param text
         *            The text of the reply
+        * @return The created reply
         */
-       public void createReply(Sone sone, Post post, long time, String text) {
+       public Reply createReply(Sone sone, Post post, long time, String text) {
                if (!isLocalSone(sone)) {
                        logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
-                       return;
+                       return null;
                }
                Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
                synchronized (replies) {
@@ -1030,6 +1039,7 @@ public class Core implements IdentityListener {
                }
                sone.addReply(reply);
                saveSone(sone);
+               return reply;
        }
 
        /**