Skip to content

Commit

Permalink
♻️ Extract HTML code modification to utility class #85
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Fihlon <[email protected]>
  • Loading branch information
McPringle committed Apr 13, 2024
1 parent ac8c063 commit 4ec0fa7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/main/java/swiss/fihlon/apus/service/SocialService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import swiss.fihlon.apus.configuration.Configuration;
import swiss.fihlon.apus.social.Message;
import swiss.fihlon.apus.social.mastodon.MastodonAPI;
import swiss.fihlon.apus.util.HtmlUtil;

import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -83,7 +84,7 @@ private void updateMessages() {
.filter(message -> !blockedProfiles.contains(message.profile()))
.filter(message -> !filterSensitive || !message.isSensitive())
.filter(message -> !filterReplies || !message.isReply())
.filter(message -> filterLength <= 0 || Jsoup.parse(message.html()).text().length() <= filterLength)
.filter(message -> filterLength <= 0 || HtmlUtil.extractText(message.html()).length() <= filterLength)
.filter(this::checkWordFilter)
.toList();
synchronized (this) {
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/swiss/fihlon/apus/ui/view/MessageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,12 @@
import com.vaadin.flow.component.html.Image;
import org.jetbrains.annotations.NotNull;
import org.ocpsoft.prettytime.PrettyTime;
import org.owasp.html.HtmlPolicyBuilder;
import org.owasp.html.PolicyFactory;
import swiss.fihlon.apus.social.Message;
import swiss.fihlon.apus.util.HtmlUtil;

@CssImport(value = "./themes/apus/views/message-view.css")
public final class MessageView extends Div {

private static final PolicyFactory POLICY_FACTORY = new HtmlPolicyBuilder()
.allowElements("p", "br", "a", "b", "i", "u", "em", "strong", "mark", "code", "img")
.allowUrlProtocols("https")
.allowAttributes("href").onElements("a")
.allowAttributes("src").onElements("img")
.toFactory();

public MessageView(@NotNull final Message message) {
setId("message-" + message.id());
addClassName("message-view");
Expand Down Expand Up @@ -70,7 +62,7 @@ private Component createAvatarComponent(@NotNull final Message message) {
@NotNull
private Component createTextComponent(@NotNull final Message message) {
final String unsafeHtml = message.html();
final String saveHtml = POLICY_FACTORY.sanitize(unsafeHtml);
final String saveHtml = HtmlUtil.sanitize(unsafeHtml);
return new Html(String.format("<div class=\"content\">%s</div>", saveHtml));
}

Expand Down
46 changes: 46 additions & 0 deletions src/main/java/swiss/fihlon/apus/util/HtmlUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Apus - A social wall for conferences with additional features.
* Copyright (C) Marcus Fihlon and the individual contributors to Apus.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package swiss.fihlon.apus.util;

import org.jetbrains.annotations.NotNull;
import org.jsoup.Jsoup;
import org.owasp.html.HtmlPolicyBuilder;
import org.owasp.html.PolicyFactory;

public final class HtmlUtil {

private static final PolicyFactory POLICY_FACTORY = new HtmlPolicyBuilder()
.allowElements("p", "br", "a", "b", "i", "u", "em", "strong", "mark", "code", "img")
.allowUrlProtocols("https")
.allowAttributes("href").onElements("a")
.allowAttributes("src").onElements("img")
.toFactory();

public static String sanitize(@NotNull final String html) {
return POLICY_FACTORY.sanitize(html);
}

public static String extractText(@NotNull final String html) {
return Jsoup.parse(html).text();
}

private HtmlUtil() {
throw new IllegalStateException("Utility class");
}

}

0 comments on commit 4ec0fa7

Please sign in to comment.