diff options
4 files changed, 17 insertions, 26 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 3ba071e2..9ff0fdbb 100644 --- a/juick-common/src/main/java/com/juick/server/CommandsManager.java +++ b/juick-common/src/main/java/com/juick/server/CommandsManager.java @@ -100,7 +100,7 @@ public class CommandsManager { return commandResult; } } - List<Tag> tags = tagService.fromString(input, false); + List<Tag> tags = tagService.fromString(input); if (tags.size() > 5) { return CommandResult.fromString("Sorry, 5 tags maximum."); } @@ -480,7 +480,7 @@ 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, true); + List<Tag> messageTags = tagService.fromString(txt); if (messageTags.size() > 0) { if (user.getUid() != msg.getUser().getUid()) { return CommandResult.fromString("It is not your message"); 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 7cd7768f..17b6c5de 100644 --- a/juick-common/src/main/java/com/juick/service/TagService.java +++ b/juick-common/src/main/java/com/juick/service/TagService.java @@ -49,7 +49,7 @@ public interface TagService { List<Tag> updateTags(int mid, Collection<Tag> newTags); - List<Tag> fromString(String txt, boolean tagsOnly); + List<Tag> fromString(String txt); List<TagStats> getMessageTags(int mid); diff --git a/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java index 3c35f5b3..d7800390 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/TagServiceImpl.java @@ -33,13 +33,12 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Objects; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.stream.IntStream; import java.util.stream.Stream; /** @@ -47,9 +46,6 @@ import java.util.stream.Stream; */ @Repository public class TagServiceImpl extends BaseJdbcService implements TagService { - private static final Pattern TAGS_PATTERN1 = Pattern.compile("^(?:(?:\\*[^ \\r\\n\\t]+)|\\s)+$"); - private static final Pattern TAGS_PATTERN2 = Pattern.compile("^\\*([^ \\r\\n\\t]+)[\\s+]?([\\s\\S]+)?"); - private static final Pattern TAG_PATTERN = Pattern.compile("\\*([^ \\r\\n\\t]+)"); @Transactional(readOnly = true) @Override @@ -212,23 +208,13 @@ public class TagServiceImpl extends BaseJdbcService implements TagService { } @Override - public List<Tag> fromString(final String txt, final boolean tagsOnly) { - Pattern tagsPattern = tagsOnly ? TAGS_PATTERN1 : TAGS_PATTERN2; - List<Tag> tags = new ArrayList<>(); - for (String line : txt.split("\n")) { - if (line.startsWith(">")) - continue; - if (tagsPattern.matcher(line).matches()) { - Matcher tagMatcher = TAG_PATTERN.matcher(line); - - // TODO: process readonly, private, friends, public - while (tagMatcher.find()) { - for (int i = 1; i <= tagMatcher.groupCount(); i++) - tags.add(getTag(tagMatcher.group(i), true)); - } - } - } - return tags; + public List<Tag> fromString(final String txt) { + String firstLine = txt.split("\\n", 2)[0]; + List<String> firstWords = Arrays.asList(firstLine.split("\\ ")); + int nonTagIndex = IntStream.range(0, firstWords.size()) + .filter(i -> !firstWords.get(i).startsWith("*")).findFirst().orElse(firstWords.size()); + return firstWords.subList(0, nonTagIndex).stream().map(t -> getTag(t.substring(1), true)) + .collect(Collectors.toList()); } @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 9a78eac9..292020d6 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 @@ -809,6 +809,11 @@ public class ServerTests { assertFalse(result.getNewMessage().get().getText().contains("VTYZkKV8FWkmu6g1")); result = commandsManager.processCommand(user, "*корм *juick_ppl *рационализм *? *мюсли а сколько микроморт в дневной порции сверхмюслей?", emptyUri); assertTrue(result.getNewMessage().isPresent()); + String tags = "*Juick *Google *Google Play"; + String data = "Вчера отправлял *NSFW постинг в топ :)"; + result = commandsManager.processCommand(user, String.format("%s %s", tags, data), emptyUri); + assertThat(result.getNewMessage().get().getTags().size(), equalTo(3)); + assertThat(result.getNewMessage().get().getText(), equalTo(data)); } @Test public void mailParserTest() throws Exception { |