From a1058eeef3963634853f8470d1957ecf58c4481a Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 29 Nov 2018 13:08:02 +0300 Subject: ActivityPub likes refactoring --- .../java/com/juick/server/ActivityPubManager.java | 31 ++++++++++++++++++++++ .../com/juick/server/api/activity/Profile.java | 28 +++++++++---------- .../juick/service/activities/ActivityListener.java | 6 +++++ .../juick/service/activities/AnnounceEvent.java | 22 +++++++++++++++ .../service/activities/UndoAnnounceEvent.java | 22 +++++++++++++++ 5 files changed, 93 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/juick/service/activities/AnnounceEvent.java create mode 100644 src/main/java/com/juick/service/activities/UndoAnnounceEvent.java (limited to 'src/main/java/com/juick') diff --git a/src/main/java/com/juick/server/ActivityPubManager.java b/src/main/java/com/juick/server/ActivityPubManager.java index dddb4344..9829760f 100644 --- a/src/main/java/com/juick/server/ActivityPubManager.java +++ b/src/main/java/com/juick/server/ActivityPubManager.java @@ -14,6 +14,7 @@ import com.juick.server.api.activity.model.objects.Mention; import com.juick.server.api.activity.model.objects.Note; import com.juick.server.api.activity.model.objects.Person; import com.juick.server.util.HttpUtils; +import com.juick.service.MessagesService; import com.juick.service.SocialService; import com.juick.service.UserService; import com.juick.service.activities.*; @@ -56,6 +57,8 @@ public class ActivityPubManager implements ActivityListener, NotificationListene @Inject private UserService userService; @Inject + private MessagesService messagesService; + @Inject private PebbleEngine pebbleEngine; @Value("${ap_base_uri:http://localhost:8080/}") private String baseUri; @@ -132,6 +135,34 @@ public class ActivityPubManager implements ActivityListener, NotificationListene }); } + @Override + public void processAnnounceEvent(AnnounceEvent event) { + UriComponents uriComponents = UriComponentsBuilder.fromUriString(event.getMessageUri()).build(); + List segments = uriComponents.getPathSegments(); + if (segments.get(0).equals("m")) { + String[] ids = segments.get(1).split("-", 2); + if (ids.length == 1) { + // only messages + logger.info("{} recommends {}", event.getActorUri(), Integer.valueOf(ids[0])); + messagesService.recommendMessage(Integer.valueOf(ids[0]), 0, event.getActorUri()); + } + } + } + + @Override + public void undoAnnounceEvent(UndoAnnounceEvent event) { + UriComponents uriComponents = UriComponentsBuilder.fromUriString(event.getMessageUri()).build(); + List segments = uriComponents.getPathSegments(); + if (segments.get(0).equals("m")) { + String[] ids = segments.get(1).split("-", 2); + if (ids.length == 1) { + // only messages + logger.info("{} stop recommending {}", event.getActorUri(), Integer.valueOf(ids[0])); + messagesService.recommendMessage(Integer.valueOf(ids[0]), 0, event.getActorUri()); + } + } + } + @Override public void processMessageEvent(MessageEvent messageEvent) { Message msg = messageEvent.getMessage(); diff --git a/src/main/java/com/juick/server/api/activity/Profile.java b/src/main/java/com/juick/server/api/activity/Profile.java index 5a2a766e..e8fb45ec 100644 --- a/src/main/java/com/juick/server/api/activity/Profile.java +++ b/src/main/java/com/juick/server/api/activity/Profile.java @@ -28,9 +28,7 @@ import com.juick.server.util.UserUtils; import com.juick.server.www.WebApp; 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.service.activities.*; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -283,9 +281,16 @@ public class Profile { } if (activity instanceof Undo) { - String follower = (String) ((Map) activity.getObject()).get("object"); - applicationEventPublisher.publishEvent(new UndoFollowEvent(this, activity.getActor(), follower)); - return new ResponseEntity<>(HttpStatus.OK); + Map object = (Map) activity.getObject(); + String objectType = (String) object.get("type"); + String objectObject = (String) object.get("object"); + if (objectType.equals("Follow")) { + applicationEventPublisher.publishEvent(new UndoFollowEvent(this, activity.getActor(), objectObject)); + return new ResponseEntity<>(HttpStatus.OK); + } else if (objectType.equals("Like") || objectType.equals("Announce")) { + applicationEventPublisher.publishEvent(new UndoAnnounceEvent(this, activity.getActor(), objectObject)); + return new ResponseEntity<>(HttpStatus.OK); + } } if (activity instanceof Delete) { if (activity.getObject() instanceof String) { @@ -356,16 +361,7 @@ public class Profile { } } if (activity instanceof Like || activity instanceof Announce) { - String msgUri = (String)activity.getObject(); - User user = new User(); - user.setUri(URI.create(activity.getActor())); - Person person = (Person) signatureManager.getContext(user.getUri()).get(); - user.setName(person.getPreferredUsername()); - String[] ids = msgUri.split("\\/", 2); - if (ids.length == 1) { - // only messages - commandsManager.processCommand(user, String.format("! #%s", ids[0]), URI.create(StringUtils.EMPTY)); - } + applicationEventPublisher.publishEvent(new AnnounceEvent(this, activity.getActor(), (String)((Map)activity.getObject()).get("object"))); return new ResponseEntity<>(HttpStatus.OK); } logger.warn("Unknown activity: {}", jsonMapper.writeValueAsString(activity)); diff --git a/src/main/java/com/juick/service/activities/ActivityListener.java b/src/main/java/com/juick/service/activities/ActivityListener.java index 863bda04..68e1d42a 100644 --- a/src/main/java/com/juick/service/activities/ActivityListener.java +++ b/src/main/java/com/juick/service/activities/ActivityListener.java @@ -16,4 +16,10 @@ public interface ActivityListener { @Async @EventListener void deleteMessageEvent(DeleteMessageEvent event); + @Async + @EventListener + void processAnnounceEvent(AnnounceEvent event); + @Async + @EventListener + void undoAnnounceEvent(UndoAnnounceEvent event); } diff --git a/src/main/java/com/juick/service/activities/AnnounceEvent.java b/src/main/java/com/juick/service/activities/AnnounceEvent.java new file mode 100644 index 00000000..00dbea8c --- /dev/null +++ b/src/main/java/com/juick/service/activities/AnnounceEvent.java @@ -0,0 +1,22 @@ +package com.juick.service.activities; + +import org.springframework.context.ApplicationEvent; + +public class AnnounceEvent extends ApplicationEvent { + private String actorUri; + private String messageUri; + + public AnnounceEvent(Object source, String actor, String messageUri) { + super(source); + this.actorUri = actor; + this.messageUri = messageUri; + } + + public String getActorUri() { + return actorUri; + } + + public String getMessageUri() { + return messageUri; + } +} diff --git a/src/main/java/com/juick/service/activities/UndoAnnounceEvent.java b/src/main/java/com/juick/service/activities/UndoAnnounceEvent.java new file mode 100644 index 00000000..115f3146 --- /dev/null +++ b/src/main/java/com/juick/service/activities/UndoAnnounceEvent.java @@ -0,0 +1,22 @@ +package com.juick.service.activities; + +import org.springframework.context.ApplicationEvent; + +public class UndoAnnounceEvent extends ApplicationEvent { + private String actorUri; + private String messageUri; + + public UndoAnnounceEvent(Object source, String actor, String messageUri) { + super(source); + this.actorUri = actor; + this.messageUri = messageUri; + } + + public String getActorUri() { + return actorUri; + } + + public String getMessageUri() { + return messageUri; + } +} -- cgit v1.2.3