package com.juick.server; import com.juick.Message; import com.juick.User; import com.juick.formatters.PlainTextFormatter; import com.juick.server.api.activity.model.Image; 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.Announce; import com.juick.server.api.activity.model.activities.Create; import com.juick.server.api.activity.model.activities.Delete; import com.juick.server.util.HttpUtils; import com.juick.service.SocialService; import com.juick.service.UserService; import com.juick.service.activities.*; import com.juick.service.component.*; import com.juick.util.MessageUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.util.UriComponentsBuilder; import javax.annotation.Nonnull; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; import java.net.URI; import java.util.Collections; @Component public class ActivityPubManager implements ActivityListener, NotificationListener { private static final Logger logger = LoggerFactory.getLogger(ActivityPubManager.class); @Inject private SignatureManager signatureManager; @Inject private SocialService socialService; @Inject private UserService userService; @Value("${ap_base_uri:http://localhost:8080/}") private String baseUri; @Value("${service_user:juick}") private String serviceUsername; private User serviceUser; @PostConstruct public void init() { serviceUser = userService.getUserByName(serviceUsername); } @Override public void processFollowEvent(@Nonnull FollowEvent followEvent) { String acct = (String)followEvent.getRequest().getObject(); logger.info("received follower request to {}", acct); User followedUser = socialService.getUserByAccountUri(acct); if (!followedUser.isAnonymous()) { // automatically accept follower requests Person me = (Person) signatureManager.getContext(URI.create(acct)).get(); Person follower = (Person) signatureManager.getContext(URI.create(followEvent.getRequest().getActor())).get(); Accept accept = new Accept(); accept.setActor(me.getId()); accept.setObject(followEvent.getRequest()); try { signatureManager.post(me, follower, accept); socialService.addFollower(followedUser, follower.getId()); logger.info("Follower added for {}", followedUser.getName()); } catch (IOException e) { logger.info("activitypub exception", e); } } } @Override public void undoFollowEvent(UndoFollowEvent event) { String actor = event.getActor(); String me = event.getObject(); logger.info("{} stopping to follow {}", actor, me); User followedUser = socialService.getUserByAccountUri(me); if (!followedUser.isAnonymous()) { socialService.removeFollower(followedUser, actor); } } @Override public void deleteUserEvent(DeleteUserEvent event) { String acct = event.getUserUri(); logger.info("Deleting {} from followers", acct); 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(); if (MessageUtils.isPM(msg)) { return; } 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(); 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); } }); } public String inboxUri() { UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); return uri.replacePath("/api/inbox").toUriString(); } public String outboxUri(User user) { UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); return uri.replacePath(String.format("/u/%s/blog/toc", user.getName())).toUriString(); } public String personUri(User user) { if (user.getUri().toString().length() > 0) { return user.getUri().toASCIIString(); } UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); return uri.replacePath(String.format("/u/%s", user.getName())).toUriString(); } public String personWebUri(User user) { UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); return uri.replacePath(String.format("/%s/", user.getName())).toUriString(); } public String followersUri(User user) { UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); return uri.replacePath(String.format("/u/%s/followers/toc", user.getName())).toUriString(); } public String followingUri(User user) { UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); return uri.replacePath(String.format("/u/%s/following/toc", user.getName())).toUriString(); } public String messageUri(Message msg) { return messageUri(msg.getMid(), msg.getRid()); } public String messageUri(int mid, int rid) { UriComponentsBuilder uri = UriComponentsBuilder.fromUriString(baseUri); uri.replacePath(String.format("/n/%d-%d", mid, rid)); return uri.toUriString(); } public Note makeNote(Message msg) { Note note = new Note(); note.setId(messageUri(msg)); note.setUrl(PlainTextFormatter.formatUrl(msg)); note.setAttributedTo(personUri(msg.getUser())); if (MessageUtils.isReply(msg)) { if (msg.getReplyToUri().toASCIIString().length() > 0) { note.setInReplyTo(msg.getReplyToUri().toASCIIString()); } else { note.setInReplyTo(messageUri(msg.getMid(), msg.getReplyto())); } } if (MessageUtils.isPM(msg)) { note.setTo(Collections.singletonList(personUri(msg.getTo()))); } else { note.setTo(Collections.singletonList("https://www.w3.org/ns/activitystreams#Public")); note.setCc(Collections.singletonList(followersUri(msg.getUser()))); } note.setPublished(msg.getTimestamp()); note.setContent(msg.isHtml() ? msg.getText() : MessageUtils.formatMessage(msg.getText())); if (StringUtils.isNotBlank(msg.getAttachmentType())) { Image attachment = new Image(); attachment.setId(msg.getAttachment().getMedium().getUrl()); attachment.setUrl(msg.getAttachment().getMedium().getUrl()); attachment.setMediaType(HttpUtils.mediaType(msg.getAttachmentType())); note.setAttachment(attachment); } return note; } @Override public void processSubscribeEvent(SubscribeEvent subscribeEvent) { } @Override public void processLikeEvent(LikeEvent likeEvent) { } @Override public void processPingEvent(PingEvent pingEvent) { } @Override public void processMessageReadEvent(MessageReadEvent messageReadEvent) { } @Override public void processTopEvent(TopEvent topEvent) { Message message = topEvent.getMessage(); Note note = makeNote(message); Announce announce = new Announce(); announce.setId(note.getId() + "#top"); announce.setActor(personUri(serviceUser)); announce.setObject(note); Person me = (Person) signatureManager.getContext(URI.create(announce.getActor())).get(); socialService.getFollowers(serviceUser).forEach(acct -> { Person follower = (Person) signatureManager.getContext(URI.create(acct)).get(); try { logger.info("Announcing top: {}", message.getMid()); signatureManager.post(me, follower, announce); } catch (IOException e) { logger.warn("activitypub exception", e); } }); } }