diff options
author | 2018-04-05 14:03:09 +0300 | |
---|---|---|
committer | 2018-04-05 14:03:09 +0300 | |
commit | 534d3c6f8ab31c0d41e6a6980f8cbc3f25e8d1b9 (patch) | |
tree | 9da4ecad27afa9750a6c0369e181e287944473bf | |
parent | 2f53f75e584e0522c8f4783bc28df4ad6eb3033d (diff) |
server: commands are not depended on xmpp now
7 files changed, 20 insertions, 30 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 a277a4a4..ffa145d9 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,17 +17,13 @@ package com.juick.server; -import com.juick.Tag; import com.juick.User; 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.UserInfo; -import com.juick.server.util.HttpUtils; -import com.juick.server.util.ImageUtils; -import com.juick.server.util.TagUtils; import com.juick.server.helpers.CommandResult; +import com.juick.server.helpers.UserInfo; import com.juick.server.xmpp.s2s.BasicXmppSession; import com.juick.server.xmpp.s2s.StanzaListener; import com.juick.service.*; @@ -79,7 +75,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; -import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; @@ -182,11 +177,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { if (jid.getDomain().equals(router.getDomain().toEscapedString()) || jid.equals(this.jid)) { com.juick.Message jmsg = message.getExtension(com.juick.Message.class); if (jmsg != null) { - if (jid.getLocal().equals("recomm")) { - User fromUser = jmsg.getUser(); - com.juick.Message msg = messagesService.getMessage(jmsg.getMid()); - applicationEventPublisher.publishEvent(new LikeEvent(this, fromUser, msg)); - } else if (jid.getLocal().equals("pm")) { + if (jid.getLocal().equals("pm")) { applicationEventPublisher.publishEvent(new MessageEvent(this, jmsg)); } else { if (jmsg.getRid() > 0) { @@ -600,10 +591,10 @@ 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) + private void incomingMessageJuick(User user_from, Jid from, String command, @Nonnull URI attachment) throws Exception { if (StringUtils.isBlank(command) && attachment.toString().isEmpty()) { - return null; + return; } int commandlen = command.length(); @@ -617,7 +608,6 @@ public class XMPPConnection implements StanzaListener, NotificationListener { if (StringUtils.isNotBlank(result.getText())) { sendReply(from, result.getText()); } - return result.getNewMessage(); } @Override 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 0de3d882..1628afc0 100644 --- a/juick-server/src/main/java/com/juick/server/MessengerManager.java +++ b/juick-server/src/main/java/com/juick/server/MessengerManager.java @@ -51,7 +51,7 @@ public class MessengerManager implements ApplicationListener<MessageEvent> { @Inject private MessengerService messengerService; @Inject - private XMPPConnection xmppConnection; + private CommandsManager commandsManager; @Value("${fb_page_access_token:12345678}") private String facebookPageAccessToken; @@ -98,7 +98,7 @@ public class MessengerManager implements ApplicationListener<MessageEvent> { logger.info("Received text message from '{}' at '{}' with content: {} (mid: {})", senderId, timestamp, text, messageId); try { - xmppConnection.incomingMessageJuick(user_from, null, text, URI.create(StringUtils.EMPTY)); + commandsManager.processCommand(user_from, null, 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 9fcf08da..54edb5db 100644 --- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java +++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java @@ -80,7 +80,7 @@ public class TelegramBotManager implements NotificationListener { @Inject private UserService userService; @Inject - private XMPPConnection xmppConnection; + private CommandsManager commandsManager; @Value("${upload_tmp_dir:#{systemEnvironment['TEMP'] ?: '/tmp'}}") private String tmpDir; @@ -184,7 +184,7 @@ public class TelegramBotManager implements NotificationListener { int rid = Integer.valueOf(uriComponents.getFragment()); prefix = String.format("#%d/%d ", mid, rid); } - xmppConnection.incomingMessageJuick(user_from, null,prefix + text, attachment); + commandsManager.processCommand(user_from, null,prefix + text, attachment); telegramNotify(message.from().id().longValue(), "Reply sent", StringUtils.EMPTY); } else { logger.warn("invalid path: {}", path); @@ -201,7 +201,7 @@ public class TelegramBotManager implements NotificationListener { "Can not reply to this message", replyMessage.messageId(), StringUtils.EMPTY); } } else { - xmppConnection.incomingMessageJuick(user_from, null, text, attachment); + commandsManager.processCommand(user_from, null, text, attachment); telegramNotify(message.from().id().longValue(), "Message sent", 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 fc7035ab..8475fd40 100644 --- a/juick-server/src/main/java/com/juick/server/WebsocketManager.java +++ b/juick-server/src/main/java/com/juick/server/WebsocketManager.java @@ -70,7 +70,7 @@ public class WebsocketManager extends TextWebSocketHandler { @Inject private ObjectMapper jsonMapper; @Inject - private XMPPConnection xmppConnection; + private CommandsManager commandsManager; @Override @@ -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(); - xmppConnection.incomingMessageJuick(draft.getUser(), null, draft.getText(), URI.create(attachmentFileName)); + commandsManager.processCommand(draft.getUser(), null, 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 ef0c7dd9..e949dad7 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(); } } - xmppConnection.incomingMessageJuick(visitor, null, body, attachmentFName); + commandsManager.processCommand(visitor, null, body, attachmentFName); } @PostMapping("/upload") public String doUploadFile(@RequestParam(required = true) MultipartFile attach) { @@ -163,7 +163,7 @@ public class Post { } } - return xmppConnection.incomingMessageJuick(visitor, null, body, attachmentFName); + return commandsManager.processCommand(visitor, null, body, attachmentFName).getNewMessage(); } 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); - xmppConnection.incomingMessageJuick(visitor, null, body[0], attachmentUri); + commandsManager.processCommand(visitor, null, body[0], attachmentUri); } else { logger.info("not registered: {}", from); } 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 d6617cc1..a30403b0 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 @@ -551,9 +551,9 @@ 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 = router.incomingMessageJuick(user, Jid.of("test@localhost"), "*yo yoyo", URI.create("http://static.juick.com/settings/facebook.png")); + Message msg = commandsManager.processCommand(user, Jid.of("test@localhost"), "*yo yoyo", URI.create("http://static.juick.com/settings/facebook.png")).getNewMessage(); assertThat(msg.getAttachmentType(), is("png")); - Message msgreply = router.incomingMessageJuick(user, Jid.of("test@localhost"), "#" + msg.getMid() + " yyy", HttpUtils.downloadImage(URI.create("http://static.juick.com/settings/xmpp.png").toURL(), tmpDir)); + 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(); assertThat(msgreply.getAttachmentType(), equalTo("png")); int mid = messagesService.createMessage(uid, "yoyo", null, Collections.singletonList(yo)); assertEquals("should be message", true, diff --git a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java index b93d6f57..07605830 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java @@ -149,7 +149,7 @@ public class NewMessage { subscriptionService.subscribeMessage(mid, visitor.getUid()); Message xmsg = new Message(); - xmsg.setFrom(Jid.of("juick@juick.com")); + xmsg.setFrom(botJid); xmsg.setType(Message.Type.CHAT); xmsg.setThread("juick-" + mid); @@ -182,7 +182,7 @@ public class NewMessage { xmsg.setBody("Reply by @" + jmsg.getUser().getName() + ":\n>" + quote + "\n" + body + "\n\n#" + mid + "/" + ridnew + " http://juick.com/" + mid + "#" + ridnew); - xmsg.setTo(Jid.of("juick@juick.com")); + xmsg.setTo(botJid); xmpp.send(xmsg); } else { logger.warn("XMPP unavailable"); @@ -218,8 +218,8 @@ public class NewMessage { if (pmQueriesService.createPM(visitor.getUid(), userTo.getUid(), body)) { if (xmpp.isConnected()) { Message msg = new Message(); - msg.setFrom(Jid.of("juick@juick.com")); - msg.setTo(Jid.of("pm@juick.com")); + msg.setFrom(botJid); + msg.setTo(botJid.withLocal("pm")); com.juick.Message jmsg = new com.juick.Message(); jmsg.setUser(visitor); jmsg.setText(body); |