From 55cf2dd357088d7bbe0efff6a483679301ab2c22 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 29 Nov 2018 10:23:04 +0300 Subject: XMPP PMs refactoring --- src/main/java/com/juick/server/XMPPConnection.java | 27 +++++++++++----------- .../java/com/juick/server/tests/ServerTests.java | 7 +++++- 2 files changed, 20 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/main/java/com/juick/server/XMPPConnection.java b/src/main/java/com/juick/server/XMPPConnection.java index 979ba1fd..f77b2354 100644 --- a/src/main/java/com/juick/server/XMPPConnection.java +++ b/src/main/java/com/juick/server/XMPPConnection.java @@ -216,7 +216,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { Jid initiator = e.getInitiator(); ClientMessage result = incomingMessageJuick( userService.getUserByJID(initiator.asBareJid().toEscapedString()), initiator, - e.getDescription(), URI.create(String.format("juick://%s", targetFilename))); + jid.getLocal(), StringUtils.defaultString(e.getDescription()).trim(), URI.create(String.format("juick://%s", targetFilename))); if (result != null) { router.send(result); } @@ -594,16 +594,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { attachment = oobX.getUri(); } try { - if (msg.getTo().asBareJid().equals(jid.asBareJid())) { - return incomingMessageJuick(user_from, msg.getFrom(), StringUtils.defaultString(msg.getBody()), attachment); - } else { - // PM - if (!StringUtils.isEmpty(msg.getBody())) { - result = incomingMessageJuick(user_from, msg.getFrom(), - String.format("@%s %s", msg.getTo().getLocal(), StringUtils.defaultString(msg.getBody())), attachment); - } - } - messagesService.getUnread(user_from).forEach(mid -> messagesService.setRead(user_from, mid)); + return incomingMessageJuick(user_from, msg.getFrom(), msg.getTo().getLocal(), StringUtils.defaultString(msg.getBody()).trim(), attachment); } catch (Exception e1) { logger.warn("message exception", e1); } @@ -621,11 +612,13 @@ public class XMPPConnection implements StanzaListener, NotificationListener { } return result; } - private ClientMessage incomingMessageJuick(User user_from, Jid from, String command, @Nonnull URI attachment) { + private ClientMessage incomingMessageJuick(User user_from, Jid from, String to, String command, @Nonnull URI attachment) { if (StringUtils.isBlank(command) && attachment.toString().isEmpty()) { return null; } + messagesService.getUnread(user_from).forEach(mid -> messagesService.setRead(user_from, mid)); + int commandlen = command.length(); // COMPATIBILITY @@ -633,8 +626,16 @@ public class XMPPConnection implements StanzaListener, NotificationListener { command = command.substring(3); } + if (!jid.getLocal().equals(to)) { + // PM + if (!StringUtils.isEmpty(command)) { + commandsManager.commandPM(user_from, null, to, command); + return null; + } + } + try { - CommandResult result = commandsManager.processCommand(user_from, command.trim(), attachment); + CommandResult result = commandsManager.processCommand(user_from, command, attachment); if (StringUtils.isNotBlank(result.getText())) { return makeReply(from, result.getText()); } diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index 7c038058..09b7d4dd 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -713,7 +713,12 @@ public class ServerTests { xmppMessage.setTo(botJid.withLocal("secretlysad")); xmppMessage.setBody("What's up?"); result = router.incomingMessage(xmppMessage); - assertThat(result.getBody(), startsWith("Private message sent")); + assertThat(result, is(nullValue())); + xmppMessage.setTo(botJid); + xmppMessage.setBody("@secretlysad Hey!"); + result = router.incomingMessage(xmppMessage); + assertThat(result.getBody(), is("Private message sent")); + assertThat(pmQueriesService.getPMMessages(renhaId, secretlySadId).size(), is(2)); String xml = "@yo:\n" + "343432434\n" + "\n" + -- cgit v1.2.3