diff options
author | Vitaly Takmazov | 2023-01-09 09:45:12 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2023-01-09 09:45:12 +0300 |
commit | f6b78c029a1e9e4f09b600131e99bbc13c65f30a (patch) | |
tree | 33511c92dd418d1eced2b4755fc16a583c135d06 /src/main/java/com/juick/ActivityPubManager.java | |
parent | 6b08c391d27a682ffece9e9c471986160e2f94af (diff) |
Mastodon API: statuses
Diffstat (limited to 'src/main/java/com/juick/ActivityPubManager.java')
-rw-r--r-- | src/main/java/com/juick/ActivityPubManager.java | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/src/main/java/com/juick/ActivityPubManager.java b/src/main/java/com/juick/ActivityPubManager.java index b62be93a..e9a04197 100644 --- a/src/main/java/com/juick/ActivityPubManager.java +++ b/src/main/java/com/juick/ActivityPubManager.java @@ -31,6 +31,7 @@ import com.juick.service.component.NotificationListener; import com.juick.service.component.PingEvent; import com.juick.service.component.SystemEvent; import com.juick.util.HttpBadRequestException; +import com.juick.util.HttpNotFoundException; import com.juick.util.HttpUtils; import com.juick.util.MessageUtils; import com.juick.util.formatters.PlainTextFormatter; @@ -149,11 +150,11 @@ public class ActivityPubManager implements ActivityListener, NotificationListene UriComponents uriComponents = UriComponentsBuilder.fromUriString(event.getMessageUri()).build(); List<String> segments = uriComponents.getPathSegments(); if (segments.get(0).equals("n")) { - String[] ids = segments.get(1).split("-", 2); - if (ids.length == 2 && Integer.parseInt(ids[1]) == 0) { + var message = findMessage(segments.get(1)); + if (message != null && !MessageUtils.isReply(message)) { // only messages - logger.info("{} recommends {}", event.getActorUri(), Integer.valueOf(ids[0])); - messagesService.likeMessage(Integer.parseInt(ids[0]), 0, Reaction.LIKE, event.getActorUri()); + logger.info("{} recommends {}", event.getActorUri(), message.getMid()); + messagesService.likeMessage(message.getMid(), 0, Reaction.LIKE, event.getActorUri()); } } } @@ -163,11 +164,11 @@ public class ActivityPubManager implements ActivityListener, NotificationListene UriComponents uriComponents = UriComponentsBuilder.fromUriString(event.getMessageUri()).build(); List<String> segments = uriComponents.getPathSegments(); if (segments.get(0).equals("n")) { - String[] ids = segments.get(1).split("-", 2); - if (ids.length == 2 && Integer.parseInt(ids[1]) == 0) { + var message = findMessage(segments.get(1)); + if (message != null && !MessageUtils.isReply(message)) { // only messages - logger.info("{} stop recommending {}", event.getActorUri(), Integer.valueOf(ids[0])); - messagesService.likeMessage(Integer.parseInt(ids[0]), 0, null, event.getActorUri()); + logger.info("{} stop recommending {}", event.getActorUri(), message.getMid()); + messagesService.likeMessage(message.getMid(), 0, null, event.getActorUri()); } } } @@ -382,7 +383,7 @@ public class ActivityPubManager implements ActivityListener, NotificationListene activityToFollowers(user, me, announce); } - public User actorToUser(URI uri) throws HttpBadRequestException, JsonProcessingException { + public User actorToUser(URI uri) throws HttpBadRequestException { var context = activityPubService.get(uri); if (context.isPresent() && context.get() instanceof Actor actor) { User user = new User(); @@ -397,4 +398,22 @@ public class ActivityPubManager implements ActivityListener, NotificationListene throw new HttpBadRequestException(); } } + public Message findMessage(String statusId) throws HttpNotFoundException { + String[]ids = statusId.split("-", 2); + try { + if (ids.length == 2) { + return findMessage(Integer.parseInt(ids[0]), Integer.parseInt(ids[1])); + } + } catch (Exception e) { + logger.warn("{}: message not found, error: {}", statusId, e.getMessage()); + } + throw new HttpNotFoundException(); + } + public Message findMessage(int mid, int rid) throws HttpNotFoundException { + try { + return rid > 0 ? messagesService.getReply(mid, rid) : messagesService.getMessage(mid).get(); + } catch (NoSuchElementException e) { + throw new HttpNotFoundException(); + } + } } |