From 9100b5bda037fcd1b051b98585744077132320bc Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 22 Aug 2021 00:13:02 +0300 Subject: Type-safe ActivityStreams deserialization --- .../java/com/juick/www/api/activity/Profile.java | 121 +++++++++------------ 1 file changed, 54 insertions(+), 67 deletions(-) (limited to 'src/main/java/com/juick/www/api/activity/Profile.java') 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 72cc849e..751d0cff 100644 --- a/src/main/java/com/juick/www/api/activity/Profile.java +++ b/src/main/java/com/juick/www/api/activity/Profile.java @@ -40,6 +40,7 @@ import com.juick.www.api.activity.model.objects.Note; import com.juick.www.api.activity.model.objects.OrderedCollection; import com.juick.www.api.activity.model.objects.OrderedCollectionPage; import com.juick.www.api.activity.model.objects.Person; +import com.juick.www.api.activity.model.objects.Tombstone; import com.juick.util.HttpNotFoundException; import com.juick.www.WebApp; import com.juick.service.MessagesService; @@ -310,32 +311,52 @@ public class Profile { } } if (activity instanceof Create) { - if (activity.getObject() instanceof Map) { - Map note = (Map) activity.getObject(); - if (note.get("type").equals("Note")) { - URI noteId = URI.create((String) note.get("id")); - if (messagesService.replyExists(noteId)) { - return new ResponseEntity<>(CommandResult.fromString("Reply already exists"), - HttpStatus.OK); - } else { - String inReplyTo = (String) note.get("inReplyTo"); - if (StringUtils.isNotBlank(inReplyTo)) { - if (inReplyTo.startsWith(baseUri)) { - String postId = activityPubManager.postId(inReplyTo); + if (activity.getObject() instanceof Note) { + Note note = (Note) activity.getObject(); + URI noteId = URI.create((String) note.getId()); + if (messagesService.replyExists(noteId)) { + return new ResponseEntity<>(CommandResult.fromString("Reply already exists"), HttpStatus.OK); + } else { + String inReplyTo = (String) note.getInReplyTo(); + if (StringUtils.isNotBlank(inReplyTo)) { + if (inReplyTo.startsWith(baseUri)) { + String postId = activityPubManager.postId(inReplyTo); + User user = new User(); + user.setUri(URI.create(activity.getActor())); + String markdown = remarkConverter.convertFragment((String) note.getContent()); + String commandBody = note.getAttachment() == null ? markdown + : note.getAttachment().stream().map(attachment -> { + String attachmentUrl = attachment.getUrl(); + String attachmentName = attachment.getName(); + return PlainTextFormatter.markdownUrl(attachmentUrl, attachmentName); + }).reduce(markdown, + (currentUrl, nextUrl) -> String.format("%s\n%s", currentUrl, nextUrl)); + + CommandResult result = commandsManager.processCommand(user, + String.format("#%s %s", postId, commandBody), URI.create(StringUtils.EMPTY)); + logger.info(jsonMapper.writeValueAsString(result)); + if (result.getNewMessage().isPresent()) { + messagesService.updateReplyUri(result.getNewMessage().get(), noteId); + return new ResponseEntity<>(result, HttpStatus.OK); + } else { + return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST); + } + } else { + Message reply = messagesService.getReplyByUri(inReplyTo); + if (reply != null) { User user = new User(); user.setUri(URI.create(activity.getActor())); - String markdown = remarkConverter.convertFragment((String) note.get("content")); - String commandBody = note.get("attachment") == null ? markdown - : ((List) note.get("attachment")).stream().map(attachmentObj -> { - Map attachment = (Map) attachmentObj; - String attachmentUrl = attachment.get("url"); - String attachmentName = attachment.get("name"); + String markdown = remarkConverter.convertFragment((String) note.getContent()); + // combine note text with attachment urls + String commandBody = note.getAttachment() == null ? markdown + : note.getAttachment().stream().map(attachment -> { + String attachmentUrl = attachment.getUrl(); + String attachmentName = attachment.getName(); return PlainTextFormatter.markdownUrl(attachmentUrl, attachmentName); }).reduce(markdown, (currentUrl, nextUrl) -> String.format("%s\n%s", currentUrl, nextUrl)); - CommandResult result = commandsManager.processCommand(user, - String.format("#%s %s", postId, commandBody), + String.format("#%d/%d %s", reply.getMid(), reply.getRid(), commandBody), URI.create(StringUtils.EMPTY)); logger.info(jsonMapper.writeValueAsString(result)); if (result.getNewMessage().isPresent()) { @@ -344,34 +365,6 @@ public class Profile { } else { return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST); } - } else { - Message reply = messagesService.getReplyByUri(inReplyTo); - if (reply != null) { - User user = new User(); - user.setUri(URI.create(activity.getActor())); - String markdown = remarkConverter.convertFragment((String) note.get("content")); - // combine note text with attachment urls - String commandBody = note.get("attachment") == null ? markdown - : ((List) note.get("attachment")).stream() - .map(attachmentObj -> { - Map attachment = (Map) attachmentObj; - String attachmentUrl = attachment.get("url"); - String attachmentName = attachment.get("name"); - return PlainTextFormatter.markdownUrl(attachmentUrl, - attachmentName); - }).reduce(markdown, (currentUrl, nextUrl) -> String - .format("%s\n%s", currentUrl, nextUrl)); - CommandResult result = commandsManager.processCommand(user, - String.format("#%d/%d %s", reply.getMid(), reply.getRid(), commandBody), - URI.create(StringUtils.EMPTY)); - logger.info(jsonMapper.writeValueAsString(result)); - if (result.getNewMessage().isPresent()) { - messagesService.updateReplyUri(result.getNewMessage().get(), noteId); - return new ResponseEntity<>(result, HttpStatus.OK); - } else { - return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST); - } - } } } } @@ -379,26 +372,23 @@ public class Profile { } } if (activity instanceof Delete) { - if (activity.getObject() instanceof String) { - // Delete gone user - // TODO: check if it is really deleted and remove copy-paste - if (activity.getActor().equals(activity.getObject())) { - return new ResponseEntity<>(CommandResult.fromString("Delete request accepted"), - HttpStatus.ACCEPTED); - } + // Delete gone user + // TODO: check if it is really deleted and remove copy-paste + if (activity.getActor().equals(activity.getObject().getUrl())) { + return new ResponseEntity<>(CommandResult.fromString("Delete request accepted"), + HttpStatus.ACCEPTED); } - Map tombstone = (Map) activity.getObject(); - if (tombstone.get("type").equals("Tombstone")) { + + if (activity.getObject() instanceof Tombstone) { + Tombstone tombstone = (Tombstone) activity.getObject(); URI actor = URI.create(activity.getActor()); - URI reply = URI.create((String) tombstone.get("id")); + URI reply = URI.create((String) tombstone.getId()); messagesService.deleteReply(actor, reply); return new ResponseEntity<>(CommandResult.fromString("Delete request accepted"), HttpStatus.OK); } } if (activity instanceof Like || activity instanceof Announce) { - String messageUri = activity.getObject() instanceof String ? (String) activity.getObject() - : activity.getObject() instanceof Context ? ((Context) activity.getObject()).getId() - : (String) ((Map) activity.getObject()).get("id"); + String messageUri = activity.getObject().getId(); applicationEventPublisher.publishEvent(new AnnounceEvent(this, activity.getActor(), messageUri)); return new ResponseEntity<>(CommandResult.fromString("Like/announce request accepted"), HttpStatus.OK); } @@ -411,12 +401,9 @@ public class Profile { return new ResponseEntity<>(CommandResult.fromString("Unknown activity"), HttpStatus.NOT_IMPLEMENTED); } if (activity instanceof Delete) { - if (activity.getObject() instanceof String) { - // Delete gone user - if (activity.getActor().equals(activity.getObject())) { - return new ResponseEntity<>(CommandResult.fromString("Delete request accepted"), - HttpStatus.ACCEPTED); - } + // Delete gone user + if (activity.getActor().equals(activity.getObject().getId())) { + return new ResponseEntity<>(CommandResult.fromString("Delete request accepted"), HttpStatus.ACCEPTED); } } return new ResponseEntity<>(CommandResult.fromString("Can not authenticate"), HttpStatus.UNAUTHORIZED); -- cgit v1.2.3