aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2019-01-21 14:49:07 +0300
committerGravatar Vitaly Takmazov2019-01-21 14:49:07 +0300
commit41b6764be12c2b21f0de55b8a80091e279577ab5 (patch)
treedf077502841f2e93820522ab9db999bf091dd9c5
parenta125a8d20c8a95bc877e14716318d6df08622af5 (diff)
ActivityPub: convert HTML to Markdown
-rw-r--r--build.gradle1
-rw-r--r--src/main/java/com/juick/server/api/activity/Profile.java9
-rw-r--r--src/main/java/com/juick/server/configuration/BaseWebConfiguration.java8
-rw-r--r--src/main/java/com/juick/service/MessagesServiceImpl.java2
-rw-r--r--src/test/java/com/juick/MessageTest.java10
-rw-r--r--src/test/java/com/juick/server/tests/ServerTests.java12
6 files changed, 33 insertions, 9 deletions
diff --git a/build.gradle b/build.gradle
index 7deec746..fcf52dfc 100644
--- a/build.gradle
+++ b/build.gradle
@@ -180,6 +180,7 @@ dependencies {
compile 'org.tomitribe:tomitribe-http-signatures:1.1'
compile 'com.squareup.okhttp3:okhttp:3.12.1'
compile 'com.google.api-client:google-api-client:1.28.0'
+ compile "com.kotcrab.remark:remark:1.0.0"
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile('net.sourceforge.htmlunit:htmlunit:2.33')
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 404f0f84..3e0179c8 100644
--- a/src/main/java/com/juick/server/api/activity/Profile.java
+++ b/src/main/java/com/juick/server/api/activity/Profile.java
@@ -29,6 +29,7 @@ import com.juick.server.www.WebApp;
import com.juick.service.MessagesService;
import com.juick.service.UserService;
import com.juick.service.activities.*;
+import com.overzealous.remark.Remark;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -82,6 +83,8 @@ public class Profile {
private ObjectMapper jsonMapper;
@Inject
private WebApp webApp;
+ @Inject
+ private Remark remarkConverter;
@GetMapping(value = "/u/{userName}", produces = {Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE})
public Person getUser(@PathVariable String userName) {
@@ -301,7 +304,8 @@ public class Profile {
Map<String, Object> attachmentObj = (Map<String, Object>) ((List<Object>) note.get("attachment")).get(0);
attachment = (String) attachmentObj.get("url");
}
- CommandResult result = commandsManager.processCommand(user, String.format("#%s %s", postId, note.get("content")), URI.create(attachment));
+ String markdown = remarkConverter.convertFragment((String)note.get("content"));
+ CommandResult result = commandsManager.processCommand(user, String.format("#%s %s", postId, markdown), URI.create(attachment));
logger.info(jsonMapper.writeValueAsString(result));
if (result.getNewMessage().isPresent()) {
messagesService.updateReplyUri(result.getNewMessage().get(), noteId);
@@ -319,7 +323,8 @@ public class Profile {
Map<String, Object> attachmentObj = (Map<String, Object>) ((List<Object>) note.get("attachment")).get(0);
attachment = (String) attachmentObj.get("url");
}
- CommandResult result = commandsManager.processCommand(user, String.format("#%d/%d %s", reply.getMid(), reply.getRid(), note.get("content")), URI.create(attachment));
+ 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));
logger.info(jsonMapper.writeValueAsString(result));
if (result.getNewMessage().isPresent()) {
messagesService.updateReplyUri(result.getNewMessage().get(), noteId);
diff --git a/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java b/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java
index 16693995..c8b88cd1 100644
--- a/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java
+++ b/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java
@@ -18,6 +18,8 @@
package com.juick.server.configuration;
import com.juick.server.KeystoreManager;
+import com.overzealous.remark.Options;
+import com.overzealous.remark.Remark;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -71,4 +73,10 @@ public class BaseWebConfiguration implements WebMvcConfigurer, SchedulingConfigu
public KeystoreManager keystoreManager() {
return new KeystoreManager(keystore, keystorePassword);
}
+ @Bean
+ public Remark remarkConverter() {
+ Options options = new Options();
+ options.inlineLinks = true;
+ return new Remark(options);
+ }
}
diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java
index 245ae783..c3d319d5 100644
--- a/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -1139,7 +1139,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
@Override
public boolean updateReplyUri(Message reply, URI replyUri) {
- return jdbcTemplate.update("UPDATE replies SET reply_uri=?, html=1 WHERE message_id=? AND reply_id=?",
+ return jdbcTemplate.update("UPDATE replies SET reply_uri=?, html=0 WHERE message_id=? AND reply_id=?",
replyUri.toASCIIString(), reply.getMid(), reply.getRid()) > 0;
}
diff --git a/src/test/java/com/juick/MessageTest.java b/src/test/java/com/juick/MessageTest.java
index 4c37a678..2d12a3df 100644
--- a/src/test/java/com/juick/MessageTest.java
+++ b/src/test/java/com/juick/MessageTest.java
@@ -20,6 +20,8 @@ package com.juick;
import com.juick.model.Entity;
import com.juick.test.util.MockUtils;
import com.juick.util.MessageUtils;
+import com.overzealous.remark.Options;
+import com.overzealous.remark.Remark;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
@@ -196,4 +198,12 @@ public class MessageTest {
assertThat(yo.getStart(), is(0));
assertThat(yo.getEnd(), is(7));
}
+ @Test
+ public void ActivityStreamsHTMLtoMarkdownTest() {
+ String text = "<p><span class=\"h-card\"><a href=\"https://juick.com/stanislavv/\" class=\"u-url mention\">@<span>stanislavv</span></a></span> я в <span class=\"h-card\"><a href=\"https://mastodonsocial.ru/@rf\" class=\"u-url mention\">@<span>rf</span></a></span> выкладывал =)</p>";
+ Options options = new Options();
+ options.inlineLinks = true;
+ Remark remark = new Remark(options);
+ String parsed = remark.convertFragment(text);
+ }
}
diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java
index fedbaba0..f1087d8c 100644
--- a/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/src/test/java/com/juick/server/tests/ServerTests.java
@@ -442,7 +442,7 @@ public class ServerTests {
@Test
public void homeTestWithMessages() throws Exception {
String msgText = "Привет, я - Угнич";
- CommandResult result = commandsManager.processCommand(ugnich, msgText, URI.create("http://static.juick.com/settings/facebook.png"));
+ CommandResult result = commandsManager.processCommand(ugnich, msgText, URI.create("https://static.juick.com/settings/facebook.png"));
int mid = result.getNewMessage().get().getMid();
Message msg = messagesService.getMessage(mid).get();
tagService.createTag("тест");
@@ -697,15 +697,15 @@ public class ServerTests {
int uid = userService.createUser("me", "secret");
User user = userService.getUserByUID(uid).orElse(AnonymousUser.INSTANCE);
Tag yo = tagService.getTag("yo", true);
- Message msg = commandsManager.processCommand(user, "*yo yoyo", URI.create("http://static.juick.com/settings/facebook.png")).getNewMessage().get();
+ Message msg = commandsManager.processCommand(user, "*yo yoyo", URI.create("https://static.juick.com/settings/facebook.png")).getNewMessage().get();
assertThat(msg.getAttachmentType(), is("png"));
- Message msgreply = commandsManager.processCommand(user, "#" + msg.getMid() + " yyy", HttpUtils.downloadImage(URI.create("http://static.juick.com/settings/xmpp.png").toURL(), tmpDir)).getNewMessage().get();
+ Message msgreply = commandsManager.processCommand(user, "#" + msg.getMid() + " yyy", HttpUtils.downloadImage(URI.create("https://static.juick.com/settings/xmpp.png").toURL(), tmpDir)).getNewMessage().get();
assertThat(msgreply.getAttachmentType(), equalTo("png"));
assertEquals("text should match", "yoyo",
messagesService.getMessage(msg.getMid()).get().getText());
assertEquals("tag should match", "yo",
tagService.getMessageTags(msg.getMid()).get(0).getTag().getName());
- CommandResult yoyoMsg = commandsManager.processCommand(user, "*yo", URI.create("http://static.juick.com/settings/facebook.png"));
+ CommandResult yoyoMsg = commandsManager.processCommand(user, "*yo", URI.create("https://static.juick.com/settings/facebook.png"));
assertTrue(yoyoMsg.getNewMessage().isPresent());
assertThat(yoyoMsg.getNewMessage().get().getTags().get(0), is(yo));
Message msg2 = yoyoMsg.getNewMessage().get();
@@ -749,10 +749,10 @@ public class ServerTests {
String expectedThirdReply = "Reply posted.\n#" + mid + "/3 "
+ "https://juick.com/m/" + mid + "#3";
assertEquals("should be second reply", expectedSecondReply,
- commandsManager.processCommand(user, "#" + mid + " yoyo", URI.create("http://static.juick.com/settings/facebook.png")).getText());
+ commandsManager.processCommand(user, "#" + mid + " yoyo", URI.create("https://static.juick.com/settings/facebook.png")).getText());
assertEquals("should be third reply", expectedThirdReply,
commandsManager.processCommand(user, " \t\n #" + mid + "/2 ",
- URI.create("http://static.juick.com/settings/facebook.png")).getText());
+ URI.create("https://static.juick.com/settings/facebook.png")).getText());
Message reply = messagesService.getReplies(user, mid).stream().filter(m -> m.getRid() == 3).findFirst()
.orElse(new Message());
Timestamp lastreply = jdbcTemplate.queryForObject("SELECT lastmessage FROM users WHERE id=?", Timestamp.class, user.getUid());