From 767aec7729a326ff1ccc225d8623a24dd1e698d5 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 5 Apr 2018 22:17:57 +0300 Subject: server: markdown command reply * also fixes new messages with one tag and attachment --- .../main/java/com/juick/server/CommandsManager.java | 20 ++++++++++++++------ .../java/com/juick/server/helpers/CommandResult.java | 13 ++++++++++--- .../main/java/com/juick/server/util/TagUtils.java | 4 ++-- .../main/java/com/juick/service/TagServiceImpl.java | 2 +- .../java/com/juick/server/TelegramBotManager.java | 8 ++++++-- .../src/main/java/com/juick/server/api/Post.java | 2 +- .../java/com/juick/server/tests/ServerTests.java | 18 +++++++++++------- 7 files changed, 45 insertions(+), 22 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 9bde0884..4f115577 100644 --- a/juick-common/src/main/java/com/juick/server/CommandsManager.java +++ b/juick-common/src/main/java/com/juick/server/CommandsManager.java @@ -90,12 +90,15 @@ public class CommandsManager { groups.add(matcher.group(i)); } } - return (CommandResult) getClass().getMethod(cmd.get().getName(), User.class, Jid.class, URI.class, String[].class) + CommandResult commandResult = (CommandResult) getClass().getMethod(cmd.get().getName(), User.class, Jid.class, URI.class, String[].class) .invoke(this, user, from, attachment, groups.toArray(new String[groups.size()])); + if (StringUtils.isNotEmpty(commandResult.getText())) { + return commandResult; + } } // new message List tags = tagService.fromString(input, false); - String body = input.substring(TagUtils.toString(tags).length()); + String body = TagUtils.toString(tags).equals(input) ? StringUtils.EMPTY : input.substring(TagUtils.toString(tags).length()).trim(); String attachmentType = StringUtils.isNotEmpty(attachment.toString()) ? attachment.toString().substring(attachment.toString().length() - 3) : null; int mid = messagesService.createMessage(user.getUid(), body, attachmentType, tags); subscriptionService.subscribeMessage(mid, user.getUid()); @@ -107,7 +110,7 @@ public class CommandsManager { } com.juick.Message msg = messagesService.getMessage(mid); applicationEventPublisher.publishEvent(new MessageEvent(this, msg)); - return CommandResult.fromMessage(msg, StringUtils.EMPTY); + return CommandResult.build(msg, StringUtils.EMPTY, String.format("[New message](%s) posted", PlainTextFormatter.formatUrl(msg))); } @UserCommand(pattern = "^ping$", patternFlags = Pattern.CASE_INSENSITIVE, @@ -407,9 +410,13 @@ public class CommandsManager { } @UserCommand(pattern = "^\\*(\\S+)(\\+?)$", help = "*tag - Show last messages with tag") public CommandResult commandShowTag(User user, Jid from, URI attachment, String... arguments) { + if (StringUtils.isNotEmpty(attachment.toString())) { + // new message with tag + return CommandResult.fromString(StringUtils.EMPTY); + } Tag tag = tagService.getTag(arguments[0], false); if (tag != null) { - // TODO: synonims + // TODO: synonyms List mids = messagesService.getTag(tag.TID, user.getUid(), 0, 10); return CommandResult.fromString("Last messages with *" + tag.getName() + ":\n" + printMessages(mids, true)); } @@ -480,8 +487,9 @@ public class CommandsManager { } Message reply = messagesService.getReply(mid, newrid); applicationEventPublisher.publishEvent(new MessageEvent(this, reply)); - return CommandResult.fromMessage(reply,"Reply posted.\n#" + mid + "/" + newrid + " " - + "https://juick.com/" + mid + "#" + newrid); + return CommandResult.build(reply,"Reply posted.\n#" + mid + "/" + newrid + " " + + "https://juick.com/" + mid + "#" + newrid, + String.format("[Reply](%s) posted", PlainTextFormatter.formatUrl(reply))); } } diff --git a/juick-common/src/main/java/com/juick/server/helpers/CommandResult.java b/juick-common/src/main/java/com/juick/server/helpers/CommandResult.java index 29244115..a772153b 100644 --- a/juick-common/src/main/java/com/juick/server/helpers/CommandResult.java +++ b/juick-common/src/main/java/com/juick/server/helpers/CommandResult.java @@ -2,21 +2,28 @@ package com.juick.server.helpers; import com.juick.Message; +import java.util.Optional; + public class CommandResult { private String text; + private String markdown; private Message newMessage; public String getText() { return text; } + public String getMarkdown() { + return markdown; + } - public Message getNewMessage() { - return newMessage; + public Optional getNewMessage() { + return Optional.of(newMessage); } - public static CommandResult fromMessage(Message newMessage, String text) { + public static CommandResult build(Message newMessage, String text, String markdown) { CommandResult result = new CommandResult(); result.newMessage = newMessage; result.text = text; + result.markdown = markdown; return result; } public static CommandResult fromString(String text) { diff --git a/juick-common/src/main/java/com/juick/server/util/TagUtils.java b/juick-common/src/main/java/com/juick/server/util/TagUtils.java index 9edeab32..cb828933 100644 --- a/juick-common/src/main/java/com/juick/server/util/TagUtils.java +++ b/juick-common/src/main/java/com/juick/server/util/TagUtils.java @@ -36,7 +36,7 @@ public class TagUtils { if (CollectionUtils.isEmpty(tags)) return StringUtils.EMPTY; - return tags.stream().map(t -> " *" + t.getName()) - .collect(Collectors.joining()); + return tags.stream().map(t -> "*" + t.getName()) + .collect(Collectors.joining(" ")); } } 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 97627406..90b70612 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 @@ -51,7 +51,7 @@ 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 TAGS_PATTERN2 = Pattern.compile("^\\*([^ \\r\\n\\t]+)[\\s+]?([\\s\\S]+)?"); private static final Pattern TAG_PATTERN = Pattern.compile("\\*([^ \\r\\n\\t]+)"); private final MessagesService messagesService; diff --git a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java index 4b2e5278..17ad4630 100644 --- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java +++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java @@ -186,7 +186,9 @@ public class TelegramBotManager implements NotificationListener { prefix = String.format("#%d/%d ", mid, rid); } CommandResult result = commandsManager.processCommand(user_from, null,prefix + text, attachment); - telegramNotify(message.from().id().longValue(), result.getText(), StringUtils.EMPTY); + String messageTxt = StringUtils.isNotEmpty(result.getMarkdown()) ? result.getMarkdown() + : "Unknown error or unsupported command"; + telegramNotify(message.from().id().longValue(), messageTxt, StringUtils.EMPTY); } else { logger.warn("invalid path: {}", path); } @@ -203,7 +205,9 @@ public class TelegramBotManager implements NotificationListener { } } else { CommandResult result = commandsManager.processCommand(user_from, null, text, attachment); - telegramNotify(message.from().id().longValue(), result.getText(), StringUtils.EMPTY); + String messageTxt = StringUtils.isNotEmpty(result.getMarkdown()) ? result.getMarkdown() + : "Unknown error or unsupported command"; + telegramNotify(message.from().id().longValue(), messageTxt, StringUtils.EMPTY); } } } diff --git a/juick-server/src/main/java/com/juick/server/api/Post.java b/juick-server/src/main/java/com/juick/server/api/Post.java index e949dad7..eddd1dd6 100644 --- a/juick-server/src/main/java/com/juick/server/api/Post.java +++ b/juick-server/src/main/java/com/juick/server/api/Post.java @@ -163,7 +163,7 @@ public class Post { } } - return commandsManager.processCommand(visitor, null, body, attachmentFName).getNewMessage(); + return commandsManager.processCommand(visitor, null, body, attachmentFName).getNewMessage().get(); } Session session = Session.getDefaultInstance(new Properties()); 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 a30403b0..0ef82f98 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 @@ -28,6 +28,7 @@ import com.juick.server.EmailManager; import com.juick.server.XMPPConnection; import com.juick.server.XMPPServer; import com.juick.server.helpers.AnonymousUser; +import com.juick.server.helpers.CommandResult; import com.juick.server.helpers.TagStats; import com.juick.server.util.HttpUtils; import com.juick.service.*; @@ -551,17 +552,20 @@ public class ServerTests { int uid = userService.createUser("me", "secret"); User user = userService.getUserByUID(uid).orElse(new User()); Tag yo = tagService.getTag("yo", true); - Message msg = commandsManager.processCommand(user, Jid.of("test@localhost"), "*yo yoyo", URI.create("http://static.juick.com/settings/facebook.png")).getNewMessage(); + Message msg = commandsManager.processCommand(user, Jid.of("test@localhost"), "*yo yoyo", URI.create("http://static.juick.com/settings/facebook.png")).getNewMessage().get(); assertThat(msg.getAttachmentType(), is("png")); - Message msgreply = commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + msg.getMid() + " yyy", HttpUtils.downloadImage(URI.create("http://static.juick.com/settings/xmpp.png").toURL(), tmpDir)).getNewMessage(); + Message msgreply = commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + msg.getMid() + " yyy", HttpUtils.downloadImage(URI.create("http://static.juick.com/settings/xmpp.png").toURL(), tmpDir)).getNewMessage().get(); assertThat(msgreply.getAttachmentType(), equalTo("png")); - int mid = messagesService.createMessage(uid, "yoyo", null, Collections.singletonList(yo)); - assertEquals("should be message", true, - commandsManager.processCommand(user, Jid.of("test@localhost"), String.format("#%d", mid), emptyUri).getText().startsWith("@me")); assertEquals("text should match", "yoyo", - messagesService.getMessage(mid).getText()); + messagesService.getMessage(msg.getMid()).getText()); assertEquals("tag should match", "yo", - tagService.getMessageTags(mid).get(0).getTag().getName()); + tagService.getMessageTags(msg.getMid()).get(0).getTag().getName()); + CommandResult yoyoMsg = commandsManager.processCommand(user, null, "*yo", URI.create("http://static.juick.com/settings/facebook.png")); + assertTrue(yoyoMsg.getNewMessage().isPresent()); + assertThat(yoyoMsg.getNewMessage().get().getTags().get(0), is(yo)); + int mid = yoyoMsg.getNewMessage().get().getMid(); + assertEquals("should be message", true, + commandsManager.processCommand(user, Jid.of("test@localhost"), String.format("#%d", mid), emptyUri).getText().startsWith("@me")); int readerUid = userService.createUser("dummyReader", "dummySecret"); User readerUser = userService.getUserByUID(readerUid).orElse(new User()); Jid dummyJid = Jid.of("dummy@localhost"); -- cgit v1.2.3