From a5f045a3780ef8530afa7493cf090771b8fdcfe1 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 3 Apr 2018 17:55:46 +0300 Subject: refactoring: all messages are processed with XMPPConnection --- .../src/main/java/com/juick/server/MessengerManager.java | 2 +- .../main/java/com/juick/server/TelegramBotManager.java | 9 +++++---- .../src/main/java/com/juick/server/WebsocketManager.java | 2 +- .../src/main/java/com/juick/server/api/Post.java | 16 ++++++++-------- .../test/java/com/juick/server/tests/ServerTests.java | 4 ++-- 5 files changed, 17 insertions(+), 16 deletions(-) (limited to 'juick-server') 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 ca206bd4..55330c3d 100644 --- a/juick-server/src/main/java/com/juick/server/MessengerManager.java +++ b/juick-server/src/main/java/com/juick/server/MessengerManager.java @@ -97,7 +97,7 @@ public class MessengerManager implements ApplicationListener { logger.info("Received text message from '{}' at '{}' with content: {} (mid: {})", senderId, timestamp, text, messageId); try { - xmppConnection.processMessage(user_from, text, null); + xmppConnection.incomingMessageJuick(user_from, null, text, null); } 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 020d2433..320ab9ae 100644 --- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java +++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java @@ -53,6 +53,7 @@ import org.springframework.web.util.UriComponentsBuilder; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; +import java.net.URI; import java.net.URL; import java.util.*; @@ -129,7 +130,7 @@ public class TelegramBotManager implements NotificationListener { if (user_from.getUid() == 0) { telegramSignupNotify(message.from().id().longValue(), userService.getSignUpHashByTelegramID(message.from().id().longValue(), username)); } else { - String attachment = StringUtils.EMPTY; + URI attachment = URI.create(StringUtils.EMPTY); if (message.photo() != null) { String fileId = Arrays.stream(message.photo()).max(Comparator.comparingInt(PhotoSize::fileSize)) .orElse(new PhotoSize()).fileId(); @@ -149,7 +150,7 @@ public class TelegramBotManager implements NotificationListener { if (StringUtils.isBlank(text)) { text = StringUtils.EMPTY; } - if (StringUtils.isNotEmpty(text) || StringUtils.isNotEmpty(attachment)) { + if (StringUtils.isNotEmpty(text) || StringUtils.isNotEmpty(attachment.toString())) { if (text.equalsIgnoreCase("LOGIN") || text.equalsIgnoreCase("PING") || text.equalsIgnoreCase("HELP") @@ -183,7 +184,7 @@ public class TelegramBotManager implements NotificationListener { int rid = Integer.valueOf(uriComponents.getFragment()); prefix = String.format("#%d/%d ", mid, rid); } - xmppConnection.processMessage(user_from, prefix + text, attachment); + xmppConnection.incomingMessageJuick(user_from, null,prefix + text, attachment); telegramNotify(message.from().id().longValue(), "Reply sent", StringUtils.EMPTY); } else { logger.warn("invalid path: {}", path); @@ -200,7 +201,7 @@ public class TelegramBotManager implements NotificationListener { "Can not reply to this message", replyMessage.messageId(), StringUtils.EMPTY); } } else { - xmppConnection.processMessage(user_from, text, attachment); + xmppConnection.incomingMessageJuick(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 498bf064..fc7035ab 100644 --- a/juick-server/src/main/java/com/juick/server/WebsocketManager.java +++ b/juick-server/src/main/java/com/juick/server/WebsocketManager.java @@ -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.processMessage(draft.getUser(), draft.getText(), attachmentFileName); + xmppConnection.incomingMessageJuick(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 312df52d..7c8fb2ea 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 @@ -101,9 +101,9 @@ public class Post { } body = body.replace("\r", StringUtils.EMPTY); - String attachmentFName = HttpUtils.receiveMultiPartFile(attach, tmpDir); + URI attachmentFName = HttpUtils.receiveMultiPartFile(attach, tmpDir); - if (StringUtils.isBlank(attachmentFName) && img != null && img.length() > 10) { + if (StringUtils.isBlank(attachmentFName.toString()) && img != null && img.length() > 10) { try { URL imgUrl = new URL(img); attachmentFName = HttpUtils.downloadImage(imgUrl, tmpDir); @@ -112,11 +112,11 @@ public class Post { throw new HttpBadRequestException(); } } - xmppConnection.processMessage(visitor, body, attachmentFName); + xmppConnection.incomingMessageJuick(visitor, null, body, attachmentFName); } @PostMapping("/upload") public String doUploadFile(@RequestParam(required = true) MultipartFile attach) { - return HttpUtils.receiveMultiPartFile(attach, tmpDir); + return HttpUtils.receiveMultiPartFile(attach, tmpDir).toString(); } @RequestMapping(value = "/comment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @@ -158,9 +158,9 @@ public class Post { throw new HttpForbiddenException(); } - String attachmentFName = HttpUtils.receiveMultiPartFile(attach, tmpDir); + URI attachmentFName = HttpUtils.receiveMultiPartFile(attach, tmpDir); - if (StringUtils.isBlank(attachmentFName) && img != null && img.length() > 10) { + if (StringUtils.isBlank(attachmentFName.toString()) && img != null && img.length() > 10) { try { attachmentFName = HttpUtils.downloadImage(new URL(img), tmpDir); } catch (Exception e) { @@ -169,7 +169,7 @@ public class Post { } } - return xmppConnection.processMessage(visitor, body, attachmentFName); + return xmppConnection.incomingMessageJuick(visitor, null, body, attachmentFName); } Session session = Session.getDefaultInstance(new Properties()); @@ -231,7 +231,7 @@ public class Post { body[0] = rid > 0 ? String.format("#%d/%d %s", mid, rid, body[0]) : String.format("#%d %s", mid, body[0]); } - xmppConnection.processMessage(visitor, body[0], attachmentFName[0]); + xmppConnection.incomingMessageJuick(visitor, null, body[0], URI.create(String.format("juick://%s", attachmentFName[0]))); } 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 28f7c645..2930d61b 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 @@ -524,7 +524,7 @@ public class ServerTests { assertThat(server.getJid(), equalTo(jid)); } @Test - public void botIsUpAndProcessingResourceConstraints() { + public void botIsUpAndProcessingResourceConstraints() throws Exception { int renhaId; renhaId = userService.createUser("renha", "umnnbt"); Jid from = Jid.of("renha@serverstorageisfull.tld"); @@ -552,7 +552,7 @@ public class ServerTests { 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")); assertThat(msg.getAttachmentType(), is("png")); - Message msgreply = router.incomingMessageJuick(user, Jid.of("test@localhost"), "#" + msg.getMid() + " yyy", URI.create("juick://" + HttpUtils.downloadImage(URI.create("http://static.juick.com/settings/xmpp.png").toURL(), tmpDir))); + 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)); assertThat(msgreply.getAttachmentType(), equalTo("png")); int mid = messagesService.createMessage(uid, "yoyo", null, Collections.singletonList(yo)); assertEquals("should be message", true, -- cgit v1.2.3