From bab10770a77f5f756a58c4ca6f19dd51e1b492ee Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 20 Aug 2021 11:25:44 +0300 Subject: map NSFW tag to as:sensitive ActivityPub property --- src/main/java/com/juick/ActivityPubManager.java | 1 + src/main/java/com/juick/util/MessageUtils.java | 4 ++++ src/main/java/com/juick/www/api/activity/model/Context.java | 1 + .../java/com/juick/www/api/activity/model/objects/Note.java | 11 ++++++++++- src/test/java/com/juick/MessageTest.java | 6 ++++++ src/test/java/com/juick/server/tests/ServerTests.java | 3 ++- 6 files changed, 24 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main/java/com/juick/ActivityPubManager.java b/src/main/java/com/juick/ActivityPubManager.java index ad887819..8aa2ef78 100644 --- a/src/main/java/com/juick/ActivityPubManager.java +++ b/src/main/java/com/juick/ActivityPubManager.java @@ -354,6 +354,7 @@ public class ActivityPubManager implements ActivityListener, NotificationListene note.setCc(cc); } }); + note.setSensitive(MessageUtils.isSensitive(msg)); if (msg.isHtml()) { note.setContent(msg.getText()); } else { diff --git a/src/main/java/com/juick/util/MessageUtils.java b/src/main/java/com/juick/util/MessageUtils.java index 75a7fbf0..b0093b38 100644 --- a/src/main/java/com/juick/util/MessageUtils.java +++ b/src/main/java/com/juick/util/MessageUtils.java @@ -396,4 +396,8 @@ public class MessageUtils { } return result; } + + public static boolean isSensitive(Message msg) { + return msg.getTags().stream().anyMatch((t) -> t.getName().equals("NSFW")); + } } diff --git a/src/main/java/com/juick/www/api/activity/model/Context.java b/src/main/java/com/juick/www/api/activity/model/Context.java index f41aa69c..e75f2440 100644 --- a/src/main/java/com/juick/www/api/activity/model/Context.java +++ b/src/main/java/com/juick/www/api/activity/model/Context.java @@ -115,6 +115,7 @@ public abstract class Context { public static Context build(Context response) { response.context = new ArrayList<>(Arrays.asList(ACTIVITY_STREAMS_URI, SECURITY_URI)); response.context.add(Collections.singletonMap("Hashtag", "as:Hashtag")); + response.context.add(Collections.singletonMap("sensitive", "as:sensitive")); return response; } diff --git a/src/main/java/com/juick/www/api/activity/model/objects/Note.java b/src/main/java/com/juick/www/api/activity/model/objects/Note.java index 8aa196a3..66470b5c 100644 --- a/src/main/java/com/juick/www/api/activity/model/objects/Note.java +++ b/src/main/java/com/juick/www/api/activity/model/objects/Note.java @@ -29,11 +29,12 @@ public class Note extends Context { private List attachment; private List to; private List cc; + private boolean sensitive; public String getContent() { return content; } - + public void setContent(String content) { this.content = content; } @@ -78,4 +79,12 @@ public class Note extends Context { public void setInReplyTo(String inReplyTo) { this.inReplyTo = inReplyTo; } + + public boolean isSensitive() { + return sensitive; + } + + public void setSensitive(boolean sensitive) { + this.sensitive = sensitive; + } } diff --git a/src/test/java/com/juick/MessageTest.java b/src/test/java/com/juick/MessageTest.java index cb4f4e45..9c079fa3 100644 --- a/src/test/java/com/juick/MessageTest.java +++ b/src/test/java/com/juick/MessageTest.java @@ -232,4 +232,10 @@ public class MessageTest { "\n" + "if [ \"$?\" -ne \"0\" ]; then\n"); } + @Test + public void sensitiveTest() { + Message testMessage = MockUtils.mockMessage(514, MockUtils.mockUser(5432, "rtfmpls", "secret"), "yo"); + testMessage.setTags(MessageUtils.parseTags("NSFW test")); + assertThat(MessageUtils.isSensitive(testMessage), is(true)); + } } diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index c37d70bc..f8e0f6a8 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -1816,8 +1816,9 @@ public class ServerTests { public void activitySerialization() throws Exception { Message msgNoTags = commandsManager.processCommand(ugnich, "people", emptyUri).getNewMessage().get(); String json = jsonMapper.writeValueAsString(Context.build(activityPubManager.makeNote(msgNoTags))); - Message msg = commandsManager.processCommand(ugnich, "*shit happens", emptyUri).getNewMessage().get(); + Message msg = commandsManager.processCommand(ugnich, "*NSFW *shit happens", emptyUri).getNewMessage().get(); Note note = activityPubManager.makeNote(msg); + assertThat(note.isSensitive(), is(true)); json = jsonMapper.writeValueAsString(Context.build(note)); Note replyNote = new Note(); replyNote.setId("http://localhost:8080/n/2-1"); -- cgit v1.2.3