From ce957b50f913960c8dff04393c5f060a8979bd50 Mon Sep 17 00:00:00 2001 From: Grishka Date: Fri, 7 Jun 2024 12:46:51 +0300 Subject: [PATCH] Fix profile links and tests --- src/main/java/smithereen/routes/ProfileRoutes.java | 4 ++-- src/main/java/smithereen/routes/SettingsAdminRoutes.java | 2 +- .../java/smithereen/templates/PostprocessHTMLFilter.java | 6 ++++-- src/main/java/smithereen/text/TextProcessor.java | 4 ++-- src/main/resources/templates/desktop/profile.twig | 2 +- src/main/resources/templates/mobile/profile.twig | 2 +- src/test/java/smithereen/HTMLProcessingTest.java | 6 +++--- 7 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main/java/smithereen/routes/ProfileRoutes.java b/src/main/java/smithereen/routes/ProfileRoutes.java index 7ee27b89..b2839b4d 100644 --- a/src/main/java/smithereen/routes/ProfileRoutes.java +++ b/src/main/java/smithereen/routes/ProfileRoutes.java @@ -140,7 +140,7 @@ public static Object profile(Request req, Response resp){ contactsFields.add(Map.of("name", l.get("profile_city"), "value", user.location)); if(StringUtils.isNotEmpty(user.website)){ String url=TextProcessor.escapeHTML(user.website); - contactsFields.add(Map.of("name", l.get("profile_website"), "value", ""+url+"", "html", "true")); + contactsFields.add(Map.of("name", l.get("profile_website"), "value", ""+url+"", "html", "true")); } for(User.ContactInfoKey key:User.ContactInfoKey.values()){ if(user.contacts.containsKey(key)){ @@ -169,7 +169,7 @@ public static Object profile(Request req, Response resp){ url=TextProcessor.escapeHTML(url); String v=""; field.put("value", v); field.put("html", "true"); diff --git a/src/main/java/smithereen/routes/SettingsAdminRoutes.java b/src/main/java/smithereen/routes/SettingsAdminRoutes.java index aec8e734..a9d36bf3 100644 --- a/src/main/java/smithereen/routes/SettingsAdminRoutes.java +++ b/src/main/java/smithereen/routes/SettingsAdminRoutes.java @@ -606,7 +606,7 @@ public static Object viewReport(Request req, Response resp, Account self, Applic }; return new ViolationReportActionViewModel(a, TextProcessor.substituteLinks(mainText, links), switch(a.actionType()){ default -> null; - case COMMENT -> TextProcessor.postprocessPostHTMLForDisplay(a.text(), false); + case COMMENT -> TextProcessor.postprocessPostHTMLForDisplay(a.text(), false, false); case RESOLVE_WITH_ACTION -> { User targetUser=users.get(report.targetID); String statusStr=switch(UserBanStatus.valueOf(a.extra().get("status").getAsString())){ diff --git a/src/main/java/smithereen/templates/PostprocessHTMLFilter.java b/src/main/java/smithereen/templates/PostprocessHTMLFilter.java index d0d1f303..9ce56bb1 100644 --- a/src/main/java/smithereen/templates/PostprocessHTMLFilter.java +++ b/src/main/java/smithereen/templates/PostprocessHTMLFilter.java @@ -14,11 +14,13 @@ public class PostprocessHTMLFilter implements Filter{ @Override public Object apply(Object input, Map args, PebbleTemplate self, EvaluationContext context, int lineNumber) throws PebbleException{ - return new SafeString(TextProcessor.postprocessPostHTMLForDisplay((String)input, (boolean)args.getOrDefault("forceTargetBlank", Boolean.FALSE))); + return new SafeString(TextProcessor.postprocessPostHTMLForDisplay((String)input, + (boolean)args.getOrDefault("forceTargetBlank", Boolean.FALSE), + (boolean)args.getOrDefault("keepLinksRel", Boolean.FALSE))); } @Override public List getArgumentNames(){ - return List.of("forceTargetBlank"); + return List.of("forceTargetBlank", "keepLinksRel"); } } diff --git a/src/main/java/smithereen/text/TextProcessor.java b/src/main/java/smithereen/text/TextProcessor.java index 3c8dfad5..b3536190 100644 --- a/src/main/java/smithereen/text/TextProcessor.java +++ b/src/main/java/smithereen/text/TextProcessor.java @@ -433,7 +433,7 @@ public void tail(@NotNull Node node, int depth){ return newBody.html(); } - public static String postprocessPostHTMLForDisplay(String text, boolean forceTargetBlank){ + public static String postprocessPostHTMLForDisplay(String text, boolean forceTargetBlank, boolean keepLinksRel){ if(text==null) return ""; Document doc=Jsoup.parseBodyFragment(text); @@ -469,7 +469,7 @@ public static String postprocessPostHTMLForDisplay(String text, boolean forceTar URI uri=new URI(href); if(forceTargetBlank || (uri.isAbsolute() && (uri.getHost()==null || !Config.isLocal(uri)))){ el.attr("target", "_blank"); - if(!el.hasAttr("rel")) + if(!keepLinksRel || !el.hasAttr("rel")) el.attr("rel", "noopener ugc"); } }catch(URISyntaxException x){} diff --git a/src/main/resources/templates/desktop/profile.twig b/src/main/resources/templates/desktop/profile.twig index 22e932e9..b084ccca 100644 --- a/src/main/resources/templates/desktop/profile.twig +++ b/src/main/resources/templates/desktop/profile.twig @@ -12,7 +12,7 @@ {% endif %} {% for fld in fields %}
{{ fld.name }}:
-
{% if fld.html %}{{ fld.value | postprocessHTML }}{% else %}{{ fld.value | nl2br }}{% endif %}
+
{% if fld.html %}{{ fld.value | postprocessHTML(keepLinksRel=true) }}{% else %}{{ fld.value | nl2br }}{% endif %}
{% else %}
{{ L('profile_no_info') }}
{% endfor %} diff --git a/src/main/resources/templates/mobile/profile.twig b/src/main/resources/templates/mobile/profile.twig index ece5acc2..ff5926c1 100644 --- a/src/main/resources/templates/mobile/profile.twig +++ b/src/main/resources/templates/mobile/profile.twig @@ -11,7 +11,7 @@ {% endif %} {% for fld in fields %}
{{ fld.name }}:
-
{% if fld.html %}{{ fld.value | postprocessHTML }}{% else %}{{ fld.value | nl2br }}{% endif %}
+
{% if fld.html %}{{ fld.value | postprocessHTML(keepLinksRel=true) }}{% else %}{{ fld.value | nl2br }}{% endif %}
{% else %}
{{ L('profile_no_info') }}
{% endfor %} diff --git a/src/test/java/smithereen/HTMLProcessingTest.java b/src/test/java/smithereen/HTMLProcessingTest.java index adb65974..a18067a2 100644 --- a/src/test/java/smithereen/HTMLProcessingTest.java +++ b/src/test/java/smithereen/HTMLProcessingTest.java @@ -29,7 +29,7 @@ public void testMastodonTootMicroformatIsKept(){ "

#photo #birds #sparrow #фото #птицы #воробей

\n"+ "

@rf @russian_mastodon

"; - String out=TextProcessor.postprocessPostHTMLForDisplay(TextProcessor.sanitizeHTML(in), false); + String out=TextProcessor.postprocessPostHTMLForDisplay(TextProcessor.sanitizeHTML(in), false, false); assertEquals(expected.replaceAll("\\s*\n", ""), out.replaceAll("\\s*\n", "")); } @@ -37,7 +37,7 @@ public void testMastodonTootMicroformatIsKept(){ public void testTargetBlankIsAddedToExternalLinks(){ String in="External link"; String expected="External link"; - String out=TextProcessor.postprocessPostHTMLForDisplay(TextProcessor.sanitizeHTML(in), false); + String out=TextProcessor.postprocessPostHTMLForDisplay(TextProcessor.sanitizeHTML(in), false, false); assertEquals(expected, out); } @@ -79,7 +79,7 @@ public void testUnsupportedUrlSchemesAreRemoved(){ public void testRelativeUrlsAreExpended(){ String in="Relative link"; String expected="Relative link"; - assertEquals(expected, TextProcessor.postprocessPostHTMLForDisplay(TextProcessor.sanitizeHTML(in, URI.create("https://example.com/")), false)); + assertEquals(expected, TextProcessor.postprocessPostHTMLForDisplay(TextProcessor.sanitizeHTML(in, URI.create("https://example.com/")), false, false)); } @Test