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 --- .../main/java/com/juick/server/MessengerManager.java | 2 +- .../src/main/java/com/juick/server/ServerManager.java | 4 ++-- .../main/java/com/juick/server/TelegramBotManager.java | 2 +- .../src/main/java/com/juick/server/XMPPConnection.java | 4 ++-- .../src/main/java/com/juick/server/api/Messages.java | 14 ++++++-------- .../main/java/com/juick/server/api/Notifications.java | 8 ++++---- juick-server/src/main/java/com/juick/server/api/PM.java | 17 +++++++---------- .../src/main/java/com/juick/server/api/Users.java | 10 ++++------ .../src/main/java/com/juick/server/api/rss/Feeds.java | 2 +- 9 files changed, 28 insertions(+), 35 deletions(-) (limited to 'juick-server/src/main/java') diff --git a/juick-server/src/main/java/com/juick/server/MessengerManager.java b/juick-server/src/main/java/com/juick/server/MessengerManager.java index 51bbb3e2..303e08ce 100644 --- a/juick-server/src/main/java/com/juick/server/MessengerManager.java +++ b/juick-server/src/main/java/com/juick/server/MessengerManager.java @@ -78,7 +78,7 @@ public class MessengerManager implements ApplicationListener { User user_from = userService.getUserByUID(messengerService.getUserId(senderId)).orElse(new User()); logger.info("Found juick user {}", user_from.getUid()); - if (user_from.getUid() == 0) { + if (user_from.isAnonymous()) { try { UserProfile profile = messenger.queryUserProfile(senderId); signupNotify(senderId, messengerService.getSignUpHash(senderId, profile.firstName())); diff --git a/juick-server/src/main/java/com/juick/server/ServerManager.java b/juick-server/src/main/java/com/juick/server/ServerManager.java index 92821feb..c3df33cb 100644 --- a/juick-server/src/main/java/com/juick/server/ServerManager.java +++ b/juick-server/src/main/java/com/juick/server/ServerManager.java @@ -87,7 +87,7 @@ public class ServerManager implements NotificationListener { .stream().map(User::getUid).collect(Collectors.toList()); synchronized (wsHandler.getClients()) { wsHandler.getClients().stream().filter(c -> - (!c.legacy && c.visitor.getUid() == 0) // anonymous users + (!c.legacy && c.visitor.isAnonymous()) // anonymous users || c.visitor.getName().equals(serviceUser) // services || (!c.legacy && uids.contains(c.visitor.getUid()))) // subscriptions .forEach(c -> { @@ -124,7 +124,7 @@ public class ServerManager implements NotificationListener { .stream().map(User::getUid).collect(Collectors.toList()); synchronized (wsHandler.getClients()) { wsHandler.getClients().stream().filter(c -> - (!c.legacy && c.visitor.getUid() == 0) // anonymous users + (!c.legacy && c.visitor.isAnonymous()) // anonymous users || c.visitor.getName().equals(serviceUser) // services || (!c.legacy && threadUsers.contains(c.visitor.getUid()))) // subscriptions .forEach(c -> { diff --git a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java index a30b2bf3..c9097cb2 100644 --- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java +++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java @@ -127,7 +127,7 @@ public class TelegramBotManager implements NotificationListener { telegramService.createTelegramUser(message.from().id(), username); telegramSignupNotify(message.from().id().longValue(), userService.getSignUpHashByTelegramID(message.from().id().longValue(), username)); } else { - if (user_from.getUid() == 0) { + if (user_from.isAnonymous()) { telegramSignupNotify(message.from().id().longValue(), userService.getSignUpHashByTelegramID(message.from().id().longValue(), username)); } else { URI attachment = URI.create(StringUtils.EMPTY); diff --git a/juick-server/src/main/java/com/juick/server/XMPPConnection.java b/juick-server/src/main/java/com/juick/server/XMPPConnection.java index 2c21a973..d15db3f2 100644 --- a/juick-server/src/main/java/com/juick/server/XMPPConnection.java +++ b/juick-server/src/main/java/com/juick/server/XMPPConnection.java @@ -159,7 +159,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { return iq.createResult(vCard); } User user = userService.getUserByName(iq.getTo().getLocal()); - if (user.getUid() > 0) { + if (!user.isAnonymous()) { UserInfo info = userService.getUserInfo(user); VCard userVCard = new VCard(); userVCard.setFormattedName(info.getFullName()); @@ -546,7 +546,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { Jid to = msg.getTo(); if (to.getDomain().equals(router.getDomain().toEscapedString()) || to.equals(this.jid)) { User user_from = userService.getUserByJID(msg.getFrom().asBareJid().toEscapedString()); - if ((user_from == null || user_from.getUid() == 0) && !msg.getFrom().equals(jid)) { + if ((user_from == null || user_from.isAnonymous()) && !msg.getFrom().equals(jid)) { String signuphash = userService.getSignUpHashByJID(msg.getFrom().asBareJid().toEscapedString()); return makeReply(msg.getFrom(), "Для того, чтобы начать пользоваться сервисом, пожалуйста пройдите быструю регистрацию: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nTo start using Juick, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account."); } diff --git a/juick-server/src/main/java/com/juick/server/api/Messages.java b/juick-server/src/main/java/com/juick/server/api/Messages.java index 666bab0a..b4d16937 100644 --- a/juick-server/src/main/java/com/juick/server/api/Messages.java +++ b/juick-server/src/main/java/com/juick/server/api/Messages.java @@ -93,7 +93,6 @@ public class Messages { @RequestParam(required = false) String tag) { User visitor = UserUtils.getCurrentUser(); - int vuid = visitor.getUid(); List mids; if (!StringUtils.isEmpty(uname)) { @@ -120,18 +119,18 @@ public class Messages { } } else { if (!StringUtils.isEmpty(popular)) { - mids = messagesService.getPopular(vuid, before); + mids = messagesService.getPopular(visitor.getUid(), before); } else if (!StringUtils.isEmpty(media)) { - mids = messagesService.getPhotos(vuid, before); + mids = messagesService.getPhotos(visitor.getUid(), before); } else if (!StringUtils.isEmpty(tag)) { Tag tagObject = tagService.getTag(tag, false); if (tagObject != null) { - mids = messagesService.getTag(tagObject.TID, vuid, before, 20); + mids = messagesService.getTag(tagObject.TID, visitor.getUid(), before, 20); } else { return NOT_FOUND; } } else { - mids = messagesService.getAll(vuid, before); + mids = messagesService.getAll(visitor.getUid(), before); } } return ResponseEntity.ok(messagesService.getMessages(mids)); @@ -145,10 +144,9 @@ public class Messages { public ResponseEntity> getThread( @RequestParam(defaultValue = "0") int mid) { User visitor = UserUtils.getCurrentUser(); - int vuid = visitor.getUid(); com.juick.Message msg = messagesService.getMessage(mid); if (msg != null) { - if (!messagesService.canViewThread(mid, vuid)) { + if (!messagesService.canViewThread(mid, visitor.getUid())) { return FORBIDDEN; } else { List replies = messagesService.getReplies(visitor, mid); @@ -162,7 +160,7 @@ public class Messages { @GetMapping(value = "/thread/mark_read/{mid}-{rid}.gif", produces = MediaType.IMAGE_GIF_VALUE) public byte[] markThreadRead(@PathVariable int mid, @PathVariable int rid) throws IOException { User visitor = UserUtils.getCurrentUser(); - if (visitor.getUid() > 0) { + if (!visitor.isAnonymous()) { messagesService.setLastReadComment(visitor, mid, rid); Message msg = messagesService.getMessage(mid); applicationEventPublisher.publishEvent(new MessageReadEvent(this, visitor, msg)); diff --git a/juick-server/src/main/java/com/juick/server/api/Notifications.java b/juick-server/src/main/java/com/juick/server/api/Notifications.java index 5c7cf7b9..e068cbe9 100644 --- a/juick-server/src/main/java/com/juick/server/api/Notifications.java +++ b/juick-server/src/main/java/com/juick/server/api/Notifications.java @@ -73,7 +73,7 @@ public class Notifications { @RequestParam(required = false, defaultValue = "0") int mid, @RequestParam(required = false, defaultValue = "0") int rid) { User visitor = UserUtils.getCurrentUser(); - if ((visitor.getUid() == 0) || !(visitor.getName().equals("juick"))) { + if (visitor.isAnonymous() || !(visitor.getName().equals("juick"))) { throw new HttpForbiddenException(); } if (uid > 0 && mid == 0) { @@ -106,7 +106,7 @@ public class Notifications { @RequestBody List list) { User visitor = UserUtils.getCurrentUser(); // FIXME: it is possible to delete other user's tokens - if ((visitor.getUid() == 0) || !(visitor.getName().equals("juick"))) { + if ((visitor.isAnonymous()) || !(visitor.getName().equals("juick"))) { throw new HttpForbiddenException(); } list.forEach(t -> { @@ -133,7 +133,7 @@ public class Notifications { public Status doPut( @RequestBody List list) throws IOException { User visitor = UserUtils.getCurrentUser(); - if (visitor.getUid() == 0) { + if (visitor.isAnonymous()) { throw new HttpForbiddenException(); } list.forEach(t -> { @@ -159,7 +159,7 @@ public class Notifications { public Status doAndroidRegister( @RequestParam(name = "regid") String regId) { User visitor = UserUtils.getCurrentUser(); - if (visitor.getUid() == 0) { + if (visitor.isAnonymous()) { throw new HttpForbiddenException(); } pushQueriesService.addGCMToken(visitor.getUid(), regId); diff --git a/juick-server/src/main/java/com/juick/server/api/PM.java b/juick-server/src/main/java/com/juick/server/api/PM.java index 3649bb5e..045d465e 100644 --- a/juick-server/src/main/java/com/juick/server/api/PM.java +++ b/juick-server/src/main/java/com/juick/server/api/PM.java @@ -51,8 +51,7 @@ public class PM { public List doGetPM( @RequestParam(required = false) String uname) { User visitor = UserUtils.getCurrentUser(); - int vuid = visitor.getUid(); - if (vuid == 0) { + if (visitor.isAnonymous()) { throw new HttpForbiddenException(); } int uid = 0; @@ -64,7 +63,7 @@ public class PM { throw new HttpBadRequestException(); } - return pmQueriesService.getPMMessages(vuid, uid); + return pmQueriesService.getPMMessages(visitor.getUid(), uid); } @RequestMapping(value = "/pm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @@ -72,8 +71,7 @@ public class PM { @RequestParam String uname, @RequestParam String body) { User visitor = UserUtils.getCurrentUser(); - int vuid = visitor.getUid(); - if (vuid == 0) { + if (visitor.isAnonymous()) { throw new HttpForbiddenException(); } User userTo = AnonymousUser.INSTANCE; @@ -85,11 +83,11 @@ public class PM { throw new HttpBadRequestException(); } - if (userService.isInBLAny(userTo.getUid(), vuid)) { + if (userService.isInBLAny(userTo.getUid(), visitor.getUid())) { throw new HttpForbiddenException(); } - if (pmQueriesService.createPM(vuid, userTo.getUid(), body)) { + if (pmQueriesService.createPM(visitor.getUid(), userTo.getUid(), body)) { com.juick.Message jmsg = new com.juick.Message(); jmsg.setUser(visitor); jmsg.setText(body); @@ -104,8 +102,7 @@ public class PM { public PrivateChats doGetGroupsPMs( @RequestParam(defaultValue = "5") int cnt) { User visitor = UserUtils.getCurrentUser(); - int vuid = visitor.getUid(); - if (vuid == 0) { + if (visitor.isAnonymous()) { throw new HttpForbiddenException(); } if (cnt < 3) { @@ -115,7 +112,7 @@ public class PM { cnt = 10; } - List lastconv = pmQueriesService.getPMLastConversationsUsers(vuid, cnt); + List lastconv = pmQueriesService.getPMLastConversationsUsers(visitor.getUid(), cnt); if (lastconv != null && !lastconv.isEmpty()) { PrivateChats pms = new PrivateChats(); pms.setUsers(lastconv); diff --git a/juick-server/src/main/java/com/juick/server/api/Users.java b/juick-server/src/main/java/com/juick/server/api/Users.java index 8b273354..3e546eac 100644 --- a/juick-server/src/main/java/com/juick/server/api/Users.java +++ b/juick-server/src/main/java/com/juick/server/api/Users.java @@ -69,13 +69,12 @@ public class Users { public List doGetUserRead( @RequestParam String uname) { User visitor = UserUtils.getCurrentUser(); - int vuid = visitor.getUid(); - if (vuid == 0) { + if (visitor.isAnonymous()) { throw new HttpForbiddenException(); } int uid = 0; if (uname == null) { - uid = vuid; + uid = visitor.getUid(); } else { if (WebUtils.isUserName(uname)) { com.juick.User u = userService.getUserByName(uname); @@ -101,13 +100,12 @@ public class Users { public List doGetUserReaders( @RequestParam String uname) { User visitor = UserUtils.getCurrentUser(); - int vuid = visitor.getUid(); - if (vuid == 0) { + if (visitor.isAnonymous()) { throw new HttpForbiddenException(); } int uid = 0; if (uname == null) { - uid = vuid; + uid = visitor.getUid(); } else { if (WebUtils.isUserName(uname)) { com.juick.User u = userService.getUserByName(uname); diff --git a/juick-server/src/main/java/com/juick/server/api/rss/Feeds.java b/juick-server/src/main/java/com/juick/server/api/rss/Feeds.java index a2d44500..d2d112d8 100644 --- a/juick-server/src/main/java/com/juick/server/api/rss/Feeds.java +++ b/juick-server/src/main/java/com/juick/server/api/rss/Feeds.java @@ -45,7 +45,7 @@ public class Feeds { @RequestMapping(value = "/rss/{userName}/blog", method = RequestMethod.GET) public ModelAndView getBlog(@PathVariable String userName) { User user = userService.getUserByName(userName); - if (user.getUid() > 0) { + if (!user.isAnonymous()) { List mids = messagesService.getUserBlog(user.getUid(), 0, 0); ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("messagesView"); -- cgit v1.2.3