Remove Sone following time if last local Sone unfollows
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryFriendDatabase.java
index 0be8738..692d7c4 100644 (file)
@@ -4,6 +4,9 @@ import java.util.Collection;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
 import com.google.common.collect.HashMultimap;
 import com.google.common.collect.Multimap;
 
@@ -48,6 +51,9 @@ class MemoryFriendDatabase {
                try {
                        if (soneFriends.put(localSoneId, friendSoneId)) {
                                configurationLoader.saveFriends(localSoneId, soneFriends.get(localSoneId));
+                               if (configurationLoader.getSoneFollowingTime(friendSoneId) == null) {
+                                       configurationLoader.setSoneFollowingTime(friendSoneId, System.currentTimeMillis());
+                               }
                        }
                } finally {
                        lock.writeLock().unlock();
@@ -60,12 +66,24 @@ class MemoryFriendDatabase {
                try {
                        if (soneFriends.remove(localSoneId, friendSoneId)) {
                                configurationLoader.saveFriends(localSoneId, soneFriends.get(localSoneId));
+                               boolean unfollowedSoneStillFollowed = false;
+                               for (String soneId : soneFriends.keys()) {
+                                       unfollowedSoneStillFollowed |= getFriends(soneId).contains(friendSoneId);
+                               }
+                               if (!unfollowedSoneStillFollowed) {
+                                       configurationLoader.removeSoneFollowingTime(friendSoneId);
+                               }
                        }
                } finally {
                        lock.writeLock().unlock();
                }
        }
 
+       @Nullable
+       Long getFollowingTime(@Nonnull String soneId) {
+               return configurationLoader.getSoneFollowingTime(soneId);
+       }
+
        private void loadFriends(String localSoneId) {
                lock.writeLock().lock();
                try {