diff options
author | Vitaly Takmazov | 2018-04-09 17:59:28 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-04-09 17:59:28 +0300 |
commit | 66fd14b44044b2b8004ddd73789345e5c7f26ae2 (patch) | |
tree | bce1a7915e88f7cf2eea4e5f268420cdf5f2e89e /juick-server-xmpp/src | |
parent | 11c210116aaa3f92cc30cb577b289c4a36ece4c7 (diff) |
server: new xmpp tests
Diffstat (limited to 'juick-server-xmpp/src')
-rw-r--r-- | juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java | 130 |
1 files changed, 70 insertions, 60 deletions
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); |