From fc1c943f8cce3bd2c95ae5730bd17d60404d0ad9 Mon Sep 17 00:00:00 2001 From: Marcus Fihlon Date: Sun, 31 Mar 2024 16:09:05 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=84=20add=20avatar,=20author=20and=20p?= =?UTF-8?q?rofile=20to=20social=20media=20messages=20closes=20#5,=20#6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/themes/apus/views/message-view.css | 9 +++++++++ .../java/swiss/fihlon/apus/social/Message.java | 2 +- .../apus/social/mastodon/MastodonAPI.java | 9 ++++++++- .../swiss/fihlon/apus/ui/view/MessageView.java | 17 ++++++++++++++++- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/frontend/themes/apus/views/message-view.css b/frontend/themes/apus/views/message-view.css index 09b5b83..620af92 100644 --- a/frontend/themes/apus/views/message-view.css +++ b/frontend/themes/apus/views/message-view.css @@ -31,6 +31,15 @@ display: none; } +.message-view header .author-container { + display: inline-block; + margin-left: var(--lumo-space-s); +} + +.message-view header .author { + font-weight: bold; +} + .message-view .datetime { font-size: var(--lumo-font-size-xs); font-style: italic; diff --git a/src/main/java/swiss/fihlon/apus/social/Message.java b/src/main/java/swiss/fihlon/apus/social/Message.java index a8d699f..6a359a8 100644 --- a/src/main/java/swiss/fihlon/apus/social/Message.java +++ b/src/main/java/swiss/fihlon/apus/social/Message.java @@ -23,7 +23,7 @@ import java.util.List; public record Message(@NotNull String id, @NotNull LocalDateTime date, - @NotNull String author, @NotNull String avatar, + @NotNull String author, @NotNull String avatar, @NotNull String profile, @NotNull String html, @NotNull List images, boolean isReply, boolean isSensitive) implements Comparable { diff --git a/src/main/java/swiss/fihlon/apus/social/mastodon/MastodonAPI.java b/src/main/java/swiss/fihlon/apus/social/mastodon/MastodonAPI.java index cde7534..82e8929 100644 --- a/src/main/java/swiss/fihlon/apus/social/mastodon/MastodonAPI.java +++ b/src/main/java/swiss/fihlon/apus/social/mastodon/MastodonAPI.java @@ -69,13 +69,20 @@ private Message convertToMessage(@NotNull final Status status) { final LocalDateTime date = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); final String author = account == null ? "" : account.getDisplayName(); final String avatar = account == null ? "" : account.getAvatar(); + final String profile = account == null ? "" : getProfile(account); final String html = status.getContent(); final List images = getImages(status.getMediaAttachments()); final String inReplyToId = status.getInReplyToId(); final boolean isReply = inReplyToId != null && !inReplyToId.isBlank(); final boolean isSensitive = status.isSensitive(); - return new Message(id, date, author, avatar, html, images, isReply, isSensitive); + return new Message(id, date, author, avatar, profile, html, images, isReply, isSensitive); + } + + @NotNull + private String getProfile(@NotNull final Account account) { + final var profile = account.getAcct(); + return profile.contains("@") ? profile : profile.concat("@").concat(instance); } private List getImages(@NotNull final List mediaAttachments) { diff --git a/src/main/java/swiss/fihlon/apus/ui/view/MessageView.java b/src/main/java/swiss/fihlon/apus/ui/view/MessageView.java index 3ab58a8..9d50ea7 100644 --- a/src/main/java/swiss/fihlon/apus/ui/view/MessageView.java +++ b/src/main/java/swiss/fihlon/apus/ui/view/MessageView.java @@ -21,8 +21,11 @@ import com.vaadin.flow.component.Html; import com.vaadin.flow.component.Text; import com.vaadin.flow.component.UI; +import com.vaadin.flow.component.avatar.Avatar; import com.vaadin.flow.component.dependency.CssImport; import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.component.html.Footer; +import com.vaadin.flow.component.html.Header; import com.vaadin.flow.component.html.Image; import org.jetbrains.annotations.NotNull; import org.jsoup.Jsoup; @@ -37,11 +40,23 @@ public final class MessageView extends Div { public MessageView(@NotNull final Message message) { addClassName("message-view"); + add(createHeaderComponent(message)); add(createTextComponent(message)); add(createImageComponents(message)); add(createDateTimeComponent(message)); } + @NotNull Component createHeaderComponent(@NotNull final Message message) { + final var avatar = new Avatar(message.author(), message.avatar()); + final var author = new Div(new Text(message.author())); + author.addClassName("author"); + final var profile = new Div(new Text(message.profile())); + profile.addClassName("profile"); + final var authorContainer = new Div(author, profile); + authorContainer.addClassName("author-container"); + return new Header(avatar, authorContainer); + } + @NotNull private Component createTextComponent(@NotNull final Message message) { final String messageText = Jsoup.parse(message.html()).text(); @@ -64,7 +79,7 @@ private Component[] createImageComponents(@NotNull final Message message) { @NotNull private Component createDateTimeComponent(@NotNull final Message message) { - final var dateTimeComponent = new Div(); + final var dateTimeComponent = new Footer(); dateTimeComponent.addClassName("datetime"); final var prettyTime = new PrettyTime(UI.getCurrent().getLocale()); dateTimeComponent.add(new Text(prettyTime.format(message.date())));