Skip to content

Commit

Permalink
✨ display images from Mastodon posts closes #37
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Fihlon <[email protected]>
  • Loading branch information
McPringle committed Mar 27, 2024
1 parent 909f447 commit 387fb10
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions frontend/themes/apus/views/message-view.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
padding: 10px;
}

.message-view img {
max-width: 100%;
border-radius: 5px;
}

.message-view .invisible {
display: none;
}
26 changes: 20 additions & 6 deletions src/main/java/swiss/fihlon/apus/social/mastodon/MastodonAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import social.bigbone.MastodonClient;
import social.bigbone.api.Pageable;
import social.bigbone.api.entity.Account;
import social.bigbone.api.entity.MediaAttachment;
import social.bigbone.api.entity.Status;
import social.bigbone.api.exception.BigBoneRequestException;
import swiss.fihlon.apus.social.Message;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

import static social.bigbone.api.method.TimelineMethods.StatusOrigin.LOCAL_AND_REMOTE;
Expand Down Expand Up @@ -56,15 +58,27 @@ public List<Message> getMessages(@NotNull final String hashtag) {
}

private Message convertToMessage(@NotNull final Status status) {
final String id = status.getId();
final Account account = status.getAccount();
final Instant instant = status.getCreatedAt().mostPreciseInstantOrNull();
final LocalDateTime date = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
return new Message(
status.getId(),
date,
account.getDisplayName(),
account.getAvatar(),
status.getContent());
final String author = account == null ? "" : account.getDisplayName();
final String avatar = account == null ? "" : account.getAvatar();
final String html = status.getContent() + convertToImages(status.getMediaAttachments());

return new Message(id, date, author, avatar, html);
}

private String convertToImages(@NotNull final List<MediaAttachment> mediaAttachments) {
final List<String> images = new ArrayList<>();

for (final MediaAttachment mediaAttachment : mediaAttachments) {
if (MediaAttachment.MediaType.IMAGE.equals(mediaAttachment.getType())) {
images.add("<img src=\"" + mediaAttachment.getUrl() + "\" />");
}
}

return String.join("", images);
}

}

0 comments on commit 387fb10

Please sign in to comment.