From 42e2a727c2bc15ac15c9a3af66f126dbefe508a2 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 30 Mar 2018 12:47:51 +0300 Subject: server: process attachments correctly --- .../main/java/com/juick/server/XMPPConnection.java | 53 ++++++++++++++-------- 1 file changed, 33 insertions(+), 20 deletions(-) (limited to 'juick-server/src/main/java/com/juick/server/XMPPConnection.java') diff --git a/juick-server/src/main/java/com/juick/server/XMPPConnection.java b/juick-server/src/main/java/com/juick/server/XMPPConnection.java index 87164755..942b4787 100644 --- a/juick-server/src/main/java/com/juick/server/XMPPConnection.java +++ b/juick-server/src/main/java/com/juick/server/XMPPConnection.java @@ -23,9 +23,11 @@ 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.xmpp.s2s.BasicXmppSession; -import com.juick.server.helpers.UserInfo; import com.juick.server.xmpp.s2s.StanzaListener; import com.juick.service.*; import com.juick.util.MessageUtils; @@ -65,7 +67,6 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import java.io.IOException; import java.io.StringWriter; -import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; @@ -104,6 +105,8 @@ public class XMPPConnection implements StanzaListener, NotificationListener { private boolean isXmppDisabled; @Value("${upload_tmp_dir:#{systemEnvironment['TEMP'] ?: '/tmp'}}") private String tmpDir; + @Value("${img_path:#{systemEnvironment['TEMP'] ?: '/tmp'}}") + private String imgDir; @Inject private MessagesService messagesService; @@ -570,20 +573,20 @@ public class XMPPConnection implements StanzaListener, NotificationListener { } } - public boolean incomingMessage(Message msg) { + public void incomingMessage(Message msg) { 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 true; + return; } } - return false; + return; } if (StringUtils.isBlank(msg.getBody())) { - return false; + return; } String username = msg.getTo().getLocal(); @@ -601,14 +604,15 @@ public class XMPPConnection implements StanzaListener, NotificationListener { reply.setType(Message.Type.CHAT); reply.setBody("Для того, чтобы начать пользоваться сервисом, пожалуйста пройдите быструю регистрацию: 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."); s2s(ClientMessage.from(reply)); - return true; + return; } if (username.equals(jid.getLocal())) { try { - return incomingMessageJuick(user_from, msg.getFrom(), msg.getBody().trim()); - } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { - return false; + OobX oobX = msg.getExtension(OobX.class); + incomingMessageJuick(user_from, msg.getFrom(), msg.getBody().trim(), oobX.getUri()); + } catch (Exception e) { + return; } } @@ -622,7 +626,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { reply.setId(msg.getId()); reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND)); s2s(ClientMessage.from(reply)); - return true; + return; } boolean success = false; @@ -646,9 +650,9 @@ public class XMPPConnection implements StanzaListener, NotificationListener { s2s(ClientMessage.from(reply)); } - return false; + return; } - public boolean incomingMessageJuick(User user_from, Jid from, String command) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { + public com.juick.Message incomingMessageJuick(User user_from, Jid from, String command, URI attachment) throws Exception { int commandlen = command.length(); // COMPATIBILITY @@ -656,18 +660,29 @@ public class XMPPConnection implements StanzaListener, NotificationListener { command = command.substring(3).trim(); } - Optional result = commandsManager.processCommand(user_from, from, command); + Optional result = commandsManager.processCommand(user_from, from, command, attachment); if (result.isPresent()) { sendReply(from, result.get()); + com.juick.Message msg = new com.juick.Message(); + msg.setText(result.get()); + return msg; } else { // new message List tags = tagService.fromString(command, false); String body = command.substring(TagUtils.toString(tags).length()); - int mid = messagesService.createMessage(user_from.getUid(), body, null, tags); + String attachmentType = attachment != null ? attachment.toString().substring(attachment.toString().length() - 3) : null; + int mid = messagesService.createMessage(user_from.getUid(), body, attachmentType, tags); subscriptionService.subscribeMessage(mid, user_from.getUid()); - applicationEventPublisher.publishEvent(new MessageEvent(this, messagesService.getMessage(mid))); + if (StringUtils.isNotEmpty(attachmentType)) { + String attachmentFName = attachment.getScheme().equals("juick") ? attachment.getPath() + : HttpUtils.downloadImage(attachment.toURL(), tmpDir); + String fname = String.format("%d.%s", mid, attachmentType); + ImageUtils.saveImageWithPreviews(attachmentFName, fname, tmpDir, imgDir); + } + com.juick.Message msg = messagesService.getMessage(mid); + applicationEventPublisher.publishEvent(new MessageEvent(this, msg)); + return msg; } - return true; } @Override @@ -679,9 +694,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener { } } else if (xmlValue instanceof Message) { Message msg = (Message) xmlValue; - if (!incomingMessage(msg)) { - router.send(msg); - } + incomingMessage(msg); } else if (xmlValue instanceof IQ) { IQ iq = (IQ) xmlValue; router.send(iq); -- cgit v1.2.3