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/CommandsManager.java | |
parent | 74740d5d989f11746266a994840f89e9f6784f73 (diff) |
getMessage refactoring
Diffstat (limited to 'src/main/java/com/juick/server/CommandsManager.java')
-rw-r--r-- | src/main/java/com/juick/server/CommandsManager.java | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/src/main/java/com/juick/server/CommandsManager.java b/src/main/java/com/juick/server/CommandsManager.java index e564f18e..2eb58188 100644 --- a/src/main/java/com/juick/server/CommandsManager.java +++ b/src/main/java/com/juick/server/CommandsManager.java @@ -76,7 +76,7 @@ public class CommandsManager { @Inject private ImagesService imagesService; - public CommandResult processCommand(User user, String data, @Nonnull URI attachment) throws Exception { + public CommandResult processCommand(@Nonnull User user, String data, @Nonnull URI attachment) throws Exception { if (!user.isAnonymous()) { userService.updateLastSeen(user); } @@ -126,7 +126,7 @@ public class CommandsManager { String fname = String.format("%d.%s", mid, attachmentType); imagesService.saveImageWithPreviews(attachmentFName, fname); } - Message msg = messagesService.getMessage(mid); + Message msg = messagesService.getMessage(mid).orElseThrow(IllegalStateException::new); subscriptionService.subscribeMessage(msg, user); applicationEventPublisher.publishEvent(new MessageReadEvent(this, user, msg)); @@ -294,17 +294,17 @@ public class CommandsManager { public CommandResult commandRecommend(User user, URI attachment, String... arguments) { int mid = NumberUtils.toInt(arguments[0], 0); if (mid > 0) { - com.juick.Message msg = messagesService.getMessage(mid); - if (msg != null) { - if (msg.getUser() == user) { + Optional<com.juick.Message> msg = messagesService.getMessage(mid); + if (msg.isPresent()) { + if (msg.get().getUser() == user) { return CommandResult.fromString("You can't recommend your own messages."); } MessagesService.RecommendStatus status = messagesService.recommendMessage(mid, user.getUid()); switch (status) { case Added: - applicationEventPublisher.publishEvent(new LikeEvent(this, user, msg, + applicationEventPublisher.publishEvent(new LikeEvent(this, user, msg.get(), subscriptionService.getUsersSubscribedToUserRecommendations( - user.getUid(), msg))); + user.getUid(), msg.get()))); return CommandResult.fromString("Message is added to your recommendations"); case Deleted: return CommandResult.fromString("Message deleted from your recommendations."); @@ -356,15 +356,15 @@ public class CommandsManager { } @UserCommand(pattern = "^(s|u)\\s+#(\\d+)$", help = "S #1234 - subscribe to comments" + "\nU #1234 - unsubscribe from comments", patternFlags = Pattern.CASE_INSENSITIVE) - public CommandResult commandSubscribeMessage(User user, URI attachment, String... args) { + public CommandResult commandSubscribeMessage(@Nonnull User user, URI attachment, String... args) { boolean subscribe = args[0].equalsIgnoreCase("s"); int mid = NumberUtils.toInt(args[1], 0); - Message msg = messagesService.getMessage(mid); - if (msg != null) { + Optional<Message> msg = messagesService.getMessage(mid); + if (msg.isPresent()) { if (subscribe) { - if (subscriptionService.subscribeMessage(msg, user)) { + if (subscriptionService.subscribeMessage(msg.get(), user)) { applicationEventPublisher.publishEvent( - new MessageReadEvent(this, user, msg)); + new MessageReadEvent(this, user, msg.get())); return CommandResult.fromString("Subscribed"); } } else { @@ -406,23 +406,23 @@ public class CommandsManager { return CommandResult.fromString("User not found"); } @UserCommand(pattern = "^#(\\d+)(\\+?)$", help = "#1234 - Show message (#1234+ - message with replies)") - public CommandResult commandShow(User user, URI attachment, String... arguments) { + public CommandResult commandShow(@Nonnull User user, URI attachment, String... arguments) { boolean showReplies = arguments[1].length() > 0; int mid = NumberUtils.toInt(arguments[0], 0); if (mid == 0) { return CommandResult.fromString("Error"); } - com.juick.Message msg = messagesService.getMessage(mid); - if (msg != null) { + Optional<com.juick.Message> msg = messagesService.getMessage(mid); + if (msg.isPresent()) { if (showReplies) { List<com.juick.Message> replies = messagesService.getReplies(user, mid); applicationEventPublisher.publishEvent( - new MessageReadEvent(this, user, msg)); - replies.add(0, msg); + new MessageReadEvent(this, user, msg.get())); + replies.add(0, msg.get()); return CommandResult.fromString(String.join("\n", replies.stream().map(PlainTextFormatter::formatPostSummary).collect(Collectors.toList()))); } - return CommandResult.fromString(PlainTextFormatter.formatPost(msg)); + return CommandResult.fromString(PlainTextFormatter.formatPost(msg.get())); } return CommandResult.fromString("Message not found"); } @@ -453,9 +453,9 @@ public class CommandsManager { @UserCommand(pattern = "^D #(\\d+)$", help = "D #1234 - Delete post", patternFlags = Pattern.CASE_INSENSITIVE) public CommandResult commandDeletePost(User user, URI attachment, String... args) { int mid = Integer.valueOf(args[0]); - Message message = messagesService.getMessage(mid); - if (message != null && messagesService.deleteMessage(user.getUid(), mid)) { - applicationEventPublisher.publishEvent(new DeleteMessageEvent(this, message)); + Optional<Message> message = messagesService.getMessage(mid); + if (message.isPresent() && messagesService.deleteMessage(user.getUid(), mid)) { + applicationEventPublisher.publishEvent(new DeleteMessageEvent(this, message.get())); return CommandResult.fromString("Message deleted"); } return CommandResult.fromString("This is not your message"); @@ -494,11 +494,11 @@ public class CommandsManager { } @UserCommand(pattern = "^(#|\\.)(\\d+)((\\.|\\-|\\/)(\\d+))?\\s([\\s\\S]+)?", help = "#1234 *tag *tag2 - edit tags\n#1234 text - reply to message") - public CommandResult EditOrReply(User user, @Nonnull URI attachment, String... args) throws Exception { + public CommandResult EditOrReply(@Nonnull User user, @Nonnull URI attachment, String... args) throws Exception { int mid = NumberUtils.toInt(args[1]); int rid = NumberUtils.toInt(args[4], 0); String txt = StringUtils.defaultString(args[5]); - Message msg = messagesService.getMessage(mid); + Message msg = messagesService.getMessage(mid).orElseThrow(IllegalStateException::new); Pair<String, List<Tag>> messageTags = tagService.fromString(txt); if (messageTags.getRight().size() > 0) { if (user.getUid() != msg.getUser().getUid()) { @@ -528,7 +528,7 @@ public class CommandsManager { } applicationEventPublisher.publishEvent( new MessageReadEvent(this, user, msg)); - Message original = messagesService.getMessage(mid); + Message original = messagesService.getMessage(mid).orElseThrow(IllegalStateException::new); subscriptionService.subscribeMessage(original, user); Message reply = messagesService.getReply(mid, newrid); applicationEventPublisher.publishEvent(new MessageEvent(this, reply, subscriptionService.getUsersSubscribedToComments(original, reply))); |