From 52014e28ee29c9620dbcc842d8bf543aaadead9e Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 7 Feb 2018 06:59:09 +0100 Subject: [PATCH] Remove Sone following time if last local Sone unfollows --- .../sone/database/memory/MemoryFriendDatabase.java | 7 ++++++ .../sone/database/memory/MemoryDatabaseTest.java | 28 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) 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)); + } + } -- 2.7.4