From deec8829a53e5b75a837d31c5061d5ab4ecef420 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 4 Oct 2018 16:40:13 +0300 Subject: ActivityPub: handle user Delete event --- .../main/java/com/juick/service/SocialService.java | 1 + .../java/com/juick/server/ActivityPubManager.java | 8 ++++++++ .../java/com/juick/server/api/activity/Profile.java | 11 ++++++++--- .../java/com/juick/service/ActivityPubService.java | 6 ++++++ .../juick/service/activities/ActivityListener.java | 2 ++ .../juick/service/activities/DeleteUserEvent.java | 20 ++++++++++++++++++++ 6 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 juick-server/src/main/java/com/juick/service/activities/DeleteUserEvent.java diff --git a/juick-common/src/main/java/com/juick/service/SocialService.java b/juick-common/src/main/java/com/juick/service/SocialService.java index d54d6fa1..eb77619b 100644 --- a/juick-common/src/main/java/com/juick/service/SocialService.java +++ b/juick-common/src/main/java/com/juick/service/SocialService.java @@ -12,4 +12,5 @@ public interface SocialService { List getFollowers(User user); void addFollower(User user, String acct); void removeFollower(User user, String acct); + void removeAccount(String acct); } 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 cbe7f623..c05deb15 100644 --- a/juick-server/src/main/java/com/juick/server/ActivityPubManager.java +++ b/juick-server/src/main/java/com/juick/server/ActivityPubManager.java @@ -9,6 +9,7 @@ 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.DeleteUserEvent; import com.juick.service.activities.FollowEvent; import com.juick.service.activities.UndoFollowEvent; import com.juick.service.component.*; @@ -79,6 +80,13 @@ public class ActivityPubManager implements ActivityListener, NotificationListene } } + @Override + public void deleteUserEvent(DeleteUserEvent event) { + String acct = event.getUserUri(); + logger.info("Deleting {} from followers", acct); + socialService.removeAccount(acct); + } + @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 622bb08b..bbb7761f 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 @@ -6,16 +6,16 @@ import com.juick.server.KeystoreManager; import com.juick.server.SignatureManager; import com.juick.server.api.activity.model.*; import com.juick.server.api.activity.model.activities.Create; +import com.juick.server.api.activity.model.activities.Delete; import com.juick.server.api.activity.model.activities.Follow; import com.juick.server.api.activity.model.activities.Undo; import com.juick.server.util.HttpNotFoundException; import com.juick.server.util.UserUtils; import com.juick.service.MessagesService; import com.juick.service.UserService; +import com.juick.service.activities.DeleteUserEvent; 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; import org.springframework.context.ApplicationEventPublisher; import org.springframework.http.HttpStatus; @@ -27,7 +27,6 @@ import org.springframework.web.util.UriComponentsBuilder; import javax.inject.Inject; import java.net.URI; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -249,6 +248,12 @@ public class Profile { applicationEventPublisher.publishEvent(new UndoFollowEvent(this, activity.getActor(), follower)); return new ResponseEntity<>(HttpStatus.OK); } + if (activity instanceof Delete) { + if (activity.getObject() instanceof String) { + // Delete user + applicationEventPublisher.publishEvent(new DeleteUserEvent(this, (String)activity.getObject())); + } + } return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); } return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); diff --git a/juick-server/src/main/java/com/juick/service/ActivityPubService.java b/juick-server/src/main/java/com/juick/service/ActivityPubService.java index 6200a75e..892022cf 100644 --- a/juick-server/src/main/java/com/juick/service/ActivityPubService.java +++ b/juick-server/src/main/java/com/juick/service/ActivityPubService.java @@ -50,4 +50,10 @@ public class ActivityPubService extends BaseJdbcService implements SocialService public void removeFollower(User user, String acct) { getJdbcTemplate().update("DELETE FROM followers WHERE user_id=? AND acct=?", user.getUid(), acct); } + + @Transactional + @Override + public void removeAccount(String acct) { + getJdbcTemplate().update("DELETE FROM followers WHERE acct=?", acct); + } } 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 index f89b401d..01baeea9 100644 --- a/juick-server/src/main/java/com/juick/service/activities/ActivityListener.java +++ b/juick-server/src/main/java/com/juick/service/activities/ActivityListener.java @@ -7,4 +7,6 @@ public interface ActivityListener { void processFollowEvent(FollowEvent event); @EventListener void undoFollowEvent(UndoFollowEvent event); + @EventListener + void deleteUserEvent(DeleteUserEvent event); } diff --git a/juick-server/src/main/java/com/juick/service/activities/DeleteUserEvent.java b/juick-server/src/main/java/com/juick/service/activities/DeleteUserEvent.java new file mode 100644 index 00000000..8b51da9d --- /dev/null +++ b/juick-server/src/main/java/com/juick/service/activities/DeleteUserEvent.java @@ -0,0 +1,20 @@ +package com.juick.service.activities; + +import org.springframework.context.ApplicationEvent; + +public class DeleteUserEvent extends ApplicationEvent { + private String userUri; + /** + * Create a new ApplicationEvent. + * + * @param source the object on which the event initially occurred (never {@code null}) + */ + public DeleteUserEvent(Object source, String userUri) { + super(source); + this.userUri = userUri; + } + + public String getUserUri() { + return userUri; + } +} -- cgit v1.2.3