diff options
author | Vitaly Takmazov | 2018-11-20 00:07:06 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-11-20 00:07:06 +0300 |
commit | 930e6ab6d604963ae6a48c58fc1bd3735071d9ee (patch) | |
tree | bb236bf36ccab695bef4d45012135c2396c84f77 /src/main/java/com/juick/server/api | |
parent | 74740d5d989f11746266a994840f89e9f6784f73 (diff) |
getMessage refactoring
Diffstat (limited to 'src/main/java/com/juick/server/api')
4 files changed, 32 insertions, 31 deletions
diff --git a/src/main/java/com/juick/server/api/Messages.java b/src/main/java/com/juick/server/api/Messages.java index 4f0009dd..f7485d55 100644 --- a/src/main/java/com/juick/server/api/Messages.java +++ b/src/main/java/com/juick/server/api/Messages.java @@ -39,10 +39,7 @@ import org.springframework.web.bind.annotation.*; import javax.inject.Inject; import java.io.IOException; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Objects; +import java.util.*; /** * @author ugnich @@ -163,8 +160,9 @@ public class Messages { public ResponseEntity<List<com.juick.Message>> getThread( @RequestParam(defaultValue = "0") int mid) { User visitor = UserUtils.getCurrentUser(); - com.juick.Message msg = messagesService.getMessage(mid); - if (msg != null) { + Optional<Message> message = messagesService.getMessage(mid); + if (message.isPresent()) { + Message msg = message.get(); if (!messagesService.canViewThread(mid, visitor.getUid())) { return FORBIDDEN; } else { @@ -189,7 +187,7 @@ public class Messages { User visitor = UserUtils.getCurrentUser(); if (!visitor.isAnonymous()) { messagesService.setLastReadComment(visitor, mid, rid); - Message msg = messagesService.getMessage(mid); + Message msg = messagesService.getMessage(mid).orElseThrow(IllegalStateException::new); userService.updateLastSeen(visitor); applicationEventPublisher.publishEvent( new MessageReadEvent(this, visitor, msg)); diff --git a/src/main/java/com/juick/server/api/Notifications.java b/src/main/java/com/juick/server/api/Notifications.java index 62275f5a..2971a07d 100644 --- a/src/main/java/com/juick/server/api/Notifications.java +++ b/src/main/java/com/juick/server/api/Notifications.java @@ -82,26 +82,23 @@ public class Notifications { } else { if (mid > 0) { // reply - Message msg = messagesService.getMessage(mid); - if (msg != null) { - List<User> users; - if (rid > 0) { - Message op = messagesService.getMessage(mid); - Message reply = messagesService.getReply(mid, rid); - users = subscriptionService.getUsersSubscribedToComments(op, reply); - } else { - users = subscriptionService.getSubscribedUsers(msg.getUser().getUid(), msg); - } - - return ResponseEntity.ok(users.stream().map(User::getUid) - .map(this::collectTokens).collect(Collectors.toList())); + Message msg = messagesService.getMessage(mid).orElseThrow(IllegalStateException::new); + List<User> users; + if (rid > 0) { + Message op = messagesService.getMessage(mid).orElseThrow(IllegalStateException::new); + Message reply = messagesService.getReply(mid, rid); + users = subscriptionService.getUsersSubscribedToComments(op, reply); + } else { + users = subscriptionService.getSubscribedUsers(msg.getUser().getUid(), msg); } + + return ResponseEntity.ok(users.stream().map(User::getUid) + .map(this::collectTokens).collect(Collectors.toList())); } else { // read return ResponseEntity.ok(Collections.singletonList(collectTokens(uid))); } } - throw new HttpBadRequestException(); } @ApiIgnore diff --git a/src/main/java/com/juick/server/api/Post.java b/src/main/java/com/juick/server/api/Post.java index 2c7346b3..d49ec332 100644 --- a/src/main/java/com/juick/server/api/Post.java +++ b/src/main/java/com/juick/server/api/Post.java @@ -41,6 +41,7 @@ import javax.validation.constraints.NotNull; import java.net.URI; import java.net.URL; import java.util.List; +import java.util.Optional; /** * Created by vt on 24/11/2016. @@ -114,11 +115,13 @@ public class Post { if (mid == 0) { throw new HttpBadRequestException(); } - com.juick.Message msg = messagesService.getMessage(mid); - if (msg == null) { + Optional<Message> message = messagesService.getMessage(mid); + if (!message.isPresent()) { throw new HttpNotFoundException(); } + Message msg = message.get(); + com.juick.Message reply = null; if (rid > 0) { reply = messagesService.getReply(mid, rid); @@ -158,10 +161,11 @@ public class Post { if (visitor.isAnonymous()) { throw new HttpForbiddenException(); } - com.juick.Message msg = messagesService.getMessage(mid); - if (msg == null) { + Optional<com.juick.Message> message = messagesService.getMessage(mid); + if (!message.isPresent()) { throw new HttpNotFoundException(); } + Message msg = message.get(); if (msg.getUser().getUid() == visitor.getUid()) { throw new HttpForbiddenException(); } @@ -177,10 +181,11 @@ public class Post { if (visitor.isAnonymous()) { throw new HttpForbiddenException(); } - com.juick.Message msg = messagesService.getMessage(mid); - if (msg == null) { + Optional<com.juick.Message> message = messagesService.getMessage(mid); + if (!message.isPresent()) { throw new HttpNotFoundException(); } + Message msg = message.get(); if (msg.getUser().getUid() == visitor.getUid()) { throw new HttpForbiddenException(); } @@ -205,10 +210,11 @@ public class Post { if (visitor.isAnonymous()) { throw new HttpForbiddenException(); } - com.juick.Message msg = messagesService.getMessage(mid); - if (msg == null) { + Optional<com.juick.Message> message = messagesService.getMessage(mid); + if (!message.isPresent()) { throw new HttpNotFoundException(); } + Message msg = message.get(); if (msg.getUser().getUid() == visitor.getUid()) { throw new HttpForbiddenException(); } @@ -228,7 +234,7 @@ public class Post { User author = rid == 0 ? messagesService.getMessageAuthor(mid) : messagesService.getReply(mid, rid).getUser(); if (visitor.equals(author)) { if (messagesService.updateMessage(mid, rid, body)) { - Message result = rid == 0 ? messagesService.getMessage(mid) : messagesService.getReply(mid, rid); + Message result = rid == 0 ? messagesService.getMessage(mid).orElseThrow(IllegalStateException::new) : messagesService.getReply(mid, rid); return CommandResult.build(result, "Message updated", StringUtils.EMPTY); } throw new HttpBadRequestException(); diff --git a/src/main/java/com/juick/server/api/activity/Profile.java b/src/main/java/com/juick/server/api/activity/Profile.java index 10390ea1..f4160eab 100644 --- a/src/main/java/com/juick/server/api/activity/Profile.java +++ b/src/main/java/com/juick/server/api/activity/Profile.java @@ -250,7 +250,7 @@ public class Profile { messagesService.getReply(mid, rid))); } return Context.build(activityPubManager.makeNote( - messagesService.getMessage(mid))); + messagesService.getMessage(mid).orElseThrow(IllegalStateException::new))); } @PostMapping(value = "/api/inbox", consumes = {Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE}) |