aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2019-02-23 18:26:05 +0300
committerGravatar Vitaly Takmazov2019-02-23 18:26:05 +0300
commit116d87c9473b8135d0466be149f070e067e8b810 (patch)
tree0ab85186dd1554c7fd3d2d39c6b606416bf1bb09 /src/main/java
parent42af5984ef2decf3763fab51f274fd9940ec23c7 (diff)
* Add text descriptions to ActivityPub error codes
* Append ActivityPub attachments as urls * Added failing test for Hubzilla activity with Link as object
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/com/juick/formatters/PlainTextFormatter.java8
-rw-r--r--src/main/java/com/juick/server/api/activity/Profile.java67
2 files changed, 48 insertions, 27 deletions
diff --git a/src/main/java/com/juick/formatters/PlainTextFormatter.java b/src/main/java/com/juick/formatters/PlainTextFormatter.java
index 378a523f..4cb07950 100644
--- a/src/main/java/com/juick/formatters/PlainTextFormatter.java
+++ b/src/main/java/com/juick/formatters/PlainTextFormatter.java
@@ -84,6 +84,14 @@ public class PlainTextFormatter {
return "https://juick.com/m/" + jmsg.getMid();
}
+ public static String markdownUrl(String url, String description) {
+ if (StringUtils.isNotBlank(description)) {
+ return String.format("[%s](%s)", description, url);
+ } else {
+ return url;
+ }
+ }
+
public static String formatPostNumber(com.juick.Message jmsg) {
if (jmsg.getRid() > 0) {
return String.format("%d/%d", jmsg.getMid(), jmsg.getRid());
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 bd8b9ea8..c26941ef 100644
--- a/src/main/java/com/juick/server/api/activity/Profile.java
+++ b/src/main/java/com/juick/server/api/activity/Profile.java
@@ -3,6 +3,7 @@ package com.juick.server.api.activity;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.juick.Message;
import com.juick.User;
+import com.juick.formatters.PlainTextFormatter;
import com.juick.model.CommandResult;
import com.juick.server.ActivityPubManager;
import com.juick.server.CommandsManager;
@@ -259,7 +260,7 @@ public class Profile {
}
@PostMapping(value = "/api/inbox", consumes = {Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE})
- public ResponseEntity<Void> processInbox(InputStream inboxData) throws Exception {
+ public ResponseEntity<CommandResult> processInbox(InputStream inboxData) throws Exception {
String inbox = IOUtils.toString(inboxData, StandardCharsets.UTF_8);
logger.info("Inbox: {}", inbox);
Activity activity = jsonMapper.readValue(inbox, Activity.class);
@@ -269,7 +270,7 @@ public class Profile {
Follow followRequest = (Follow) activity;
applicationEventPublisher.publishEvent(
new FollowEvent(this, followRequest));
- return new ResponseEntity<>(HttpStatus.ACCEPTED);
+ return new ResponseEntity<>(CommandResult.fromString("Follow request accepted"), HttpStatus.ACCEPTED);
}
if (activity instanceof Undo) {
@@ -278,10 +279,10 @@ public class Profile {
String objectObject = (String) object.get("object");
if (objectType.equals("Follow")) {
applicationEventPublisher.publishEvent(new UndoFollowEvent(this, activity.getActor(), objectObject));
- return new ResponseEntity<>(HttpStatus.OK);
+ return new ResponseEntity<>(CommandResult.fromString("Undo follow request accepted"), HttpStatus.OK);
} else if (objectType.equals("Like") || objectType.equals("Announce")) {
applicationEventPublisher.publishEvent(new UndoAnnounceEvent(this, activity.getActor(), objectObject));
- return new ResponseEntity<>(HttpStatus.OK);
+ return new ResponseEntity<>(CommandResult.fromString("Undo like/announce request accepted"), HttpStatus.OK);
}
}
if (activity instanceof Create) {
@@ -290,7 +291,7 @@ public class Profile {
if (note.get("type").equals("Note")) {
URI noteId = URI.create((String) note.get("id"));
if (messagesService.replyExists(noteId)) {
- return new ResponseEntity<>(HttpStatus.OK);
+ return new ResponseEntity<>(CommandResult.fromString("Reply already exists"), HttpStatus.OK);
} else {
String inReplyTo = (String) note.get("inReplyTo");
if (StringUtils.isNotBlank(inReplyTo)) {
@@ -298,38 +299,50 @@ public class Profile {
String postId = activityPubManager.postId(inReplyTo);
User user = new User();
user.setUri(URI.create(activity.getActor()));
- String attachment = StringUtils.EMPTY;
- if (note.get("attachment") != null && ((List) note.get("attachment")).size() > 0) {
- Map<String, Object> attachmentObj = (Map<String, Object>) ((List<Object>) note.get("attachment")).get(0);
- attachment = (String) attachmentObj.get("url");
- }
- String markdown = remarkConverter.convertFragment((String)note.get("content"));
- CommandResult result = commandsManager.processCommand(user, String.format("#%s %s", postId, markdown), URI.create(attachment));
+ String markdown = remarkConverter.convertFragment((String) note.get("content"));
+ String commandBody = note.get("attachment") == null ? markdown :
+ ((List<Object>) note.get("attachment")).stream().map(attachmentObj -> {
+ Map<String, String> attachment = (Map<String, String>) attachmentObj;
+ String attachmentUrl = attachment.get("url");
+ String attachmentName = attachment.get("name");
+ return PlainTextFormatter.markdownUrl(attachmentUrl, attachmentName);
+ }).reduce((source, url) -> String.format("%s\n%s", source, url))
+ .orElse(markdown);
+
+ CommandResult result = commandsManager.processCommand(
+ user, String.format("#%s %s", postId, commandBody),
+ URI.create(StringUtils.EMPTY));
logger.info(jsonMapper.writeValueAsString(result));
if (result.getNewMessage().isPresent()) {
messagesService.updateReplyUri(result.getNewMessage().get(), noteId);
- return new ResponseEntity<>(HttpStatus.OK);
+ return new ResponseEntity<>(result, HttpStatus.OK);
} else {
- return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
+ return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST);
}
} else {
Message reply = messagesService.getReplyByUri(inReplyTo);
if (reply != null) {
User user = new User();
user.setUri(URI.create(activity.getActor()));
- String attachment = StringUtils.EMPTY;
- if (note.get("attachment") != null && ((List) note.get("attachment")).size() > 0) {
- Map<String, Object> attachmentObj = (Map<String, Object>) ((List<Object>) note.get("attachment")).get(0);
- attachment = (String) attachmentObj.get("url");
- }
String markdown = remarkConverter.convertFragment((String)note.get("content"));
- CommandResult result = commandsManager.processCommand(user, String.format("#%d/%d %s", reply.getMid(), reply.getRid(), markdown), URI.create(attachment));
+ String commandBody = note.get("attachment") == null ? markdown :
+ ((List<Object>) note.get("attachment")).stream().map(attachmentObj -> {
+ Map<String, String> attachment = (Map<String, String>) attachmentObj;
+ String attachmentUrl = attachment.get("url");
+ String attachmentName = attachment.get("name");
+ return PlainTextFormatter.markdownUrl(attachmentUrl, attachmentName);
+ }).reduce((source, url) -> String.format("%s\n%s", source, url))
+ .orElse(markdown);
+ CommandResult result = commandsManager.processCommand(
+ user,
+ String.format("#%d/%d %s", reply.getMid(), reply.getRid(), commandBody),
+ URI.create(StringUtils.EMPTY));
logger.info(jsonMapper.writeValueAsString(result));
if (result.getNewMessage().isPresent()) {
messagesService.updateReplyUri(result.getNewMessage().get(), noteId);
- return new ResponseEntity<>(HttpStatus.OK);
+ return new ResponseEntity<>(result, HttpStatus.OK);
} else {
- return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
+ return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST);
}
}
}
@@ -344,25 +357,25 @@ public class Profile {
URI actor = URI.create(activity.getActor());
URI reply = URI.create((String)tombstone.get("id"));
messagesService.deleteReply(actor, reply);
- return new ResponseEntity<>(HttpStatus.OK);
+ return new ResponseEntity<>(CommandResult.fromString("Delete request accepted"), HttpStatus.OK);
}
}
if (activity instanceof Like || activity instanceof Announce) {
applicationEventPublisher.publishEvent(new AnnounceEvent(this, activity.getActor(), (String)activity.getObject()));
- return new ResponseEntity<>(HttpStatus.OK);
+ return new ResponseEntity<>(CommandResult.fromString("Like/announce request accepted"), HttpStatus.OK);
}
logger.warn("Unknown activity: {}", jsonMapper.writeValueAsString(activity));
- return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
+ return new ResponseEntity<>(CommandResult.fromString("Unknown activity"), HttpStatus.NOT_IMPLEMENTED);
}
if (activity instanceof Delete) {
if (activity.getObject() instanceof String) {
// Delete gone user
if (activity.getActor().equals(activity.getObject())) {
- return new ResponseEntity<>(HttpStatus.ACCEPTED);
+ return new ResponseEntity<>(CommandResult.fromString("Delete request accepted"), HttpStatus.ACCEPTED);
}
}
}
- return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
+ return new ResponseEntity<>(CommandResult.fromString("Can not authenticate"), HttpStatus.UNAUTHORIZED);
}
@PostMapping(value = "/u/", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public User fetchUser(@RequestParam URI uri) {