aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-04-09 17:59:28 +0300
committerGravatar Vitaly Takmazov2018-04-09 17:59:28 +0300
commit66fd14b44044b2b8004ddd73789345e5c7f26ae2 (patch)
treebce1a7915e88f7cf2eea4e5f268420cdf5f2e89e
parent11c210116aaa3f92cc30cb577b289c4a36ece4c7 (diff)
server: new xmpp tests
-rw-r--r--juick-common/src/main/java/com/juick/server/CommandsManager.java2
-rw-r--r--juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java130
-rw-r--r--juick-server/src/test/java/com/juick/server/tests/ServerTests.java26
3 files changed, 94 insertions, 64 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 3883e677..8dbe99e3 100644
--- a/juick-common/src/main/java/com/juick/server/CommandsManager.java
+++ b/juick-common/src/main/java/com/juick/server/CommandsManager.java
@@ -109,7 +109,7 @@ public class CommandsManager {
}
com.juick.Message msg = messagesService.getMessage(mid);
applicationEventPublisher.publishEvent(new MessageEvent(this, msg));
- return CommandResult.build(msg, StringUtils.EMPTY, String.format("[New message](%s) posted", PlainTextFormatter.formatUrl(msg)));
+ return CommandResult.build(msg, "New message posted.\n#" + msg.getMid() + " https://juick.com/" + msg.getMid(), String.format("[New message](%s) posted", PlainTextFormatter.formatUrl(msg)));
}
@UserCommand(pattern = "^ping$", patternFlags = Pattern.CASE_INSENSITIVE,
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 fd451aeb..c73b7cb1 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
@@ -17,6 +17,7 @@
package com.juick.server;
+import ch.qos.logback.core.net.server.Client;
import com.juick.User;
import com.juick.server.component.LikeEvent;
import com.juick.server.component.MessageEvent;
@@ -172,7 +173,10 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
}
});
router.addInboundMessageListener(e -> {
- incomingMessage(e.getMessage());
+ ClientMessage result = incomingMessage(e.getMessage());
+ if (result != null) {
+ router.send(result);
+ }
});
router.addInboundIQListener(e -> {
IQ iq = e.getIQ();
@@ -201,7 +205,12 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
logger.info("transfer completed");
try {
Jid initiator = e.getInitiator();
- incomingMessageJuick(userService.getUserByJID(initiator.asBareJid().toEscapedString()), initiator, e.getDescription(), URI.create(String.format("juick://%s", targetFilename)));
+ ClientMessage result = incomingMessageJuick(
+ userService.getUserByJID(initiator.asBareJid().toEscapedString()), initiator,
+ e.getDescription(), URI.create(String.format("juick://%s", targetFilename)));
+ if (result != null) {
+ router.send(result);
+ }
} catch (Exception e1) {
logger.error("ft error", e1);
}
@@ -352,20 +361,17 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
sendJuickComment(msg);
}
else {
- String notify = "New message posted.\n#" + msg.getMid() + " https://juick.com/" + msg.getMid();
- userService.getJIDsbyUID(msg.getUser().getUid())
- .forEach(jid -> sendReply(Jid.of(jid), notify));
sendJuickMessage(msg);
}
}
- private void sendReply(Jid jidTo, String txt) {
+ private ClientMessage makeReply(Jid jidTo, String txt) {
Message reply = new Message();
reply.setFrom(jid);
reply.setTo(jidTo);
reply.setType(Message.Type.CHAT);
reply.setBody(txt);
- router.send(ClientMessage.from(reply));
+ return ClientMessage.from(reply);
}
@Override
@@ -516,24 +522,29 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
}
}
- public void incomingMessage(Message msg) {
+ public ClientMessage incomingMessage(Message msg) {
+ ClientMessage result = null;
if (msg.getType() != null && msg.getType().equals(Message.Type.ERROR)) {
StanzaError error = msg.getError();
if (error != null && error.getCondition().equals(Condition.RESOURCE_CONSTRAINT)) {
// offline query is full, deactivating this jid
if (userService.setActiveStatusForJID(msg.getFrom().toEscapedString(), UserService.ActiveStatus.Inactive)) {
logger.info("{} is inactive now", msg.getFrom());
- return;
+ return null;
}
}
- return;
+ return null;
}
User user_from;
- user_from = userService.getUserByJID(msg.getFrom().asBareJid().toEscapedString());
- if (user_from == null) {
+ if (msg.getFrom().getDomain().equals("uid.juick.com")) {
+ user_from = userService.getUserByUID(NumberUtils.toInt(msg.getFrom().getLocal(),
+ 0)).orElse(new User());
+ } else {
+ user_from = userService.getUserByJID(msg.getFrom().asBareJid().toEscapedString());
+ }
+ if (user_from == null || user_from.getUid() == 0) {
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;
+ return makeReply(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.");
}
Jid jid = msg.getTo();
if (jid.getDomain().equals(router.getDomain().toEscapedString()) || jid.equals(this.jid)) {
@@ -558,48 +569,43 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
attachment = oobX.getUri();
}
try {
- Jid from = msg.getFrom();
- if (from.getDomain().equals("uid.juick.com")) {
- User fromUser = userService.getUserByUID(NumberUtils.toInt(from.getLocal(),
- 0)).orElse(new User());
- incomingMessageJuick(fromUser, from, msg.getBody(), attachment);
+ if (msg.getTo().equals(jid)) {
+ result = incomingMessageJuick(user_from, msg.getFrom(), StringUtils.defaultString(msg.getBody()), attachment);
+ if (result != null) {
+ router.send(result);
+ }
} else {
- if (msg.getTo().equals(jid)) {
- incomingMessageJuick(user_from, msg.getFrom(), StringUtils.defaultString(msg.getBody()), attachment);
- } else {
- int uid_to = userService.getUIDbyName(msg.getTo().getLocal());
-
- if (uid_to == 0) {
- Message reply = new Message();
- reply.setFrom(msg.getTo());
- reply.setTo(msg.getFrom());
- reply.setType(Message.Type.ERROR);
- reply.setId(msg.getId());
- reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND));
- router.send(ClientMessage.from(reply));
- return;
- }
+ int uid_to = userService.getUIDbyName(msg.getTo().getLocal());
+
+ if (uid_to == 0) {
+ Message reply = new Message();
+ reply.setFrom(msg.getTo());
+ reply.setTo(msg.getFrom());
+ reply.setType(Message.Type.ERROR);
+ reply.setId(msg.getId());
+ reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND));
+ return ClientMessage.from(reply);
+ }
- boolean success = false;
- if (!userService.isInBLAny(uid_to, user_from.getUid())) {
- success = pmQueriesService.createPM(user_from.getUid(), uid_to, msg.getBody());
- }
+ boolean success = false;
+ if (!userService.isInBLAny(uid_to, user_from.getUid())) {
+ success = pmQueriesService.createPM(user_from.getUid(), uid_to, msg.getBody());
+ }
- if (success) {
- com.juick.Message pm = new com.juick.Message();
- pm.setUser(user_from);
- pm.setTo(userService.getUserByUID(uid_to).get());
- pm.setText(msg.getBody());
- applicationEventPublisher.publishEvent(new MessageEvent(this, pm));
- } else {
- Message reply = new Message();
- reply.setFrom(msg.getTo());
- reply.setTo(msg.getFrom());
- reply.setType(Message.Type.ERROR);
- reply.setId(msg.getId());
- reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.NOT_ALLOWED));
- router.send(ClientMessage.from(reply));
- }
+ if (success) {
+ com.juick.Message pm = new com.juick.Message();
+ pm.setUser(user_from);
+ pm.setTo(userService.getUserByUID(uid_to).get());
+ pm.setText(msg.getBody());
+ applicationEventPublisher.publishEvent(new MessageEvent(this, pm));
+ } else {
+ Message reply = new Message();
+ reply.setFrom(msg.getTo());
+ reply.setTo(msg.getFrom());
+ reply.setType(Message.Type.ERROR);
+ reply.setId(msg.getId());
+ reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.NOT_ALLOWED));
+ return ClientMessage.from(reply);
}
}
} catch (Exception e1) {
@@ -616,12 +622,13 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
}
}
} else {
- router.send(ClientMessage.from(msg));
+ return ClientMessage.from(msg);
}
+ return result;
}
- private void incomingMessageJuick(User user_from, Jid from, String command, @Nonnull URI attachment) {
+ private ClientMessage incomingMessageJuick(User user_from, Jid from, String command, @Nonnull URI attachment) {
if (StringUtils.isBlank(command) && attachment.toString().isEmpty()) {
- return;
+ return null;
}
int commandlen = command.length();
@@ -634,12 +641,12 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
try {
CommandResult result = commandsManager.processCommand(user_from, command.trim(), attachment);
if (StringUtils.isNotBlank(result.getText())) {
- sendReply(from, result.getText());
+ return makeReply(from, result.getText());
}
} catch (Exception e) {
- sendReply(from, "Error processing command");
+ return makeReply(from, "Error processing command");
}
-
+ return null;
}
@Override
@@ -651,7 +658,10 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
}
} else if (xmlValue instanceof Message) {
Message msg = (Message) xmlValue;
- incomingMessage(msg);
+ ClientMessage result = incomingMessage(msg);
+ if (result != null) {
+ router.send(result);
+ }
} else if (xmlValue instanceof IQ) {
IQ iq = (IQ) xmlValue;
router.send(iq);
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 d30b1303..43bc119e 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
@@ -58,6 +58,9 @@ import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.stanza.model.Stanza;
+import rocks.xmpp.core.stanza.model.StanzaError;
+import rocks.xmpp.core.stanza.model.client.ClientMessage;
+import rocks.xmpp.core.stanza.model.errors.Condition;
import rocks.xmpp.core.stanza.model.server.ServerMessage;
import javax.inject.Inject;
@@ -533,13 +536,30 @@ public class ServerTests {
renhaId = userService.createUser("renha", "umnnbt");
Jid from = Jid.of("renha@serverstorageisfull.tld");
jdbcTemplate.update("INSERT INTO jids(user_id,jid,active) VALUES(?,?,?)", renhaId, from.toEscapedString(), 1);
- String xmlMessage = "<message xmlns=\"jabber:server\" from=\"renha@serverstorageisfull.tld\" to=\"" + botJid.toEscapedString() + "\" type=\"error\"><body>Reply by @LexX</body><juick xmlns=\"http://juick.com/message\" mid=\"2885759\" privacy=\"1\" replyto=\"0\" rid=\"8\" ts=\"2017-10-10 07:41:10\"><body>Похоже нынче можно публично заявлять о своем веганстве. </body><user xmlns=\"http://juick.com/user\" uname=\"LexX\" uid=\"6340\"></user></juick><error type=\"wait\"><resource-constraint xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"></resource-constraint><text xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\">Your contact offline message queue is full. The message has been discarded.</text></error></message>";
+ rocks.xmpp.core.stanza.model.Message xmppMessage = new rocks.xmpp.core.stanza.model.Message();
+ xmppMessage.setType(rocks.xmpp.core.stanza.model.Message.Type.ERROR);
+ xmppMessage.setFrom(from);
+ xmppMessage.setTo(botJid);
+ StanzaError err = new StanzaError(StanzaError.Type.CANCEL, Condition.RESOURCE_CONSTRAINT);
+ xmppMessage.setError(err);
Function<Integer, Boolean> isActive = f -> jdbcTemplate.queryForObject("SELECT active FROM jids WHERE user_id=?", Integer.class, f) == 1;
assertThat(isActive.apply(renhaId), equalTo(true));
- server.onStanzaReceived(xmlMessage);
+ ClientMessage result = router.incomingMessage(xmppMessage);
+ assertNull(result);
assertThat(isActive.apply(renhaId), equalTo(false));
- commandsManager.processCommand(userService.getUserByJID(from.asBareJid().toEscapedString()), "On", URI.create(StringUtils.EMPTY));
+ xmppMessage.setError(null);
+ xmppMessage.setType(rocks.xmpp.core.stanza.model.Message.Type.CHAT);
+ xmppMessage.setBody("On");
+ result = router.incomingMessage(xmppMessage);
+ assertThat(result.getBody(), equalTo("XMPP notifications are activated"));
assertTrue(isActive.apply(renhaId));
+ xmppMessage.setBody("*test test");
+ result = router.incomingMessage(xmppMessage);
+ assertThat(result.getBody(), startsWith("New message posted"));
+ xmppMessage.setFrom(Jid.of(String.valueOf(renhaId), "uid.juick.com", "perl"));
+ xmppMessage.setBody("PING");
+ result = router.incomingMessage(xmppMessage);
+ assertThat(result.getBody(), equalTo("PONG"));
}
@Test
public void botCommandsTests() throws Exception {