From 174c1af0738a3839586aa3b932540ad9c0c9537b Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 27 Aug 2021 13:27:05 +0300 Subject: ActivityPub: respond with NOT_FOUND when message is not found --- src/main/java/com/juick/www/api/activity/Profile.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src/main/java/com/juick') diff --git a/src/main/java/com/juick/www/api/activity/Profile.java b/src/main/java/com/juick/www/api/activity/Profile.java index c2b36cbe..618ae387 100644 --- a/src/main/java/com/juick/www/api/activity/Profile.java +++ b/src/main/java/com/juick/www/api/activity/Profile.java @@ -77,6 +77,7 @@ import java.io.InputStream; import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.List; +import java.util.NoSuchElementException; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -274,12 +275,15 @@ public class Profile { @GetMapping(value = "/n/{mid}-{rid}", produces = { Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE }) public Context showNote(@PathVariable int mid, @PathVariable int rid) { - if (rid > 0) { - // reply - return Context.build(activityPubManager.makeNote(messagesService.getReply(mid, rid))); + try { + Message message = rid > 0 ? messagesService.getReply(mid, rid) : messagesService.getMessage(mid).get(); + if (message != null) { + return Context.build(activityPubManager.makeNote(message)); + } + } catch (NoSuchElementException e) { + throw new HttpNotFoundException(); } - return Context.build( - activityPubManager.makeNote(messagesService.getMessage(mid).orElseThrow(IllegalStateException::new))); + throw new HttpNotFoundException(); } @PostMapping(value = "/api/inbox", consumes = { Context.LD_JSON_MEDIA_TYPE, @@ -362,13 +366,14 @@ public class Profile { messagesService.updateReplyUri(result.getNewMessage().get(), noteId); return new ResponseEntity<>(result, HttpStatus.OK); } - } + } } } else { if (note.getTo().stream().anyMatch(recipient -> recipient.startsWith(baseUri))) { logger.warn("Possible direct message from {}", note.getAttributedTo()); applicationEventPublisher.publishEvent(new DirectMessageEvent(this, note)); - return new ResponseEntity<>(CommandResult.fromString("Message accepted"), HttpStatus.ACCEPTED); + return new ResponseEntity<>(CommandResult.fromString("Message accepted"), + HttpStatus.ACCEPTED); } } logger.warn("Request with invalid recipient from {}", activity.getActor()); -- cgit v1.2.3