Load and save “show custom avatars” option.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 1c9d668..a51a189 100644 (file)
@@ -38,9 +38,11 @@ import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Client;
 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.Reply;
 import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
 import net.pterodactylus.sone.data.TemporaryImage;
 import net.pterodactylus.sone.data.Profile.Field;
 import net.pterodactylus.sone.fcp.FcpInterface;
@@ -135,6 +137,9 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        /* synchronize access on itself. */
        private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
 
+       /** The times Sones were followed. */
+       private final Map<Sone, Long> soneFollowingTimes = new HashMap<Sone, Long>();
+
        /** Locked local Sones. */
        /* synchronize on itself. */
        private final Set<Sone> lockedSones = new HashSet<Sone>();
@@ -173,7 +178,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        private Set<String> knownPosts = new HashSet<String>();
 
        /** All replies. */
-       private Map<String, Reply> replies = new HashMap<String, Reply>();
+       private Map<String, PostReply> replies = new HashMap<String, PostReply>();
 
        /** All new replies. */
        private Set<String> newReplies = new HashSet<String>();
@@ -505,17 +510,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *
         * @param id
         *            The ID of the remote Sone to get
-        * @return The Sone with the given ID
-        */
-       public Sone getRemoteSone(String id) {
-               return getRemoteSone(id, true);
-       }
-
-       /**
-        * Returns the remote Sone with the given ID.
-        *
-        * @param id
-        *            The ID of the remote Sone to get
         * @param create
         *            {@code true} to always create a Sone, {@code false} to return
         *            {@code null} if no Sone with the given ID exists
@@ -524,7 +518,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        public Sone getRemoteSone(String id, boolean create) {
                synchronized (remoteSones) {
                        Sone sone = remoteSones.get(id);
-                       if ((sone == null) && create) {
+                       if ((sone == null) && create && (id != null) && (id.length() == 43)) {
                                sone = new Sone(id);
                                remoteSones.put(id, sone);
                                setSoneStatus(sone, SoneStatus.unknown);
@@ -587,6 +581,23 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        }
 
        /**
+        * Returns the time when the given was first followed by any local Sone.
+        *
+        * @param sone
+        *            The Sone to get the time for
+        * @return The time (in milliseconds since Jan 1, 1970) the Sone has first
+        *         been followed, or {@link Long#MAX_VALUE}
+        */
+       public long getSoneFollowingTime(Sone sone) {
+               synchronized (soneFollowingTimes) {
+                       if (soneFollowingTimes.containsKey(sone)) {
+                               return soneFollowingTimes.get(sone);
+                       }
+                       return Long.MAX_VALUE;
+               }
+       }
+
+       /**
         * Returns whether the target Sone is trusted by the origin Sone.
         *
         * @param origin
@@ -676,7 +687,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            The ID of the reply to get
         * @return The reply
         */
