Skip to content

Commit

Permalink
✨ trim long social media messages closes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
McPringle committed Mar 31, 2024
1 parent dc90621 commit 1bd87a1
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/main/java/swiss/fihlon/apus/ui/view/MessageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Image;
import org.jetbrains.annotations.NotNull;
import org.jsoup.Jsoup;
import swiss.fihlon.apus.social.Message;

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

private static final int MAX_LENGTH = 500;
private static final String TRUNC_INDICATOR = " […]";
private final transient Message message;

public MessageView(@NotNull final Message message) {
Expand All @@ -37,9 +40,17 @@ public MessageView(@NotNull final Message message) {
@Override
protected void onAttach(@NotNull final AttachEvent attachEvent) {
addClassName("message-view");
add(new Html("<div>" + message.html() + "</div>"));
final String messageText = Jsoup.parse(message.html()).text();
add(new Html(String.format("<div>%s</div>",
messageText.length() > MAX_LENGTH ? truncateMessageText(messageText) : message.html()
)));
for (final String image : message.images()) {
add(new Image(image, image));
}
}

@NotNull
private String truncateMessageText(@NotNull final String messageText) {
return "<p>" + messageText.substring(0, MAX_LENGTH) + TRUNC_INDICATOR + "</p>";
}
}

0 comments on commit 1bd87a1

Please sign in to comment.