Move management of Sone following times to database.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 6ca7372..291cb95 100644 (file)
@@ -143,9 +143,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        /** The trust updater. */
        private final WebOfTrustUpdater webOfTrustUpdater;
 
-       /** The times Sones were followed. */
-       private final Map<String, Long> soneFollowingTimes = new HashMap<String, Long>();
-
        /** Locked local Sones. */
        /* synchronize on itself. */
        private final Set<LocalSone> lockedSones = new HashSet<LocalSone>();
@@ -373,9 +370,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *         been followed, or {@link Long#MAX_VALUE}
         */
        public long getSoneFollowingTime(Sone sone) {
-               synchronized (soneFollowingTimes) {
-                       return Optional.fromNullable(soneFollowingTimes.get(sone.getId())).or(Long.MAX_VALUE);
-               }
+               return database.getSoneFollowingTime(sone.getId()).or(Long.MAX_VALUE);
        }
 
        /**
@@ -694,28 +689,25 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        public void followSone(LocalSone sone, String soneId) {
                checkNotNull(sone, "sone must not be null");
                checkNotNull(soneId, "soneId must not be null");
+               boolean newFriend = !database.getSoneFollowingTime(soneId).isPresent();
                database.addFriend(sone, soneId);
-               synchronized (soneFollowingTimes) {
-                       if (!soneFollowingTimes.containsKey(soneId)) {
-                               long now = System.currentTimeMillis();
-                               soneFollowingTimes.put(soneId, now);
-                               Optional<Sone> followedSone = getSone(soneId);
-                               if (!followedSone.isPresent()) {
-                                       return;
-                               }
-                               for (Post post : followedSone.get().getPosts()) {
-                                       if (post.getTime() < now) {
-                                               markPostKnown(post);
-                                       }
+               if (newFriend) {
+                       long now = System.currentTimeMillis();
+                       Optional<Sone> followedSone = getSone(soneId);
+                       if (!followedSone.isPresent()) {
+                               return;
+                       }
+                       for (Post post : followedSone.get().getPosts()) {
+                               if (post.getTime() < now) {
+                                       markPostKnown(post);
                                }
-                               for (PostReply reply : followedSone.get().getReplies()) {
-                                       if (reply.getTime() < now) {
-                                               markReplyKnown(reply);
-                                       }
+                       }
+                       for (PostReply reply : followedSone.get().getReplies()) {
+                               if (reply.getTime() < now) {
+                                       markReplyKnown(reply);
                                }
                        }
                }
-               touchConfiguration();
        }
 
        /**
@@ -730,16 +722,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                checkNotNull(sone, "sone must not be null");
                checkNotNull(soneId, "soneId must not be null");
                database.removeFriend(sone, soneId);
-               boolean unfollowedSoneStillFollowed = false;
-               for (Sone localSone : getLocalSones()) {
-                       unfollowedSoneStillFollowed |= localSone.hasFriend(soneId);
-               }
-               if (!unfollowedSoneStillFollowed) {
-                       synchronized (soneFollowingTimes) {
-                               soneFollowingTimes.remove(soneId);
-                       }
-               }
-               touchConfiguration();
        }
 
        /**
@@ -1276,17 +1258,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                try {
                        preferences.saveTo(configuration);
 
-                       /* save Sone following times. */
-                       int soneCounter = 0;
-                       synchronized (soneFollowingTimes) {
-                               for (Entry<String, Long> soneFollowingTime : soneFollowingTimes.entrySet()) {
-                                       configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey());
-                                       configuration.getLongValue("SoneFollowingTimes/" + soneCounter + "/Time").setValue(soneFollowingTime.getValue());
-                                       ++soneCounter;
-                               }
-                               configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(null);
-                       }
-
                        /* save known posts. */
                        database.save();
 
@@ -1309,20 +1280,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         */
        private void loadConfiguration() {
                new PreferencesLoader(preferences).loadFrom(configuration);
-
-               /* load Sone following times. */
-               int 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);
-                       synchronized (soneFollowingTimes) {
-                               soneFollowingTimes.put(soneId, time);
-                       }
-                       ++soneCounter;
-               }
        }
 
        /**