From c9617232d5eeced643b510c108384619a7fc79b0 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 15 Dec 2022 19:51:49 +0300 Subject: Extract user to actor conversion code into Converter --- .../java/com/juick/www/api/activity/Profile.java | 37 ++++------------- .../activity/converters/UserToActorConverter.java | 48 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/juick/www/api/activity/converters/UserToActorConverter.java (limited to 'src/main/java/com/juick/www/api') 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 95fd2fc1..ff1227a3 100644 --- a/src/main/java/com/juick/www/api/activity/Profile.java +++ b/src/main/java/com/juick/www/api/activity/Profile.java @@ -24,7 +24,6 @@ import com.juick.util.formatters.PlainTextFormatter; import com.juick.model.CommandResult; import com.juick.ActivityPubManager; import com.juick.CommandsManager; -import com.juick.KeystoreManager; 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; @@ -36,16 +35,11 @@ import com.juick.www.api.activity.model.activities.Like; import com.juick.www.api.activity.model.activities.Undo; import com.juick.www.api.activity.model.activities.Update; import com.juick.www.api.activity.model.objects.Actor; -import com.juick.www.api.activity.model.objects.Application; -import com.juick.www.api.activity.model.objects.Image; -import com.juick.www.api.activity.model.objects.Key; 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; import com.juick.service.UserService; import com.juick.service.activities.AnnounceEvent; @@ -61,8 +55,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.core.convert.ConversionService; import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -89,8 +83,6 @@ public class Profile { @Inject private MessagesService messagesService; @Inject - private KeystoreManager keystoreManager; - @Inject private ActivityPubManager activityPubManager; @Inject private ApplicationEventPublisher applicationEventPublisher; @@ -103,36 +95,21 @@ public class Profile { @Inject private ObjectMapper jsonMapper; @Inject - private WebApp webApp; - @Inject private Remark remarkConverter; @Inject private User serviceUser; + @Inject + private ConversionService conversionService; @GetMapping(value = "/u/{userName}", produces = { Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE, Context.FALLBACK_JSON_MEDIA_TYPE }) public Actor getUser(@PathVariable String userName) { User user = userService.getUserByName(userName); if (!user.isAnonymous()) { - Actor profile = user.equals(serviceUser) ? new Application() : new Person(); - profile.setId(activityPubManager.personUri(user)); - profile.setUrl(activityPubManager.personWebUri(user)); - profile.setName(userName); - profile.setPreferredUsername(userName); - Key publicKey = new Key(); - publicKey.setId(profile.getId() + "#main-key"); - publicKey.setOwner(profile.getId()); - publicKey.setPublicKeyPem(keystoreManager.getPublicKeyPem()); - profile.setPublicKey(publicKey); - profile.setInbox(activityPubManager.inboxUri()); - profile.setOutbox(activityPubManager.outboxUri(user)); - profile.setFollowers(activityPubManager.followersUri(user)); - profile.setFollowing(activityPubManager.followingUri(user)); - Image avatar = new Image(); - avatar.setUrl(webApp.getAvatarUrl(user)); - avatar.setMediaType("image/png"); - profile.setIcon(avatar); - return (Actor) Context.build(profile); + if (user.equals(serviceUser)) { + user.setService(true); + } + return conversionService.convert(user, Actor.class); } throw new HttpNotFoundException(); } diff --git a/src/main/java/com/juick/www/api/activity/converters/UserToActorConverter.java b/src/main/java/com/juick/www/api/activity/converters/UserToActorConverter.java new file mode 100644 index 00000000..405c27ab --- /dev/null +++ b/src/main/java/com/juick/www/api/activity/converters/UserToActorConverter.java @@ -0,0 +1,48 @@ +package com.juick.www.api.activity.converters; + +import org.springframework.core.convert.converter.Converter; + +import com.juick.ActivityPubManager; +import com.juick.KeystoreManager; +import com.juick.model.User; +import com.juick.www.WebApp; +import com.juick.www.api.activity.model.Context; +import com.juick.www.api.activity.model.objects.Actor; +import com.juick.www.api.activity.model.objects.Application; +import com.juick.www.api.activity.model.objects.Image; +import com.juick.www.api.activity.model.objects.Key; +import com.juick.www.api.activity.model.objects.Person; + +import lombok.AllArgsConstructor; + +@AllArgsConstructor +public class UserToActorConverter implements Converter { + + private ActivityPubManager activityPubManager; + private KeystoreManager keystoreManager; + private WebApp webApp; + + @Override + public Actor convert(User user) { + Actor profile = user.isService() ? new Application() : new Person(); + profile.setId(activityPubManager.personUri(user)); + profile.setUrl(activityPubManager.personWebUri(user)); + profile.setName(user.getName()); + profile.setPreferredUsername(user.getName()); + Key publicKey = new Key(); + publicKey.setId(profile.getId() + "#main-key"); + publicKey.setOwner(profile.getId()); + publicKey.setPublicKeyPem(keystoreManager.getPublicKeyPem()); + profile.setPublicKey(publicKey); + profile.setInbox(activityPubManager.inboxUri()); + profile.setOutbox(activityPubManager.outboxUri(user)); + profile.setFollowers(activityPubManager.followersUri(user)); + profile.setFollowing(activityPubManager.followingUri(user)); + Image avatar = new Image(); + avatar.setUrl(webApp.getAvatarUrl(user)); + avatar.setMediaType("image/png"); + profile.setIcon(avatar); + return (Actor) Context.build(profile); + } + +} -- cgit v1.2.3