aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/server
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-11-20 00:07:06 +0300
committerGravatar Vitaly Takmazov2018-11-20 00:07:06 +0300
commit930e6ab6d604963ae6a48c58fc1bd3735071d9ee (patch)
treebb236bf36ccab695bef4d45012135c2396c84f77 /src/main/java/com/juick/server
parent74740d5d989f11746266a994840f89e9f6784f73 (diff)
getMessage refactoring
Diffstat (limited to 'src/main/java/com/juick/server')
-rw-r--r--src/main/java/com/juick/server/CommandsManager.java48
-rw-r--r--src/main/java/com/juick/server/EmailManager.java4
-rw-r--r--src/main/java/com/juick/server/ServerManager.java5
-rw-r--r--src/main/java/com/juick/server/TelegramBotManager.java1
-rw-r--r--src/main/java/com/juick/server/TopManager.java2
-rw-r--r--src/main/java/com/juick/server/XMPPConnection.java9
-rw-r--r--src/main/java/com/juick/server/api/Messages.java12
-rw-r--r--src/main/java/com/juick/server/api/Notifications.java25
-rw-r--r--src/main/java/com/juick/server/api/Post.java24
-rw-r--r--src/main/java/com/juick/server/api/activity/Profile.java2
-rw-r--r--src/main/java/com/juick/server/www/controllers/MessagesWWW.java6
11 files changed, 70 insertions, 68 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)));
diff --git a/src/main/java/com/juick/server/EmailManager.java b/src/main/java/com/juick/server/EmailManager.java
index 1cdafac6..af4ed416 100644
--- a/src/main/java/com/juick/server/EmailManager.java
+++ b/src/main/java/com/juick/server/EmailManager.java
@@ -55,7 +55,7 @@ public class EmailManager implements NotificationListener {
emailNotify(email, subject, msg);
});
} else if (MessageUtils.isReply(msg)) {
- Message originalMessage = messagesService.getMessage(msg.getMid());
+ Message originalMessage = messagesService.getMessage(msg.getMid()).orElseThrow(IllegalStateException::new);
String subject = String.format("New reply to %s", originalMessage.getUser().getName());
subscribedUsers.stream()
.flatMap(user -> emailService.getEmails(user.getUid(), true).stream())
@@ -103,7 +103,7 @@ public class EmailManager implements NotificationListener {
Message replyto = messagesService.getReply(msg.getMid(), msg.getReplyto());
headers.put("In-Reply-To", String.format("<%d.%d@juick.com>", replyto.getMid(), replyto.getRid()));
} else {
- Message original = messagesService.getMessage(msg.getMid());
+ Message original = messagesService.getMessage(msg.getMid()).orElseThrow(IllegalStateException::new);
headers.put("In-Reply-To", String.format("<%d.%d@juick.com>", original.getMid(), original.getRid()));
}
}
diff --git a/src/main/java/com/juick/server/ServerManager.java b/src/main/java/com/juick/server/ServerManager.java
index ef848526..586d5962 100644
--- a/src/main/java/com/juick/server/ServerManager.java
+++ b/src/main/java/com/juick/server/ServerManager.java
@@ -140,7 +140,6 @@ public class ServerManager implements NotificationListener {
try {
String json = jsonMapper.writeValueAsString(jmsg);
- com.juick.Message op = messagesService.getMessage(jmsg.getMid());
List<Integer> threadUsers =
subscribedUsers
.stream().map(User::getUid).collect(Collectors.toList());
@@ -187,10 +186,10 @@ public class ServerManager implements NotificationListener {
onJuickPM(jmsg.getTo(), jmsg);
} else if (!MessageUtils.isReply(jmsg)) {
// to get full message with attachment, etc.
- onJuickMessagePost(messagesService.getMessage(jmsg.getMid()), subscribedUsers);
+ onJuickMessagePost(messagesService.getMessage(jmsg.getMid()).orElseThrow(IllegalStateException::new), subscribedUsers);
} else {
// to get quote and attachment
- Message op = messagesService.getMessage(jmsg.getMid());
+ Message op = messagesService.getMessage(jmsg.getMid()).orElseThrow(IllegalStateException::new);
com.juick.Message reply = messagesService.getReply(jmsg.getMid(), jmsg.getRid());
subscriptionService.getUsersSubscribedToComments(op, reply, true).stream()
.filter(u -> userService.isReplyToBL(u, reply))
diff --git a/src/main/java/com/juick/server/TelegramBotManager.java b/src/main/java/com/juick/server/TelegramBotManager.java
index 8e8d0104..82114fb5 100644
--- a/src/main/java/com/juick/server/TelegramBotManager.java
+++ b/src/main/java/com/juick/server/TelegramBotManager.java
@@ -349,7 +349,6 @@ public class TelegramBotManager implements NotificationListener {
telegramService.getTelegramIdentifiers(Collections.singletonList(jmsg.getTo()))
.forEach(c -> telegramNotify(c, formatPost(jmsg, true), jmsg));
} else if (MessageUtils.isReply(jmsg)) {
- com.juick.Message op = messagesService.getMessage(jmsg.getMid());
String fmsg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg, true));
telegramService.getTelegramIdentifiers(
subscribedUsers
diff --git a/src/main/java/com/juick/server/TopManager.java b/src/main/java/com/juick/server/TopManager.java
index e5c00242..fe3d605d 100644
--- a/src/main/java/com/juick/server/TopManager.java
+++ b/src/main/java/com/juick/server/TopManager.java
@@ -42,7 +42,7 @@ public class TopManager {
@Scheduled(fixedRate = 3600000)
public void updateTop() {
messagesService.getPopularCandidates().forEach(m -> {
- Message jmsg = messagesService.getMessage(m);
+ Message jmsg = messagesService.getMessage(m).orElseThrow(IllegalStateException::new);
logger.info("added {} to popular", m);
messagesService.setMessagePopular(m, 1);
List<String> tags = jmsg.getTags().stream().map(Tag::getName).map(String::toLowerCase).collect(Collectors.toList());
diff --git a/src/main/java/com/juick/server/XMPPConnection.java b/src/main/java/com/juick/server/XMPPConnection.java
index 5ea105af..0adc7796 100644
--- a/src/main/java/com/juick/server/XMPPConnection.java
+++ b/src/main/java/com/juick/server/XMPPConnection.java
@@ -288,7 +288,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
for (User user : users) {
jids.addAll(userService.getJIDsbyUID(user.getUid()));
}
- com.juick.Message fullMsg = messagesService.getMessage(jmsg.getMid());
+ com.juick.Message fullMsg = messagesService.getMessage(jmsg.getMid()).orElseThrow(IllegalStateException::new);
String txt = "@" + jmsg.getUser().getName() + ":" + MessageUtils.getTagsString(fullMsg) + "\n";
String attachmentUrl = MessageUtils.attachmentUrl(fullMsg);
if (StringUtils.isNotEmpty(attachmentUrl)) {
@@ -325,7 +325,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
String replyTo;
com.juick.Message replyMessage = jmsg.getReplyto() > 0 ? messagesService.getReply(jmsg.getMid(), jmsg.getReplyto())
- : messagesService.getMessage(jmsg.getMid());
+ : messagesService.getMessage(jmsg.getMid()).orElseThrow(IllegalStateException::new);
replyTo = replyMessage.getUser().getName();
com.juick.Message fullReply = messagesService.getReply(jmsg.getMid(), jmsg.getRid());
replyQuote = StringUtils.defaultString(fullReply.getReplyQuote());
@@ -594,13 +594,14 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
if (jmsg != null) {
if (MessageUtils.isReply(jmsg)) {
// to get quote and attachment
- com.juick.Message original = messagesService.getMessage(jmsg.getMid());
+ com.juick.Message original = messagesService.getMessage(jmsg.getMid()).orElseThrow(IllegalStateException::new);
com.juick.Message reply = messagesService.getReply(jmsg.getMid(), jmsg.getRid());
applicationEventPublisher.publishEvent(new MessageEvent(this, reply,
subscriptionService.getUsersSubscribedToComments(original, reply)));
} else if (!MessageUtils.isPM(jmsg)) {
applicationEventPublisher.publishEvent(new MessageEvent(this,
- messagesService.getMessage(jmsg.getMid()), subscriptionService.getSubscribedUsers(jmsg.getUser().getUid(), jmsg)));
+ messagesService.getMessage(jmsg.getMid()).orElseThrow(IllegalStateException::new),
+ subscriptionService.getSubscribedUsers(jmsg.getUser().getUid(), jmsg)));
}
} else {
URI attachment = URI.create(StringUtils.EMPTY);
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})
diff --git a/src/main/java/com/juick/server/www/controllers/MessagesWWW.java b/src/main/java/com/juick/server/www/controllers/MessagesWWW.java
index 0708e27f..77ee6e15 100644
--- a/src/main/java/com/juick/server/www/controllers/MessagesWWW.java
+++ b/src/main/java/com/juick/server/www/controllers/MessagesWWW.java
@@ -496,12 +496,14 @@ public class MessagesWWW {
throw new HttpForbiddenException();
}
- com.juick.Message msg = messagesService.getMessage(mid);
+ Optional<com.juick.Message> message = messagesService.getMessage(mid);
- if (msg == null || msg.getUser().isBanned()) {
+ if (!message.isPresent() || message.get().getUser().isBanned()) {
throw new HttpNotFoundException();
}
+ Message msg = message.get();
+
com.juick.User user = userService.getUserByName(uname);
if (user.isAnonymous() || !msg.getUser().equals(user)) {
return String.format("redirect:/%s/%d", msg.getUser().getName(), mid);