From 9ad08dc78e0cdbe2cdebf10a96c800e72d48e48b Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 22 Jan 2023 14:14:58 +0300 Subject: Refactor messages service, remove 2 unneeded SQL queries --- src/main/java/com/juick/www/api/Post.java | 2 +- src/main/java/com/juick/www/controllers/Site.java | 2 -- .../java/com/juick/www/filters/AnythingFilter.java | 22 ++++++++++++---------- 3 files changed, 13 insertions(+), 13 deletions(-) (limited to 'src/main/java/com/juick/www') diff --git a/src/main/java/com/juick/www/api/Post.java b/src/main/java/com/juick/www/api/Post.java index 97311c21..4f1c2cad 100644 --- a/src/main/java/com/juick/www/api/Post.java +++ b/src/main/java/com/juick/www/api/Post.java @@ -224,7 +224,7 @@ public class Post { @RequestParam Integer mid, @RequestParam(required = false, defaultValue = "0") Integer rid, @RequestParam String body) { - User author = rid == 0 ? messagesService.getMessageAuthor(mid) : messagesService.getReply(mid, rid).getUser(); + User author = rid == 0 ? messagesService.getMessage(mid).orElseThrow(HttpBadRequestException::new).getUser() : messagesService.getReply(mid, rid).getUser(); if (visitor.equals(author)) { if (messagesService.updateMessage(mid, rid, body, false)) { Message result = rid == 0 ? diff --git a/src/main/java/com/juick/www/controllers/Site.java b/src/main/java/com/juick/www/controllers/Site.java index 06513fcc..bfe7d654 100644 --- a/src/main/java/com/juick/www/controllers/Site.java +++ b/src/main/java/com/juick/www/controllers/Site.java @@ -569,8 +569,6 @@ public class Site { model.addAttribute("headers", headers); model.addAttribute("visitorSubscribed", messagesService.isSubscribed(visitor.getUid(), msg.getMid())); model.addAttribute("visitorInBL", userService.isInBL(msg.getUser().getUid(), visitor.getUid())); - model.addAttribute("recomm", messagesService.getMessagesRecommendations(Collections.singletonList(msg.getMid())) - .stream().map(Pair::getRight).toList()); List blUIDs = new ArrayList<>(); for (Message reply : replies) { if (reply.getUser().getUid() != msg.getUser().getUid() && !blUIDs.contains(reply.getUser().getUid())) { diff --git a/src/main/java/com/juick/www/filters/AnythingFilter.java b/src/main/java/com/juick/www/filters/AnythingFilter.java index 2ec82801..ffd11b29 100644 --- a/src/main/java/com/juick/www/filters/AnythingFilter.java +++ b/src/main/java/com/juick/www/filters/AnythingFilter.java @@ -45,8 +45,8 @@ public class AnythingFilter extends OncePerRequestFilter { @Override public void doFilterInternal(HttpServletRequest servletRequest, - HttpServletResponse servletResponse, - FilterChain filterChain) throws IOException, ServletException { + HttpServletResponse servletResponse, + FilterChain filterChain) throws IOException, ServletException { String upgrade = servletRequest.getHeader("Connection"); if (upgrade != null && upgrade.equals("Upgrade")) { filterChain.doFilter(servletRequest, servletResponse); @@ -57,21 +57,23 @@ public class AnythingFilter extends OncePerRequestFilter { int before = NumberUtils.toInt(components.getQueryParams().getFirst("before"), 0); if (before == 0) { boolean isPostNumber = WebUtils.isPostNumber(anything); - int messageId = isPostNumber ? - NumberUtils.toInt(anything) : 0; + int messageId = isPostNumber ? NumberUtils.toInt(anything) : 0; if (isPostNumber && anything.equals(Integer.toString(messageId))) { if (messageId > 0) { - User author = messagesService.getMessageAuthor(messageId); + var message = messagesService.getMessage(messageId); + if (message.isPresent()) { + User author = message.get().getUser(); - if (author != null) { - servletResponse.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); - servletResponse.setHeader("Location", "/" + author.getName() + "/" + anything); - return; + if (author != null) { + servletResponse.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); + servletResponse.setHeader("Location", "/" + author.getName() + "/" + anything); + return; + } } } } - ; + ; if (userService.getUsernames().stream().anyMatch(name -> name.equals(anything))) { servletResponse.sendRedirect("/" + anything + "/"); } else { -- cgit v1.2.3