Remove Sone following time if last local Sone unfollows
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 7 Feb 2018 05:59:09 +0000 (06:59 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 7 Feb 2018 05:59:09 +0000 (06:59 +0100)
src/main/java/net/pterodactylus/sone/database/memory/MemoryFriendDatabase.java
src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java

index a2d51a3..692d7c4 100644 (file)
@@ -66,6 +66,13 @@ 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();
index 31532e2..bbf1477 100644 (file)
@@ -429,4 +429,32 @@ public class MemoryDatabaseTest {
                assertThat(configuration.getStringValue("SoneFollowingTimes/1/Sone").getValue(), nullValue());
        }
 
+       @Test
+       public void unfollowingASoneRemovesTheFollowingTime() throws ConfigurationException {
+               prepareConfigurationValues();
+               configuration.getStringValue("Sone/sone/Friends/0/ID").setValue("Friend");
+               configuration.getStringValue("SoneFollowingTimes/0/Sone").setValue("Friend");
+               configuration.getLongValue("SoneFollowingTimes/0/Time").setValue(1000L);
+               when(sone.isLocal()).thenReturn(true);
+               memoryDatabase.removeFriend(sone, "Friend");
+               assertThat(configuration.getStringValue("SoneFollowingTimes/0/Sone").getValue(), nullValue());
+       }
+
+       @Test
+       public void unfollowingASoneDoesNotRemoveTheFollowingTimeIfAnotherLocalSoneFollowsIt() throws ConfigurationException {
+               prepareConfigurationValues();
+               configuration.getStringValue("Sone/sone/Friends/0/ID").setValue("Friend");
+               configuration.getStringValue("Sone/other-sone/Friends/0/ID").setValue("Friend");
+               configuration.getStringValue("SoneFollowingTimes/0/Sone").setValue("Friend");
+               configuration.getLongValue("SoneFollowingTimes/0/Time").setValue(1000L);
+               Sone otherSone = mock(Sone.class);
+               when(otherSone.isLocal()).thenReturn(true);
+               when(otherSone.getId()).thenReturn("other-sone");
+               memoryDatabase.getFriends(otherSone);
+               when(sone.isLocal()).thenReturn(true);
+               memoryDatabase.removeFriend(sone, "Friend");
+               assertThat(configuration.getStringValue("SoneFollowingTimes/0/Sone").getValue(), equalTo("Friend"));
+               assertThat(configuration.getLongValue("SoneFollowingTimes/0/Time").getValue(), equalTo(1000L));
+       }
+
 }