Skip to content

Commit

Permalink
♻️ Refactor SocialService to handle multiple plugins closes #92
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Fihlon <[email protected]>
  • Loading branch information
McPringle committed Apr 29, 2024
1 parent 12dab8d commit 1cdf2a6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
12 changes: 7 additions & 5 deletions src/main/java/swiss/fihlon/apus/plugin/social/SocialService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SocialPlugin> socialPlugins;
private final int filterLength;
private final boolean filterReplies;
private final boolean filterSensitive;
Expand All @@ -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<SocialPlugin> socialPlugins) {
this.socialPlugins = socialPlugins;
filterLength = configuration.getFilter().length();
filterReplies = configuration.getFilter().replies();
filterSensitive = configuration.getFilter().sensitive();
Expand All @@ -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 {
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Post> 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<Post> posts = socialService.getPosts(5);
assertEquals(5, posts.size());
assertEquals("P1", posts.get(0).id());
Expand All @@ -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<Post> postsBefore = socialService.getPosts(10);
assertEquals(10, postsBefore.size());

Expand All @@ -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<Post> postsBefore = socialService.getPosts(10);
assertEquals(10, postsBefore.size());

Expand All @@ -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<Post> posts = socialService.getPosts(0);
final List<String> ids = posts.stream().map(Post::id).distinct().toList();
assertFalse(ids.contains("P5"));
Expand All @@ -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<Post> posts = socialService.getPosts(0);
final List<String> profiles = posts.stream().map(Post::profile).distinct().toList();
assertFalse(profiles.contains("profile1@localhost"));
Expand Down

0 comments on commit 1cdf2a6

Please sign in to comment.