diff options
4 files changed, 25 insertions, 15 deletions
diff --git a/juick-common/src/main/java/com/juick/server/CommandsManager.java b/juick-common/src/main/java/com/juick/server/CommandsManager.java index 13d9b436..7c4692d2 100644 --- a/juick-common/src/main/java/com/juick/server/CommandsManager.java +++ b/juick-common/src/main/java/com/juick/server/CommandsManager.java @@ -33,6 +33,7 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.reflect.MethodUtils; +import org.apache.commons.lang3.tuple.Pair; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; @@ -100,12 +101,12 @@ public class CommandsManager { return commandResult; } } - List<Tag> tags = tagService.fromString(input); - if (tags.size() > 5) { + Pair<String, List<Tag>> tags = tagService.fromString(input); + if (tags.getRight().size() > 5) { return CommandResult.fromString("Sorry, 5 tags maximum."); } // new message - String body = TagUtils.toString(tags).equals(input) ? StringUtils.EMPTY : input.substring(TagUtils.toString(tags).length()).trim(); + String body = tags.getLeft().trim(); boolean haveAttachment = StringUtils.isNotEmpty(attachment.toString()); String attachmentFName = null; String attachmentType = null; @@ -114,7 +115,7 @@ public class CommandsManager { : HttpUtils.downloadImage(attachment.toURL(), tmpDir).getHost(); attachmentType = attachmentFName.substring(attachmentFName.length() - 3); } - int mid = messagesService.createMessage(user.getUid(), body, attachmentType, tags); + int mid = messagesService.createMessage(user.getUid(), body, attachmentType, tags.getRight()); if (haveAttachment) { String fname = String.format("%d.%s", mid, attachmentType); imagesService.saveImageWithPreviews(attachmentFName, fname); @@ -488,12 +489,12 @@ public class CommandsManager { int rid = NumberUtils.toInt(args[4], 0); String txt = StringUtils.defaultString(args[5]); Message msg = messagesService.getMessage(mid); - List<Tag> messageTags = tagService.fromString(txt); - if (messageTags.size() > 0) { + 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"); } - if (!CollectionUtils.isEqualCollection(tagService.updateTags(mid, messageTags), msg.getTags())) { + if (!CollectionUtils.isEqualCollection(tagService.updateTags(mid, messageTags.getRight()), msg.getTags())) { return CommandResult.fromString("Tags are updated"); } else { return CommandResult.fromString("Tags are NOT updated (5 tags maximum?)"); diff --git a/juick-common/src/main/java/com/juick/service/TagService.java b/juick-common/src/main/java/com/juick/service/TagService.java index 17b6c5de..7f89b478 100644 --- a/juick-common/src/main/java/com/juick/service/TagService.java +++ b/juick-common/src/main/java/com/juick/service/TagService.java @@ -20,6 +20,7 @@ package com.juick.service; import com.juick.Tag; import com.juick.User; import com.juick.server.helpers.TagStats; +import org.apache.commons.lang3.tuple.Pair; import java.util.Collection; import java.util.List; @@ -49,7 +50,7 @@ public interface TagService { List<Tag> updateTags(int mid, Collection<Tag> newTags); - List<Tag> fromString(String txt); + Pair<String, List<Tag>> fromString(String txt); List<TagStats> getMessageTags(int mid); diff --git a/juick-server/src/main/java/com/juick/service/TagServiceImpl.java b/juick-server/src/main/java/com/juick/service/TagServiceImpl.java index 3ee21aec..aa6c3713 100644 --- a/juick-server/src/main/java/com/juick/service/TagServiceImpl.java +++ b/juick-server/src/main/java/com/juick/service/TagServiceImpl.java @@ -23,6 +23,7 @@ import com.juick.server.helpers.TagStats; import com.juick.util.StreamUtils; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.Pair; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.support.GeneratedKeyHolder; @@ -38,8 +39,8 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Objects; +import java.util.function.Supplier; import java.util.stream.Collectors; -import java.util.stream.IntStream; import java.util.stream.Stream; /** @@ -209,12 +210,15 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { } @Override - public List<Tag> fromString(final String txt) { + public Pair<String, List<Tag>> fromString(final String txt) { String firstLine = txt.split("\\n", 2)[0]; - return StreamUtils.takeWhile(Arrays.stream(firstLine.split("\\ ")), - t -> !t.equals("*") && t.startsWith("*")) - .map(t -> getTag(t.substring(1), true)) + Supplier<Stream<String>> tagsStream = () -> StreamUtils.takeWhile(Arrays.stream(firstLine.split("\\ ")), + t -> !t.equals("*") && t.startsWith("*")); + int tagsLength = tagsStream.get().collect(Collectors.joining(" ")).length(); + String body = txt.substring(tagsLength); + List<Tag> tags = tagsStream.get().map(t -> getTag(t.substring(1), true)) .distinct().collect(Collectors.toList()); + return Pair.of(body, tags); } @Transactional(readOnly = true) diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java index c4915acf..924864f6 100644 --- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java +++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java @@ -1692,8 +1692,12 @@ public class ServerTests { } @Test public void newMessageShouldNotContainDuplicatedTags() throws Exception { - CommandResult result = commandsManager.processCommand(ugnich, "*test *test test", emptyUri); + CommandResult result = commandsManager.processCommand(ugnich, "*test1 *test2 *test1 test3", emptyUri); assertThat(result.getNewMessage().isPresent(), is(true)); - assertThat(result.getNewMessage().get().getTags().size(), is (1)); + Message msg = result.getNewMessage().get(); + assertThat(msg.getTags().size(), is (2)); + assertThat(msg.getTags().get(0).getName(), is("test1")); + assertThat(msg.getTags().get(1).getName(), is("test2")); + assertThat(msg.getText(), is("test3")); } } |