aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-11-28 14:58:45 +0300
committerGravatar Vitaly Takmazov2018-11-28 14:58:45 +0300
commit7b97f3df5a5c31c5d85ee71c902fa9bf979ffc54 (patch)
treed52c9ee2bf5066b84f348a1f4aeb27cbcf5e0678 /src
parent0fea66317d608cb0f67bb92695a40d100d504c8d (diff)
Fix EditOrReply command flow
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/juick/server/CommandsManager.java20
-rw-r--r--src/test/java/com/juick/server/tests/ServerTests.java15
2 files changed, 25 insertions, 10 deletions
diff --git a/src/main/java/com/juick/server/CommandsManager.java b/src/main/java/com/juick/server/CommandsManager.java
index 2eb58188..3257304b 100644
--- a/src/main/java/com/juick/server/CommandsManager.java
+++ b/src/main/java/com/juick/server/CommandsManager.java
@@ -498,13 +498,19 @@ public class CommandsManager {
int mid = NumberUtils.toInt(args[1]);
int rid = NumberUtils.toInt(args[4], 0);
String txt = StringUtils.defaultString(args[5]);
- 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()) {
- return CommandResult.fromString("It is not your message");
+ Optional<Message> msg = messagesService.getMessage(mid);
+ if (!msg.isPresent()) {
+ return CommandResult.fromString("Message not found");
+ }
+ if (rid > 0) {
+ Message reply = messagesService.getReply(mid, rid);
+ if (reply == null) {
+ return CommandResult.fromString("Reply not found");
}
- if (!CollectionUtils.isEqualCollection(tagService.updateTags(mid, messageTags.getRight()), msg.getTags())) {
+ }
+ Pair<String, List<Tag>> messageTags = tagService.fromString(txt);
+ if (user.getUid() == msg.get().getUser().getUid() && rid == 0 && messageTags.getRight().size() > 0) {
+ if (!CollectionUtils.isEqualCollection(tagService.updateTags(mid, messageTags.getRight()), msg.get().getTags())) {
return CommandResult.fromString("Tags are updated");
} else {
return CommandResult.fromString("Tags are NOT updated (5 tags maximum?)");
@@ -527,7 +533,7 @@ public class CommandsManager {
imagesService.saveImageWithPreviews(attachmentFName, fname);
}
applicationEventPublisher.publishEvent(
- new MessageReadEvent(this, user, msg));
+ new MessageReadEvent(this, user, msg.get()));
Message original = messagesService.getMessage(mid).orElseThrow(IllegalStateException::new);
subscriptionService.subscribeMessage(original, user);
Message reply = messagesService.getReply(mid, newrid);
diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java
index 4b0de65d..7c038058 100644
--- a/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/src/test/java/com/juick/server/tests/ServerTests.java
@@ -798,9 +798,8 @@ public class ServerTests {
Timestamp lastreply = jdbcTemplate.queryForObject("SELECT lastmessage FROM users WHERE id=?", Timestamp.class, user.getUid());
assertThat(lastreply.toInstant(), equalTo(reply.getTimestamp()));
assertEquals("should be reply to second comment", 2, reply.getReplyto());
- assertEquals("tags should NOT be updated", "It is not your message",
- commandsManager.processCommand(readerUser, "#" + mid + " *yo *there", emptyUri)
- .getText());
+ assertThat(commandsManager.processCommand(readerUser, "#" + mid + " *yo *there", emptyUri)
+ .getText(), startsWith("Reply posted"));
assertEquals("tags should be updated", "Tags are updated",
commandsManager.processCommand(user, "#" + mid + " *there", emptyUri).getText());
assertEquals("number of tags should match", 2,
@@ -1809,4 +1808,14 @@ public class ServerTests {
com.juick.User testuser = jsonMapper.readValue(result.getResponse().getContentAsString(), User.class);
assertThat(testuser.getName(), is("testuser"));
}
+ @Test
+ public void doNotAllowRepliesToNonExistingReplyAndNotAllowToEditTagsOfComment() throws Exception {
+ assertThat(commandsManager.processCommand(ugnich, "#23213213/2 BAD COMMENT", emptyUri).getText(), is("Message not found"));
+ Message msg = commandsManager.processCommand(ugnich, "YO", emptyUri).getNewMessage().get();
+ assertThat(commandsManager.processCommand(ugnich, String.format("#%d/1 BAD COMMENT", msg.getMid()), emptyUri).getText(), is("Reply not found"));
+ CommandResult result = commandsManager.processCommand(freefd,
+ String.format("#%d *GOOD *COMMENT", msg.getMid()), emptyUri);
+ Message reply = result.getNewMessage().get();
+ assertThat(commandsManager.processCommand(ugnich, String.format("#%d/%d *GOOD *BAD", reply.getMid(), reply.getRid()), emptyUri).getText(), startsWith("Reply posted"));
+ }
}