From b642c55f053583533e23b1fc7fc5ac318fcbd7f0 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 10 May 2018 15:04:57 +0300 Subject: using User::isAnonymous where possible --- .../com/juick/www/controllers/AnythingFilter.java | 2 +- .../com/juick/www/controllers/MessagesWWW.java | 46 ++++++++++------------ .../java/com/juick/www/controllers/NewMessage.java | 8 ++-- .../java/com/juick/www/controllers/Settings.java | 4 +- .../java/com/juick/www/controllers/SignUp.java | 4 +- 5 files changed, 30 insertions(+), 34 deletions(-) (limited to 'juick-www/src/main/java') diff --git a/juick-www/src/main/java/com/juick/www/controllers/AnythingFilter.java b/juick-www/src/main/java/com/juick/www/controllers/AnythingFilter.java index e5d2ec3f..52e748f0 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/AnythingFilter.java +++ b/juick-www/src/main/java/com/juick/www/controllers/AnythingFilter.java @@ -52,7 +52,7 @@ public class AnythingFilter implements Filter { } } else { com.juick.User user = userService.getUserByName(anything); - if (user.getUid() > 0) { + if (!user.isAnonymous()) { ((HttpServletResponse) servletResponse).sendRedirect("/" + user.getName() + "/?before=" + before); } else { filterChain.doFilter(servletRequest, servletResponse); diff --git a/juick-www/src/main/java/com/juick/www/controllers/MessagesWWW.java b/juick-www/src/main/java/com/juick/www/controllers/MessagesWWW.java index 01efd6c9..033404b6 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/MessagesWWW.java +++ b/juick-www/src/main/java/com/juick/www/controllers/MessagesWWW.java @@ -112,7 +112,7 @@ public class MessagesWWW { title = "Поиск: " + StringEscapeUtils.escapeHtml4(paramSearch); mids = messagesService.getSearch(Utils.encodeSphinx(paramSearch), paramBefore); } else if (paramShow == null) { - if (visitor.getUid() > 0) { + if (!visitor.isAnonymous()) { title = "Популярные"; mids = messagesService.getPopular(visitor.getUid(), paramBefore); model.addAttribute("discover", true); @@ -157,7 +157,7 @@ public class MessagesWWW { model.addAttribute("noindex", !(paramShow == null && paramBefore == 0)); List msgs = messagesService.getMessages(mids); - if (visitor.getUid() != 0) { + if (!visitor.isAnonymous()) { fillUserModel(model, visitor, visitor); List unread = messagesService.getUnread(visitor); visitor.setUnreadCount(unread.size()); @@ -172,7 +172,7 @@ public class MessagesWWW { model.addAttribute("tags", tagService.getPopularTags()); model.addAttribute("headers", head); model.addAttribute("showAdv", - paramShow == null && paramBefore == 0 && paramSearch == null && visitor.getUid() == 0); + paramShow == null && paramBefore == 0 && paramSearch == null && visitor.isAnonymous()); if (mids.size() >= 20) { String nextpage = (paramShow != null && paramShow.equals("discuss")) ? "?to=" + msgs.get(msgs.size() - 1).getUpdated().toEpochMilli() : "?before=" + mids.get(mids.size() - 1); if (paramShow != null) { @@ -186,7 +186,7 @@ public class MessagesWWW { UriComponents builder = ServletUriComponentsBuilder.fromCurrentRequestUri().build(); String queryString = builder.getQuery(); String requestURI = builder.toUri().getPath(); - if (sape != null && visitor.getUid() == 0 && queryString == null) { + if (sape != null && visitor.isAnonymous() && queryString == null) { String links = sape.getPageLinks(requestURI, sapeCookie).render(); model.addAttribute("links", links); } @@ -227,7 +227,7 @@ public class MessagesWWW { } int privacy = 0; - if (visitor.getUid() > 0) { + if (!visitor.isAnonymous()) { if (user.getUid() == visitor.getUid() || visitor.getUid() == 1) { privacy = -3; } else if (userService.isInWL(user.getUid(), visitor.getUid())) { @@ -273,7 +273,7 @@ public class MessagesWWW { model.addAttribute("paramTag", paramTag); List msgs = messagesService.getMessages(mids); - if (visitor.getUid() != 0) { + if (!visitor.isAnonymous()) { List unread = messagesService.getUnread(visitor); visitor.setUnreadCount(unread.size()); List blUIDs = userService.checkBL(visitor.getUid(), @@ -303,7 +303,7 @@ public class MessagesWWW { UriComponents builder = ServletUriComponentsBuilder.fromCurrentRequestUri().build(); String queryString = builder.getQuery(); String requestURI = builder.toUri().getPath(); - if (sape != null && visitor.getUid() == 0 && queryString == null) { + if (sape != null && visitor.isAnonymous() && queryString == null) { String links = sape.getPageLinks(requestURI, sapeCookie).render(); model.addAttribute("links", links); } @@ -402,13 +402,11 @@ public class MessagesWWW { return "redirect:" + url; } - int visitor_uid = visitor.getUid(); - String title = "*" + StringEscapeUtils.escapeHtml4(paramTag.getName()); model.addAttribute("title", title); - List mids = messagesService.getTag(paramTag.TID, visitor_uid, before, (visitor_uid == 0) ? 40 : 20); + List mids = messagesService.getTag(paramTag.TID, visitor.getUid(), before, (visitor.isAnonymous()) ? 40 : 20); List msgs = messagesService.getMessages(mids); - if (visitor.getUid() != 0) { + if (!visitor.isAnonymous()) { List unread = messagesService.getUnread(visitor); visitor.setUnreadCount(unread.size()); List blUIDs = userService.checkBL( @@ -435,7 +433,7 @@ public class MessagesWWW { model.addAttribute("msgs", msgs); model.addAttribute("tags", tagService.getPopularTags()); model.addAttribute("noindex", before > 0); - model.addAttribute("showAdv", before == 0 && visitor.getUid() == 0); + model.addAttribute("showAdv", before == 0 && visitor.isAnonymous()); model.addAttribute("isSubscribed", tagService.isSubscribed(visitor, paramTag)); model.addAttribute("isInBL", tagService.isInBL(visitor, paramTag)); if (mids.size() >= 20) { @@ -445,7 +443,7 @@ public class MessagesWWW { UriComponents builder = ServletUriComponentsBuilder.fromCurrentRequestUri().build(); String queryString = builder.getQuery(); String requestURI = builder.toUri().getPath(); - if (sape != null && visitor.getUid() == 0 && queryString == null) { + if (sape != null && visitor.isAnonymous() && queryString == null) { String links = sape.getPageLinks(requestURI, sapeCookie).render(); model.addAttribute("links", links); } @@ -454,7 +452,7 @@ public class MessagesWWW { @GetMapping("/pm/inbox") protected String doGetInbox(ModelMap model) { com.juick.User visitor = UserUtils.getCurrentUser(); - if (visitor.getUid() == 0) { + if (visitor.isAnonymous()) { return "redirect:/login"; } String title = "PM: Inbox"; @@ -471,7 +469,7 @@ public class MessagesWWW { protected String doGetSent(@RequestParam(required = false) String uname, ModelMap model) { com.juick.User visitor = UserUtils.getCurrentUser(); - if (visitor.getUid() == 0) { + if (visitor.isAnonymous()) { return "redirect:/login"; } String title = "PM: Sent"; @@ -507,19 +505,17 @@ public class MessagesWWW { } com.juick.User user = userService.getUserByName(uname); - if (user.getUid() == 0 || !msg.getUser().equals(user)) { + if (user.isAnonymous() || !msg.getUser().equals(user)) { return String.format("redirect:/%s/%d", msg.getUser().getName(), mid); } - msg.VisitorCanComment = visitor.getUid() > 0; + msg.VisitorCanComment = !visitor.isAnonymous(); List replies = messagesService.getReplies(visitor, msg.getMid()); - if (!visitor.isAnonymous()) { - applicationEventPublisher.publishEvent(new MessageReadEvent(this, visitor, msg)); - } // this should be after getReplies to mark thread as read fillUserModel(model, user, visitor); - if (visitor.getUid() > 0) { + if (!visitor.isAnonymous()) { List unread = messagesService.getUnread(visitor); visitor.setUnreadCount(unread.size()); + applicationEventPublisher.publishEvent(new MessageReadEvent(this, visitor, msg)); boolean isMsgAuthor = visitor.getUid() == msg.getUser().getUid(); boolean isInBL = userService.isInBLAny(msg.getUser().getUid(), visitor.getUid()); msg.VisitorCanComment = isMsgAuthor || !(msg.ReadOnly || isInBL); @@ -572,8 +568,8 @@ public class MessagesWWW { && !blUIDs.contains(reply.getUser().getUid())) { blUIDs.add(reply.getUser().getUid()); } - reply.VisitorCanComment = visitor.getUid() > 0; - if (visitor.getUid() > 0) { + reply.VisitorCanComment = !visitor.isAnonymous(); + if (!visitor.isAnonymous()) { boolean isMsgAuthor = visitor.getUid() == msg.getUser().getUid(); boolean isReplyAuthor = visitor.getUid() == reply.getUser().getUid(); reply.VisitorCanComment = isMsgAuthor || (!msg.ReadOnly @@ -581,11 +577,11 @@ public class MessagesWWW { } } model.addAttribute("replies", replies); - model.addAttribute("showAdv", visitor.getUid() == 0); + model.addAttribute("showAdv", visitor.isAnonymous()); UriComponents builder = ServletUriComponentsBuilder.fromCurrentRequestUri().build(); String queryString = builder.getQuery(); String requestURI = builder.toUri().getPath(); - if (sape != null && visitor.getUid() == 0 && queryString == null) { + if (sape != null && visitor.isAnonymous() && queryString == null) { String links = sape.getPageLinks(requestURI, sapeCookie).render(); model.addAttribute("links", links); } diff --git a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java index 1b3db61d..a464add2 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java @@ -115,7 +115,7 @@ public class NewMessage { @RequestParam(required = false, defaultValue = StringUtils.EMPTY) String img, @RequestParam(required = false) MultipartFile attach) throws IOException { com.juick.User visitor = UserUtils.getCurrentUser(); - if (visitor.getUid() == 0 || visitor.isBanned()) { + if (visitor.isAnonymous() || visitor.isBanned()) { throw new HttpForbiddenException(); } com.juick.Message msg = messagesService.getMessage(mid); @@ -177,7 +177,7 @@ public class NewMessage { public String doPostPM(@RequestParam(name = "uname", required = false) String unameParam, @RequestParam String body) throws IOException { com.juick.User visitor = UserUtils.getCurrentUser(); - if (visitor.getUid() == 0 || visitor.isBanned()) { + if (visitor.isAnonymous() || visitor.isBanned()) { throw new HttpForbiddenException(); } String uname = unameParam; @@ -189,7 +189,7 @@ public class NewMessage { userTo = userService.getUserByName(uname); } - if (userTo.getUid() == 0 || body.length() > 10240) { + if (userTo.isAnonymous() || body.length() > 10240) { throw new HttpBadRequestException(); } @@ -218,7 +218,7 @@ public class NewMessage { @RequestParam(required = false) MultipartFile attach) throws JsonProcessingException { com.juick.User visitor = UserUtils.getCurrentUser(); - if (visitor.getUid() == 0 || visitor.isBanned()) { + if (visitor.isAnonymous() || visitor.isBanned()) { throw new HttpForbiddenException(); } String body = StringUtils.isNotEmpty(bodyParam) ? bodyParam.replace("\r", StringUtils.EMPTY) : StringUtils.EMPTY; diff --git a/juick-www/src/main/java/com/juick/www/controllers/Settings.java b/juick-www/src/main/java/com/juick/www/controllers/Settings.java index 3d381815..7f6cce44 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Settings.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Settings.java @@ -82,7 +82,7 @@ public class Settings { @GetMapping("/settings") protected String doGet(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException { com.juick.User visitor = UserUtils.getCurrentUser(); - if (visitor.getUid() == 0) { + if (visitor.isAnonymous()) { response.sendRedirect("/login"); } List pages = Arrays.asList("main", "password", "about", "auth-email", "privacy"); @@ -124,7 +124,7 @@ public class Settings { ModelMap model) throws IOException { com.juick.User visitor = UserUtils.getCurrentUser(); - if (visitor.getUid() == 0) { + if (visitor.isAnonymous()) { throw new HttpBadRequestException(); } List pages = Arrays.asList("main", "password", "about", "email", "email-add", "email-del", diff --git a/juick-www/src/main/java/com/juick/www/controllers/SignUp.java b/juick-www/src/main/java/com/juick/www/controllers/SignUp.java index a5851215..02a8006b 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/SignUp.java +++ b/juick-www/src/main/java/com/juick/www/controllers/SignUp.java @@ -103,7 +103,7 @@ public class SignUp { if (action.charAt(0) == 'l') { - if (visitor.getUid() == 0) { + if (visitor.isAnonymous()) { if (username.length() > 32) { throw new HttpBadRequestException(); } @@ -145,7 +145,7 @@ public class SignUp { } } - if (visitor.getUid() == 0) { + if (visitor.isAnonymous()) { hash = userService.getHashByUID(uid); Cookie c = new Cookie("hash", hash); c.setMaxAge(365 * 24 * 60 * 60); -- cgit v1.2.3