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 --- src/main/java/com/juick/User.java | 8 +++--- src/main/java/com/juick/model/AnonymousUser.java | 4 +-- .../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 +++ .../java/com/juick/service/UserServiceImpl.java | 28 ++++++++++++--------- src/main/resources/static/av-96.png | Bin 0 -> 2018 bytes .../resources/templates/views/partial/message.html | 2 +- .../templates/views/partial/navigation.html | 2 +- .../templates/views/partial/usercolumn.html | 2 +- src/main/resources/templates/views/pm_inbox.html | 2 +- src/main/resources/templates/views/pm_sent.html | 2 +- src/main/resources/templates/views/thread.html | 4 +-- 17 files changed, 89 insertions(+), 38 deletions(-) create mode 100644 src/main/resources/static/av-96.png diff --git a/src/main/java/com/juick/User.java b/src/main/java/com/juick/User.java index e2e45122..f1f74892 100644 --- a/src/main/java/com/juick/User.java +++ b/src/main/java/com/juick/User.java @@ -40,7 +40,7 @@ import java.util.Objects; public class User { private int uid; private String name; - private Object avatar; + private String avatar; private String fullName; private int messagesCount; private String authHash; @@ -139,12 +139,12 @@ public class User { this.banned = banned; } - public Object getAvatar() { + public String getAvatar() { return avatar; } - public void setAvatar(Object avatar) { - this.avatar = avatar; + public void setAvatar(String avatarUri) { + this.avatar = avatarUri; } @XmlTransient diff --git a/src/main/java/com/juick/model/AnonymousUser.java b/src/main/java/com/juick/model/AnonymousUser.java index f4511194..069bb5ce 100644 --- a/src/main/java/com/juick/model/AnonymousUser.java +++ b/src/main/java/com/juick/model/AnonymousUser.java @@ -72,7 +72,7 @@ public final class AnonymousUser extends User { } @Override - public Object getAvatar() { + public String getAvatar() { return null; } @@ -116,7 +116,7 @@ public final class AnonymousUser extends User { } @Override - public void setAvatar(Object avatar) { + public void setAvatar(String avatar) { } @Override 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); diff --git a/src/main/java/com/juick/service/UserServiceImpl.java b/src/main/java/com/juick/service/UserServiceImpl.java index fdc4f28c..f05fc93f 100644 --- a/src/main/java/com/juick/service/UserServiceImpl.java +++ b/src/main/java/com/juick/service/UserServiceImpl.java @@ -354,18 +354,22 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { @Transactional @Override public boolean updateUserInfo(final User user, final UserInfo info) { - return getJdbcTemplate().update( - "INSERT INTO usersinfo(user_id, fullname, country, url, descr) VALUES (?, ?, ?, ?, ?) " + - "ON DUPLICATE KEY UPDATE fullname = ?, country = ?, url = ?, descr = ?", - user.getUid(), - info.getFullName(), - info.getCountry(), - info.getUrl(), - info.getDescription(), - info.getFullName(), - info.getCountry(), - info.getUrl(), - info.getDescription()) > 0; + try { + return getJdbcTemplate().update( + "INSERT INTO usersinfo(user_id, fullname, country, url, descr) VALUES (?, ?, ?, ?, ?)", + user.getUid(), + info.getFullName(), + info.getCountry(), + info.getUrl(), + info.getDescription()) > 0; + } catch (DuplicateKeyException e) { + return getJdbcTemplate().update("UPDATE usersinfo SET fullname = ?, country = ?, url = ?, descr = ? WHERE user_id = ?", + info.getFullName(), + info.getCountry(), + info.getUrl(), + info.getDescription(), + user.getUid()) > 0; + } } @Transactional(readOnly = true) diff --git a/src/main/resources/static/av-96.png b/src/main/resources/static/av-96.png new file mode 100644 index 00000000..911c0522 Binary files /dev/null and b/src/main/resources/static/av-96.png differ diff --git a/src/main/resources/templates/views/partial/message.html b/src/main/resources/templates/views/partial/message.html index 00ca048c..b1d27ae5 100644 --- a/src/main/resources/templates/views/partial/message.html +++ b/src/main/resources/templates/views/partial/message.html @@ -4,7 +4,7 @@ {{ msg.user.name }}
- {{ msg.user.name }} + {{ msg.user.name }}
diff --git a/src/main/resources/templates/views/partial/navigation.html b/src/main/resources/templates/views/partial/navigation.html index 03b6c56d..dce2b83e 100644 --- a/src/main/resources/templates/views/partial/navigation.html +++ b/src/main/resources/templates/views/partial/navigation.html @@ -3,7 +3,7 @@ {% if visitor.uid > 0 %} {% else %} diff --git a/src/main/resources/templates/views/partial/usercolumn.html b/src/main/resources/templates/views/partial/usercolumn.html index 2b1963e3..0896bd4e 100644 --- a/src/main/resources/templates/views/partial/usercolumn.html +++ b/src/main/resources/templates/views/partial/usercolumn.html @@ -1,7 +1,7 @@ {% if visitor is not empty and visitor.uid > 0 and visitor.uid != user.uid %}
    diff --git a/src/main/resources/templates/views/pm_inbox.html b/src/main/resources/templates/views/pm_inbox.html index e82e120e..4f97bc86 100644 --- a/src/main/resources/templates/views/pm_inbox.html +++ b/src/main/resources/templates/views/pm_inbox.html @@ -9,7 +9,7 @@ @{{ msg.user.name }}:
    {{ msg.timestamp | prettyTime }}
    diff --git a/src/main/resources/templates/views/pm_sent.html b/src/main/resources/templates/views/pm_sent.html index dcda64d8..ace25301 100644 --- a/src/main/resources/templates/views/pm_sent.html +++ b/src/main/resources/templates/views/pm_sent.html @@ -16,7 +16,7 @@ @{{ msg.user.name }}:
    {{ msg.timestamp | prettyTime }}
    diff --git a/src/main/resources/templates/views/thread.html b/src/main/resources/templates/views/thread.html index 478258cf..47dfd000 100644 --- a/src/main/resources/templates/views/thread.html +++ b/src/main/resources/templates/views/thread.html @@ -6,7 +6,7 @@
    - {{ msg.user.name }} + {{ msg.user.name }}
    {{ msg.user.name }} @@ -125,7 +125,7 @@ {{ msg.user.name }} {% else %} -- cgit v1.2.3