-       public Reply getReply(String replyId) {
+       public PostReply getReply(String replyId) {
                return getReply(replyId, true);
        }
 
@@ -692,11 +703,11 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            to return {@code null} if no reply can be found
         * @return The reply, or {@code null} if there is no such reply
         */
-       public Reply getReply(String replyId, boolean create) {
+       public PostReply getReply(String replyId, boolean create) {
                synchronized (replies) {
-                       Reply reply = replies.get(replyId);
+                       PostReply reply = replies.get(replyId);
                        if (create && (reply == null)) {
-                               reply = new Reply(replyId);
+                               reply = new PostReply(replyId);
                                replies.put(replyId, reply);
                        }
                        return reply;
@@ -710,11 +721,11 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            The post to get all replies for
         * @return All replies for the given post
         */
-       public List<Reply> getReplies(Post post) {
+       public List<PostReply> getReplies(Post post) {
                Set<Sone> sones = getSones();
-               List<Reply> replies = new ArrayList<Reply>();
+               List<PostReply> replies = new ArrayList<PostReply>();
                for (Sone sone : sones) {
-                       for (Reply reply : sone.getReplies()) {
+                       for (PostReply reply : sone.getReplies()) {
                                if (reply.getPost().equals(post)) {
                                        replies.add(reply);
                                }
@@ -762,7 +773,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            The reply to get the liking Sones for
         * @return The Sones that like the given reply
         */
-       public Set<Sone> getLikes(Reply reply) {
+       public Set<Sone> getLikes(PostReply reply) {
                Set<Sone> sones = new HashSet<Sone>();
                for (Sone sone : getSones()) {
                        if (sone.getLikedReplyIds().contains(reply.getId())) {
@@ -935,29 +946,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        }
 
        /**
-        * Adds a local Sone from the given ID which has to be the ID of an own
-        * identity.
-        *
-        * @param id
-        *            The ID of an own identity to add a Sone for
-        * @return The added (or already existing) Sone
-        */
-       public Sone addLocalSone(String id) {
-               synchronized (localSones) {
-                       if (localSones.containsKey(id)) {
-                               logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
-                               return localSones.get(id);
-                       }
-                       OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
-                       if (ownIdentity == null) {
-                               logger.log(Level.INFO, "Invalid Sone ID: %s", id);
-                               return null;
-                       }
-                       return addLocalSone(ownIdentity);
-               }
-       }
-
-       /**
         * Adds a local Sone from the given own identity.
         *
         * @param ownIdentity
@@ -1007,7 +995,13 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                }
                Sone sone = addLocalSone(ownIdentity);
                sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption<Boolean>(false));
-               sone.addFriend("nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI");
+               sone.getOptions().addBooleanOption("EnableSoneInsertNotifications", new DefaultOption<Boolean>(false));
+               sone.getOptions().addBooleanOption("ShowNotification/NewSones", new DefaultOption<Boolean>(true));
+               sone.getOptions().addBooleanOption("ShowNotification/NewPosts", new DefaultOption<Boolean>(true));
+               sone.getOptions().addBooleanOption("ShowNotification/NewReplies", new DefaultOption<Boolean>(true));
+               sone.getOptions().addEnumOption("ShowCustomAvatars", new DefaultOption<ShowCustomAvatars>(ShowCustomAvatars.NEVER));
+
+               followSone(sone, getSone("nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"));
                touchConfiguration();
                return sone;
        }
@@ -1025,7 +1019,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        return null;
                }
                synchronized (remoteSones) {
-                       final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
+                       final Sone sone = getRemoteSone(identity.getId(), true).setIdentity(identity);
                        boolean newSone = sone.getRequestUri() == null;
                        sone.setRequestUri(getSoneUri(identity.getRequestUri()));
                        sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
@@ -1040,13 +1034,11 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                        coreListenerManager.fireNewSoneFound(sone);
                                        for (Sone localSone : getLocalSones()) {
                                                if (localSone.getOptions().getBooleanOption("AutoFollow").get()) {
-                                                       localSone.addFriend(sone.getId());
-                                                       touchConfiguration();
+                                                       followSone(localSone, sone);
                                                }
                                        }
                                }
                        }
-                       remoteSones.put(identity.getId(), sone);
                        soneDownloader.addSone(sone);
                        setSoneStatus(sone, SoneStatus.unknown);
                        soneDownloaders.execute(new Runnable() {
@@ -1063,6 +1055,95 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
        }
 
        /**
+        * Lets the given local Sone follow the Sone with the given ID.
+        *
+        * @param sone
+        *            The local Sone that should follow another Sone
+        * @param soneId
+        *            The ID of the Sone to follow
+        */
+       public void followSone(Sone sone, String soneId) {
+               Validation.begin().isNotNull("Sone", sone).isNotNull("Sone ID", soneId).check();
+               Sone followedSone = getSone(soneId, true);
+               if (followedSone == null) {
+                       logger.log(Level.INFO, String.format("Ignored Sone with invalid ID: %s", soneId));
+                       return;
+               }
+               followSone(sone, getSone(soneId));
+       }
+
+       /**
+        * Lets the given local Sone follow the other given Sone. If the given Sone
+        * was not followed by any local Sone before, this will mark all elements of
+        * the followed Sone as read that have been created before the current
+        * moment.
+        *
+        * @param sone
+        *            The local Sone that should follow the other Sone
+        * @param followedSone
+        *            The Sone that should be followed
+        */
+       public void followSone(Sone sone, Sone followedSone) {
+               Validation.begin().isNotNull("Sone", sone).isNotNull("Followed Sone", followedSone).check();
+               sone.addFriend(followedSone.getId());
+               synchronized (soneFollowingTimes) {
+                       if (!soneFollowingTimes.containsKey(followedSone)) {
+                               long now = System.currentTimeMillis();
+                               soneFollowingTimes.put(followedSone, now);
+                               for (Post post : followedSone.getPosts()) {
+                                       if (post.getTime() < now) {
+                                               markPostKnown(post);
+                                       }
+                               }
+                               for (PostReply reply : followedSone.getReplies()) {
+                                       if (reply.getTime() < now) {
+                                               markReplyKnown(reply);
+                                       }
+                               }
+                       }
+               }
+               touchConfiguration();
+       }
+
+       /**
+        * Lets the given local Sone unfollow the Sone with the given ID.
+        *
+        * @param sone
+        *            The local Sone that should unfollow another Sone
+        * @param soneId
+        *            The ID of the Sone being unfollowed
+        */
+       public void unfollowSone(Sone sone, String soneId) {
+               Validation.begin().isNotNull("Sone", sone).isNotNull("Sone ID", soneId).check();
+               unfollowSone(sone, getSone(soneId, false));
+       }
+
+       /**
+        * Lets the given local Sone unfollow the other given Sone. If the given
+        * local Sone is the last local Sone that followed the given Sone, its
+        * following time will be removed.
+        *
+        * @param sone
+        *            The local Sone that should unfollow another Sone
+        * @param unfollowedSone
+        *            The Sone being unfollowed
+        */
+       public void unfollowSone(Sone sone, Sone unfollowedSone) {
+               Validation.begin().isNotNull("Sone", sone).isNotNull("Unfollowed Sone", unfollowedSone).check();
+               sone.removeFriend(unfollowedSone.getId());
+               boolean unfollowedSoneStillFollowed = false;
+               for (Sone localSone : getLocalSones()) {
+                       unfollowedSoneStillFollowed |= localSone.hasFriend(unfollowedSone.getId());
+               }
+               if (!unfollowedSoneStillFollowed) {
+                       synchronized (soneFollowingTimes) {
+                               soneFollowingTimes.remove(unfollowedSone);
+                       }
+               }
+               touchConfiguration();
+       }
+
+       /**
         * Retrieves the trust relationship from the origin to the target. If the
         * trust relationship can not be retrieved, {@code null} is returned.
         *
@@ -1194,9 +1275,13 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                synchronized (newPosts) {
                                        for (Post post : sone.getPosts()) {
                                                post.setSone(storedSone);
-                                               if (!storedPosts.contains(post) && !knownPosts.contains(post.getId())) {
-                                                       newPosts.add(post.getId());
-                                                       coreListenerManager.fireNewPostFound(post);
+                                               if (!storedPosts.contains(post)) {
+                                                       if (post.getTime() < getSoneFollowingTime(sone)) {
+                                                               knownPosts.add(post.getId());
+                                                       } else if (!knownPosts.contains(post.getId())) {
+                                                               newPosts.add(post.getId());
+                                                               coreListenerManager.fireNewPostFound(post);
+                                                       }
                                                }
                                                posts.put(post.getId(), post);
                                        }
@@ -1204,20 +1289,24 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        }
                        synchronized (replies) {
                                if (!soneRescueMode) {
-                                       for (Reply reply : storedSone.getReplies()) {
+                                       for (PostReply reply : storedSone.getReplies()) {
                                                replies.remove(reply.getId());
                                                if (!sone.getReplies().contains(reply)) {
                                                        coreListenerManager.fireReplyRemoved(reply);
                                                }
                                        }
                                }
-                               Set<Reply> storedReplies = storedSone.getReplies();
+                               Set<PostReply> storedReplies = storedSone.getReplies();
                                synchronized (newReplies) {
-                                       for (Reply reply : sone.getReplies()) {
+                                       for (PostReply reply : sone.getReplies()) {
                                                reply.setSone(storedSone);
-                                               if (!storedReplies.contains(reply) && !knownReplies.contains(reply.getId())) {
-                                                       newReplies.add(reply.getId());
-                                                       coreListenerManager.fireNewReplyFound(reply);
+                                               if (!storedReplies.contains(reply)) {
+                                                       if (reply.getTime() < getSoneFollowingTime(sone)) {
+                                                               knownReplies.add(reply.getId());
+                                                       } else if (!knownReplies.contains(reply.getId())) {
+                                                               newReplies.add(reply.getId());
+                                                               coreListenerManager.fireNewReplyFound(reply);
+                                                       }
                                                }
                                                replies.put(reply.getId(), reply);
                                        }
@@ -1249,7 +1338,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                        for (Post post : sone.getPosts()) {
                                                storedSone.addPost(post);
                                        }
-                                       for (Reply reply : sone.getReplies()) {
+                                       for (PostReply reply : sone.getReplies()) {
                                                storedSone.addReply(reply);
                                        }
                                        for (String likedPostId : sone.getLikedPostIds()) {
@@ -1342,6 +1431,10 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                /* initialize options. */
                sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption<Boolean>(false));
                sone.getOptions().addBooleanOption("EnableSoneInsertNotifications", new DefaultOption<Boolean>(false));
+               sone.getOptions().addBooleanOption("ShowNotification/NewSones", new DefaultOption<Boolean>(true));
+               sone.getOptions().addBooleanOption("ShowNotification/NewPosts", new DefaultOption<Boolean>(true));
+               sone.getOptions().addBooleanOption("ShowNotification/NewReplies", new DefaultOption<Boolean>(true));
+               sone.getOptions().addEnumOption("ShowCustomAvatars", new DefaultOption<ShowCustomAvatars>(ShowCustomAvatars.NEVER));
 
                /* load Sone. */
                String sonePrefix = "Sone/" + sone.getId();
@@ -1353,7 +1446,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
 
                /* load profile. */
-               Profile profile = new Profile();
+               Profile profile = new Profile(sone);
                profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
                profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
                profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
@@ -1395,7 +1488,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                }
 
                /* load replies. */
-               Set<Reply> replies = new HashSet<Reply>();
+               Set<PostReply> replies = new HashSet<PostReply>();
                while (true) {
                        String replyPrefix = sonePrefix + "/Replies/" + replies.size();
                        String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
@@ -1501,9 +1594,19 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        album.addImage(image);
                }
 
+               /* load avatar. */
+               String avatarId = configuration.getStringValue(sonePrefix + "/Profile/Avatar").getValue(null);
+               if (avatarId != null) {
+                       profile.setAvatar(getImage(avatarId, false));
+               }
+
                /* load options. */
                sone.getOptions().getBooleanOption("AutoFollow").set(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
                sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").set(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(null));
+               sone.getOptions().getBooleanOption("ShowNotification/NewSones").set(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(null));
+               sone.getOptions().getBooleanOption("ShowNotification/NewPosts").set(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(null));
+               sone.getOptions().getBooleanOption("ShowNotification/NewReplies").set(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(null));
+               sone.getOptions().<ShowCustomAvatars> getEnumOption("ShowCustomAvatars").set(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name())));
 
                /* if we’re still here, Sone was loaded successfully. */
                synchronized (sone) {
@@ -1513,7 +1616,9 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        sone.setReplies(replies);
                        sone.setLikePostIds(likedPostIds);
                        sone.setLikeReplyIds(likedReplyIds);
-                       sone.setFriends(friends);
+                       for (String friendId : friends) {
+                               followSone(sone, friendId);
+                       }
                        sone.setAlbums(topLevelAlbums);
                        soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
                }
@@ -1528,7 +1633,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        }
                }
                synchronized (newReplies) {
-                       for (Reply reply : replies) {
+                       for (PostReply reply : replies) {
                                knownReplies.add(reply.getId());
                        }
                }
@@ -1718,7 +1823,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            The text of the reply
         * @return The created reply
         */
-       public Reply createReply(Sone sone, Post post, String text) {
+       public PostReply createReply(Sone sone, Post post, String text) {
                return createReply(sone, post, System.currentTimeMillis(), text);
        }
 
@@ -1735,12 +1840,12 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         *            The text of the reply
         * @return The created reply
         */
-       public Reply createReply(Sone sone, Post post, long time, String text) {
+       public PostReply 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 null;
                }
-               final Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
+               final PostReply reply = new PostReply(sone, post, System.currentTimeMillis(), text);
                synchronized (replies) {
                        replies.put(reply.getId(), reply);
                }
@@ -1769,7 +1874,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         * @param reply
         *            The reply to delete
         */
-       public void deleteReply(Reply reply) {
+       public void deleteReply(PostReply reply) {
                Sone sone = reply.getSone();
                if (!isLocalSone(sone)) {
                        logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
@@ -1793,7 +1898,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
         * @param reply
         *            The reply to mark as known
         */
-       public void markReplyKnown(Reply reply) {
+       public void markReplyKnown(PostReply reply) {
                synchronized (newReplies) {
                        if (newReplies.remove(reply.getId())) {
                                knownReplies.add(reply.getId());
@@ -2041,6 +2146,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
                        configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
                        configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
+                       configuration.getStringValue(sonePrefix + "/Profile/Avatar").setValue(profile.getAvatar());
 
                        /* save profile fields. */
                        int fieldCounter = 0;
@@ -2064,7 +2170,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
 
                        /* save replies. */
                        int replyCounter = 0;
-                       for (Reply reply : sone.getReplies()) {
+                       for (PostReply reply : sone.getReplies()) {
                                String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
                                configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
                                configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
@@ -2130,7 +2236,11 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
 
                        /* save options. */
                        configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().getBooleanOption("AutoFollow").getReal());
+                       configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").setValue(sone.getOptions().getBooleanOption("ShowNotification/NewSones").getReal());
+                       configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().getBooleanOption("ShowNotification/NewPosts").getReal());
+                       configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().getBooleanOption("ShowNotification/NewReplies").getReal());
                        configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").setValue(sone.getOptions().getBooleanOption("EnableSoneInsertNotifications").getReal());
+                       configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions().<ShowCustomAvatars> getEnumOption("ShowCustomAvatars").get().name());
 
                        configuration.save();
 
@@ -2182,6 +2292,17 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                                configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
                        }
 
+                       /* save Sone following times. */
+                       soneCounter = 0;
+                       synchronized (soneFollowingTimes) {
+                               for (Entry<Sone, Long> soneFollowingTime : soneFollowingTimes.entrySet()) {
+                                       configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey().getId());
+                                       configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").setValue(soneFollowingTime.getValue());
+                                       ++soneCounter;
+                               }
+                               configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null);
+                       }
+
                        /* save known posts. */
                        int postCounter = 0;
                        synchronized (newPosts) {
@@ -2298,6 +2419,25 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        }
                }
 
+               /* load Sone following times. */
+               soneCounter = 0;
+               while (true) {
+                       String soneId = configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").getValue(null);
+                       if (soneId == null) {
+                               break;
+                       }
+                       long time = configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").getValue(Long.MAX_VALUE);
+                       Sone followedSone = getSone(soneId);
+                       if (followedSone == null) {
+                               logger.log(Level.WARNING, String.format("Ignoring Sone with invalid ID: %s", soneId));
+                       } else {
+                               synchronized (soneFollowingTimes) {
+                                       soneFollowingTimes.put(getSone(soneId), time);
+                               }
+                       }
+                       ++soneCounter;
+               }
+
                /* load known posts. */
                int postCounter = 0;
                while (true) {
@@ -2413,7 +2553,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                        @Override
                        @SuppressWarnings("synthetic-access")
                        public void run() {
-                               Sone sone = getRemoteSone(identity.getId());
+                               Sone sone = getRemoteSone(identity.getId(), false);
                                sone.setIdentity(identity);
                                sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.getLatestEdition()));
                                soneDownloader.addSone(sone);
@@ -2457,7 +2597,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis
                }
                synchronized (replies) {
                        synchronized (newReplies) {
-                               for (Reply reply : sone.getReplies()) {
+                               for (PostReply reply : sone.getReplies()) {
                                        replies.remove(reply.getId());
                                        newReplies.remove(reply.getId());
                                        coreListenerManager.fireReplyRemoved(reply);