diff options
author | Vitaly Takmazov | 2022-12-15 21:23:42 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2022-12-15 21:23:42 +0300 |
commit | ef5e5fc956e419e2ba053e4ac3df341af218aaf7 (patch) | |
tree | c76270768a43d9adeccb77d01cf2638c5101d601 /src | |
parent | 725f2fde72df4bdd92a2262fbb335f40e0a81a1c (diff) |
Use ConversionService in ActivityPubManager flow
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/juick/ActivityPubManager.java | 102 |
1 files changed, 34 insertions, 68 deletions
diff --git a/src/main/java/com/juick/ActivityPubManager.java b/src/main/java/com/juick/ActivityPubManager.java index 16f310a7..7d797627 100644 --- a/src/main/java/com/juick/ActivityPubManager.java +++ b/src/main/java/com/juick/ActivityPubManager.java @@ -17,40 +17,12 @@ package com.juick; -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; -import java.net.URI; -import java.security.NoSuchAlgorithmException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; - -import javax.annotation.Nonnull; -import javax.inject.Inject; - import com.juick.model.Message; import com.juick.model.Reaction; -import com.juick.model.Tag; import com.juick.model.User; import com.juick.service.MessagesService; import com.juick.service.SocialService; -import com.juick.service.activities.ActivityListener; -import com.juick.service.activities.AnnounceEvent; -import com.juick.service.activities.DeleteMessageEvent; -import com.juick.service.activities.DeleteUserEvent; -import com.juick.service.activities.FollowEvent; -import com.juick.service.activities.UndoAnnounceEvent; -import com.juick.service.activities.UndoFollowEvent; -import com.juick.service.activities.UpdateEvent; -import com.juick.service.activities.UpdateUserEvent; +import com.juick.service.activities.*; import com.juick.service.component.NotificationListener; import com.juick.service.component.PingEvent; import com.juick.service.component.SystemEvent; @@ -61,25 +33,27 @@ import com.juick.util.formatters.PlainTextFormatter; import com.juick.www.api.SystemActivity.ActivityType; import com.juick.www.api.activity.helpers.ProfileUriBuilder; import com.juick.www.api.activity.model.Context; -import com.juick.www.api.activity.model.activities.Accept; -import com.juick.www.api.activity.model.activities.Announce; -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.Update; -import com.juick.www.api.activity.model.objects.Actor; -import com.juick.www.api.activity.model.objects.Hashtag; -import com.juick.www.api.activity.model.objects.Image; -import com.juick.www.api.activity.model.objects.Mention; -import com.juick.www.api.activity.model.objects.Note; +import com.juick.www.api.activity.model.activities.*; +import com.juick.www.api.activity.model.objects.*; import io.pebbletemplates.pebble.PebbleEngine; import io.pebbletemplates.pebble.template.PebbleTemplate; - import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.core.convert.ConversionService; import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; +import javax.annotation.Nonnull; +import javax.inject.Inject; +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; +import java.net.URI; +import java.security.NoSuchAlgorithmException; +import java.util.*; +import java.util.stream.Collectors; + public class ActivityPubManager implements ActivityListener, NotificationListener { private static final Logger logger = LoggerFactory.getLogger("ActivityPub"); @Inject @@ -92,6 +66,8 @@ public class ActivityPubManager implements ActivityListener, NotificationListene private PebbleEngine pebbleEngine; @Inject ProfileUriBuilder profileUriBuilder; + @Inject + ConversionService conversionService; @Override public void processFollowEvent(@Nonnull FollowEvent followEvent) { @@ -100,7 +76,7 @@ public class ActivityPubManager implements ActivityListener, NotificationListene User followedUser = socialService.getUserByAccountUri(acct); if (!followedUser.isAnonymous()) { // automatically accept follower requests - Actor me = (Actor) signatureManager.getContext(URI.create(acct)).get(); + Actor me = conversionService.convert(followedUser, Actor.class); Actor follower = (Actor) signatureManager.getContext(URI.create(followEvent.getRequest().getActor())).get(); Accept accept = new Accept(); accept.setActor(me.getId()); @@ -137,9 +113,8 @@ public class ActivityPubManager implements ActivityListener, NotificationListene public void deleteMessageEvent(DeleteMessageEvent event) { Message msg = event.getMessage(); User user = msg.getUser(); - String userUri = profileUriBuilder.personUri(user); Note note = makeNote(msg); - Actor me = (Actor) signatureManager.getContext(URI.create(userUri)).get(); + Actor me = conversionService.convert(user, Actor.class); socialService.getFollowers(user).forEach(acct -> { try { Actor follower = (Actor) signatureManager.getContext(URI.create(acct)).orElseThrow(); @@ -150,8 +125,6 @@ public class ActivityPubManager implements ActivityListener, NotificationListene delete.setObject(note); logger.info("Deletion to follower {}", follower.getId()); signatureManager.post(me, follower, delete); - } catch (NoSuchElementException e) { - logger.warn("Unverified actor: {}", userUri); } catch (IOException | NoSuchAlgorithmException e) { logger.warn("activitypub exception", e); } @@ -190,8 +163,7 @@ public class ActivityPubManager implements ActivityListener, NotificationListene public void processUpdateEvent(UpdateEvent event) { String objectUri = event.getMessageUri(); User user = event.getUser(); - String userUri = profileUriBuilder.personUri(user); - Actor me = (Actor) signatureManager.getContext(URI.create(userUri)).get(); + Actor me = conversionService.convert(user, Actor.class); socialService.getFollowers(user).forEach(acct -> { try { Actor follower = (Actor) signatureManager.getContext(URI.create(acct)).orElseThrow(); @@ -201,8 +173,6 @@ public class ActivityPubManager implements ActivityListener, NotificationListene update.setObject(new Context(objectUri)); logger.info("Update to follower {}", follower.getId()); signatureManager.post(me, follower, update); - } catch (NoSuchElementException e) { - logger.warn("Person not verified: {}", acct); } catch (IOException | NoSuchAlgorithmException e) { logger.warn("activitypub exception", e); } @@ -223,7 +193,7 @@ public class ActivityPubManager implements ActivityListener, NotificationListene public void processUpdateUserEvent(UpdateUserEvent event) { User user = event.getUser(); String userUri = profileUriBuilder.personUri(user); - Actor me = (Actor) signatureManager.getContext(URI.create(userUri)).get(); + Actor me = conversionService.convert(user, Actor.class); socialService.getFollowers(user).forEach(acct -> { try { Actor follower = (Actor) signatureManager.getContext(URI.create(acct)).orElseThrow(); @@ -233,8 +203,6 @@ public class ActivityPubManager implements ActivityListener, NotificationListene update.setObject(new Context(userUri)); logger.info("Update to follower {}", follower.getId()); signatureManager.post(me, follower, update); - } catch (NoSuchElementException e) { - logger.warn("Person not verified: {}", acct); } catch (IOException | NoSuchAlgorithmException e) { logger.warn("activitypub exception", e); } @@ -246,19 +214,19 @@ public class ActivityPubManager implements ActivityListener, NotificationListene return; } User user = msg.getUser(); - String userUri = profileUriBuilder.personUri(user); Note note = makeNote(msg); - signatureManager.getContext(URI.create(userUri)).ifPresentOrElse((me) -> { - Set<String> subscribers = new HashSet<>(socialService.getFollowers(user)); - if (MessageUtils.isReply(msg) && msg.getTo().getUri().toASCIIString().length() > 0) { - String replier = msg.getTo().getUri().toASCIIString(); - subscribers.add(replier); - List<String> cc = new ArrayList<>(note.getCc()); - cc.add(replier); - note.setCc(cc); - } - subscribers.addAll(note.getCc()); - subscribers.forEach(acct -> { + var me = conversionService.convert(user, Actor.class); + Set<String> subscribers = new HashSet<>(socialService.getFollowers(user)); + if (MessageUtils.isReply(msg) && msg.getTo().getUri().toASCIIString().length() > 0) { + String replier = msg.getTo().getUri().toASCIIString(); + subscribers.add(replier); + List<String> cc = new ArrayList<>(note.getCc()); + cc.add(replier); + note.setCc(cc); + } + subscribers.addAll(note.getCc()); + subscribers.forEach(acct -> { + if (!acct.equals(profileUriBuilder.followersUri(user))) { Optional<Context> context = signatureManager.getContext(URI.create(acct)); if (context.isPresent() && context.get() instanceof Actor) { Actor follower = (Actor) context.get(); @@ -268,16 +236,14 @@ public class ActivityPubManager implements ActivityListener, NotificationListene create.setPublished(note.getPublished()); create.setObject(note); try { - signatureManager.post((Actor)me, follower, create); + signatureManager.post(me, follower, create); } catch (IOException | NoSuchAlgorithmException e) { logger.warn("activitypub exception", e); } } else { logger.warn("Unhandled context: {}", acct); } - }); - }, () -> { - logger.warn("Context not found: {}", userUri); + } }); } |