From 9fd4ce97047f8c4ffd2bd3374b0288b53580f34b Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 4 Oct 2018 17:06:15 +0300 Subject: ActivityPub: broadcast message deletion --- .../java/com/juick/server/ActivityPubManager.java | 29 +++++++++++++++++++--- .../java/com/juick/server/CommandsManager.java | 5 +++- .../juick/service/activities/ActivityListener.java | 2 ++ .../service/activities/DeleteMessageEvent.java | 21 ++++++++++++++++ 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 juick-server/src/main/java/com/juick/service/activities/DeleteMessageEvent.java (limited to 'juick-server/src') 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 c05deb15..49895bc8 100644 --- a/juick-server/src/main/java/com/juick/server/ActivityPubManager.java +++ b/juick-server/src/main/java/com/juick/server/ActivityPubManager.java @@ -7,11 +7,9 @@ import com.juick.server.api.activity.model.Note; 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.server.api.activity.model.activities.Delete; 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.activities.*; import com.juick.service.component.*; import com.juick.util.MessageUtils; import org.apache.commons.lang3.StringUtils; @@ -87,6 +85,29 @@ public class ActivityPubManager implements ActivityListener, NotificationListene socialService.removeAccount(acct); } + @Override + public void deleteMessageEvent(DeleteMessageEvent event) { + Message msg = event.getMessage(); + User user = msg.getUser(); + String userUri = personUri(user); + 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(); + Delete delete = new Delete(); + delete.setId(note.getId()); + delete.setActor(me.getId()); + delete.setPublished(note.getPublished()); + delete.setObject(note); + try { + logger.info("Deletion to follower {}", follower.getId()); + signatureManager.post(me, follower, delete); + } catch (IOException e) { + logger.warn("activitypub exception", e); + } + }); + } + @Override public void processMessageEvent(MessageEvent messageEvent) { Message msg = messageEvent.getMessage(); diff --git a/juick-server/src/main/java/com/juick/server/CommandsManager.java b/juick-server/src/main/java/com/juick/server/CommandsManager.java index e9cf11de..297e986f 100644 --- a/juick-server/src/main/java/com/juick/server/CommandsManager.java +++ b/juick-server/src/main/java/com/juick/server/CommandsManager.java @@ -21,6 +21,7 @@ import com.juick.Message; import com.juick.Tag; import com.juick.User; import com.juick.formatters.PlainTextFormatter; +import com.juick.service.activities.DeleteMessageEvent; import com.juick.service.component.*; import com.juick.model.CommandResult; import com.juick.model.TagStats; @@ -446,7 +447,9 @@ public class CommandsManager { @UserCommand(pattern = "^D #(\\d+)$", help = "D #1234 - Delete post", patternFlags = Pattern.CASE_INSENSITIVE) public CommandResult commandDeletePost(User user, URI attachment, String... args) { int mid = Integer.valueOf(args[0]); - if (messagesService.deleteMessage(user.getUid(), mid)) { + Message message = messagesService.getMessage(mid); + if (message != null && messagesService.deleteMessage(user.getUid(), mid)) { + applicationEventPublisher.publishEvent(new DeleteMessageEvent(this, message)); return CommandResult.fromString("Message deleted"); } return CommandResult.fromString("This is not your message"); 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 01baeea9..3174ff23 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 @@ -9,4 +9,6 @@ public interface ActivityListener { void undoFollowEvent(UndoFollowEvent event); @EventListener void deleteUserEvent(DeleteUserEvent event); + @EventListener + void deleteMessageEvent(DeleteMessageEvent event); } diff --git a/juick-server/src/main/java/com/juick/service/activities/DeleteMessageEvent.java b/juick-server/src/main/java/com/juick/service/activities/DeleteMessageEvent.java new file mode 100644 index 00000000..67e40f44 --- /dev/null +++ b/juick-server/src/main/java/com/juick/service/activities/DeleteMessageEvent.java @@ -0,0 +1,21 @@ +package com.juick.service.activities; + +import com.juick.Message; +import org.springframework.context.ApplicationEvent; + +public class DeleteMessageEvent extends ApplicationEvent { + private Message message; + /** + * Create a new ApplicationEvent. + * + * @param source the object on which the event initially occurred (never {@code null}) + */ + public DeleteMessageEvent(Object source, Message message) { + super(source); + this.message = message; + } + + public Message getMessage() { + return message; + } +} -- cgit v1.2.3