From 8b2af1e107c89191b924c8020dcd06c8b60a4223 Mon Sep 17 00:00:00 2001 From: Marcus Fihlon Date: Thu, 18 Apr 2024 09:56:33 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor=20configuration?= =?UTF-8?q?=20to=20be=20easier=20extendable=20#58?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- .../{Admin.java => AdminConfig.java} | 2 +- .../apus/configuration/Configuration.java | 50 ++++++++++++------- .../{Filter.java => FilterConfig.java} | 2 +- .../conference/doag/DoagConfig.java} | 4 +- .../social/mastodon/MastodonConfig.java} | 4 +- .../social/mastodon/MastodonPlugin.java | 2 +- .../conference/ConferenceServiceTest.java | 4 +- .../social/mastodon/MastodonPluginTest.java | 3 +- 9 files changed, 45 insertions(+), 30 deletions(-) rename src/main/java/swiss/fihlon/apus/configuration/{Admin.java => AdminConfig.java} (94%) rename src/main/java/swiss/fihlon/apus/configuration/{Filter.java => FilterConfig.java} (89%) rename src/main/java/swiss/fihlon/apus/{configuration/DOAG.java => plugin/conference/doag/DoagConfig.java} (89%) rename src/main/java/swiss/fihlon/apus/{configuration/Mastodon.java => plugin/social/mastodon/MastodonConfig.java} (84%) diff --git a/README.md b/README.md index 774b3ce..173e92c 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ You can now also attach breakpoints in code for debugging purposes, by clicking | MASTODON_INSTANCE | | The Mastodon instance used to read the posts from (empty = disabled). | | MASTODON_HASHTAG | | The hashtag for the mastodon wall (empty = disabled). | | MASTODON_IMAGES_ENABLED | true | Enable or disable images in mastodon posts. | -| MASTODON_IMAGE_LIMIT | 1 | Limit number of images per post. | +| MASTODON_IMAGE_LIMIT | 1 | Limit number of images per post (0 = no limit). | | TZ | UTC | The timezone used for date and time calculations. | The environment variables will override the default values. @@ -204,7 +204,7 @@ Plugins for other social media services are planned. Everyone is welcome to contribute a plugin themselves. The implementation is very simple. There are two types of plugins: `ConferencePlugin` and `SocialPlugin`. For a new plugin, a new package is created under `swiss.fihlon.apus.plugin.conference` or `swiss.fihlon.apus.plugin.social`, based on the plugin type. The implementation is carried out in this new package. Implement one of these two interfaces depending on the plugin type you want to contribute. -If your implementation requires a configuration, the package `swiss.fihlon.apus.configuration` must be extended accordingly. Default settings belong in the file `application.properties` and the corresponding schema is stored in `additional-spring-configuration-metadata.json`. Of course, `README.md` must also be adapted. +If your implementation requires a configuration, the `Configuration` class must be extended accordingly. Add a property and corresponding setters and getters in the marked sections. Implement the settings object as a `record` in your new plugin package. Take one of the existing plugins as a template. Default settings belong in the file `application.properties` and the corresponding schema is stored in `additional-spring-configuration-metadata.json`. Of course, this `README.md` must also be adapted. ## Communication diff --git a/src/main/java/swiss/fihlon/apus/configuration/Admin.java b/src/main/java/swiss/fihlon/apus/configuration/AdminConfig.java similarity index 94% rename from src/main/java/swiss/fihlon/apus/configuration/Admin.java rename to src/main/java/swiss/fihlon/apus/configuration/AdminConfig.java index 77fbd68..ad0ae46 100644 --- a/src/main/java/swiss/fihlon/apus/configuration/Admin.java +++ b/src/main/java/swiss/fihlon/apus/configuration/AdminConfig.java @@ -19,4 +19,4 @@ import org.jetbrains.annotations.NotNull; -public record Admin(@NotNull String password) { } +public record AdminConfig(@NotNull String password) { } diff --git a/src/main/java/swiss/fihlon/apus/configuration/Configuration.java b/src/main/java/swiss/fihlon/apus/configuration/Configuration.java index 7c8a54c..472183d 100644 --- a/src/main/java/swiss/fihlon/apus/configuration/Configuration.java +++ b/src/main/java/swiss/fihlon/apus/configuration/Configuration.java @@ -20,6 +20,8 @@ import org.jetbrains.annotations.NotNull; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import swiss.fihlon.apus.plugin.conference.doag.DoagConfig; +import swiss.fihlon.apus.plugin.social.mastodon.MastodonConfig; @org.springframework.context.annotation.Configuration @ConfigurationProperties(prefix = "apus") @@ -28,10 +30,15 @@ public class Configuration { private String version; - private Admin admin; - private DOAG doag; - private Mastodon mastodon; - private Filter filter; + private AdminConfig admin; + private FilterConfig filter; + + // Conference Plugin Configs + private DoagConfig doag; + + // Social Plugin Configs + private MastodonConfig mastodon; + public String getVersion() { return version; } @@ -40,35 +47,44 @@ public void setVersion(@NotNull final String version) { this.version = version; } - public Admin getAdmin() { + public AdminConfig getAdmin() { return admin; } - public void setAdmin(@NotNull final Admin admin) { + public void setAdmin(@NotNull final AdminConfig admin) { this.admin = admin; } - public DOAG getDoag() { + public FilterConfig getFilter() { + return filter; + } + + public void setFilter(@NotNull final FilterConfig filter) { + this.filter = filter; + } + + /////////////////////////////// + // Conference Plugin Configs // + /////////////////////////////// + + public DoagConfig getDoag() { return doag; } - public void setDoag(@NotNull final DOAG doag) { + public void setDoag(@NotNull final DoagConfig doag) { this.doag = doag; } - public Mastodon getMastodon() { + /////////////////////////// + // Social Plugin Configs // + /////////////////////////// + + public MastodonConfig getMastodon() { return mastodon; } - public void setMastodon(@NotNull final Mastodon mastodon) { + public void setMastodon(@NotNull final MastodonConfig mastodon) { this.mastodon = mastodon; } - public Filter getFilter() { - return filter; - } - - public void setFilter(@NotNull final Filter filter) { - this.filter = filter; - } } diff --git a/src/main/java/swiss/fihlon/apus/configuration/Filter.java b/src/main/java/swiss/fihlon/apus/configuration/FilterConfig.java similarity index 89% rename from src/main/java/swiss/fihlon/apus/configuration/Filter.java rename to src/main/java/swiss/fihlon/apus/configuration/FilterConfig.java index 97c848a..b4760a6 100644 --- a/src/main/java/swiss/fihlon/apus/configuration/Filter.java +++ b/src/main/java/swiss/fihlon/apus/configuration/FilterConfig.java @@ -21,4 +21,4 @@ import java.util.List; -public record Filter(int length, boolean replies, boolean sensitive, @NotNull List words) { } +public record FilterConfig(int length, boolean replies, boolean sensitive, @NotNull List words) { } diff --git a/src/main/java/swiss/fihlon/apus/configuration/DOAG.java b/src/main/java/swiss/fihlon/apus/plugin/conference/doag/DoagConfig.java similarity index 89% rename from src/main/java/swiss/fihlon/apus/configuration/DOAG.java rename to src/main/java/swiss/fihlon/apus/plugin/conference/doag/DoagConfig.java index 7e8f927..4a34304 100644 --- a/src/main/java/swiss/fihlon/apus/configuration/DOAG.java +++ b/src/main/java/swiss/fihlon/apus/plugin/conference/doag/DoagConfig.java @@ -15,6 +15,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -package swiss.fihlon.apus.configuration; +package swiss.fihlon.apus.plugin.conference.doag; -public record DOAG(int eventId) { } +public record DoagConfig(int eventId) { } diff --git a/src/main/java/swiss/fihlon/apus/configuration/Mastodon.java b/src/main/java/swiss/fihlon/apus/plugin/social/mastodon/MastodonConfig.java similarity index 84% rename from src/main/java/swiss/fihlon/apus/configuration/Mastodon.java rename to src/main/java/swiss/fihlon/apus/plugin/social/mastodon/MastodonConfig.java index becb55e..c213957 100644 --- a/src/main/java/swiss/fihlon/apus/configuration/Mastodon.java +++ b/src/main/java/swiss/fihlon/apus/plugin/social/mastodon/MastodonConfig.java @@ -15,6 +15,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -package swiss.fihlon.apus.configuration; +package swiss.fihlon.apus.plugin.social.mastodon; -public record Mastodon(String instance, String hashtag, boolean imagesEnabled, int imageLimit) { } +public record MastodonConfig(String instance, String hashtag, boolean imagesEnabled, int imageLimit) { } diff --git a/src/main/java/swiss/fihlon/apus/plugin/social/mastodon/MastodonPlugin.java b/src/main/java/swiss/fihlon/apus/plugin/social/mastodon/MastodonPlugin.java index 1b1b87c..26032ba 100644 --- a/src/main/java/swiss/fihlon/apus/plugin/social/mastodon/MastodonPlugin.java +++ b/src/main/java/swiss/fihlon/apus/plugin/social/mastodon/MastodonPlugin.java @@ -107,7 +107,7 @@ private String getProfile(@NotNull final Account account) { private List getImages(@NotNull final List mediaAttachments) { final List images = new ArrayList<>(); for (final MediaAttachment mediaAttachment : mediaAttachments) { - if (images.size() < imageLimit + if (imageLimit == 0 || images.size() < imageLimit && MediaAttachment.MediaType.IMAGE.equals(mediaAttachment.getType())) { images.add(mediaAttachment.getUrl()); } diff --git a/src/test/java/swiss/fihlon/apus/plugin/conference/ConferenceServiceTest.java b/src/test/java/swiss/fihlon/apus/plugin/conference/ConferenceServiceTest.java index 7190438..5531748 100644 --- a/src/test/java/swiss/fihlon/apus/plugin/conference/ConferenceServiceTest.java +++ b/src/test/java/swiss/fihlon/apus/plugin/conference/ConferenceServiceTest.java @@ -24,7 +24,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.scheduling.support.NoOpTaskScheduler; import swiss.fihlon.apus.configuration.Configuration; -import swiss.fihlon.apus.configuration.DOAG; +import swiss.fihlon.apus.plugin.conference.doag.DoagConfig; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.when; @@ -39,7 +39,7 @@ class ConferenceServiceTest { @Disabled // TODO inject test data instead of relying on an external API void displaySampleData() { - when(configuration.getDoag()).thenReturn(new DOAG(773)); + when(configuration.getDoag()).thenReturn(new DoagConfig(773)); final ConferenceService conferenceService = new ConferenceService(new NoOpTaskScheduler(), configuration); assertEquals(12, conferenceService.getRoomsWithSessions().size()); diff --git a/src/test/java/swiss/fihlon/apus/plugin/social/mastodon/MastodonPluginTest.java b/src/test/java/swiss/fihlon/apus/plugin/social/mastodon/MastodonPluginTest.java index 1a47bf9..40eb2ae 100644 --- a/src/test/java/swiss/fihlon/apus/plugin/social/mastodon/MastodonPluginTest.java +++ b/src/test/java/swiss/fihlon/apus/plugin/social/mastodon/MastodonPluginTest.java @@ -22,7 +22,6 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import swiss.fihlon.apus.configuration.Configuration; -import swiss.fihlon.apus.configuration.Mastodon; import swiss.fihlon.apus.social.Message; import java.util.List; @@ -39,7 +38,7 @@ class MastodonPluginTest { @Test void getMessages() { when(configuration.getMastodon()).thenReturn( - new Mastodon("mastodon.social", "java", true, 0)); + new MastodonConfig("mastodon.social", "java", true, 0)); final MastodonPlugin mastodonPlugin = new MastodonPlugin(configuration); final List messages = mastodonPlugin.getMessages();