From 4c39169bee102b2bfa82b55717a3a4aa75297946 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 28 Dec 2019 18:51:46 +0300 Subject: ActivityPub: fix formatting note with attachments --- src/main/java/com/juick/server/api/activity/Profile.java | 7 +++---- src/test/java/com/juick/server/tests/ServerTests.java | 16 ++++++++++++++++ src/test/resources/note_with_attachment.json | 1 + 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 src/test/resources/note_with_attachment.json 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 8e735642..9aa9dc37 100644 --- a/src/main/java/com/juick/server/api/activity/Profile.java +++ b/src/main/java/com/juick/server/api/activity/Profile.java @@ -323,8 +323,7 @@ public class Profile { 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); + }).reduce(markdown, (currentUrl, nextUrl) -> String.format("%s\n%s", currentUrl, nextUrl)); CommandResult result = commandsManager.processCommand( user, String.format("#%s %s", postId, commandBody), @@ -342,14 +341,14 @@ public class Profile { User user = new User(); user.setUri(URI.create(activity.getActor())); String markdown = remarkConverter.convertFragment((String)note.get("content")); + // combine note text with attachment urls String commandBody = note.get("attachment") == null ? markdown : ((List) note.get("attachment")).stream().map(attachmentObj -> { Map attachment = (Map) 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); + }).reduce(markdown, (currentUrl, nextUrl) -> String.format("%s\n%s", currentUrl, nextUrl)); CommandResult result = commandsManager.processCommand( user, String.format("#%d/%d %s", reply.getMid(), reply.getRid(), commandBody), diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index a8fc4563..5b3d87eb 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -57,6 +57,7 @@ import com.juick.util.MessageUtils; import com.mitchellbosecke.pebble.PebbleEngine; import com.mitchellbosecke.pebble.error.PebbleException; import com.mitchellbosecke.pebble.template.PebbleTemplate; +import com.overzealous.remark.Remark; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jws; import io.jsonwebtoken.Jwts; @@ -244,12 +245,16 @@ public class ServerTests { private Resource hubzillaFollow; @Value("classpath:announce.json") private Resource noteWithDocument; + @Value("classpath:note_with_attachment.json") + private Resource noteWithAttachment; @Value("classpath:2936611-57.jpg") private Resource jpegNoJfifTiff; @Value("classpath:Transparent.gif") private Resource invisiblePixel; @Inject AppleClientSecretGenerator clientSecretGenerator; + @Inject + private Remark remarkConverter; @Inject private KeystoreManager testKeystoreManager; @@ -2158,6 +2163,17 @@ public class ServerTests { Message testMessage = MockUtils.mockMessage(mid, freefd, "reply"); String activity = IOUtils.toString(noteWithDocument.getInputStream(), StandardCharsets.UTF_8); Announce announce = jsonMapper.readValue(activity, Announce.class); + String noteString = IOUtils.toString(noteWithAttachment.getInputStream(), StandardCharsets.UTF_8); + Create create = jsonMapper.readValue(noteString, Create.class); + Map note = (Map) create.getObject(); + String markdown = remarkConverter.convertFragment((String) note.get("content")); + String commandBody = note.get("attachment") == null ? markdown : + ((List) note.get("attachment")).stream().map(attachmentObj -> { + Map attachment = (Map) attachmentObj; + String attachmentUrl = attachment.get("url"); + String attachmentName = attachment.get("name"); + return PlainTextFormatter.markdownUrl(attachmentUrl, attachmentName); + }).reduce(markdown, (current, next) -> String.format("%s\n%s", current, next)); } @Test diff --git a/src/test/resources/note_with_attachment.json b/src/test/resources/note_with_attachment.json new file mode 100644 index 00000000..5ef43d10 --- /dev/null +++ b/src/test/resources/note_with_attachment.json @@ -0,0 +1 @@ +{"@context":["https://www.w3.org/ns/activitystreams",{"ostatus":"http://ostatus.org#","atomUri":"ostatus:atomUri","inReplyToAtomUri":"ostatus:inReplyToAtomUri","conversation":"ostatus:conversation","sensitive":"as:sensitive","toot":"http://joinmastodon.org/ns#","votersCount":"toot:votersCount","blurhash":"toot:blurhash","focalPoint":{"@container":"@list","@id":"toot:focalPoint"}}],"id":"https://mastodon.host/users/velociraptor/statuses/103385180763561450/activity","type":"Create","actor":"https://mastodon.host/users/velociraptor","published":"2019-12-28T11:31:05Z","to":["https://www.w3.org/ns/activitystreams#Public"],"cc":["https://mastodon.host/users/velociraptor/followers","https://juick.com/u/Shura"],"object":{"id":"https://mastodon.host/users/velociraptor/statuses/103385180763561450","type":"Note","summary":null,"inReplyTo":"https://juick.com/n/2959243-0","published":"2019-12-28T11:31:05Z","url":"https://mastodon.host/@velociraptor/103385180763561450","attributedTo":"https://mastodon.host/users/velociraptor","to":["https://www.w3.org/ns/activitystreams#Public"],"cc":["https://mastodon.host/users/velociraptor/followers","https://juick.com/u/Shura"],"sensitive":false,"atomUri":"https://mastodon.host/users/velociraptor/statuses/103385180763561450","inReplyToAtomUri":"https://juick.com/n/2959243-0","conversation":"tag:mastodon.host,2019-12-28:objectId=171743672:objectType=Conversation","content":"\u003cp\u003e\u003cspanclass=\"h-card\"\u003e\u003ca href=\"https://juick.com/Shura/\" class=\"u-url mention\"\u003e@\u003cspan\u003eShura\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e я уже два года так 😝 Есть даже что с нового аппарата вешать, но некуда. И старое некуда деть.\u003c/p\u003e","contentMap":{"ru":"\u003cp\u003e\u003cspan class=\"h-card\"\u003e\u003ca href=\"https://juick.com/Shura/\" class=\"u-url mention\"\u003e@\u003cspan\u003eShura\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e я уже два года так 😝 Есть даже что с нового аппарата вешать, но некуда. И старое некуда деть.\u003c/p\u003e"},"attachment":[{"type":"Document","mediaType":"image/jpeg","url":"https://mastodon.host/system/media_attachments/files/022/747/661/original/1e30a7a4d04a2090.jpeg","name":null,"blurhash":"UADJS99a9GRk01s,t6NLOYxvV@k8V@Rkt7xt"}],"tag":[{"type":"Mention","href":"https://juick.com/u/Shura","name":"@Shura@juick.com"}],"replies":{"id":"https://mastodon.host/users/velociraptor/statuses/103385180763561450/replies","type":"Collection","first":{"type":"CollectionPage","next":"https://mastodon.host/users/velociraptor/statuses/103385180763561450/replies?only_other_accounts=true\u0026page=true","partOf":"https://mastodon.host/users/velociraptor/statuses/103385180763561450/replies","items":[]}}},"signature":{"type":"RsaSignature2017","creator":"https://mastodon.host/users/velociraptor#main-key","created":"2019-12-28T11:31:05Z","signatureValue":"VwWdFNIcTd3uMtATC5To08mMUcwf7snwAzp6PpwQ9Ww4QPVXbVpQJZnyDcmNsbzbCyOkT7ByA+zMRe7a/lfhUGVjyNDPAYJu+DrITZ3RFxJxIeWEP/vLCM96Esmllk5iMQ6BugVXQVkUvFOv9jZVGIW7UQrEkXpHH2IGo6JMxz74s/wuiBgpMj6IS6WkVwUsd8MkZinrRcLEkvaRw1aGm/lb6IZ3be3lDVEGEuPS+vrAQI7g4LzF9k0GaE8gtVZBDLsGoSIn65h4wSDAg4TEx+ruaqaNBiVkKlaCZJr3Xsybfjd+7sE7m9NdSh0Z3oZNpCLcSV+7HYDN3yuNPaRi9A=="}} \ No newline at end of file -- cgit v1.2.3