diff --git a/README.md b/README.md index e1e5ba7..f47311d 100644 --- a/README.md +++ b/README.md @@ -95,14 +95,15 @@ You can now also attach breakpoints in code for debugging purposes, by clicking *Apus* can be started without any specific configuration. All configuration options have working default values. To modify these default values just specify environment variables with the following names: -| Variable | Default | Description | -|-------------------|-----------------|-----------------------------------------------------------------| -| DOAG_EVENT_ID | 0 | The ID of the DOAG event to read the conference agenda. | -| FILTER_REPLIES | true | Hide social media messages which are replies. | -| FILTER_SENSITIVE | true | Hide social media messages which contain sensitive information. | -| MASTODON_INSTANCE | mastodon.social | The Mastodon instance used to read the posts from. | -| MASTODON_HASHTAG | java | The hashtag for the mastodon wall. | -| TZ | UTC | The timezone used for date and time calculations. | +| Variable | Default | Description | +|-------------------|------------------------------------|-----------------------------------------------------------------| +| DOAG_EVENT_ID | 0 | The ID of the DOAG event to read the conference agenda. | +| FILTER_REPLIES | true | Hide social media messages which are replies. | +| FILTER_SENSITIVE | true | Hide social media messages which contain sensitive information. | +| FILTER_WORDS | fuck you, motherfucker, cocksucker | Hide social media messages which contain these words. | +| MASTODON_INSTANCE | mastodon.social | The Mastodon instance used to read the posts from. | +| MASTODON_HASHTAG | java | The hashtag for the mastodon wall. | +| TZ | UTC | The timezone used for date and time calculations. | The environment variables will override the default values. diff --git a/src/main/java/swiss/fihlon/apus/configuration/Filter.java b/src/main/java/swiss/fihlon/apus/configuration/Filter.java index 3118068..6b24c09 100644 --- a/src/main/java/swiss/fihlon/apus/configuration/Filter.java +++ b/src/main/java/swiss/fihlon/apus/configuration/Filter.java @@ -17,4 +17,8 @@ */ package swiss.fihlon.apus.configuration; -public record Filter(boolean replies, boolean sensitive) { } +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public record Filter(boolean replies, boolean sensitive, @NotNull List words) { } diff --git a/src/main/java/swiss/fihlon/apus/service/SocialService.java b/src/main/java/swiss/fihlon/apus/service/SocialService.java index fed1104..1b0f444 100644 --- a/src/main/java/swiss/fihlon/apus/service/SocialService.java +++ b/src/main/java/swiss/fihlon/apus/service/SocialService.java @@ -19,6 +19,7 @@ import jakarta.annotation.PreDestroy; import org.jetbrains.annotations.NotNull; +import org.jsoup.Jsoup; import org.springframework.scheduling.TaskScheduler; import org.springframework.stereotype.Service; import swiss.fihlon.apus.configuration.Configuration; @@ -28,18 +29,21 @@ import java.time.Duration; import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.concurrent.ScheduledFuture; @Service public final class SocialService { private static final Duration UPDATE_FREQUENCY = Duration.ofMinutes(1); + private static final Locale DEFAULT_LOCALE = Locale.getDefault(); private final ScheduledFuture updateScheduler; private final MastodonAPI mastodonAPI; private final String hashtag; private final boolean filterReplies; private final boolean filterSensitive; + private final List filterWords; private List messages = List.of(); public SocialService(@NotNull final TaskScheduler taskScheduler, @@ -48,6 +52,9 @@ public SocialService(@NotNull final TaskScheduler taskScheduler, hashtag = configuration.getMastodon().hashtag(); filterReplies = configuration.getFilter().replies(); filterSensitive = configuration.getFilter().sensitive(); + filterWords = configuration.getFilter().words().stream() + .map(filterWord -> filterWord.toLowerCase(DEFAULT_LOCALE).trim()) + .toList(); updateMessages(); updateScheduler = taskScheduler.scheduleAtFixedRate(this::updateMessages, UPDATE_FREQUENCY); } @@ -61,12 +68,23 @@ private void updateMessages() { final var newMessages = mastodonAPI.getMessages(hashtag).stream() .filter(message -> !filterSensitive || !message.isSensitive()) .filter(message -> !filterReplies || !message.isReply()) + .filter(this::checkWordFilter) .toList(); synchronized (this) { messages = newMessages; } } + private boolean checkWordFilter(@NotNull final Message message) { + final String messageText = Jsoup.parse(message.html()).text().toLowerCase(DEFAULT_LOCALE); + for (final String filterWord : filterWords) { + if (messageText.contains(filterWord)) { + return false; + } + } + return true; + } + public List getMessages(final int limit) { synchronized (this) { if (limit <= 0 || messages.isEmpty()) { diff --git a/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 36bc71f..06c0827 100644 --- a/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -24,5 +24,10 @@ "name" : "apus.filter.sensitive", "type" : "java.lang.Boolean", "description" : "Hide social media messages which contain sensitive information." + }, + { + "name" : "apus.filter.words", + "type" : "java.lang.String", + "description" : "Hide social media messages which contain these words." } ] } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7ac86cb..3235483 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -14,3 +14,4 @@ apus.mastodon.instance=${MASTODON_INSTANCE:mastodon.social} apus.mastodon.hashtag=${MASTODON_HASHTAG:java} apus.filter.replies=${FILTER_REPLIES:true} apus.filter.sensitive=${FILTER_SENSITIVE:true} +apus.filter.words=${FILTER_WORDS:fuck you, motherfucker, cocksucker}