From 1cdf2a6dc02e904161f0d3c66d28a86f8ff3082f Mon Sep 17 00:00:00 2001 From: Marcus Fihlon Date: Mon, 29 Apr 2024 19:12:31 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20SocialService?= =?UTF-8?q?=20to=20handle=20multiple=20plugins=20closes=20#92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcus Fihlon --- .../fihlon/apus/plugin/social/SocialService.java | 12 +++++++----- .../fihlon/apus/plugin/social/SocialServiceTest.java | 12 ++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/main/java/swiss/fihlon/apus/plugin/social/SocialService.java b/src/main/java/swiss/fihlon/apus/plugin/social/SocialService.java index be041c5..53992b8 100644 --- a/src/main/java/swiss/fihlon/apus/plugin/social/SocialService.java +++ b/src/main/java/swiss/fihlon/apus/plugin/social/SocialService.java @@ -47,7 +47,7 @@ public final class SocialService { private static final Logger LOGGER = LoggerFactory.getLogger(SocialService.class); private final ScheduledFuture updateScheduler; - private final SocialPlugin socialPlugin; + private final List socialPlugins; private final int filterLength; private final boolean filterReplies; private final boolean filterSensitive; @@ -58,8 +58,8 @@ public final class SocialService { public SocialService(@NotNull final TaskScheduler taskScheduler, @NotNull final Configuration configuration, - @NotNull final SocialPlugin socialPlugin) { - this.socialPlugin = socialPlugin; + @NotNull final List socialPlugins) { + this.socialPlugins = socialPlugins; filterLength = configuration.getFilter().length(); filterReplies = configuration.getFilter().replies(); filterSensitive = configuration.getFilter().sensitive(); @@ -68,7 +68,7 @@ public SocialService(@NotNull final TaskScheduler taskScheduler, .toList(); loadHiddenPostIds(); loadBlockedProfiles(); - if (socialPlugin.isEnabled()) { + if (socialPlugins.stream().anyMatch(SocialPlugin::isEnabled)) { updatePosts(); updateScheduler = taskScheduler.scheduleAtFixedRate(this::updatePosts, UPDATE_FREQUENCY); } else { @@ -83,7 +83,9 @@ public void stopUpdateScheduler() { } private void updatePosts() { - final var newPosts = socialPlugin.getPosts().stream() + final var newPosts = socialPlugins.stream() + .filter(SocialPlugin::isEnabled) + .flatMap(socialPlugin -> socialPlugin.getPosts().stream()) .filter(post -> !hiddenPosts.contains(post.id())) .filter(post -> !blockedProfiles.contains(post.profile())) .filter(post -> !filterSensitive || !post.isSensitive()) diff --git a/src/test/java/swiss/fihlon/apus/plugin/social/SocialServiceTest.java b/src/test/java/swiss/fihlon/apus/plugin/social/SocialServiceTest.java index 1f124df..487630e 100644 --- a/src/test/java/swiss/fihlon/apus/plugin/social/SocialServiceTest.java +++ b/src/test/java/swiss/fihlon/apus/plugin/social/SocialServiceTest.java @@ -63,14 +63,14 @@ void cleanUp() { @Test void getPostsWithoutLimit() { - final SocialService socialService = new SocialService(new NoOpTaskScheduler(), configuration, new TestSocialPlugin()); + final SocialService socialService = new SocialService(new NoOpTaskScheduler(), configuration, List.of(new TestSocialPlugin())); final List posts = socialService.getPosts(0); assertEquals(10, posts.size()); } @Test void getPostsWithLimit() { - final SocialService socialService = new SocialService(new NoOpTaskScheduler(), configuration, new TestSocialPlugin()); + final SocialService socialService = new SocialService(new NoOpTaskScheduler(), configuration, List.of(new TestSocialPlugin())); final List posts = socialService.getPosts(5); assertEquals(5, posts.size()); assertEquals("P1", posts.get(0).id()); @@ -82,7 +82,7 @@ void getPostsWithLimit() { @Test void hidePost() { - final SocialService socialService = new SocialService(new NoOpTaskScheduler(), configuration, new TestSocialPlugin()); + final SocialService socialService = new SocialService(new NoOpTaskScheduler(), configuration, List.of(new TestSocialPlugin())); final List postsBefore = socialService.getPosts(10); assertEquals(10, postsBefore.size()); @@ -94,7 +94,7 @@ void hidePost() { @Test void blockProfile() { - final SocialService socialService = new SocialService(new NoOpTaskScheduler(), configuration, new TestSocialPlugin()); + final SocialService socialService = new SocialService(new NoOpTaskScheduler(), configuration, List.of(new TestSocialPlugin())); final List postsBefore = socialService.getPosts(10); assertEquals(10, postsBefore.size()); @@ -108,7 +108,7 @@ void loadHiddenPosts() throws IOException { final var filePath = getConfigDir().resolve("hiddenPosts"); Files.writeString(filePath, "P5\nP6"); - final SocialService socialService = new SocialService(new NoOpTaskScheduler(), configuration, new TestSocialPlugin()); + final SocialService socialService = new SocialService(new NoOpTaskScheduler(), configuration, List.of(new TestSocialPlugin())); final List posts = socialService.getPosts(0); final List ids = posts.stream().map(Post::id).distinct().toList(); assertFalse(ids.contains("P5")); @@ -120,7 +120,7 @@ void loadBlockedProfiles() throws IOException { final var filePath = getConfigDir().resolve("blockedProfiles"); Files.writeString(filePath, "profile1@localhost"); - final SocialService socialService = new SocialService(new NoOpTaskScheduler(), configuration, new TestSocialPlugin()); + final SocialService socialService = new SocialService(new NoOpTaskScheduler(), configuration, List.of(new TestSocialPlugin())); final List posts = socialService.getPosts(0); final List profiles = posts.stream().map(Post::profile).distinct().toList(); assertFalse(profiles.contains("profile1@localhost"));