-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds initial mastodon status stream support
- listen to status updates containing defined hash tags - listen to status messages of defined users Signed-off-by: Patrick Reinhart <[email protected]>
- Loading branch information
Showing
17 changed files
with
1,574 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...impl-mastodon4j/src/main/java/org/tweetwallfx/tweet/impl/mastodon4j/AccountPredicate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2023 TweetWallFX | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.tweetwallfx.tweet.impl.mastodon4j; | ||
|
||
import org.mastodon4j.core.api.entities.Account; | ||
import org.mastodon4j.core.api.entities.Status; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.TreeSet; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
public class AccountPredicate implements Predicate<Status> { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(AccountPredicate.class); | ||
private final Set<String> userList; | ||
|
||
public AccountPredicate(List<String> userList) { | ||
this.userList = Objects.requireNonNull(userList, "userList must not be null").stream() | ||
.map(user -> user.substring(1)) | ||
.collect(Collectors.toCollection(() -> new TreeSet<>(String::compareToIgnoreCase))); | ||
} | ||
|
||
@Override | ||
public boolean test(Status status) { | ||
final String username = status.account().username(); | ||
if (userList.contains(username)) { | ||
return true; | ||
} else { | ||
LOGGER.debug("No matching users {} for account {}", userList, username); | ||
return false; | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...l-mastodon4j/src/main/java/org/tweetwallfx/tweet/impl/mastodon4j/EventStatusConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2023 TweetWallFX | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.tweetwallfx.tweet.impl.mastodon4j; | ||
|
||
import jakarta.json.bind.Jsonb; | ||
import jakarta.json.bind.JsonbBuilder; | ||
import org.mastodon4j.core.api.entities.Event; | ||
import org.mastodon4j.core.api.entities.Status; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
import java.util.function.Predicate; | ||
|
||
public class EventStatusConsumer implements Consumer<Event> { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(EventStatusConsumer.class); | ||
private final Consumer<Status> statusConsumer; | ||
private final Predicate<Status> statusPredicate; | ||
|
||
EventStatusConsumer(Consumer<Status> statusConsumer) { | ||
this(statusConsumer, status -> true); | ||
} | ||
|
||
EventStatusConsumer(Consumer<Status> statusConsumer, Predicate<Status> statusPredicate) { | ||
this.statusConsumer = Objects.requireNonNull(statusConsumer, "statusConsumer must not be null"); | ||
this.statusPredicate = Objects.requireNonNull(statusPredicate, "statusPredicate must not be null"); | ||
} | ||
|
||
private void notifyStatusPayload(String payload) { | ||
LOGGER.debug("Processing payload:\n{}", payload); | ||
try (Jsonb jsonb = JsonbBuilder.create()) { | ||
final Status status = jsonb.fromJson(payload, Status.class); | ||
if (statusPredicate.test(status)) { | ||
statusConsumer.accept(status); | ||
} else { | ||
LOGGER.debug("Status {} not matching criteria", status.id()); | ||
} | ||
} catch (Exception e) { | ||
LOGGER.error("Failed to notify status", e); | ||
} | ||
} | ||
|
||
@Override | ||
public void accept(Event event) { | ||
switch (event.event()) { | ||
case "update", "status.update" -> notifyStatusPayload(event.payload()); | ||
default -> LOGGER.debug("Ignoring event:\n{}", event); | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...-impl-mastodon4j/src/main/java/org/tweetwallfx/tweet/impl/mastodon4j/MastodonAccount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2023 TweetWallFX | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.tweetwallfx.tweet.impl.mastodon4j; | ||
|
||
import org.mastodon4j.core.api.entities.Account; | ||
import org.mastodon4j.core.api.entities.Field; | ||
import org.tweetwallfx.tweet.api.User; | ||
|
||
import java.util.Objects; | ||
|
||
public class MastodonAccount implements User { | ||
private final Account account; | ||
|
||
public MastodonAccount(Account account) { | ||
this.account = account; | ||
} | ||
|
||
@Override | ||
public String getBiggerProfileImageUrl() { | ||
return getProfileImageUrl(); | ||
} | ||
|
||
@Override | ||
public long getId() { | ||
return Long.parseLong(account.id()); | ||
} | ||
|
||
@Override | ||
public String getLang() { | ||
return "not-supported"; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return account.username(); | ||
} | ||
|
||
@Override | ||
public String getProfileImageUrl() { | ||
return account.avatar(); | ||
} | ||
|
||
@Override | ||
public String getScreenName() { | ||
return account.display_name(); | ||
} | ||
|
||
@Override | ||
public int getFollowersCount() { | ||
return account.followers_count(); | ||
} | ||
|
||
@Override | ||
public boolean isVerified() { | ||
return account.fields().stream().map(Field::verified_at).anyMatch(Objects::nonNull); | ||
} | ||
} |
Oops, something went wrong.