From 58359b1e45861fea725aa7accfe5879d2d7fbeb7 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 4 Jan 2023 16:24:47 +0300 Subject: ActivityPub: fix actor to user conversion, handle Block activity --- src/main/java/com/juick/ActivityPubManager.java | 26 +++++++++++++++------- .../java/com/juick/www/api/activity/Profile.java | 9 ++++++-- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/juick/ActivityPubManager.java b/src/main/java/com/juick/ActivityPubManager.java index 1dd3b784..9dc6a4f0 100644 --- a/src/main/java/com/juick/ActivityPubManager.java +++ b/src/main/java/com/juick/ActivityPubManager.java @@ -17,6 +17,9 @@ package com.juick; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.json.JsonMapper; import com.juick.model.Message; import com.juick.model.Reaction; import com.juick.model.User; @@ -69,6 +72,8 @@ public class ActivityPubManager implements ActivityListener, NotificationListene ProfileUriBuilder profileUriBuilder; @Inject ConversionService conversionService; + @Inject + ObjectMapper jsonMapper; @Override public void processFollowEvent(@Nonnull FollowEvent followEvent) { @@ -365,14 +370,19 @@ public class ActivityPubManager implements ActivityListener, NotificationListene logger.warn("Context not found: {}", announce.getActor()); }); } - public User actorToUser(URI uri) throws HttpBadRequestException { - Actor person = (Actor) signatureManager.getContext(uri).orElseThrow(HttpBadRequestException::new); - User user = new User(); - user.setUri(URI.create(person.getId())); - user.setName(person.getPreferredUsername()); - if (person.getIcon() != null) { - user.setAvatar(person.getIcon().getUrl()); + public User actorToUser(URI uri) throws HttpBadRequestException, JsonProcessingException { + var context = signatureManager.getContext(uri); + if (context.isPresent() && context.get() instanceof Actor actor) { + User user = new User(); + user.setUri(URI.create(actor.getId())); + user.setName(actor.getPreferredUsername()); + if (actor.getIcon() != null) { + user.setAvatar(actor.getIcon().getUrl()); + } + return user; + } else { + logger.warn("Unhandled context: {}", jsonMapper.writeValueAsString(context)); + throw new HttpBadRequestException(); } - return user; } } 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 4dcd4910..d2468d70 100644 --- a/src/main/java/com/juick/www/api/activity/Profile.java +++ b/src/main/java/com/juick/www/api/activity/Profile.java @@ -17,6 +17,7 @@ package com.juick.www.api.activity; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.model.Message; import com.juick.model.User; @@ -28,6 +29,7 @@ import com.juick.www.api.activity.helpers.ProfileUriBuilder; import com.juick.www.api.activity.model.Activity; import com.juick.www.api.activity.model.Context; import com.juick.www.api.activity.model.activities.Announce; +import com.juick.www.api.activity.model.activities.Block; import com.juick.www.api.activity.model.activities.Create; import com.juick.www.api.activity.model.activities.Delete; import com.juick.www.api.activity.model.activities.Flag; @@ -40,6 +42,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.util.HttpBadRequestException; import com.juick.util.HttpNotFoundException; import com.juick.service.MessagesService; import com.juick.service.UserService; @@ -70,7 +73,6 @@ 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; @RestController @@ -405,6 +407,9 @@ public class Profile { } } } + if (activity instanceof Block) { + logger.info("{} blocks {} (room_full_of_people_who_care.jpg)", activity.getActor(), activity.getObject().getId()); + } logger.warn("Unknown activity: {}", jsonMapper.writeValueAsString(activity)); return new ResponseEntity<>(CommandResult.fromString("Unknown activity"), HttpStatus.NOT_IMPLEMENTED); } @@ -418,7 +423,7 @@ public class Profile { } @PostMapping(value = { "/u/", "/api/u/" }, produces = MediaType.APPLICATION_JSON_VALUE) - public User fetchUser(@RequestParam URI uri) { + public User fetchUser(@RequestParam URI uri) throws JsonProcessingException, HttpBadRequestException { return activityPubManager.actorToUser(uri); } } -- cgit v1.2.3