From fa698d98e0f24362fe55800ca6a431ada213a78d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 6 Apr 2018 13:21:03 +0300 Subject: server: CommandsManager does not depend on Jid --- .../java/com/juick/server/CommandsManager.java | 73 +++++++++++----------- .../main/java/com/juick/server/XMPPConnection.java | 2 +- .../java/com/juick/server/MessengerManager.java | 2 +- .../java/com/juick/server/TelegramBotManager.java | 4 +- .../java/com/juick/server/WebsocketManager.java | 2 +- .../src/main/java/com/juick/server/api/Post.java | 11 ++-- .../java/com/juick/server/tests/ServerTests.java | 58 ++++++++--------- 7 files changed, 76 insertions(+), 76 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 4f115577..3883e677 100644 --- a/juick-common/src/main/java/com/juick/server/CommandsManager.java +++ b/juick-common/src/main/java/com/juick/server/CommandsManager.java @@ -38,7 +38,6 @@ import org.apache.commons.lang3.reflect.MethodUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; -import rocks.xmpp.addr.Jid; import javax.annotation.Nonnull; import javax.inject.Inject; @@ -76,7 +75,7 @@ public class CommandsManager { @Inject private ApplicationEventPublisher applicationEventPublisher; - public CommandResult processCommand(User user, Jid from, String input, @Nonnull URI attachment) throws Exception { + public CommandResult processCommand(User user, String input, @Nonnull URI attachment) throws Exception { Optional cmd = MethodUtils.getMethodsListWithAnnotation(getClass(), UserCommand.class).stream() .filter(m -> Pattern.compile(m.getAnnotation(UserCommand.class).pattern(), m.getAnnotation(UserCommand.class).patternFlags()).matcher(input).matches()) @@ -90,8 +89,8 @@ public class CommandsManager { groups.add(matcher.group(i)); } } - 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()])); + CommandResult commandResult = (CommandResult) getClass().getMethod(cmd.get().getName(), User.class, URI.class, String[].class) + .invoke(this, user, attachment, groups.toArray(new String[groups.size()])); if (StringUtils.isNotEmpty(commandResult.getText())) { return commandResult; } @@ -115,14 +114,14 @@ public class CommandsManager { @UserCommand(pattern = "^ping$", patternFlags = Pattern.CASE_INSENSITIVE, help = "PING - returns you a PONG") - public CommandResult commandPing(User user, Jid from, URI attachment, String[] input) { + public CommandResult commandPing(User user, URI attachment, String[] input) { applicationEventPublisher.publishEvent(new PingEvent(this, user)); return CommandResult.fromString("PONG"); } @UserCommand(pattern = "^help$", patternFlags = Pattern.CASE_INSENSITIVE, help = "HELP - returns this help message") - public CommandResult commandHelp(User user, Jid from, URI attachment, String[] input) { + public CommandResult commandHelp(User user, URI attachment, String[] input) { return CommandResult.fromString(Arrays.stream(getClass().getDeclaredMethods()) .filter(m -> m.isAnnotationPresent(UserCommand.class)) .map(m -> m.getAnnotation(UserCommand.class).help()) @@ -131,11 +130,11 @@ public class CommandsManager { @UserCommand(pattern = "^login$", patternFlags = Pattern.CASE_INSENSITIVE, help = "LOGIN - log in to Juick website") - public CommandResult commandLogin(User user_from, Jid from, URI attachment, String[] input) { + public CommandResult commandLogin(User user_from, URI attachment, String[] input) { return CommandResult.fromString("http://juick.com/login?hash=" + userService.getHashByUID(user_from.getUid())); } @UserCommand(pattern = "^\\@(\\S+)\\s+([\\s\\S]+)$", help = "@username message - send PM to username") - public CommandResult commandPM(User user_from, Jid from, URI attachment, String... arguments) { + public CommandResult commandPM(User user_from, URI attachment, String... arguments) { String body = arguments[1]; User user_to = userService.getUserByName(arguments[0]); @@ -156,7 +155,7 @@ public class CommandsManager { } @UserCommand(pattern = "^bl$", patternFlags = Pattern.CASE_INSENSITIVE, help = "BL - Show your blacklist") - public CommandResult commandBLShow(User user_from, Jid from, URI attachment, String... arguments) { + public CommandResult commandBLShow(User user_from, URI attachment, String... arguments) { List blusers = userService.getUserBLUsers(user_from.getUid()); List bltags = tagService.getUserBLTags(user_from.getUid()); @@ -183,13 +182,13 @@ public class CommandsManager { } @UserCommand(pattern = "^#\\+$", help = "#+ - Show last Juick messages") - public CommandResult commandLast(User user_from, Jid from, URI attachment, String... arguments) { + public CommandResult commandLast(User user_from, URI attachment, String... arguments) { return CommandResult.fromString("Last messages:\n" + printMessages(messagesService.getAll(user_from.getUid(), 0), true)); } @UserCommand(pattern = "@", help = "@ - Show recommendations and popular personal blogs") - public CommandResult commandUsers(User user_from, Jid from, URI attachment, String... arguments) { + public CommandResult commandUsers(User user_from, URI attachment, String... arguments) { StringBuilder msg = new StringBuilder(); msg.append("Recommended blogs"); List recommendedUsers = showQueriesService.getRecommendedUsers(user_from); @@ -213,7 +212,7 @@ public class CommandsManager { } @UserCommand(pattern = "^bl\\s+@([^\\s\\n\\+]+)", patternFlags = Pattern.CASE_INSENSITIVE, help = "BL @username - add @username to your blacklist") - public CommandResult blacklistUser(User user_from, Jid from, URI attachment, String... arguments) { + public CommandResult blacklistUser(User user_from, URI attachment, String... arguments) { User blUser = userService.getUserByName(arguments[0]); if (blUser != null) { PrivacyQueriesService.PrivacyResult result = privacyQueriesService.blacklistUser(user_from, blUser); @@ -227,7 +226,7 @@ public class CommandsManager { } @UserCommand(pattern = "^bl\\s\\*(\\S+)$", patternFlags = Pattern.CASE_INSENSITIVE, help = "BL *tag - add *tag to your blacklist") - public CommandResult blacklistTag(User user_from, Jid from, URI attachment, String... arguments) { + public CommandResult blacklistTag(User user_from, URI attachment, String... arguments) { User blUser = userService.getUserByName(arguments[0]); if (blUser != null) { Tag tag = tagService.getTag(arguments[0], false); @@ -243,7 +242,7 @@ public class CommandsManager { return CommandResult.fromString("Tag not found"); } @UserCommand(pattern = "\\*", help = "* - Show your tags") - public CommandResult commandTags(User currentUser, Jid from, URI attachment, String... args) { + public CommandResult commandTags(User currentUser, URI attachment, String... args) { List tags = tagService.getUserTagStats(currentUser.getUid()); String msg = "Your tags: (tag - messages)\n" + tags.stream() @@ -251,7 +250,7 @@ public class CommandsManager { return CommandResult.fromString(msg); } @UserCommand(pattern = "S", help = "S - Show your subscriptions") - public CommandResult commandSubscriptions(User currentUser, Jid from, URI attachment, String... args) { + public CommandResult commandSubscriptions(User currentUser, URI attachment, String... args) { List friends = userService.getUserFriends(currentUser.getUid()); List tags = subscriptionService.getSubscribedTags(currentUser); String msg = friends.size() > 0 ? "You are subscribed to users:" + friends.stream().map(u -> "\n@" + u.getName()) @@ -263,7 +262,7 @@ public class CommandsManager { return CommandResult.fromString(msg); } @UserCommand(pattern = "!", help = "! - Show your favorite messages") - public CommandResult commandFavorites(User currentUser, Jid from, URI attachment, String... args) { + public CommandResult commandFavorites(User currentUser, URI attachment, String... args) { List mids = messagesService.getUserRecommendations(currentUser.getUid(), 0); if (mids.size() > 0) { return CommandResult.fromString("Favorite messages: \n" + printMessages(mids, false)); @@ -271,7 +270,7 @@ public class CommandsManager { return CommandResult.fromString("No favorite messages, try to \"like\" something ;)"); } @UserCommand(pattern = "^\\!\\s+#(\\d+)", help = "! #12345 - recommend message") - public CommandResult commandRecommend(User user, Jid from, URI attachment, String... arguments) { + 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); @@ -295,7 +294,7 @@ public class CommandsManager { // TODO: target notification @UserCommand(pattern = "^(s|u)\\s+\\@(\\S+)$", help = "S @username - subscribe to user" + "\nU @username - unsubscribe from user", patternFlags = Pattern.CASE_INSENSITIVE) - public CommandResult commandSubscribeUser(User user, Jid from, URI attachment, String... args) { + public CommandResult commandSubscribeUser(User user, URI attachment, String... args) { boolean subscribe = args[0].equalsIgnoreCase("s"); User toUser = userService.getUserByName(args[1]); if (subscribe) { @@ -314,7 +313,7 @@ public class CommandsManager { } @UserCommand(pattern = "^(s|u)\\s+\\*(\\S+)$", help = "S *tag - subscribe to tag" + "\nU *tag - unsubscribe from tag", patternFlags = Pattern.CASE_INSENSITIVE) - public CommandResult commandSubscribeTag(User user, Jid from, URI attachment, String... args) { + public CommandResult commandSubscribeTag(User user, URI attachment, String... args) { boolean subscribe = args[0].equalsIgnoreCase("s"); Tag tag = tagService.getTag(args[1], true); if (subscribe) { @@ -331,7 +330,7 @@ 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, Jid from, URI attachment, String... args) { + public CommandResult commandSubscribeMessage(User user, URI attachment, String... args) { boolean subscribe = args[0].equalsIgnoreCase("s"); int mid = NumberUtils.toInt(args[1], 0); if (messagesService.getMessage(mid) != null) { @@ -350,26 +349,24 @@ public class CommandsManager { } @UserCommand(pattern = "^(on|off)$", patternFlags = Pattern.CASE_INSENSITIVE, help = "ON/OFF - Enable/disable subscriptions delivery") - public CommandResult commandOnOff(User user, Jid from, URI attachment, String[] input) { + public CommandResult commandOnOff(User user, URI attachment, String[] input) { UserService.ActiveStatus newStatus; String retValUpdated; if (input[0].toLowerCase().equals("on")) { newStatus = UserService.ActiveStatus.Active; - retValUpdated = "Notifications are activated for " + from.asBareJid().toEscapedString(); + retValUpdated = "XMPP notifications are activated"; } else { newStatus = UserService.ActiveStatus.Inactive; - retValUpdated = "Notifications are disabled for " + from.asBareJid().toEscapedString(); + retValUpdated = "XMPP notifications are disabled"; } - - if (userService.setActiveStatusForJID(from.asBareJid().toEscapedString(), newStatus)) { + if (userService.getAllJIDs(user).stream().allMatch(jid -> userService.setActiveStatusForJID(jid, newStatus))) { return CommandResult.fromString(retValUpdated); - } else { - return CommandResult.fromString(String.format("Subscriptions status for %s was not changed", from.toEscapedString())); } + return CommandResult.fromString("Error"); } @UserCommand(pattern = "^\\@([^\\s\\n\\+]+)(\\+?)$", help = "@username+ - Show user's info and last 20 messages") - public CommandResult commandUser(User user, Jid from, URI attachment, String... arguments) { + public CommandResult commandUser(User user, URI attachment, String... arguments) { User blogUser = userService.getUserByName(arguments[0]); int page = arguments[1].length(); if (blogUser.getUid() > 0) { @@ -380,7 +377,7 @@ 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, Jid from, URI attachment, String... arguments) { + public CommandResult commandShow(User user, URI attachment, String... arguments) { boolean showReplies = arguments[1].length() > 0; int mid = NumberUtils.toInt(arguments[0], 0); if (mid == 0) { @@ -399,7 +396,7 @@ public class CommandsManager { return CommandResult.fromString("Message not found"); } @UserCommand(pattern = "^#(\\d+)\\/(\\d+)$", help = "#1234/5 - Show reply") - public CommandResult commandShowReply(User user, Jid from, URI attachment, String... arguments) { + public CommandResult commandShowReply(User user, URI attachment, String... arguments) { int mid = NumberUtils.toInt(arguments[0], 0); int rid = NumberUtils.toInt(arguments[1], 0); com.juick.Message reply = messagesService.getReply(mid, rid); @@ -409,7 +406,7 @@ public class CommandsManager { return CommandResult.fromString("Reply not found"); } @UserCommand(pattern = "^\\*(\\S+)(\\+?)$", help = "*tag - Show last messages with tag") - public CommandResult commandShowTag(User user, Jid from, URI attachment, String... arguments) { + public CommandResult commandShowTag(User user, URI attachment, String... arguments) { if (StringUtils.isNotEmpty(attachment.toString())) { // new message with tag return CommandResult.fromString(StringUtils.EMPTY); @@ -423,7 +420,7 @@ public class CommandsManager { return CommandResult.fromString("Tag not found"); } @UserCommand(pattern = "^D #(\\d+)$", help = "D #1234 - Delete post", patternFlags = Pattern.CASE_INSENSITIVE) - public CommandResult commandDeletePost(User user, Jid from, URI attachment, String... args) { + public CommandResult commandDeletePost(User user, URI attachment, String... args) { int mid = Integer.valueOf(args[0]); if (messagesService.deleteMessage(user.getUid(), mid)) { return CommandResult.fromString("Message deleted"); @@ -431,7 +428,7 @@ public class CommandsManager { return CommandResult.fromString("This is not your message"); } @UserCommand(pattern = "^D #(\\d+)(\\.|\\-|\\/)(\\d+)$", help = "D #1234/5 - Delete comment", patternFlags = Pattern.CASE_INSENSITIVE) - public CommandResult commandDeleteReply(User user, Jid from, URI attachment, String... args) { + public CommandResult commandDeleteReply(User user, URI attachment, String... args) { int mid = Integer.valueOf(args[0]); int rid = Integer.valueOf(args[2]); if (messagesService.deleteReply(user.getUid(), mid, rid)) { @@ -441,19 +438,19 @@ public class CommandsManager { } } @UserCommand(pattern = "^(D L|DL|D LAST)$", help = "D L - Delete last message", patternFlags = Pattern.CASE_INSENSITIVE) - public CommandResult commandDeleteLast(User user, Jid from, URI attachment, String... args) { + public CommandResult commandDeleteLast(User user, URI attachment, String... args) { return CommandResult.fromString("Temporarily unavailable"); } @UserCommand(pattern = "^\\?\\s+\\@([a-zA-Z0-9\\-\\.\\@]+)\\s+([\\s\\S]+)$", help = "? @user string - search in user messages") - public CommandResult commandSearch(User user, Jid from, URI attachment, String... args) { + public CommandResult commandSearch(User user, URI attachment, String... args) { return CommandResult.fromString("Temporarily unavailable"); } @UserCommand(pattern = "^\\?\\s+([\\s\\S]+)$", help = "? string - search in all messages") - public CommandResult commandSearchAll(User user, Jid from, URI attachment, String... args) { + public CommandResult commandSearchAll(User user, URI attachment, String... args) { return CommandResult.fromString("Temporarily unavailable"); } @UserCommand(pattern = "^(#+)$", help = "# - Show last messages from your feed (## - second page, ...)") - public CommandResult commandMyFeed(User user, Jid from, URI attachment, String... arguments) { + public CommandResult commandMyFeed(User user, URI attachment, String... arguments) { // number of # is the page count int page = arguments[0].length() - 1; List mids = messagesService.getMyFeed(user.getUid(), page, false); @@ -464,7 +461,7 @@ 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, Jid from, @Nonnull URI attachment, String... args) throws Exception { + public CommandResult EditOrReply(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]); diff --git a/juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java b/juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java index 2bdec0a7..c800b65d 100644 --- a/juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java +++ b/juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java @@ -603,7 +603,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { command = command.substring(3); } - CommandResult result = commandsManager.processCommand(user_from, from, command.trim(), attachment); + CommandResult result = commandsManager.processCommand(user_from, command.trim(), attachment); if (StringUtils.isNotBlank(result.getText())) { sendReply(from, result.getText()); } diff --git a/juick-server/src/main/java/com/juick/server/MessengerManager.java b/juick-server/src/main/java/com/juick/server/MessengerManager.java index 1628afc0..9192288f 100644 --- a/juick-server/src/main/java/com/juick/server/MessengerManager.java +++ b/juick-server/src/main/java/com/juick/server/MessengerManager.java @@ -98,7 +98,7 @@ public class MessengerManager implements ApplicationListener { logger.info("Received text message from '{}' at '{}' with content: {} (mid: {})", senderId, timestamp, text, messageId); try { - commandsManager.processCommand(user_from, null, text, URI.create(StringUtils.EMPTY)); + commandsManager.processCommand(user_from, text, URI.create(StringUtils.EMPTY)); } catch (Exception e) { logger.warn("messenger error", e); } 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 17ad4630..b0803265 100644 --- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java +++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java @@ -185,7 +185,7 @@ public class TelegramBotManager implements NotificationListener { int rid = Integer.valueOf(uriComponents.getFragment()); prefix = String.format("#%d/%d ", mid, rid); } - CommandResult result = commandsManager.processCommand(user_from, null,prefix + text, attachment); + CommandResult result = commandsManager.processCommand(user_from, prefix + text, attachment); String messageTxt = StringUtils.isNotEmpty(result.getMarkdown()) ? result.getMarkdown() : "Unknown error or unsupported command"; telegramNotify(message.from().id().longValue(), messageTxt, StringUtils.EMPTY); @@ -204,7 +204,7 @@ public class TelegramBotManager implements NotificationListener { "Can not reply to this message", replyMessage.messageId(), StringUtils.EMPTY); } } else { - CommandResult result = commandsManager.processCommand(user_from, null, text, attachment); + CommandResult result = commandsManager.processCommand(user_from, text, attachment); 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/WebsocketManager.java b/juick-server/src/main/java/com/juick/server/WebsocketManager.java index 8475fd40..f55bd92b 100644 --- a/juick-server/src/main/java/com/juick/server/WebsocketManager.java +++ b/juick-server/src/main/java/com/juick/server/WebsocketManager.java @@ -151,7 +151,7 @@ public class WebsocketManager extends TextWebSocketHandler { Message draft = jsonMapper.readValue(message.asBytes(), Message.class); if (draft.getUser() != null && (draft.getText() != null || draft.getAttachment() != null)) { String attachmentFileName = draft.getAttachment() == null ? "" : draft.getAttachment().getUrl(); - commandsManager.processCommand(draft.getUser(), null, draft.getText(), URI.create(attachmentFileName)); + commandsManager.processCommand(draft.getUser(), draft.getText(), URI.create(attachmentFileName)); } } catch (Exception e) { throw new HttpBadRequestException(); 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 eddd1dd6..68465de1 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 @@ -106,7 +106,7 @@ public class Post { throw new HttpBadRequestException(); } } - commandsManager.processCommand(visitor, null, body, attachmentFName); + commandsManager.processCommand(visitor, body, attachmentFName); } @PostMapping("/upload") public String doUploadFile(@RequestParam(required = true) MultipartFile attach) { @@ -163,7 +163,7 @@ public class Post { } } - return commandsManager.processCommand(visitor, null, body, attachmentFName).getNewMessage().get(); + return commandsManager.processCommand(visitor, body, attachmentFName).getNewMessage().get(); } Session session = Session.getDefaultInstance(new Properties()); @@ -227,7 +227,7 @@ public class Post { } URI attachmentUri = StringUtils.isNotEmpty(attachmentFName[0]) ? URI.create(String.format("juick://%s", attachmentFName[0])) : URI.create(StringUtils.EMPTY); - commandsManager.processCommand(visitor, null, body[0], attachmentUri); + commandsManager.processCommand(visitor, body[0], attachmentUri); } else { logger.info("not registered: {}", from); } @@ -237,7 +237,7 @@ public class Post { } @PostMapping("/like") @ResponseStatus(value = HttpStatus.OK) - public Status doPostRecomm(@RequestParam Integer mid) { + public Status doPostRecomm(@RequestParam Integer mid) throws Exception { com.juick.User visitor = UserUtils.getCurrentUser(); if (visitor.getUid() == 0) { throw new HttpForbiddenException(); @@ -249,7 +249,8 @@ public class Post { if (msg.getUser().getUid() == visitor.getUid()) { throw new HttpForbiddenException(); } - CommandResult status = commandsManager.commandRecommend(visitor, null, null, String.valueOf(mid)); + CommandResult status = commandsManager.processCommand(visitor, String.format("! #%d", mid), + URI.create(StringUtils.EMPTY)); return Status.getStatus(status.getText()); } } 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 0ef82f98..1ba65721 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 @@ -69,6 +69,8 @@ import java.nio.file.Paths; import java.sql.Timestamp; import java.time.Instant; import java.util.*; +import java.util.function.BiFunction; +import java.util.function.Function; import java.util.stream.IntStream; import static org.hamcrest.MatcherAssert.assertThat; @@ -532,18 +534,19 @@ public class ServerTests { String xmlMessage = "Reply by @LexXПохоже нынче можно публично заявлять о своем веганстве. Your contact offline message queue is full. The message has been discarded."; Stanza msg = server.parse(xmlMessage); assertThat(from, equalTo(msg.getFrom())); - boolean isActive = jdbcTemplate.queryForObject("SELECT active FROM jids WHERE user_id=?", Integer.class, renhaId) == 1; - assertThat(isActive, equalTo(true)); + Function isActive = f -> jdbcTemplate.queryForObject("SELECT active FROM jids WHERE user_id=?", Integer.class, f) == 1; + assertThat(isActive.apply(renhaId), equalTo(true)); router.incomingMessage((ServerMessage)msg); - isActive = jdbcTemplate.queryForObject("SELECT active FROM jids WHERE user_id=?", Integer.class, renhaId) == 1; - assertThat(isActive, equalTo(false)); + assertThat(isActive.apply(renhaId), equalTo(false)); + commandsManager.processCommand(userService.getUserByJID(from.asBareJid().toEscapedString()), "On", URI.create(StringUtils.EMPTY)); + assertTrue(isActive.apply(renhaId)); } @Test public void botCommandsTests() throws Exception { URI emptyUri = URI.create(StringUtils.EMPTY); - assertThat(commandsManager.processCommand(new User(), Jid.of("test@localhost"), "PING", emptyUri).getText(), is("PONG")); + assertThat(commandsManager.processCommand(new User(), "PING", emptyUri).getText(), is("PONG")); // subscription commands have two lines, others have 1 - assertThat(commandsManager.processCommand(new User(), Jid.of("test@localhost"), "help", emptyUri).getText().split("\n").length, is(32)); + assertThat(commandsManager.processCommand(new User(), "help", emptyUri).getText().split("\n").length, is(32)); } @Test @@ -552,27 +555,26 @@ 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().get(); + Message msg = commandsManager.processCommand(user, "*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().get(); + 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()); assertEquals("tag should match", "yo", tagService.getMessageTags(msg.getMid()).get(0).getTag().getName()); - CommandResult yoyoMsg = commandsManager.processCommand(user, null, "*yo", URI.create("http://static.juick.com/settings/facebook.png")); + CommandResult yoyoMsg = commandsManager.processCommand(user, "*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")); + commandsManager.processCommand(user, 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"); assertEquals("should be subscribed", "Subscribed", - commandsManager.processCommand(readerUser, dummyJid, "S #" + mid, emptyUri).getText()); + commandsManager.processCommand(readerUser, "S #" + mid, emptyUri).getText()); assertEquals("should be favorited", "Message is added to your recommendations", - commandsManager.processCommand(readerUser, dummyJid, "! #" + mid, emptyUri).getText()); + commandsManager.processCommand(readerUser, "! #" + mid, emptyUri).getText()); int rid = messagesService.createReply(mid, 0, uid, "comment", null); assertEquals("number of subscribed users should match", 1, subscriptionService.getUsersSubscribedToComments( @@ -584,7 +586,7 @@ public class ServerTests { messagesService.getMessage(mid), messagesService.getReply(mid, rid)).size()); assertEquals("should be subscribed", "Subscribed to @" + user.getName(), - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "S @" + user.getName(), emptyUri) + commandsManager.processCommand(readerUser, "S @" + user.getName(), emptyUri) .getText()); List friends = userService.getUserFriends(readerUid); assertEquals("number of friend users should match", 2, @@ -596,60 +598,60 @@ public class ServerTests { String expectedThirdReply = "Reply posted.\n#" + mid + "/3 " + "https://juick.com/" + mid + "#3"; assertEquals("should be second reply", expectedSecondReply, - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " yoyo", URI.create("http://static.juick.com/settings/facebook.png")).getText()); + commandsManager.processCommand(user, "#" + mid + " yoyo", URI.create("http://static.juick.com/settings/facebook.png")).getText()); assertEquals("should be third reply", expectedThirdReply, - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + "/2 ", + commandsManager.processCommand(user, "#" + mid + "/2 ", URI.create("http://static.juick.com/settings/facebook.png")).getText()); Message reply = messagesService.getReplies(mid).stream().filter(m -> m.getRid() == 3).findFirst() .orElse(new Message()); assertEquals("should be reply to second comment", 2, reply.getReplyto()); assertEquals("tags should NOT be updated", "It is not your message", - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "#" + mid + " *yo *there", emptyUri) + commandsManager.processCommand(readerUser, "#" + mid + " *yo *there", emptyUri) .getText()); assertEquals("tags should be updated", "Tags are updated", - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there", emptyUri).getText()); + commandsManager.processCommand(user, "#" + mid + " *there", emptyUri).getText()); assertEquals("number of tags should match", 2, tagService.getMessageTags(mid).size()); assertEquals("should be blacklisted", "Tag added to your blacklist", - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "BL *there", emptyUri).getText()); + commandsManager.processCommand(readerUser, "BL *there", emptyUri).getText()); assertEquals("number of subscribed users should match", 0, subscriptionService.getSubscribedUsers(uid, mid).size()); assertEquals("tags should be updated", "Tags are updated", - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there", emptyUri).getText()); + commandsManager.processCommand(user, "#" + mid + " *there", emptyUri).getText()); assertEquals("number of tags should match", 1, tagService.getMessageTags(mid).size()); int taggerUid = userService.createUser("dummyTagger", "dummySecret"); User taggerUser = userService.getUserByUID(taggerUid).orElse(new User()); assertEquals("should be subscribed", "Subscribed", - commandsManager.processCommand(taggerUser, Jid.of("tagger@localhost"), "S *yo", emptyUri).getText()); + commandsManager.processCommand(taggerUser, "S *yo", emptyUri).getText()); assertEquals("number of subscribed users should match", 2, subscriptionService.getSubscribedUsers(uid, mid).size()); assertEquals("should be unsubscribed", "Unsubscribed from yo", - commandsManager.processCommand(taggerUser, Jid.of("tagger@localhost"), "U *yo", emptyUri).getText()); + commandsManager.processCommand(taggerUser, "U *yo", emptyUri).getText()); assertEquals("number of subscribed users should match", 1, subscriptionService.getSubscribedUsers(uid, mid).size()); assertEquals("number of readers should match", 1, userService.getUserReaders(uid).size()); - String readerFeed = commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "#", emptyUri).getText(); + String readerFeed = commandsManager.processCommand(readerUser, "#", emptyUri).getText(); assertTrue("description should match", readerFeed.startsWith("Your feed")); assertEquals("should be unsubscribed", "Unsubscribed from @" + user.getName(), - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "U @" + user.getName(), emptyUri) + commandsManager.processCommand(readerUser, "U @" + user.getName(), emptyUri) .getText()); assertEquals("number of readers should match", 0, userService.getUserReaders(uid).size()); assertEquals("number of friends should match", 1, userService.getUserFriends(uid).size()); assertEquals("should be unsubscribed", "Unsubscribed from #" + mid, - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "u #" + mid, emptyUri).getText()); + commandsManager.processCommand(readerUser, "u #" + mid, emptyUri).getText()); assertEquals("number of subscribed users should match", 0, subscriptionService.getUsersSubscribedToComments(messagesService.getMessage(mid), messagesService.getReply(mid, rid)).size()); assertNotEquals("should NOT be deleted", String.format("Message %s deleted", mid), - commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "D #" + mid, emptyUri).getText()); + commandsManager.processCommand(readerUser, "D #" + mid, emptyUri).getText()); assertEquals("should be deleted", "Message deleted", - commandsManager.processCommand(user, Jid.of("test@localhost"), "D #" + mid, emptyUri).getText()); + commandsManager.processCommand(user, "D #" + mid, emptyUri).getText()); assertEquals("should be not found", "Message not found", - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid, emptyUri).getText()); + commandsManager.processCommand(user, "#" + mid, emptyUri).getText()); } @Test public void mailParserTest() throws Exception { -- cgit v1.2.3