aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-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
-rw-r--r--src/main/java/com/juick/service/MessagesService.java7
-rw-r--r--src/main/java/com/juick/service/MessagesServiceImpl.java6
-rw-r--r--src/main/java/com/juick/service/component/MessageReadEvent.java4
-rw-r--r--src/test/java/com/juick/server/tests/ServerTests.java79
15 files changed, 117 insertions, 117 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);
diff --git a/src/main/java/com/juick/service/MessagesService.java b/src/main/java/com/juick/service/MessagesService.java
index 362501b5..fb2b3d95 100644
--- a/src/main/java/com/juick/service/MessagesService.java
+++ b/src/main/java/com/juick/service/MessagesService.java
@@ -23,10 +23,7 @@ import com.juick.User;
import com.juick.model.ResponseReply;
import java.net.URI;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
/**
* Created by aalexeev on 11/13/16.
@@ -63,7 +60,7 @@ public interface MessagesService {
int getMessagePrivacy(int mid);
- com.juick.Message getMessage(int mid);
+ Optional<Message> getMessage(int mid);
com.juick.Message getReply(int mid, int rid);
diff --git a/src/main/java/com/juick/service/MessagesServiceImpl.java b/src/main/java/com/juick/service/MessagesServiceImpl.java
index efe480c0..145d42c3 100644
--- a/src/main/java/com/juick/service/MessagesServiceImpl.java
+++ b/src/main/java/com/juick/service/MessagesServiceImpl.java
@@ -343,7 +343,7 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
@Transactional(readOnly = true)
@Override
- public com.juick.Message getMessage(final int mid) {
+ public Optional<com.juick.Message> getMessage(final int mid) {
List<com.juick.Message> list = getJdbcTemplate().query(
"SELECT messages.message_id as mid, 0 as rid, 0 as replyto, "
@@ -369,9 +369,9 @@ public class MessagesServiceImpl extends BaseJdbcService implements MessagesServ
Map<Integer, Set<Reaction>> reactionStats = updateReactionsFor(Collections.singletonList(mid));
message.setReactions(reactionStats.get(message.getMid()));
message.setEntities(MessageUtils.getEntities(message));
- return message;
+ return Optional.of(message);
}
- return null;
+ return Optional.empty();
}
@Transactional(readOnly = true)
diff --git a/src/main/java/com/juick/service/component/MessageReadEvent.java b/src/main/java/com/juick/service/component/MessageReadEvent.java
index b070c8cb..b2af4a08 100644
--- a/src/main/java/com/juick/service/component/MessageReadEvent.java
+++ b/src/main/java/com/juick/service/component/MessageReadEvent.java
@@ -4,7 +4,7 @@ import com.juick.Message;
import com.juick.User;
import org.springframework.context.ApplicationEvent;
-import java.util.List;
+import javax.annotation.Nonnull;
public class MessageReadEvent extends ApplicationEvent {
private User user;
@@ -14,7 +14,7 @@ public class MessageReadEvent extends ApplicationEvent {
*
* @param source the object on which the event initially occurred (never {@code null})
*/
- public MessageReadEvent(Object source, User user, Message message) {
+ public MessageReadEvent(Object source, @Nonnull User user, Message message) {
super(source);
this.user = user;
this.message = message;
diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java
index 69251e10..4345d607 100644
--- a/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/src/test/java/com/juick/server/tests/ServerTests.java
@@ -257,14 +257,14 @@ public class ServerTests {
assertThat(messagesService.getMyFeed(freefd.getUid(), 0, true).get(0), equalTo(mid3));
assertThat(messagesService.getMyFeed(freefd.getUid(), mid2, true).get(0), equalTo(mid0));
assertThat(messagesService.recommendMessage(mid0, ugnich.getUid()), equalTo(MessagesService.RecommendStatus.Added));
- assertThat(messagesService.getMessage(mid0).getLikes(), equalTo(1));
+ assertThat(messagesService.getMessage(mid0).orElseThrow(IllegalStateException::new).getLikes(), equalTo(1));
assertThat(messagesService.recommendMessage(mid0, ugnich.getUid()), equalTo(MessagesService.RecommendStatus.Deleted));
- assertThat(messagesService.getMessage(mid0).getLikes(), equalTo(0));
+ assertThat(messagesService.getMessage(mid0).get().getLikes(), equalTo(0));
assertThat(messagesService.getAll(ugnich.getUid(), 0).get(0), equalTo(mid3));
Tag yoTag = tagService.getTag("yoyo", true);
assertThat(tagService.getTag("YOYO", false), equalTo(yoTag));
int mid = messagesService.createMessage(ugnich.getUid(), "yo", null, Collections.singletonList(yoTag));
- Message msg = messagesService.getMessage(mid);
+ Message msg = messagesService.getMessage(mid).get();
List<User> subscribers = subscriptionService.getSubscribedUsers(ugnich.getUid(), msg);
telegramService.createTelegramUser(12345, "freefd");
@@ -304,7 +304,7 @@ public class ServerTests {
User user = userService.getUserByUID(user_id).orElse(AnonymousUser.INSTANCE);
assertEquals("it should be me", "mmmme", user.getName());
int mid = messagesService.createMessage(user_id, "yo", null, new ArrayList<>());
- Message msg = messagesService.getMessage(mid);
+ Message msg = messagesService.getMessage(mid).get();
assertEquals("yo", msg.getText());
User me = msg.getUser();
assertEquals("mmmme", me.getName());
@@ -314,7 +314,7 @@ public class ServerTests {
List<Tag> tagList = new ArrayList<>();
tagList.add(tag);
int mid2 = messagesService.createMessage(user_id, "yo2", null, tagList);
- Message msg2 = messagesService.getMessage(mid2);
+ Message msg2 = messagesService.getMessage(mid2).get();
assertEquals(1, msg2.getTags().size());
assertEquals("we already have ugnich", -1, userService.createUser("ugnich", "x"));
int ugnich_id = userService.createUser("hugnich", "x");
@@ -334,7 +334,7 @@ public class ServerTests {
assertThat(replies2.size(), equalTo(2));
assertThat(replies2.get(1), equalTo(reply2));
- Message msg3 = messagesService.getMessage(mid2);
+ Message msg3 = messagesService.getMessage(mid2).get();
assertEquals(2, msg3.getReplies());
assertEquals("weather", msg3.getTags().get(0).getName());
@@ -357,7 +357,7 @@ public class ServerTests {
String dbTagName = jdbcTemplate.queryForObject("select name from tags where name=?", String.class, htmlTagName);
assertEquals("db tags should not be escaped", dbTagName, htmlTag.getName());
int mid4 = messagesService.createMessage(user_id, "yoyoyo", null, null);
- Message msg4 = messagesService.getMessage(mid4);
+ Message msg4 = messagesService.getMessage(mid4).get();
assertEquals("tags string should be empty", StringUtils.EMPTY, MessageUtils.getTagsString(msg4));
messagesService.deleteMessage(user_id, mid4);
}
@@ -373,7 +373,7 @@ public class ServerTests {
messagesService.likeMessage(mid, freefdId,3);
messagesService.likeMessage(mid, freefdId,1);
- Message msg4 = messagesService.getMessage(mid);
+ Message msg4 = messagesService.getMessage(mid).get();
assertThat(msg4.getLikes(), equalTo(1));
Assert.assertEquals(2, msg4.getReactions().stream().filter(r -> r.getId() == 2)
@@ -436,7 +436,7 @@ public class ServerTests {
String msgText = "Привет, я - Угнич";
CommandResult result = commandsManager.processCommand(ugnich, msgText, URI.create("http://static.juick.com/settings/facebook.png"));
int mid = result.getNewMessage().get().getMid();
- Message msg = messagesService.getMessage(mid);
+ Message msg = messagesService.getMessage(mid).get();
tagService.createTag("тест");
mockMvc.perform(
get("/api/home")
@@ -738,7 +738,7 @@ public class ServerTests {
Message msgreply = commandsManager.processCommand(user, "#" + msg.getMid() + " yyy", HttpUtils.downloadImage(URI.create("http://static.juick.com/settings/xmpp.png").toURL(), tmpDir)).getNewMessage().get();
assertThat(msgreply.getAttachmentType(), equalTo("png"));
assertEquals("text should match", "yoyo",
- messagesService.getMessage(msg.getMid()).getText());
+ messagesService.getMessage(msg.getMid()).get().getText());
assertEquals("tag should match", "yo",
tagService.getMessageTags(msg.getMid()).get(0).getTag().getName());
CommandResult yoyoMsg = commandsManager.processCommand(user, "*yo", URI.create("http://static.juick.com/settings/facebook.png"));
@@ -761,16 +761,16 @@ public class ServerTests {
int rid = messagesService.createReply(mid, 0, user, "comment", null);
assertEquals("number of subscribed users should match", 1,
subscriptionService.getUsersSubscribedToComments(
- messagesService.getMessage(mid),
+ messagesService.getMessage(mid).get(),
messagesService.getReply(mid, rid)).size());
privacyQueriesService.blacklistUser(user, readerUser);
assertEquals("number of subscribed users should match", 0,
subscriptionService.getUsersSubscribedToComments(
- messagesService.getMessage(mid),
+ messagesService.getMessage(mid).get(),
messagesService.getReply(mid, rid)).size());
assertEquals("number of subscribed users should match", 1,
subscriptionService.getUsersSubscribedToComments(
- messagesService.getMessage(mid),
+ messagesService.getMessage(mid).get(),
messagesService.getReply(mid, rid), true).size());
assertEquals("should be subscribed", "Subscribed to @" + user.getName(),
commandsManager.processCommand(readerUser, "S @" + user.getName(), emptyUri)
@@ -801,7 +801,7 @@ public class ServerTests {
commandsManager.processCommand(user, "#" + mid + " *there", emptyUri).getText());
assertEquals("number of tags should match", 2,
tagService.getMessageTags(mid).size());
- assertThat(messagesService.getMessage(mid).getTags().size(), is(2));
+ assertThat(messagesService.getMessage(mid).get().getTags().size(), is(2));
assertEquals("should be blacklisted", "Tag added to your blacklist",
commandsManager.processCommand(readerUser, "BL *there", emptyUri).getText());
assertEquals("number of subscribed users should match", 0,
@@ -834,7 +834,7 @@ public class ServerTests {
assertEquals("should be unsubscribed", "Unsubscribed from #" + mid,
commandsManager.processCommand(readerUser, "u #" + mid, emptyUri).getText());
assertEquals("number of subscribed users should match", 0,
- subscriptionService.getUsersSubscribedToComments(messagesService.getMessage(mid),
+ subscriptionService.getUsersSubscribedToComments(messagesService.getMessage(mid).get(),
messagesService.getReply(mid, rid)).size());
assertNotEquals("should NOT be deleted", String.format("Message %s deleted", mid),
commandsManager.processCommand(readerUser, "D #" + mid, emptyUri).getText());
@@ -938,7 +938,7 @@ public class ServerTests {
mockMvc.perform(post("/api/react?mid=" + mid1 + "&hash=" + freefdHash+ "&reactionId=2"))
.andExpect(status().isOk());
- Message msg4 = messagesService.getMessage(mid1);
+ Message msg4 = messagesService.getMessage(mid1).get();
assertThat(msg4.getLikes(), is(0));
assertThat(messagesService.getMessages(AnonymousUser.INSTANCE, Collections.singletonList(mid1)).get(0).getLikes(), is(0));
Assert.assertEquals(1, msg4.getReactions().stream().filter(r -> r.getId() == 2)
@@ -947,15 +947,16 @@ public class ServerTests {
.andExpect(status().isOk());
mockMvc.perform(post("/api/react?mid=" + mid1 + "&hash=" + freefdHash+ "&reactionId=1"))
.andExpect(status().isOk());
- assertThat(messagesService.getMessage(mid1).getLikes(), is(1));
+ assertThat(messagesService.getMessage(mid1).get().getLikes(), is(1));
}
@Test
public void lastReadTests() throws Exception {
jdbcTemplate.execute("DELETE FROM bl_users");
+ jdbcTemplate.execute("DELETE FROM messages");
assertThat(userService.isInBLAny(ugnich.getUid(), freefd.getUid()), is(false));
int mid = messagesService.createMessage(ugnich.getUid(), "to be watched", null, null);
- subscriptionService.subscribeMessage(messagesService.getMessage(mid), ugnich);
+ subscriptionService.subscribeMessage(messagesService.getMessage(mid).get(), ugnich);
messagesService.createReply(mid, 0, freefd, "new reply", null);
BiFunction<User, Integer, Integer> lastRead = (user, m) -> jdbcTemplate.queryForObject(
"SELECT last_read_rid FROM subscr_messages WHERE suser_id=? AND message_id=?",
@@ -1017,7 +1018,7 @@ public class ServerTests {
assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getMyFeed(freefd.getUid(), 0, true))
.stream().noneMatch(m -> m.getTags().contains(banned)));
tagService.updateTags(newMid, Collections.singletonList(banned));
- assertThat(messagesService.getMessage(newMid).getTags().size(), is(0));
+ assertThat(messagesService.getMessage(newMid).get().getTags().size(), is(0));
privacyQueriesService.blacklistUser(freefd, userService.getUserByUID(newUid).orElse(AnonymousUser.INSTANCE));
assertTrue(messagesService.getMessages(AnonymousUser.INSTANCE, messagesService.getMyFeed(freefd.getUid(), 0, true))
.stream().noneMatch(m -> m.getMid() == newMid));
@@ -1127,17 +1128,17 @@ public class ServerTests {
commandsManager.processCommand(freefd, String.format("#%d ugnich - dick too", mid), emptyUri);
commandsManager.processCommand(juick, String.format("#%d/1 ban for a hour!", mid), emptyUri);
commandsManager.processCommand(juick, String.format("#%d freefd is here but it is hidden from you", mid), emptyUri);
- assertThat(messagesService.getMessage(mid).getReplies(), is(3));
+ assertThat(messagesService.getMessage(mid).get().getReplies(), is(3));
Message reply = messagesService.getReply(mid, 3);
assertThat(userService.isReplyToBL(ugnich, reply), is(false));
List<Message> replies = messagesService.getReplies(ugnich, mid);
assertThat(replies.size(), is(1));
commandsManager.processCommand(freefd, String.format("#%d/3 hahaha!", mid), emptyUri);
- assertThat(messagesService.getMessage(mid).getReplies(), is(4));
+ assertThat(messagesService.getMessage(mid).get().getReplies(), is(4));
replies = messagesService.getReplies(ugnich, mid);
assertThat(replies.size(), is(1));
commandsManager.processCommand(juick, String.format("#%d/4 mmm?!", mid), emptyUri);
- assertThat(messagesService.getMessage(mid).getReplies(), is(5));
+ assertThat(messagesService.getMessage(mid).get().getReplies(), is(5));
replies = messagesService.getReplies(ugnich, mid);
reply = messagesService.getReply(mid, 5);
assertThat(userService.isReplyToBL(ugnich, reply), is(true));
@@ -1269,23 +1270,23 @@ public class ServerTests {
messagesService.recommendMessage(posterMid, recommenderId);
BiFunction<Integer, Message, List<User>> subscribers = (recommId, msg) ->
subscriptionService.getUsersSubscribedToUserRecommendations(recommId, msg);
- List<User> recommendSubscribers = subscribers.apply(recommenderId, messagesService.getMessage(posterMid));
+ List<User> recommendSubscribers = subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get());
assertThat(recommendSubscribers.size(), is(1));
assertThat(recommendSubscribers.get(0).getUid(), is(readerId));
privacyQueriesService.blacklistUser(reader, poster);
- assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid)).size(), is(0));
+ assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get()).size(), is(0));
privacyQueriesService.blacklistUser(reader, poster);
- assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid)).size(), is(1));
+ assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get()).size(), is(1));
tagService.blacklistTag(reader, sampleTag);
- assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid)).size(), is(0));
+ assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get()).size(), is(0));
tagService.blacklistTag(reader, sampleTag);
- assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid)).size(), is(1));
+ assertThat(subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get()).size(), is(1));
messagesService.recommendMessage(posterMid, lateRecommenderId);
- List<User> lateRecommendSubscribers = subscribers.apply(recommenderId, messagesService.getMessage(posterMid));
+ List<User> lateRecommendSubscribers = subscribers.apply(recommenderId, messagesService.getMessage(posterMid).get());
assertThat(lateRecommendSubscribers.size(), is(0));
int readerMid = messagesService.createMessage(readerId, "PEOPLE", null, null);
messagesService.recommendMessage(readerMid, recommenderId);
- assertThat(subscribers.apply(recommenderId, messagesService.getMessage(readerMid)).size(), is(0));
+ assertThat(subscribers.apply(recommenderId, messagesService.getMessage(readerMid).get()).size(), is(0));
}
@Test
public void mentionsInComments() {
@@ -1295,7 +1296,7 @@ public class ServerTests {
int mentionerId = userService.createUser("mmm", "secret");
User mentioner = userService.getUserByUID(mentionerId).get();
int mid = messagesService.createMessage(posterId, "who is dick?", null, null);
- Message msg = messagesService.getMessage(mid);
+ Message msg = messagesService.getMessage(mid).get();
int rid = messagesService.createReply(mid, 0, commenter,
"@mmm is dick", null);
Message reply = messagesService.getReply(mid, rid);
@@ -1395,10 +1396,10 @@ public class ServerTests {
assertThat(messagesService.recommendMessage(mid, ermineId), is(MessagesService.RecommendStatus.Added));
assertThat(messagesService.recommendMessage(mid, fmapId), is(MessagesService.RecommendStatus.Added));
assertThat(messagesService.recommendMessage(mid, pogoId), is(MessagesService.RecommendStatus.Added));
- assertThat(messagesService.getMessage(mid).getLikes(), is(3));
+ assertThat(messagesService.getMessage(mid).get().getLikes(), is(3));
assertThat(CollectionUtils.isEqualCollection(messagesService.getMessageRecommendations(mid), Arrays.asList("fmap", "ermine", "pogo")), is(true));
privacyQueriesService.blacklistUser(userService.getUserByName("monstreek"), userService.getUserByName("pogo"));
- assertThat(messagesService.getMessage(mid).getLikes(), is(3));
+ assertThat(messagesService.getMessage(mid).get().getLikes(), is(3));
assertThat(CollectionUtils.isEqualCollection(messagesService.getMessageRecommendations(mid), Arrays.asList("fmap", "ermine")), is(true));
}
@Test
@@ -1526,7 +1527,7 @@ public class ServerTests {
assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid), "twitter:card")
.getAttribute("content"), equalTo("summary"));
int mid2 = messagesService.createMessage(ugnich.getUid(), "with image", "png", null);
- Message message = messagesService.getMessage(mid2);
+ Message message = messagesService.getMessage(mid2).get();
assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "twitter:card")
.getAttribute("content"), equalTo("summary_large_image"));
assertThat(fetchMeta(String.format("http://localhost:8080/ugnich/%d", mid2), "og:description")
@@ -1576,10 +1577,10 @@ public class ServerTests {
String discussionsUrl = "http://localhost:8080/";
HtmlPage discussions = webClient.getPage(discussionsUrl);
assertThat(discussions.querySelectorAll("article").size(), is(0));
- subscriptionService.subscribeMessage(messagesService.getMessage(mid), freefd);
+ subscriptionService.subscribeMessage(messagesService.getMessage(mid).get(), freefd);
discussions = (HtmlPage) discussions.refresh();
assertThat(discussions.querySelectorAll("article").size(), is(1));
- subscriptionService.subscribeMessage(messagesService.getMessage(midNew), freefd);
+ subscriptionService.subscribeMessage(messagesService.getMessage(midNew).get(), freefd);
discussions = (HtmlPage) discussions.refresh();
assertThat(discussions.querySelectorAll("article").size(), is(2));
assertThat(discussions.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew)));
@@ -1587,13 +1588,13 @@ public class ServerTests {
discussions = (HtmlPage) discussions.refresh();
assertThat(discussions.querySelectorAll("article").size(), is(2));
assertThat(discussions.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(mid)));
- Message msg = messagesService.getMessage(mid);
+ Message msg = messagesService.getMessage(mid).get();
HtmlPage discussionsOld = webClient.getPage(discussionsUrl + "?to=" + msg.getUpdated().toEpochMilli());
assertThat(discussionsOld.querySelectorAll("article").size(), is(1));
assertThat(discussionsOld.querySelectorAll("article").get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew)));
List<Integer> newMids = IntStream.rangeClosed(1, 19).map(i -> messagesService.createMessage(ugnich.getUid(), String.valueOf(i), null, null)).boxed().collect(Collectors.toList());
for (Integer m : newMids) {
- subscriptionService.subscribeMessage(messagesService.getMessage(m), freefd);
+ subscriptionService.subscribeMessage(messagesService.getMessage(m).get(), freefd);
}
discussions = (HtmlPage) discussions.refresh();
assertThat(discussions.querySelectorAll("article").size(), is(20));
@@ -1603,7 +1604,7 @@ public class ServerTests {
discussions = (HtmlPage) discussions.refresh();
assertThat(discussions.querySelectorAll("article")
.get(0).getAttributes().getNamedItem("data-mid").getNodeValue(), is(String.valueOf(midNew)));
- Message old = messagesService.getMessage(newMids.get(0));
+ Message old = messagesService.getMessage(newMids.get(0)).get();
discussionsOld = webClient.getPage(discussionsUrl + "?to=" + old.getUpdated().toEpochMilli());
assertThat(discussionsOld.querySelectorAll("article").size(), is(1));
assertThat(discussionsOld.querySelectorAll("article")
@@ -1653,7 +1654,7 @@ public class ServerTests {
loginCookie.getName(),
loginCookie.getValue()));
int mid = messagesService.createMessage(ugnich.getUid(), "new test", null, null);
- subscriptionService.subscribeMessage(messagesService.getMessage(mid), freefd);
+ subscriptionService.subscribeMessage(messagesService.getMessage(mid).get(), freefd);
int rid = messagesService.createReply(mid, 0, ugnich, "new reply", null);
HtmlPage discussionsPage = webClient.getPage("http://localhost:8080/?show=discuss");
assertThat(discussionsPage.querySelectorAll("#global a .badge").size(), is(1));