From 5a2b506b5ecb56793807aacb47b7615bba80a7cc Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 10 Oct 2018 15:40:15 +0300 Subject: ActivityPub: clean up 410 followers --- .../java/com/juick/server/ActivityPubManager.java | 27 +++++++++++++--------- .../java/com/juick/server/SignatureManager.java | 6 +++++ .../com/juick/server/api/activity/Profile.java | 3 +++ 3 files changed, 25 insertions(+), 11 deletions(-) (limited to 'juick-server') diff --git a/juick-server/src/main/java/com/juick/server/ActivityPubManager.java b/juick-server/src/main/java/com/juick/server/ActivityPubManager.java index 5aecdd33..d494154c 100644 --- a/juick-server/src/main/java/com/juick/server/ActivityPubManager.java +++ b/juick-server/src/main/java/com/juick/server/ActivityPubManager.java @@ -3,6 +3,7 @@ package com.juick.server; import com.juick.Message; import com.juick.User; import com.juick.formatters.PlainTextFormatter; +import com.juick.server.api.activity.model.Context; import com.juick.server.api.activity.model.Image; import com.juick.server.api.activity.model.Note; import com.juick.server.api.activity.model.Person; @@ -29,6 +30,7 @@ import javax.inject.Inject; import java.io.IOException; import java.net.URI; import java.util.Collections; +import java.util.Optional; @Component public class ActivityPubManager implements ActivityListener, NotificationListener { @@ -125,17 +127,20 @@ public class ActivityPubManager implements ActivityListener, NotificationListene Note note = makeNote(msg); Person me = (Person) signatureManager.getContext(URI.create(userUri)).get(); socialService.getFollowers(user).forEach(acct -> { - Person follower = (Person) signatureManager.getContext(URI.create(acct)).get(); - Create create = new Create(); - create.setId(note.getId()); - create.setActor(me.getId()); - create.setPublished(note.getPublished()); - create.setObject(note); - try { - logger.info("Posting to follower {}", follower.getId()); - signatureManager.post(me, follower, create); - } catch (IOException e) { - logger.warn("activitypub exception", e); + Optional context = signatureManager.getContext(URI.create(acct)); + if (context.isPresent()) { + Person follower = (Person)context.get(); + Create create = new Create(); + create.setId(note.getId()); + create.setActor(me.getId()); + create.setPublished(note.getPublished()); + create.setObject(note); + try { + logger.info("Posting to follower {}", follower.getId()); + signatureManager.post(me, follower, create); + } catch (IOException e) { + logger.warn("activitypub exception", e); + } } }); } diff --git a/juick-server/src/main/java/com/juick/server/SignatureManager.java b/juick-server/src/main/java/com/juick/server/SignatureManager.java index c39cdac9..869edbfc 100644 --- a/juick-server/src/main/java/com/juick/server/SignatureManager.java +++ b/juick-server/src/main/java/com/juick/server/SignatureManager.java @@ -3,10 +3,12 @@ package com.juick.server; import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.server.api.activity.model.Context; import com.juick.server.api.activity.model.Person; +import com.juick.service.activities.DeleteUserEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.HttpClientErrorException; @@ -83,6 +85,10 @@ public class SignatureManager { return Optional.of(new RestTemplate().getForEntity(contextUri, Context.class).getBody()); } catch (HttpClientErrorException e) { logger.warn("Cannot identify {}", contextUri); + if (e.getStatusCode().equals(HttpStatus.GONE)) { + logger.warn("Server report {} is gone, deleting", contextUri); + new DeleteUserEvent(this, contextUri.toASCIIString()); + } } return Optional.empty(); } diff --git a/juick-server/src/main/java/com/juick/server/api/activity/Profile.java b/juick-server/src/main/java/com/juick/server/api/activity/Profile.java index 669a28df..357ca29c 100644 --- a/juick-server/src/main/java/com/juick/server/api/activity/Profile.java +++ b/juick-server/src/main/java/com/juick/server/api/activity/Profile.java @@ -274,9 +274,12 @@ public class Profile { if (valid) { if (activity instanceof Follow) { Follow followRequest = (Follow) activity; + String actor = followRequest.getActor(); + Person follower = (Person) signatureManager.getContext(URI.create(actor)).orElseThrow(HttpBadRequestException::new); applicationEventPublisher.publishEvent( new FollowEvent(this, followRequest)); return new ResponseEntity<>(HttpStatus.ACCEPTED); + } if (activity instanceof Undo) { String follower = (String) ((Map) activity.getObject()).get("object"); -- cgit v1.2.3