From: David ‘Bombe’ Roden Date: Wed, 7 Feb 2018 05:59:09 +0000 (+0100) Subject: Remove Sone following time if last local Sone unfollows X-Git-Tag: v79^2~142 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=52014e28ee29c9620dbcc842d8bf543aaadead9e Remove Sone following time if last local Sone unfollows --- diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryFriendDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryFriendDatabase.java index a2d51a3..692d7c4 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryFriendDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryFriendDatabase.java @@ -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(); diff --git a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java index 31532e2..bbf1477 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java @@ -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)); + } + }