From b0cfa955c77c4e36bd334324d0ccb2ff24ff549f Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 20 Nov 2018 15:16:08 +0300 Subject: versioned avatars in www --- .../server/configuration/WwwAppConfiguration.java | 20 ++++++++++------ src/main/java/com/juick/server/www/WebApp.java | 9 ++++++++ .../com/juick/server/www/controllers/Help.java | 5 +++- .../juick/server/www/controllers/MessagesWWW.java | 27 ++++++++++++++++++---- .../juick/server/www/controllers/NewMessage.java | 4 ++++ .../com/juick/server/www/controllers/Settings.java | 4 ++++ .../com/juick/server/www/controllers/SignUp.java | 4 ++++ 7 files changed, 60 insertions(+), 13 deletions(-) (limited to 'src/main/java/com/juick/server') diff --git a/src/main/java/com/juick/server/configuration/WwwAppConfiguration.java b/src/main/java/com/juick/server/configuration/WwwAppConfiguration.java index 534154af..2286165d 100644 --- a/src/main/java/com/juick/server/configuration/WwwAppConfiguration.java +++ b/src/main/java/com/juick/server/configuration/WwwAppConfiguration.java @@ -43,6 +43,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.resource.VersionResourceResolver; import javax.inject.Inject; +import java.net.MalformedURLException; +import java.nio.file.Paths; import java.util.Collections; import java.util.concurrent.TimeUnit; @@ -56,7 +58,7 @@ public class WwwAppConfiguration implements WebMvcConfigurer { private UserService userService; @Inject private TagService tagService; - @Value("${avatars_path:#{systemEnvironment['TEMP'] ?: 'file:/tmp/'}}") + @Value("${avatars_path:#{systemEnvironment['TEMP'] ?: '/tmp'}}") private String avatarsPath; @Bean public CaffeineCacheManager cacheManager() { @@ -126,11 +128,15 @@ public class WwwAppConfiguration implements WebMvcConfigurer { } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry - .addResourceHandler("/**", "/i/a/**") - .addResourceLocations("classpath:/static/", avatarsPath) - .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)) - .resourceChain(false) - .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**", "/i/a/**")); + try { + registry + .addResourceHandler("/**", "/i/a/**") + .addResourceLocations("classpath:/static/", Paths.get(avatarsPath, "/a/").toUri().toURL().toString()) + .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)) + .resourceChain(false) + .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**", "/i/a/**")); + } catch (MalformedURLException e) { + e.printStackTrace(); + } } } diff --git a/src/main/java/com/juick/server/www/WebApp.java b/src/main/java/com/juick/server/www/WebApp.java index 98327a5d..d2729c89 100644 --- a/src/main/java/com/juick/server/www/WebApp.java +++ b/src/main/java/com/juick/server/www/WebApp.java @@ -17,7 +17,9 @@ package com.juick.server.www; import com.juick.Tag; +import com.juick.User; import com.juick.service.TagService; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import org.springframework.web.servlet.resource.ResourceUrlProvider; @@ -68,4 +70,11 @@ public class WebApp { public String getScriptsUrl() { return resourceUrlProvider.getForLookupPath("/scripts.js"); } + public String getAvatarUrl(User user) { + String avatarUri = resourceUrlProvider.getForLookupPath(String.format("/i/a/%d.png", user.getUid())); + if (StringUtils.isNotEmpty(avatarUri)) { + return avatarUri; + } + return resourceUrlProvider.getForLookupPath("/av-96.png"); + } } diff --git a/src/main/java/com/juick/server/www/controllers/Help.java b/src/main/java/com/juick/server/www/controllers/Help.java index 61b58a9d..e2a89bd6 100644 --- a/src/main/java/com/juick/server/www/controllers/Help.java +++ b/src/main/java/com/juick/server/www/controllers/Help.java @@ -19,6 +19,7 @@ package com.juick.server.www.controllers; import com.juick.server.util.HttpNotFoundException; import com.juick.server.util.UserUtils; +import com.juick.server.www.WebApp; import com.juick.service.MessagesService; import com.juick.server.www.HelpService; import org.commonmark.parser.Parser; @@ -47,6 +48,8 @@ public class Help { private Parser cmParser; @Inject private HtmlRenderer helpRenderer; + @Inject + private WebApp webApp; @GetMapping({"/help/", "/help", "/help/{langOrPage}", "/help/{lang}/{page}"}) public String showHelp( @@ -56,7 +59,7 @@ public class Help { @PathVariable(required = false, name = "langOrPage") String langOrPage, Model model) throws IOException, URISyntaxException { com.juick.User visitor = UserUtils.getCurrentUser(); - + visitor.setAvatar(webApp.getAvatarUrl(visitor)); String navigation = null; if (langOrPage != null) { diff --git a/src/main/java/com/juick/server/www/controllers/MessagesWWW.java b/src/main/java/com/juick/server/www/controllers/MessagesWWW.java index 77ee6e15..50d90555 100644 --- a/src/main/java/com/juick/server/www/controllers/MessagesWWW.java +++ b/src/main/java/com/juick/server/www/controllers/MessagesWWW.java @@ -24,6 +24,7 @@ import com.juick.server.util.HttpForbiddenException; import com.juick.server.util.HttpNotFoundException; import com.juick.server.util.UserUtils; import com.juick.server.util.WebUtils; +import com.juick.server.www.WebApp; import com.juick.service.*; import com.juick.util.MessageUtils; import org.apache.commons.codec.CharEncoding; @@ -68,9 +69,10 @@ public class MessagesWWW { @Inject private CrosspostService crosspostService; @Inject - private ApplicationEventPublisher applicationEventPublisher; + private WebApp webApp; - void fillUserModel(ModelMap model, com.juick.User user, com.juick.User visitor) { + private void fillUserModel(ModelMap model, com.juick.User user, com.juick.User visitor) { + user.setAvatar(webApp.getAvatarUrl(user)); model.addAttribute("user", user); model.addAttribute("isSubscribed", userService.isSubscribed(visitor.getUid(), user.getUid())); model.addAttribute("isInBL", userService.isInBL(visitor.getUid(), user.getUid())); @@ -100,6 +102,8 @@ public class MessagesWWW { } com.juick.User visitor = UserUtils.getCurrentUser(); + visitor.setAvatar(webApp.getAvatarUrl(visitor)); + if (paramSearch != null && paramSearch.length() > 64) { paramSearch = null; } @@ -160,6 +164,7 @@ public class MessagesWWW { List blUIDs = userService.checkBL(visitor.getUid(), msgs.stream().map(m -> m.getUser().getUid()).collect(Collectors.toList())); msgs.forEach(m -> { + m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser())); m.ReadOnly |= blUIDs.contains(m.getUser().getUid()); m.setUnread(unread.contains(m.getMid())); }); @@ -204,6 +209,7 @@ public class MessagesWWW { if (user.isBanned() || user.isAnonymous()) { throw new HttpNotFoundException(); } + visitor.setAvatar(webApp.getAvatarUrl(visitor)); List mids; @@ -277,6 +283,7 @@ public class MessagesWWW { List blUIDs = userService.checkBL(visitor.getUid(), msgs.stream().map(m -> m.getUser().getUid()).collect(Collectors.toList())); msgs.forEach(m -> { + m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser())); m.ReadOnly |= blUIDs.contains(m.getUser().getUid()); m.setUnread(unread.contains(m.getMid())); }); @@ -315,6 +322,7 @@ public class MessagesWWW { if (visitor.isBanned()) { throw new HttpNotFoundException(); } + visitor.setAvatar(webApp.getAvatarUrl(visitor)); model.addAttribute("title", "Теги " + user.getName()); model.addAttribute("headers", ""); @@ -333,6 +341,7 @@ public class MessagesWWW { if (visitor.isBanned()) { throw new HttpNotFoundException(); } + visitor.setAvatar(webApp.getAvatarUrl(visitor)); model.addAttribute("title", "Подписки " + user.getName()); model.addAttribute("headers", ""); model.addAttribute("visitor", visitor); @@ -349,6 +358,7 @@ public class MessagesWWW { if (visitor.isBanned()) { throw new HttpForbiddenException(); } + visitor.setAvatar(webApp.getAvatarUrl(visitor)); model.addAttribute("title", "Читатели " + user.getName()); model.addAttribute("headers", ""); model.addAttribute("visitor", visitor); @@ -365,6 +375,7 @@ public class MessagesWWW { if (visitor.isBanned() || visitor.getUid() != user.getUid()) { throw new HttpForbiddenException(); } + visitor.setAvatar(webApp.getAvatarUrl(visitor)); model.addAttribute("title", "Черный список " + user.getName()); model.addAttribute("headers", ""); model.addAttribute("visitor", visitor); @@ -380,7 +391,7 @@ public class MessagesWWW { @RequestParam(required = false, defaultValue = "0") int before, ModelMap model) throws IOException { com.juick.User visitor = UserUtils.getCurrentUser(); - + visitor.setAvatar(webApp.getAvatarUrl(visitor)); String paramTagStr = StringEscapeUtils.unescapeHtml4(tagName); com.juick.Tag paramTag = tagService.getTag(paramTagStr, false); if (paramTag == null) { @@ -412,6 +423,7 @@ public class MessagesWWW { msgs.stream().map(m -> m.getUser().getUid()).collect(Collectors.toList()) ); msgs.forEach(m -> { + m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser())); m.ReadOnly |= blUIDs.contains(m.getUser().getUid()); m.setUnread(unread.contains(m.getMid())); }); @@ -453,8 +465,10 @@ public class MessagesWWW { if (visitor.isAnonymous()) { return "redirect:/login"; } + visitor.setAvatar(webApp.getAvatarUrl(visitor)); String title = "PM: Inbox"; List msgs = pmQueriesService.getLastPMInbox(visitor.getUid()); + msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser()))); fillUserModel(model, visitor, visitor); model.addAttribute("title", title); model.addAttribute("visitor", visitor); @@ -470,9 +484,10 @@ public class MessagesWWW { if (visitor.isAnonymous()) { return "redirect:/login"; } + visitor.setAvatar(webApp.getAvatarUrl(visitor)); String title = "PM: Sent"; List msgs = pmQueriesService.getLastPMSent(visitor.getUid()); - + msgs.forEach(m -> m.getUser().setAvatar(webApp.getAvatarUrl(m.getUser()))); if (WebUtils.isNotUserName(uname)) { uname = StringUtils.EMPTY; } @@ -495,7 +510,7 @@ public class MessagesWWW { if (!messagesService.canViewThread(mid, visitor.getUid())) { throw new HttpForbiddenException(); } - + visitor.setAvatar(webApp.getAvatarUrl(visitor)); Optional message = messagesService.getMessage(mid); if (!message.isPresent() || message.get().getUser().isBanned()) { @@ -517,6 +532,7 @@ public class MessagesWWW { visitor.setUnreadCount(unread.size()); boolean isMsgAuthor = visitor.getUid() == msg.getUser().getUid(); boolean isInBL = userService.isInBLAny(msg.getUser().getUid(), visitor.getUid()); + msg.getUser().setAvatar(webApp.getAvatarUrl(msg.getUser())); msg.VisitorCanComment = isMsgAuthor || !(msg.ReadOnly || isInBL); } model.addAttribute("msg", msg); @@ -571,6 +587,7 @@ public class MessagesWWW { if (!visitor.isAnonymous()) { boolean isMsgAuthor = visitor.getUid() == msg.getUser().getUid(); boolean isReplyAuthor = visitor.getUid() == reply.getUser().getUid(); + reply.getUser().setAvatar(webApp.getAvatarUrl(reply.getUser())); reply.VisitorCanComment = isMsgAuthor || (!msg.ReadOnly && msg.VisitorCanComment && (isReplyAuthor || !userService.isInBLAny(visitor.getUid(), reply.getUser().getUid()))); } diff --git a/src/main/java/com/juick/server/www/controllers/NewMessage.java b/src/main/java/com/juick/server/www/controllers/NewMessage.java index 6b5938a5..418d8805 100644 --- a/src/main/java/com/juick/server/www/controllers/NewMessage.java +++ b/src/main/java/com/juick/server/www/controllers/NewMessage.java @@ -17,6 +17,7 @@ package com.juick.server.www.controllers; import com.juick.server.util.UserUtils; +import com.juick.server.www.WebApp; import com.juick.service.TagService; import org.apache.commons.lang3.StringUtils; import org.apache.commons.text.StringEscapeUtils; @@ -36,9 +37,12 @@ public class NewMessage { @Inject private TagService tagService; + @Inject + private WebApp webApp; @GetMapping("/post") protected String postAction(@RequestParam(required = false) String body, ModelMap model) { com.juick.User visitor = UserUtils.getCurrentUser(); + visitor.setAvatar(webApp.getAvatarUrl(visitor)); model.addAttribute("title", "Написать"); model.addAttribute("headers", ""); model.addAttribute("visitor", visitor); diff --git a/src/main/java/com/juick/server/www/controllers/Settings.java b/src/main/java/com/juick/server/www/controllers/Settings.java index cc8f43eb..82eb593a 100644 --- a/src/main/java/com/juick/server/www/controllers/Settings.java +++ b/src/main/java/com/juick/server/www/controllers/Settings.java @@ -22,6 +22,7 @@ import com.juick.model.UserInfo; import com.juick.server.util.HttpBadRequestException; import com.juick.server.util.HttpUtils; import com.juick.server.util.UserUtils; +import com.juick.server.www.WebApp; import com.juick.service.*; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; @@ -77,6 +78,8 @@ public class Settings { private TelegramService telegramService; @Inject private ImagesService imagesService; + @Inject + private WebApp webApp; @GetMapping("/settings") protected String doGet(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException { @@ -84,6 +87,7 @@ public class Settings { if (visitor.isAnonymous()) { response.sendRedirect("/login"); } + visitor.setAvatar(webApp.getAvatarUrl(visitor)); List pages = Arrays.asList("main", "password", "about", "auth-email", "privacy"); String page = request.getParameter("page"); if (StringUtils.isEmpty(page) || !pages.contains(page)) { diff --git a/src/main/java/com/juick/server/www/controllers/SignUp.java b/src/main/java/com/juick/server/www/controllers/SignUp.java index 6a4fe063..ee9c6703 100644 --- a/src/main/java/com/juick/server/www/controllers/SignUp.java +++ b/src/main/java/com/juick/server/www/controllers/SignUp.java @@ -19,6 +19,7 @@ package com.juick.server.www.controllers; import com.juick.server.util.HttpBadRequestException; import com.juick.server.util.HttpForbiddenException; import com.juick.server.util.UserUtils; +import com.juick.server.www.WebApp; import com.juick.service.CrosspostService; import com.juick.service.EmailService; import com.juick.service.MessengerService; @@ -48,6 +49,8 @@ public class SignUp { private MessengerService messengerService; @Inject private EmailService emailService; + @Inject + private WebApp webApp; @GetMapping("/signup") @@ -84,6 +87,7 @@ public class SignUp { } model.addAttribute("title", "Новый пользователь"); + visitor.setAvatar(webApp.getAvatarUrl(visitor)); model.addAttribute("visitor", visitor); model.addAttribute("account", account); model.addAttribute("type", type); -- cgit v1.2.3