Skip to content

Commit

Permalink
♻️ refactor configuration to be easier extendable #58
Browse files Browse the repository at this point in the history
  • Loading branch information
McPringle committed Apr 18, 2024
1 parent f41f935 commit 8b2af1e
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@

import org.jetbrains.annotations.NotNull;

public record Admin(@NotNull String password) { }
public record AdminConfig(@NotNull String password) { }
50 changes: 33 additions & 17 deletions src/main/java/swiss/fihlon/apus/configuration/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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;
}
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@

import java.util.List;

public record Filter(int length, boolean replies, boolean sensitive, @NotNull List<String> words) { }
public record FilterConfig(int length, boolean replies, boolean sensitive, @NotNull List<String> words) { }
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package swiss.fihlon.apus.configuration;
package swiss.fihlon.apus.plugin.conference.doag;

public record DOAG(int eventId) { }
public record DoagConfig(int eventId) { }
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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) { }
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private String getProfile(@NotNull final Account account) {
private List<String> getImages(@NotNull final List<MediaAttachment> mediaAttachments) {
final List<String> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Message> messages = mastodonPlugin.getMessages();
Expand Down

0 comments on commit 8b2af1e

Please sign in to comment.