Skip to content

Commit

Permalink
✨ add word filter to social media messages closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
McPringle committed Mar 31, 2024
1 parent 87e5f11 commit 13c5209
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
6 changes: 5 additions & 1 deletion src/main/java/swiss/fihlon/apus/configuration/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> words) { }
18 changes: 18 additions & 0 deletions src/main/java/swiss/fihlon/apus/service/SocialService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> filterWords;
private List<Message> messages = List.of();

public SocialService(@NotNull final TaskScheduler taskScheduler,
Expand All @@ -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);
}
Expand All @@ -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<Message> getMessages(final int limit) {
synchronized (this) {
if (limit <= 0 || messages.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
] }
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}

0 comments on commit 13c5209

Please sign in to comment.