aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-10-24 15:30:51 +0300
committerGravatar Vitaly Takmazov2018-10-24 15:30:51 +0300
commit0886b0290dd35b01776856db40e960863f200c68 (patch)
tree6ff79fcbdd5f9eb56691427cd3c7bd40af6e6a99 /juick-server/src
parent13bf7289cc2dac1897530e6d465bf0a0fef8b3d1 (diff)
AcitvityPub: render Note using pebble
Diffstat (limited to 'juick-server/src')
-rw-r--r--juick-server/src/main/java/com/juick/server/ActivityPubManager.java32
-rw-r--r--juick-server/src/main/resources/templates/layouts/note.html7
-rw-r--r--juick-server/src/main/resources/templates/views/macros/tags.html6
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/ServerTests.java18
4 files changed, 50 insertions, 13 deletions
diff --git a/juick-server/src/main/java/com/juick/server/ActivityPubManager.java b/juick-server/src/main/java/com/juick/server/ActivityPubManager.java
index 14ff7419..bd837eb8 100644
--- a/juick-server/src/main/java/com/juick/server/ActivityPubManager.java
+++ b/juick-server/src/main/java/com/juick/server/ActivityPubManager.java
@@ -15,6 +15,8 @@ import com.juick.service.UserService;
import com.juick.service.activities.*;
import com.juick.service.component.*;
import com.juick.util.MessageUtils;
+import com.mitchellbosecke.pebble.PebbleEngine;
+import com.mitchellbosecke.pebble.template.PebbleTemplate;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -26,8 +28,12 @@ import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
import java.net.URI;
import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
@@ -40,6 +46,8 @@ public class ActivityPubManager implements ActivityListener, NotificationListene
private SocialService socialService;
@Inject
private UserService userService;
+ @Inject
+ private PebbleEngine pebbleEngine;
@Value("${ap_base_uri:http://localhost:8080/}")
private String baseUri;
@Value("${service_user:juick}")
@@ -207,8 +215,22 @@ public class ActivityPubManager implements ActivityListener, NotificationListene
note.setCc(Collections.singletonList(followersUri(msg.getUser())));
}
note.setPublished(msg.getTimestamp());
- note.setContent(msg.isHtml() ? msg.getText()
- : MessageUtils.formatMessage(StringUtils.defaultString(msg.getText())));
+ if (msg.isHtml()) {
+ note.setContent(msg.getText());
+ } else {
+ PebbleTemplate noteTemplate = pebbleEngine.getTemplate("layouts/note");
+ Map<String, Object> context = new HashMap<>();
+ context.put("msg", msg);
+ context.put("baseUri", baseUri);
+ try {
+ Writer writer = new StringWriter();
+ noteTemplate.evaluate(writer, context);
+ note.setContent(writer.toString());
+ } catch (IOException e) {
+ logger.warn("template not rendered, falling back");
+ note.setContent(MessageUtils.formatMessage(StringUtils.defaultString(msg.getText())));
+ }
+ }
if (StringUtils.isNotBlank(msg.getAttachmentType())) {
Image attachment = new Image();
attachment.setId(msg.getAttachment().getMedium().getUrl());
@@ -219,7 +241,7 @@ public class ActivityPubManager implements ActivityListener, NotificationListene
note.setTags(msg.getTags().stream().map(t -> {
Hashtag hashtag = new Hashtag();
hashtag.setId(tagUri(t));
- hashtag.setName("#" + t.getName());
+ hashtag.setName(t.getName());
return hashtag;
}).collect(Collectors.toList()));
if (msg.getReplyToUri() != null && msg.getReplyToUri().toASCIIString().length() > 0) {
@@ -229,9 +251,11 @@ public class ActivityPubManager implements ActivityListener, NotificationListene
Optional<Context> personContext = signatureManager.getContext(URI.create(activity.getAttributedTo()));
if (personContext.isPresent()) {
Person person = (Person) personContext.get();
- note.getTags().add(new Mention(person.getUrl(), "@" + person.getPreferredUsername()));
+ note.getTags().add(new Mention(person.getUrl(), person.getPreferredUsername()));
}
}
+ } else if (MessageUtils.isReply(msg)) {
+ note.getTags().add(new Mention(personWebUri(msg.getTo()), msg.getTo().getName()));
}
return note;
}
diff --git a/juick-server/src/main/resources/templates/layouts/note.html b/juick-server/src/main/resources/templates/layouts/note.html
new file mode 100644
index 00000000..57ec667f
--- /dev/null
+++ b/juick-server/src/main/resources/templates/layouts/note.html
@@ -0,0 +1,7 @@
+{% import "views/macros/tags" %}
+{% if msg.tags.size > 0 %}
+<div class="msg-tags">
+ {{ allTags(baseUri, msg.tags | tagsList) }}
+</div>
+{% endif %}
+<p>{{ msg | formatMessage }}</p> \ No newline at end of file
diff --git a/juick-server/src/main/resources/templates/views/macros/tags.html b/juick-server/src/main/resources/templates/views/macros/tags.html
index 09278ffe..1e668292 100644
--- a/juick-server/src/main/resources/templates/views/macros/tags.html
+++ b/juick-server/src/main/resources/templates/views/macros/tags.html
@@ -2,4 +2,10 @@
{% for tag in tagsList %}
<a href="/{{ uname }}/?tag={{ tag | urlencode }}">{{ tag | raw }}</a>
{% endfor %}
+{% endmacro %}
+
+{% macro allTags(baseUri, tagsList) %}
+{% for tag in tagsList %}
+<a href="{{ baseUri }}tag/{{ tag | urlencode }}">{{ tag | raw }}</a>
+{% endfor %}
{% endmacro %} \ No newline at end of file
diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
index cf189799..8884beef 100644
--- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java
@@ -74,6 +74,7 @@ import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
+import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@@ -1719,16 +1720,15 @@ public class ServerTests {
Note create2 = jsonMapper.readValue(noteStr, Note.class);
}
@Test
- public void activitySerialization() throws JsonProcessingException {
- Message msg = new Message();
- msg.setMid(1);
- User user = new User();
- user.setName("ugnich");
- msg.setUser(user);
- msg.setReplyToUri(URI.create("http://localhost:8080/n/1"));
- msg.setTags(Collections.singletonList(new Tag("YO")));
+ @Transactional
+ public void activitySerialization() throws Exception {
+ Message msg = commandsManager.processCommand(ugnich, "*shit happens", emptyUri).getNewMessage().get();
Note note = activityPubManager.makeNote(msg);
String json = jsonMapper.writeValueAsString(Context.build(note));
+ Message reply = commandsManager.processCommand(freefd, String.format("#%d HI", msg.getMid()), emptyUri).getNewMessage().get();
+ json = jsonMapper.writeValueAsString(Context.build(activityPubManager.makeNote(reply)));
+ Message msgNoTags = commandsManager.processCommand(ugnich, "people", emptyUri).getNewMessage().get();
+ json = jsonMapper.writeValueAsString(Context.build(activityPubManager.makeNote(msgNoTags)));
}
@Test
public void signingSpec() throws IOException {
@@ -1765,6 +1765,6 @@ public class ServerTests {
int newUserUid = userService.createUser("newuser", "assword");
assertThat(userService.getUserByUID(newUserUid).get().getSeen(), is(nullValue()));
messagesService.createMessage(newUserUid, "YO", "", null);
- assertThat(userService.getUserByUID(newUserUid).get().getSeen(), greaterThan(now));
+ assertThat(userService.getUserByUID(newUserUid).get().getSeen(), greaterThanOrEqualTo(now));
}
}