aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-10-08 15:37:48 +0300
committerGravatar Vitaly Takmazov2018-11-29 12:07:00 +0300
commit7647ea6728e9203787dff4f640acb4d6ce3a704f (patch)
tree3afe9a6cb1f1cc16c6fea79c798567ed911bb8cd
parent55cf2dd357088d7bbe0efff6a483679301ab2c22 (diff)
save ActivityPub likes
-rw-r--r--src/main/java/com/juick/server/ActivityPubManager.java6
-rw-r--r--src/main/java/com/juick/server/CommandsManager.java2
-rw-r--r--src/main/java/com/juick/server/TelegramBotManager.java8
-rw-r--r--src/main/java/com/juick/server/api/activity/Profile.java17
-rw-r--r--src/main/java/com/juick/util/MessageUtils.java6
-rw-r--r--src/test/java/com/juick/server/tests/ServerTests.java2
-rw-r--r--src/test/resources/like.json1
7 files changed, 35 insertions, 7 deletions
diff --git a/src/main/java/com/juick/server/ActivityPubManager.java b/src/main/java/com/juick/server/ActivityPubManager.java
index 4601f7d1..dddb4344 100644
--- a/src/main/java/com/juick/server/ActivityPubManager.java
+++ b/src/main/java/com/juick/server/ActivityPubManager.java
@@ -26,6 +26,7 @@ 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.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import javax.annotation.Nonnull;
@@ -212,6 +213,11 @@ public class ActivityPubManager implements ActivityListener, NotificationListene
return uri.replacePath(String.format("/t/%s", tag.getName())).toUriString();
}
+ public String postId(String messageUri) {
+ UriComponents uri = UriComponentsBuilder.fromUriString(messageUri).build();
+ return uri.getPath().substring(uri.getPath().lastIndexOf('/') + 1).replace("-", "/");
+ }
+
public Note makeNote(Message msg) {
Note note = new Note();
note.setId(messageUri(msg));
diff --git a/src/main/java/com/juick/server/CommandsManager.java b/src/main/java/com/juick/server/CommandsManager.java
index 3257304b..fa3c5537 100644
--- a/src/main/java/com/juick/server/CommandsManager.java
+++ b/src/main/java/com/juick/server/CommandsManager.java
@@ -299,7 +299,7 @@ public class CommandsManager {
if (msg.get().getUser() == user) {
return CommandResult.fromString("You can't recommend your own messages.");
}
- MessagesService.RecommendStatus status = messagesService.recommendMessage(mid, user.getUid());
+ MessagesService.RecommendStatus status = messagesService.recommendMessage(mid, user.getUid(), user.getUri().toASCIIString());
switch (status) {
case Added:
applicationEventPublisher.publishEvent(new LikeEvent(this, user, msg.get(),
diff --git a/src/main/java/com/juick/server/TelegramBotManager.java b/src/main/java/com/juick/server/TelegramBotManager.java
index bf2f5a84..1836ffa9 100644
--- a/src/main/java/com/juick/server/TelegramBotManager.java
+++ b/src/main/java/com/juick/server/TelegramBotManager.java
@@ -18,13 +18,19 @@
package com.juick.server;
import com.juick.User;
-import com.juick.service.component.*;
import com.juick.model.AnonymousUser;
import com.juick.model.CommandResult;
import com.juick.server.util.HttpUtils;
import com.juick.service.MessagesService;
import com.juick.service.TelegramService;
import com.juick.service.UserService;
+import com.juick.service.component.LikeEvent;
+import com.juick.service.component.MessageEvent;
+import com.juick.service.component.MessageReadEvent;
+import com.juick.service.component.NotificationListener;
+import com.juick.service.component.PingEvent;
+import com.juick.service.component.SubscribeEvent;
+import com.juick.service.component.TopEvent;
import com.juick.util.MessageUtils;
import com.pengrad.telegrambot.Callback;
import com.pengrad.telegrambot.TelegramBot;
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 d8a8fd90..5a2a766e 100644
--- a/src/main/java/com/juick/server/api/activity/Profile.java
+++ b/src/main/java/com/juick/server/api/activity/Profile.java
@@ -14,6 +14,7 @@ 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.api.activity.model.activities.Follow;
+import com.juick.server.api.activity.model.activities.Like;
import com.juick.server.api.activity.model.activities.Undo;
import com.juick.server.api.activity.model.objects.Image;
import com.juick.server.api.activity.model.objects.Key;
@@ -304,8 +305,7 @@ public class Profile {
String inReplyTo = (String) note.get("inReplyTo");
if (StringUtils.isNotBlank(inReplyTo)) {
if (inReplyTo.startsWith(baseUri)) {
- UriComponents uri = UriComponentsBuilder.fromUriString(inReplyTo).build();
- String postId = uri.getPath().substring(uri.getPath().lastIndexOf('/') + 1).replace("-", "/");
+ String postId = activityPubManager.postId(inReplyTo);
User user = new User();
user.setUri(URI.create(activity.getActor()));
String attachment = StringUtils.EMPTY;
@@ -355,8 +355,17 @@ public class Profile {
return new ResponseEntity<>(HttpStatus.OK);
}
}
- if (activity instanceof Announce) {
- logger.info("Announce: {}", jsonMapper.writeValueAsString(activity));
+ 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));
+ }
return new ResponseEntity<>(HttpStatus.OK);
}
logger.warn("Unknown activity: {}", jsonMapper.writeValueAsString(activity));
diff --git a/src/main/java/com/juick/util/MessageUtils.java b/src/main/java/com/juick/util/MessageUtils.java
index e6cd6306..cc0d7b12 100644
--- a/src/main/java/com/juick/util/MessageUtils.java
+++ b/src/main/java/com/juick/util/MessageUtils.java
@@ -250,7 +250,11 @@ public class MessageUtils {
}
public static String getMarkdownUser(final User user) {
- return String.format("[%s](https://juick.com/%s/)", user.getName(), user.getName());
+ if (user.getUri().toASCIIString().length() > 0) {
+ return String.format("[%s](%s)", user.getName(), user.getUri());
+ } else {
+ return String.format("[%s](https://juick.com/%s/)", user.getName(), user.getName());
+ }
}
// TODO: check if it is really needed
diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java
index 09b7d4dd..3e4fe539 100644
--- a/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/src/test/java/com/juick/server/tests/ServerTests.java
@@ -36,6 +36,7 @@ import com.juick.server.api.activity.model.Context;
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.Like;
import com.juick.server.api.activity.model.activities.Undo;
import com.juick.server.api.activity.model.objects.Note;
import com.juick.server.api.activity.model.objects.Person;
@@ -1742,6 +1743,7 @@ public class ServerTests {
jsonMapper.readValue(IOUtils.toString(new ClassPathResource("webfinger.json").getURI(), StandardCharsets.UTF_8), Account.class);
NodeInfo info = jsonMapper.readValue(IOUtils.toString(new ClassPathResource("xnodeinfo2.json").getURI(), StandardCharsets.UTF_8), NodeInfo.class);
assertThat(info.getUsage().getUsers().getActiveHalfyear(), is(42));
+ Like like = jsonMapper.readValue(IOUtils.toString(new ClassPathResource("like.json").getURI(), StandardCharsets.UTF_8), Like.class);
}
@Test
public void activitySerialization() throws Exception {
diff --git a/src/test/resources/like.json b/src/test/resources/like.json
new file mode 100644
index 00000000..47fcd3a8
--- /dev/null
+++ b/src/test/resources/like.json
@@ -0,0 +1 @@
+{"type":"Like","id":"https://quey.org/users/pazan#likes/63544","actor":"https://quey.org/users/pazan","object":"https://juick.com/n/2928290-0","type":"Like","@context":["https://www.w3.org/ns/activitystreams","https://w3id.org/security/v1",{"manuallyApprovesFollowers":"as:manuallyApprovesFollowers","sensitive":"as:sensitive","movedTo":{"@id":"as:movedTo","@type":"@id"},"Hashtag":"as:Hashtag","ostatus":"http://ostatus.org#","atomUri":"ostatus:atomUri","inReplyToAtomUri":"ostatus:inReplyToAtomUri","conversation":"ostatus:conversation","toot":"http://joinmastodon.org/ns#","Emoji":"toot:Emoji","focalPoint":{"@container":"@list","@id":"toot:focalPoint"},"featured":{"@id":"toot:featured","@type":"@id"},"schema":"http://schema.org#","PropertyValue":"schema:PropertyValue","value":"schema:value"}]} \ No newline at end of file