From 189b4e5a432e51802929ea0528830bfe594b350e Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 19 Apr 2022 18:48:42 +0300 Subject: ActivityPub: handle context issues when processing messages --- src/main/java/com/juick/ActivityPubManager.java | 51 +++++++++++++------------ 1 file changed, 27 insertions(+), 24 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/juick/ActivityPubManager.java b/src/main/java/com/juick/ActivityPubManager.java index a86fc301..52343d9c 100644 --- a/src/main/java/com/juick/ActivityPubManager.java +++ b/src/main/java/com/juick/ActivityPubManager.java @@ -249,31 +249,34 @@ public class ActivityPubManager implements ActivityListener, NotificationListene User user = msg.getUser(); String userUri = personUri(user); Note note = makeNote(msg); - Actor me = (Actor) signatureManager.getContext(URI.create(userUri)).get(); - Set 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 cc = new ArrayList<>(note.getCc()); - cc.add(replier); - note.setCc(cc); - } - subscribers.addAll(note.getCc()); - subscribers.forEach(acct -> { - Optional context = signatureManager.getContext(URI.create(acct)); - if (context.isPresent() && context.get() instanceof Actor) { - Actor follower = (Actor)context.get(); - Create create = new Create(); - create.setId(note.getId()); - create.setActor(me.getId()); - create.setPublished(note.getPublished()); - create.setObject(note); - try { - signatureManager.post(me, follower, create); - } catch (IOException | NoSuchAlgorithmException e) { - logger.warn("activitypub exception", e); - } + signatureManager.getContext(URI.create(userUri)).ifPresentOrElse((me) -> { + Set 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 cc = new ArrayList<>(note.getCc()); + cc.add(replier); + note.setCc(cc); } + subscribers.addAll(note.getCc()); + subscribers.forEach(acct -> { + Optional context = signatureManager.getContext(URI.create(acct)); + if (context.isPresent() && context.get() instanceof Actor) { + Actor follower = (Actor) context.get(); + Create create = new Create(); + create.setId(note.getId()); + create.setActor(me.getId()); + create.setPublished(note.getPublished()); + create.setObject(note); + try { + signatureManager.post((Actor)me, follower, create); + } catch (IOException | NoSuchAlgorithmException e) { + logger.warn("activitypub exception", e); + } + } + }); + }, () -> { + logger.warn("Context not found: {}", userUri); }); } -- cgit v1.2.3