From a44510ff17ac6e099ceed9401a62d6d66366c73d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 23 Aug 2021 15:04:09 +0300 Subject: ActivityPub: send Update event on user profile updates --- src/main/java/com/juick/ActivityPubManager.java | 23 +++++++++++++ .../juick/service/activities/ActivityListener.java | 3 ++ .../juick/service/activities/UpdateUserEvent.java | 40 ++++++++++++++++++++++ src/main/java/com/juick/www/api/Users.java | 2 ++ .../java/com/juick/www/controllers/Settings.java | 5 +++ 5 files changed, 73 insertions(+) create mode 100644 src/main/java/com/juick/service/activities/UpdateUserEvent.java (limited to 'src') diff --git a/src/main/java/com/juick/ActivityPubManager.java b/src/main/java/com/juick/ActivityPubManager.java index cb4d0b54..1e97a54e 100644 --- a/src/main/java/com/juick/ActivityPubManager.java +++ b/src/main/java/com/juick/ActivityPubManager.java @@ -217,6 +217,29 @@ public class ActivityPubManager implements ActivityListener, NotificationListene } } } + + @Override + public void processUpdateUserEvent(UpdateUserEvent event) { + User user = event.getUser(); + String userUri = personUri(user); + Person me = (Person) signatureManager.getContext(URI.create(userUri)).get(); + socialService.getFollowers(user).forEach(acct -> { + try { + Person follower = (Person) signatureManager.getContext(URI.create(acct)).orElseThrow(); + Update update = new Update(); + update.setId(userUri + "#update"); + update.setActor(me.getId()); + update.setObject(new Context(userUri)); + logger.info("Update to follower {}", follower.getId()); + signatureManager.post(me, follower, update); + } catch (NoSuchElementException e) { + logger.warn("Person not verified: {}", acct); + } catch (IOException | NoSuchAlgorithmException e) { + logger.warn("activitypub exception", e); + } + }); + } + private void processMessage(Message msg) { if (MessageUtils.isPM(msg) || msg.isService()) { return; diff --git a/src/main/java/com/juick/service/activities/ActivityListener.java b/src/main/java/com/juick/service/activities/ActivityListener.java index d3ce1eaf..3dcd123c 100644 --- a/src/main/java/com/juick/service/activities/ActivityListener.java +++ b/src/main/java/com/juick/service/activities/ActivityListener.java @@ -42,4 +42,7 @@ public interface ActivityListener { @Async @EventListener void processUpdateEvent(UpdateEvent event); + @Async + @EventListener + void processUpdateUserEvent(UpdateUserEvent event); } diff --git a/src/main/java/com/juick/service/activities/UpdateUserEvent.java b/src/main/java/com/juick/service/activities/UpdateUserEvent.java new file mode 100644 index 00000000..5294ea64 --- /dev/null +++ b/src/main/java/com/juick/service/activities/UpdateUserEvent.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2008-2021, Juick + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.juick.service.activities; + +import com.juick.model.User; +import org.springframework.context.ApplicationEvent; + +public class UpdateUserEvent extends ApplicationEvent { + private final User actor; + /** + * Create a new {@code ApplicationEvent}. + * + * @param source the object on which the event initially occurred or with + * which the event is associated (never {@code null}) + * @param actor the event author + */ + public UpdateUserEvent(Object source, User actor) { + super(source); + this.actor = actor; + } + + public User getUser() { + return actor; + } +} diff --git a/src/main/java/com/juick/www/api/Users.java b/src/main/java/com/juick/www/api/Users.java index 8743a275..030d8ced 100644 --- a/src/main/java/com/juick/www/api/Users.java +++ b/src/main/java/com/juick/www/api/Users.java @@ -26,6 +26,7 @@ import com.juick.util.HttpUtils; import com.juick.util.WebUtils; import com.juick.www.WebApp; import com.juick.service.*; +import com.juick.service.activities.UpdateUserEvent; import com.juick.service.component.MailVerificationEvent; import com.juick.service.security.annotation.Visitor; import org.apache.commons.io.IOUtils; @@ -175,6 +176,7 @@ public class Users { String avatarTmpPath = HttpUtils.receiveMultiPartFile(avatar, tmpDir).getHost(); if (StringUtils.isNotEmpty(avatarTmpPath)) { imagesService.saveAvatar(avatarTmpPath, visitor.getUid()); + applicationEventPublisher.publishEvent(new UpdateUserEvent(this, visitor)); } } diff --git a/src/main/java/com/juick/www/controllers/Settings.java b/src/main/java/com/juick/www/controllers/Settings.java index 5cda7bc4..a9240cab 100644 --- a/src/main/java/com/juick/www/controllers/Settings.java +++ b/src/main/java/com/juick/www/controllers/Settings.java @@ -22,12 +22,14 @@ import com.juick.util.HttpBadRequestException; import com.juick.util.HttpUtils; import com.juick.www.WebApp; import com.juick.service.*; +import com.juick.service.activities.UpdateUserEvent; import com.juick.service.security.annotation.Visitor; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; @@ -79,6 +81,8 @@ public class Settings { private ImagesService imagesService; @Inject private WebApp webApp; + @Inject + private ApplicationEventPublisher applicationEventPublisher; @GetMapping("/settings") protected String doGet( @@ -171,6 +175,7 @@ public class Settings { if (userService.updateUserInfo(visitor)) { result = String.format("

Your info is updated.

Back to blog.

", visitor.getName()); } + applicationEventPublisher.publishEvent(new UpdateUserEvent(this, visitor)); break; case "jid-del": // FIXME: stop using ugnich-csv in parameters -- cgit v1.2.3