From 873fbbc8b7f59c68c6bf37ff9af79c8fb2aef8ed Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 4 Oct 2018 13:59:28 +0300 Subject: ActivityPub: handle Undo following requests --- .../java/com/juick/server/ActivityPubManager.java | 17 ++++++++++++-- .../com/juick/server/api/activity/Profile.java | 3 +++ .../juick/service/activities/ActivityListener.java | 10 ++++++++ .../juick/service/activities/UndoFollowEvent.java | 27 ++++++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 juick-server/src/main/java/com/juick/service/activities/ActivityListener.java create mode 100644 juick-server/src/main/java/com/juick/service/activities/UndoFollowEvent.java 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 223d28fd..17522423 100644 --- a/juick-server/src/main/java/com/juick/server/ActivityPubManager.java +++ b/juick-server/src/main/java/com/juick/server/ActivityPubManager.java @@ -8,7 +8,9 @@ import com.juick.server.api.activity.model.Person; import com.juick.server.api.activity.model.activities.Accept; import com.juick.server.api.activity.model.activities.Create; import com.juick.service.SocialService; +import com.juick.service.activities.ActivityListener; import com.juick.service.activities.FollowEvent; +import com.juick.service.activities.UndoFollowEvent; import com.juick.service.component.*; import com.juick.util.MessageUtils; import org.apache.commons.lang3.StringUtils; @@ -27,7 +29,7 @@ import java.net.URI; import java.util.Collections; @Component -public class ActivityPubManager implements ApplicationListener, NotificationListener { +public class ActivityPubManager implements ActivityListener, NotificationListener { private static final Logger logger = LoggerFactory.getLogger(ActivityPubManager.class); @Inject SignatureManager signatureManager; @@ -46,7 +48,7 @@ public class ActivityPubManager implements ApplicationListener, Not } @Override - public void onApplicationEvent(@Nonnull FollowEvent followEvent) { + public void processFollowEvent(@Nonnull FollowEvent followEvent) { String acct = (String)followEvent.getRequest().getObject(); logger.info("received follower request to {}", acct); User followedUser = socialService.getUserByAccountUri(acct); @@ -67,6 +69,17 @@ public class ActivityPubManager implements ApplicationListener, Not } } + @Override + public void undoFollowEvent(UndoFollowEvent event) { + String actor = event.getActor(); + String me = (String)event.getFollowRequest().getObject(); + logger.info("{} stopping to follow {}", actor, me); + User followedUser = socialService.getUserByAccountUri(me); + if (!followedUser.isAnonymous()) { + socialService.removeFollower(followedUser, actor); + } + } + @Override public void processMessageEvent(MessageEvent messageEvent) { Message msg = messageEvent.getMessage(); 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 7b854d52..2e089330 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 @@ -13,6 +13,7 @@ import com.juick.server.util.UserUtils; import com.juick.service.MessagesService; import com.juick.service.UserService; import com.juick.service.activities.FollowEvent; +import com.juick.service.activities.UndoFollowEvent; import com.juick.util.MessageUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; @@ -239,6 +240,8 @@ public class Profile { return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); } if (activity instanceof Undo) { + Follow undoFollow = (Follow) ((Undo) activity).getObject(); + applicationEventPublisher.publishEvent(new UndoFollowEvent(this, ((Undo) activity).getActor(), undoFollow)); return new ResponseEntity<>(HttpStatus.OK); } return new ResponseEntity<>(HttpStatus.BAD_REQUEST); diff --git a/juick-server/src/main/java/com/juick/service/activities/ActivityListener.java b/juick-server/src/main/java/com/juick/service/activities/ActivityListener.java new file mode 100644 index 00000000..f89b401d --- /dev/null +++ b/juick-server/src/main/java/com/juick/service/activities/ActivityListener.java @@ -0,0 +1,10 @@ +package com.juick.service.activities; + +import org.springframework.context.event.EventListener; + +public interface ActivityListener { + @EventListener + void processFollowEvent(FollowEvent event); + @EventListener + void undoFollowEvent(UndoFollowEvent event); +} diff --git a/juick-server/src/main/java/com/juick/service/activities/UndoFollowEvent.java b/juick-server/src/main/java/com/juick/service/activities/UndoFollowEvent.java new file mode 100644 index 00000000..fa162b2e --- /dev/null +++ b/juick-server/src/main/java/com/juick/service/activities/UndoFollowEvent.java @@ -0,0 +1,27 @@ +package com.juick.service.activities; + +import com.juick.server.api.activity.model.activities.Follow; +import org.springframework.context.ApplicationEvent; + +public class UndoFollowEvent extends ApplicationEvent { + private String actor; + private Follow followRequest; + /** + * Create a new ApplicationEvent. + * + * @param source the object on which the event initially occurred (never {@code null}) + */ + public UndoFollowEvent(Object source, String actor, Follow object) { + super(source); + this.actor = actor; + this.followRequest = object; + } + + public String getActor() { + return actor; + } + + public Follow getFollowRequest() { + return followRequest; + } +} -- cgit v1.2.3