diff options
-rw-r--r-- | juick-common/src/main/java/com/juick/server/CommandsManager.java (renamed from juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java) | 29 | ||||
-rw-r--r-- | juick-common/src/main/java/com/juick/server/helpers/CommandResult.java (renamed from juick-server-xmpp/src/main/java/com/juick/server/xmpp/helpers/CommandResult.java) | 2 | ||||
-rw-r--r-- | juick-common/src/main/java/com/juick/server/helpers/annotation/UserCommand.java (renamed from juick-server-xmpp/src/main/java/com/juick/server/xmpp/helpers/annotation/UserCommand.java) | 2 | ||||
-rw-r--r-- | juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java | 40 | ||||
-rw-r--r-- | juick-server/src/main/java/com/juick/server/api/Post.java | 2 | ||||
-rw-r--r-- | juick-server/src/test/java/com/juick/server/tests/ServerTests.java | 51 |
6 files changed, 63 insertions, 63 deletions
diff --git a/juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java b/juick-common/src/main/java/com/juick/server/CommandsManager.java index 38b8e254..9bde0884 100644 --- a/juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java +++ b/juick-common/src/main/java/com/juick/server/CommandsManager.java @@ -25,11 +25,12 @@ import com.juick.server.component.LikeEvent; import com.juick.server.component.MessageEvent; import com.juick.server.component.PingEvent; import com.juick.server.component.SubscribeEvent; +import com.juick.server.helpers.CommandResult; import com.juick.server.helpers.TagStats; +import com.juick.server.helpers.annotation.UserCommand; import com.juick.server.util.HttpUtils; import com.juick.server.util.ImageUtils; -import com.juick.server.xmpp.helpers.CommandResult; -import com.juick.server.xmpp.helpers.annotation.UserCommand; +import com.juick.server.util.TagUtils; import com.juick.service.*; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; @@ -41,7 +42,6 @@ import rocks.xmpp.addr.Jid; import javax.annotation.Nonnull; import javax.inject.Inject; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URI; import java.util.*; @@ -76,8 +76,7 @@ public class CommandsManager { @Inject private ApplicationEventPublisher applicationEventPublisher; - public Optional<CommandResult> processCommand(User user, Jid from, String input, @Nonnull URI attachment) throws InvocationTargetException, - IllegalAccessException, NoSuchMethodException { + public CommandResult processCommand(User user, Jid from, String input, @Nonnull URI attachment) throws Exception { Optional<Method> cmd = MethodUtils.getMethodsListWithAnnotation(getClass(), UserCommand.class).stream() .filter(m -> Pattern.compile(m.getAnnotation(UserCommand.class).pattern(), m.getAnnotation(UserCommand.class).patternFlags()).matcher(input).matches()) @@ -91,10 +90,24 @@ public class CommandsManager { groups.add(matcher.group(i)); } } - return Optional.of((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()]))); + return (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()])); } - return Optional.empty(); + // new message + List<Tag> tags = tagService.fromString(input, false); + String body = input.substring(TagUtils.toString(tags).length()); + 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()); + if (StringUtils.isNotEmpty(attachmentType)) { + String attachmentFName = attachment.getScheme().equals("juick") ? attachment.getHost() + : HttpUtils.downloadImage(attachment.toURL(), tmpDir).getHost(); + String fname = String.format("%d.%s", mid, attachmentType); + ImageUtils.saveImageWithPreviews(attachmentFName, fname, tmpDir, imgDir); + } + com.juick.Message msg = messagesService.getMessage(mid); + applicationEventPublisher.publishEvent(new MessageEvent(this, msg)); + return CommandResult.fromMessage(msg, StringUtils.EMPTY); } @UserCommand(pattern = "^ping$", patternFlags = Pattern.CASE_INSENSITIVE, diff --git a/juick-server-xmpp/src/main/java/com/juick/server/xmpp/helpers/CommandResult.java b/juick-common/src/main/java/com/juick/server/helpers/CommandResult.java index f952b579..29244115 100644 --- a/juick-server-xmpp/src/main/java/com/juick/server/xmpp/helpers/CommandResult.java +++ b/juick-common/src/main/java/com/juick/server/helpers/CommandResult.java @@ -1,4 +1,4 @@ -package com.juick.server.xmpp.helpers; +package com.juick.server.helpers; import com.juick.Message; diff --git a/juick-server-xmpp/src/main/java/com/juick/server/xmpp/helpers/annotation/UserCommand.java b/juick-common/src/main/java/com/juick/server/helpers/annotation/UserCommand.java index 383383c9..4f07001c 100644 --- a/juick-server-xmpp/src/main/java/com/juick/server/xmpp/helpers/annotation/UserCommand.java +++ b/juick-common/src/main/java/com/juick/server/helpers/annotation/UserCommand.java @@ -15,7 +15,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package com.juick.server.xmpp.helpers.annotation; +package com.juick.server.helpers.annotation; import org.apache.commons.lang3.StringUtils; 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 19fddd78..a277a4a4 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 @@ -27,7 +27,7 @@ import com.juick.server.helpers.UserInfo; import com.juick.server.util.HttpUtils; import com.juick.server.util.ImageUtils; import com.juick.server.util.TagUtils; -import com.juick.server.xmpp.helpers.CommandResult; +import com.juick.server.helpers.CommandResult; import com.juick.server.xmpp.s2s.BasicXmppSession; import com.juick.server.xmpp.s2s.StanzaListener; import com.juick.service.*; @@ -57,7 +57,6 @@ import rocks.xmpp.extensions.filetransfer.FileTransferManager; import rocks.xmpp.extensions.nick.model.Nickname; import rocks.xmpp.extensions.oob.model.x.OobX; import rocks.xmpp.extensions.ping.PingManager; -import rocks.xmpp.extensions.receipts.MessageDeliveryReceiptsManager; import rocks.xmpp.extensions.vcard.temp.model.VCard; import rocks.xmpp.extensions.version.SoftwareVersionManager; import rocks.xmpp.extensions.version.model.SoftwareVersion; @@ -589,6 +588,11 @@ public class XMPPConnection implements StanzaListener, NotificationListener { User user_from; user_from = userService.getUserByJID(msg.getFrom().asBareJid().toEscapedString()); + if (user_from == null) { + String signuphash = userService.getSignUpHashByJID(msg.getFrom().asBareJid().toEscapedString()); + sendReply(msg.getFrom(), "Для того, чтобы начать пользоваться сервисом, пожалуйста пройдите быструю регистрацию: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nTo start using Juick, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account."); + return; + } URI attachmentURI = URI.create(StringUtils.EMPTY); OobX oobX = msg.getExtension(OobX.class); if (oobX != null) { @@ -596,12 +600,8 @@ public class XMPPConnection implements StanzaListener, NotificationListener { } incomingMessageJuick(user_from, msg.getFrom(), msg.getBody(), attachmentURI); } - public com.juick.Message incomingMessageJuick(User user_from, Jid from, String command, @Nonnull URI attachment) throws Exception { - if (user_from == null) { - String signuphash = userService.getSignUpHashByJID(from.asBareJid().toEscapedString()); - sendReply(from, "Для того, чтобы начать пользоваться сервисом, пожалуйста пройдите быструю регистрацию: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nTo start using Juick, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account."); - return null; - } + public com.juick.Message incomingMessageJuick(User user_from, Jid from, String command, @Nonnull URI attachment) + throws Exception { if (StringUtils.isBlank(command) && attachment.toString().isEmpty()) { return null; } @@ -613,27 +613,11 @@ public class XMPPConnection implements StanzaListener, NotificationListener { command = command.substring(3).trim(); } - Optional<CommandResult> result = commandsManager.processCommand(user_from, from, command, attachment); - if (result.isPresent()) { - sendReply(from, result.get().getText()); - return result.get().getNewMessage(); - } else { - // new message - List<Tag> tags = tagService.fromString(command, false); - String body = command.substring(TagUtils.toString(tags).length()); - String attachmentType = attachment != null && StringUtils.isNotEmpty(attachment.toString()) ? attachment.toString().substring(attachment.toString().length() - 3) : null; - int mid = messagesService.createMessage(user_from.getUid(), body, attachmentType, tags); - subscriptionService.subscribeMessage(mid, user_from.getUid()); - if (StringUtils.isNotEmpty(attachmentType)) { - String attachmentFName = attachment.getScheme().equals("juick") ? attachment.getHost() - : HttpUtils.downloadImage(attachment.toURL(), tmpDir).getHost(); - String fname = String.format("%d.%s", mid, attachmentType); - ImageUtils.saveImageWithPreviews(attachmentFName, fname, tmpDir, imgDir); - } - com.juick.Message msg = messagesService.getMessage(mid); - applicationEventPublisher.publishEvent(new MessageEvent(this, msg)); - return msg; + CommandResult result = commandsManager.processCommand(user_from, from, command, attachment); + if (StringUtils.isNotBlank(result.getText())) { + sendReply(from, result.getText()); } + return result.getNewMessage(); } @Override 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 827b9ffc..ef0c7dd9 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 @@ -23,7 +23,7 @@ import com.juick.server.CommandsManager; import com.juick.server.EmailManager; import com.juick.server.XMPPConnection; import com.juick.server.util.*; -import com.juick.server.xmpp.helpers.CommandResult; +import com.juick.server.helpers.CommandResult; import com.juick.service.MessagesService; import com.juick.service.SubscriptionService; import com.juick.service.UserService; 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 f9776f2f..d6617cc1 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 @@ -72,8 +72,7 @@ import java.util.stream.IntStream; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.*; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @@ -539,11 +538,11 @@ public class ServerTests { assertThat(isActive, equalTo(false)); } @Test - public void botCommandsTests() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { + public void botCommandsTests() throws Exception { URI emptyUri = URI.create(StringUtils.EMPTY); - assertThat(commandsManager.processCommand(new User(), Jid.of("test@localhost"), "PING", emptyUri).get().getText(), is("PONG")); + assertThat(commandsManager.processCommand(new User(), Jid.of("test@localhost"), "PING", emptyUri).getText(), is("PONG")); // subscription commands have two lines, others have 1 - assertThat(commandsManager.processCommand(new User(), Jid.of("test@localhost"), "help", emptyUri).get().getText().split("\n").length, is(32)); + assertThat(commandsManager.processCommand(new User(), Jid.of("test@localhost"), "help", emptyUri).getText().split("\n").length, is(32)); } @Test @@ -558,7 +557,7 @@ public class ServerTests { 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).get().getText().startsWith("@me")); + commandsManager.processCommand(user, Jid.of("test@localhost"), String.format("#%d", mid), emptyUri).getText().startsWith("@me")); assertEquals("text should match", "yoyo", messagesService.getMessage(mid).getText()); assertEquals("tag should match", "yo", @@ -567,9 +566,9 @@ public class ServerTests { 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).get().getText()); + commandsManager.processCommand(readerUser, dummyJid, "S #" + mid, emptyUri).getText()); assertEquals("should be favorited", "Message is added to your recommendations", - commandsManager.processCommand(readerUser, dummyJid, "! #" + mid, emptyUri).get().getText()); + commandsManager.processCommand(readerUser, dummyJid, "! #" + mid, emptyUri).getText()); int rid = messagesService.createReply(mid, 0, uid, "comment", null); assertEquals("number of subscribed users should match", 1, subscriptionService.getUsersSubscribedToComments( @@ -581,7 +580,8 @@ 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).get().getText()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "S @" + user.getName(), emptyUri) + .getText()); List<User> friends = userService.getUserFriends(readerUid); assertEquals("number of friend users should match", 2, friends.size()); @@ -592,57 +592,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")).get().getText()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + 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 ", URI.create("http://static.juick.com/settings/facebook.png")).get().getText()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + 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).get().getText()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "#" + mid + " *yo *there", emptyUri) + .getText()); assertEquals("tags should be updated", "Tags are updated", - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid + " *there", emptyUri).get().getText()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + 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).get().getText()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "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).get().getText()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + 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).get().getText()); + commandsManager.processCommand(taggerUser, Jid.of("tagger@localhost"), "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).get().getText()); + commandsManager.processCommand(taggerUser, Jid.of("tagger@localhost"), "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).get().getText(); - assertEquals("description should match", true, readerFeed.startsWith("Your feed")); + String readerFeed = commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "#", 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).get().getText()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "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).get().getText()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "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).get().getText()); + commandsManager.processCommand(readerUser, Jid.of("dummy@localhost"), "D #" + mid, emptyUri).getText()); assertEquals("should be deleted", "Message deleted", - commandsManager.processCommand(user, Jid.of("test@localhost"), "D #" + mid, emptyUri).get().getText()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "D #" + mid, emptyUri).getText()); assertEquals("should be not found", "Message not found", - commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid, emptyUri).get().getText()); + commandsManager.processCommand(user, Jid.of("test@localhost"), "#" + mid, emptyUri).getText()); } @Test public void mailParserTest() throws Exception { |