diff options
Diffstat (limited to 'src/main/java/com/juick/server/CommandsManager.java')
-rw-r--r-- | src/main/java/com/juick/server/CommandsManager.java | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/src/main/java/com/juick/server/CommandsManager.java b/src/main/java/com/juick/server/CommandsManager.java index fa3c5537..f6f29941 100644 --- a/src/main/java/com/juick/server/CommandsManager.java +++ b/src/main/java/com/juick/server/CommandsManager.java @@ -25,6 +25,7 @@ import com.juick.model.CommandResult; import com.juick.model.TagStats; import com.juick.server.helpers.annotation.UserCommand; import com.juick.server.util.HttpUtils; +import com.juick.server.www.WebApp; import com.juick.service.*; import com.juick.service.activities.DeleteMessageEvent; import com.juick.service.component.*; @@ -34,12 +35,15 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.reflect.MethodUtils; import org.apache.commons.lang3.tuple.Pair; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Component; import javax.annotation.Nonnull; import javax.inject.Inject; +import java.io.IOException; import java.lang.reflect.Method; import java.net.URI; import java.util.*; @@ -53,6 +57,7 @@ import java.util.stream.Collectors; */ @Component public class CommandsManager { + private static final Logger logger = LoggerFactory.getLogger(CommandsManager.class); @Inject private MessagesService messagesService; @Inject @@ -75,6 +80,8 @@ public class CommandsManager { private ApplicationEventPublisher applicationEventPublisher; @Inject private ImagesService imagesService; + @Inject + private WebApp webApp; public CommandResult processCommand(@Nonnull User user, String data, @Nonnull URI attachment) throws Exception { if (!user.isAnonymous()) { @@ -121,12 +128,16 @@ public class CommandsManager { : HttpUtils.downloadImage(attachment.toURL(), tmpDir).getHost(); attachmentType = attachmentFName.substring(attachmentFName.length() - 3); } + if (StringUtils.isEmpty(body) && !haveAttachment) { + return CommandResult.fromString("Empty message"); + } int mid = messagesService.createMessage(user.getUid(), body, attachmentType, tags.getRight()); if (haveAttachment) { String fname = String.format("%d.%s", mid, attachmentType); imagesService.saveImageWithPreviews(attachmentFName, fname); } Message msg = messagesService.getMessage(mid).orElseThrow(IllegalStateException::new); + msg.getUser().setAvatar(webApp.getAvatarUrl(msg.getUser())); subscriptionService.subscribeMessage(msg, user); applicationEventPublisher.publishEvent(new MessageReadEvent(this, user, msg)); @@ -160,7 +171,7 @@ public class CommandsManager { String body = arguments[1]; User user_to = userService.getUserByName(arguments[0]); - + user_to.setAvatar(webApp.getAvatarUrl(user_to)); if (!user_to.isAnonymous()) { if (!userService.isInBLAny(user_to.getUid(), user_from.getUid())) { if (pmQueriesService.createPM(user_from.getUid(), user_to.getUid(), body)) { @@ -523,12 +534,22 @@ public class CommandsManager { String attachmentFName = null; String attachmentType = null; if (haveAttachment) { - attachmentFName = attachment.getScheme().equals("juick") ? attachment.getHost() - : HttpUtils.downloadImage(attachment.toURL(), tmpDir).getHost(); - attachmentType = attachmentFName.substring(attachmentFName.length() - 3); + if (attachment.getScheme().equals("juick")) { + attachmentFName = attachment.getHost(); + attachmentType = attachmentFName.substring(attachmentFName.length() - 3); + } else { + try { + attachmentFName = HttpUtils.downloadImage(attachment.toURL(), tmpDir).getHost(); + attachmentType = attachmentFName.substring(attachmentFName.length() - 3); + } catch (IOException e) { + logger.warn("Can not download {}", attachment.toURL()); + } + } } - int newrid = messagesService.createReply(mid, rid, user, txt, attachmentType); - if (haveAttachment) { + boolean attachmentProcessed = !haveAttachment || StringUtils.isNotEmpty(attachmentType); + String messageText = attachmentProcessed ? txt : String.format("%s %s", txt, attachment.toASCIIString()); + int newrid = messagesService.createReply(mid, rid, user, messageText, attachmentType); + if (haveAttachment && attachmentProcessed) { String fname = String.format("%d-%d.%s", mid, newrid, attachmentType); imagesService.saveImageWithPreviews(attachmentFName, fname); } @@ -537,6 +558,7 @@ public class CommandsManager { Message original = messagesService.getMessage(mid).orElseThrow(IllegalStateException::new); subscriptionService.subscribeMessage(original, user); Message reply = messagesService.getReply(mid, newrid); + reply.getUser().setAvatar(webApp.getAvatarUrl(reply.getUser())); applicationEventPublisher.publishEvent(new MessageEvent(this, reply, subscriptionService.getUsersSubscribedToComments(original, reply))); return CommandResult.build(reply,"Reply posted.\n#" + mid + "/" + newrid + " " + "https://juick.com/m/" + mid + "#" + newrid, |