Skip to content

Commit

Permalink
✨ Add support for configuring multiple hashtags closes #211
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Fihlon <[email protected]>
  • Loading branch information
McPringle committed Sep 17, 2024
1 parent ac32e53 commit 7919154
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ For questions and support requests, please use
* Add configuration option to adjust event dates
* Add configuration option to hide closed event rooms
* Display optional image below the event agenda
* Add support for configuring multiple hashtags

### Fixed Bugs

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ To modify the default configuration values, just specify environment variables w
| FILTER_SENSITIVE | true | Hide social media posts which contain sensitive information. |
| FILTER_WORDS | | Hide social media posts which contain these words. |
| JFS_DB_URL | | The URL of the database file for Java Forum Stuttgart. |
| MASTODON_HASHTAG | | The hashtag for the mastodon wall (empty = disabled). |
| MASTODON_HASHTAGS | | A list of comma separated hashtags for the Mastodon wall (empty = disabled). |
| MASTODON_IMAGE_LIMIT | 1 | Limit number of images per post (0 = no limit). |
| MASTODON_IMAGES_ENABLED | true | Enable or disable images in mastodon posts. |
| MASTODON_INSTANCE | | The Mastodon instance used to read the posts from (empty = disabled). |
Expand Down Expand Up @@ -297,7 +297,7 @@ docker run \
-e ADMIN_PASSWORD=sEcrEt \
-e DEMO_ROOM_COUNT=12 \
-e MASTODON_INSTANCE=mastodon.social \
-e MASTODON_HASHTAG=java \
-e MASTODON_HASHTAGS=java \
-e TZ=CET \
-d \
--rm \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
@Service
public final class DefaultMastodonLoader implements MastodonLoader {

private static final int MASTODON_POST_RANGE_LIMIT = 30;

@Override
@NotNull public List<Status> getStatuses(@NotNull final String instance, @NotNull final String hashtag) throws MastodonException {
final MastodonClient client = new MastodonClient.Builder(instance).build();
final Range range = new Range(null, null, null, 100);
final Range range = new Range(null, null, null, MASTODON_POST_RANGE_LIMIT);
try {
return client.timelines().getTagTimeline(hashtag, LOCAL_AND_REMOTE, range).execute().getPart();
} catch (final BigBoneRequestException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
*/
package swiss.fihlon.apus.plugin.social.mastodon;

public record MastodonConfig(String instance, String hashtag, boolean imagesEnabled, int imageLimit) { }
public record MastodonConfig(String instance, String hashtags, boolean imagesEnabled, int imageLimit) { }
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class MastodonPlugin implements SocialPlugin {

private final MastodonLoader mastodonLoader;
private final String instance;
private final String hashtag;
private final String hashtags;
private final boolean imagesEnabled;
private final int imageLimit;

Expand All @@ -51,24 +51,31 @@ public MastodonPlugin(@NotNull final MastodonLoader mastodonLoader,
this.mastodonLoader = mastodonLoader;
final var mastodonConfig = configuration.getMastodon();
this.instance = mastodonConfig.instance();
this.hashtag = mastodonConfig.hashtag();
this.hashtags = mastodonConfig.hashtags();
this.imagesEnabled = mastodonConfig.imagesEnabled();
this.imageLimit = mastodonConfig.imageLimit();
}

@Override
public boolean isEnabled() {
return instance != null && !instance.isBlank() && hashtag != null && !hashtag.isBlank();
return instance != null && !instance.isBlank() && hashtags != null && !hashtags.isBlank();
}

@Override
public Stream<Post> getPosts() {
try {
LOGGER.info("Starting download of posts with hashtag '{}' from instance '{}'", hashtag, instance);
final List<Status> statuses = mastodonLoader.getStatuses(instance, hashtag);
LOGGER.info("Successfully downloaded {} posts with hashtag '{}' from instance '{}'", statuses.size(), hashtag, instance);
final List<Status> statuses = new ArrayList<>();
for (final String hashtag : hashtags.split(",")) {
if (hashtag.isBlank()) {
continue;
}
LOGGER.info("Starting download of posts with hashtag '{}' from instance '{}'", hashtag, instance);
statuses.addAll(mastodonLoader.getStatuses(instance, hashtag.trim()));
LOGGER.info("Successfully downloaded {} posts with hashtag '{}' from instance '{}'", statuses.size(), hashtag, instance);
}
return statuses.stream()
.map(this::convertToPost)
.distinct()
.sorted();
} catch (final MastodonException e) {
LOGGER.error(e.getMessage(), e);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/swiss/fihlon/apus/ui/view/SocialView.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public SocialView(@NotNull final SocialService socialService,

setId("social-view");
if (configuration.getSocial().headline().isBlank()) {
add(new H2(getTranslation("social.heading", configuration.getMastodon().hashtag())));
add(new H2(getTranslation("social.heading", configuration.getMastodon().hashtags().split(",")[0])));
} else {
add(new H2(configuration.getSocial().headline()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@
"description": "The URL of the database file for Java Forum Stuttgart."
},
{
"name": "apus.mastodon.hashtag",
"name": "apus.mastodon.hashtags",
"type": "java.lang.String",
"description": "The hashtag for the Mastodon wall."
"description": "A list of comma separated hashtags for the Mastodon wall."
},
{
"name": "apus.mastodon.imageLimit",
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ apus.filter.replies=${FILTER_REPLIES:true}
apus.filter.sensitive=${FILTER_SENSITIVE:true}
apus.filter.words=${FILTER_WORDS:}
apus.jfs.dbUrl=${JFS_DB_URL:}
apus.mastodon.hashtag=${MASTODON_HASHTAG:}
apus.mastodon.hashtags=${MASTODON_HASHTAGS:}
apus.mastodon.imageLimit=${MASTODON_IMAGE_LIMIT:1}
apus.mastodon.imagesEnabled=${MASTODON_IMAGES_ENABLED:true}
apus.mastodon.instance=${MASTODON_INSTANCE:}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void testMastodonConfig() {
final var mastodonConfig = configuration.getMastodon();
assertNotNull(mastodonConfig);
assertEquals("ijug.social", mastodonConfig.instance());
assertEquals("java", mastodonConfig.hashtag());
assertEquals("java", mastodonConfig.hashtags());
assertTrue(mastodonConfig.imagesEnabled());
assertEquals(1, mastodonConfig.imageLimit());
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ apus.filter.replies=false
apus.filter.sensitive=true
apus.filter.words=foo, bar, foobar, barfoo
apus.jfs.dbUrl=http://localhost/test.db
apus.mastodon.hashtag=java
apus.mastodon.hashtags=java
apus.mastodon.imageLimit=1
apus.mastodon.imagesEnabled=true
apus.mastodon.instance=ijug.social
Expand Down

0 comments on commit 7919154

Please sign in to comment.