From 37e6d26e02d51a4de257200bbf207712fa3a6980 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 23 Oct 2021 05:37:09 +0300 Subject: ImagesService refactoring * Merge ImagesService and ImageUtils * add avatar metadata to RSS feeds --- src/main/java/com/juick/www/rss/Feeds.java | 1 + src/main/java/com/juick/www/rss/MessagesView.java | 44 +++++++++++++++-------- 2 files changed, 30 insertions(+), 15 deletions(-) (limited to 'src/main/java/com/juick/www/rss') diff --git a/src/main/java/com/juick/www/rss/Feeds.java b/src/main/java/com/juick/www/rss/Feeds.java index 8b396fd2..4423b263 100644 --- a/src/main/java/com/juick/www/rss/Feeds.java +++ b/src/main/java/com/juick/www/rss/Feeds.java @@ -55,6 +55,7 @@ public class Feeds { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("messagesView"); modelAndView.addObject("user", user); + modelAndView.addObject("feedType", feedType.name()); modelAndView.addObject("messages", messagesService.getMessages(visitor, mids)); return modelAndView; } diff --git a/src/main/java/com/juick/www/rss/MessagesView.java b/src/main/java/com/juick/www/rss/MessagesView.java index 06a42420..54983273 100644 --- a/src/main/java/com/juick/www/rss/MessagesView.java +++ b/src/main/java/com/juick/www/rss/MessagesView.java @@ -17,8 +17,10 @@ package com.juick.www.rss; +import com.juick.model.Attachment; import com.juick.model.Message; import com.juick.model.User; +import com.juick.service.ImagesService; import com.juick.www.rss.extension.JuickModule; import com.juick.www.rss.extension.JuickModuleImpl; import com.juick.www.WebApp; @@ -44,6 +46,8 @@ import javax.annotation.PostConstruct; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; + +import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.Collections; @@ -57,10 +61,12 @@ import java.util.stream.Collectors; */ public class MessagesView extends AbstractRssFeedView { - private static final Logger logger = LoggerFactory.getLogger(MessagesView.class); + private static final Logger logger = LoggerFactory.getLogger(Feeds.class); @Inject private WebApp webApp; + @Inject + private ImagesService imagesService; @PostConstruct public void init() { @@ -69,16 +75,16 @@ public class MessagesView extends AbstractRssFeedView { @SuppressWarnings("unchecked") @Override - protected List buildFeedItems(@Nonnull Map model, - @Nonnull HttpServletRequest request, - @Nonnull HttpServletResponse response) { - List msgs = (List)model.get("messages"); + protected List buildFeedItems(@Nonnull Map model, @Nonnull HttpServletRequest request, + @Nonnull HttpServletResponse response) { + List msgs = (List) model.get("messages"); return msgs.stream().map(this::createRssItem).collect(Collectors.toList()); } @Override protected void buildFeedMetadata(Map model, Channel feed, HttpServletRequest request) { Object userObj = model.get("user"); + String feedType = (String) model.get("feedType"); if (userObj != null) { User user = (User) userObj; feed.setDescription(String.format("The latest messages by @%s at Juick", user.getName())); @@ -86,12 +92,21 @@ public class MessagesView extends AbstractRssFeedView { feed.setTitle(title); String link = String.format("https://juick.com/%s/", user.getName()); feed.setLink(link); - Image rssImage = new Image(); - rssImage.setUrl(webApp.getAvatarUrl(user)); - rssImage.setTitle(title); - rssImage.setLink(link); - feed.setImage(rssImage); - String href = String.format("https://rss.juick.com/%s/blog", user.getName()); + try { + Image rssImage = new Image(); + rssImage.setUrl(webApp.getAvatarUrl(user)); + rssImage.setTitle(title); + rssImage.setLink(link); + Attachment avatar; + avatar = imagesService.getImageMetadata(rssImage.getUrl()); + rssImage.setHeight(avatar.getHeight()); + rssImage.setWidth(avatar.getWidth()); + feed.setImage(rssImage); + } catch (IOException e) { + logger.warn("Feed avatar not found for {}", user.getName()); + } + + String href = String.format("https://rss.juick.com/%s/%s", user.getName(), feedType); AtomLinkModule atomLinkModule = new AtomLinkModuleImpl(); Link atomLink = new Link(); atomLink.setHref(href); @@ -109,7 +124,6 @@ public class MessagesView extends AbstractRssFeedView { MediaModule mediaModule = new MediaModuleImpl(); feed.getModules().add(mediaModule); - } private Item createRssItem(Message msg) { @@ -120,7 +134,7 @@ public class MessagesView extends AbstractRssFeedView { String messageDescription = isCode ? MessageUtils.formatMessageCode(StringUtils.defaultString(msg.getText())) : MessageUtils.formatMessage(StringUtils.defaultString(msg.getText())); item.setLink(messageUrl); - //item.setGuid(messageUrl); + // item.setGuid(messageUrl); item.setTitle(messageTitle); Description description = new Description(); description.setType("text/html"); @@ -144,9 +158,9 @@ public class MessagesView extends AbstractRssFeedView { MediaContent mediaContent = new MediaContent(reference); mediaContent.setType(type); Metadata metadata = new Metadata(); - metadata.setThumbnail(new Thumbnail[]{new Thumbnail(new URI(msg.getPhoto().getThumbnail()))}); + metadata.setThumbnail(new Thumbnail[] { new Thumbnail(new URI(msg.getPhoto().getThumbnail())) }); module.setMetadata(metadata); - module.setMediaContents(new MediaContent[]{mediaContent}); + module.setMediaContents(new MediaContent[] { mediaContent }); item.getModules().add(module); } catch (URISyntaxException e) { logger.error("Invalid URI", e); -- cgit v1.2.3