From 4ad62bb9635f7177ecd8de21d255396b54c04985 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 15 May 2019 12:16:44 +0300 Subject: allow to empty message only when image present --- .../java/com/juick/server/TelegramBotManager.java | 28 ++++++++++++++++------ 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/juick/server/TelegramBotManager.java b/src/main/java/com/juick/server/TelegramBotManager.java index 1adf16f7..54247076 100644 --- a/src/main/java/com/juick/server/TelegramBotManager.java +++ b/src/main/java/com/juick/server/TelegramBotManager.java @@ -134,15 +134,18 @@ public class TelegramBotManager implements NotificationListener { } User user_from = userService.getUserByUID(telegramService.getUser(message.chat().id())).orElse(AnonymousUser.INSTANCE); logger.info("Found juick user {}", user_from.getUid()); - Optional> originalMessage = messagesService.findMessageByProperty("durovId", + Optional> originalMessageData = messagesService.findMessageByProperty("durovId", String.valueOf(message.messageId())); - if (originalMessage.isPresent()) { - int mid = originalMessage.get().getLeft(); - int rid = originalMessage.get().getRight(); + if (originalMessageData.isPresent()) { + int mid = originalMessageData.get().getLeft(); + int rid = originalMessageData.get().getRight(); // TODO: this is copypaste from api, need switch to api - User author = rid == 0 ? messagesService.getMessageAuthor(mid) : messagesService.getReply(mid, rid).getUser(); - if (user_from.equals(author)) { - if (messagesService.updateMessage(mid, rid, message.text())) { + com.juick.Message originalMessage = rid == 0 ? messagesService.getMessage(mid).orElseThrow(IllegalStateException::new) + : messagesService.getReply(mid, rid); + User author = originalMessage.getUser(); + String newMessageText = StringUtils.defaultString(message.text()); + if (user_from.equals(author) && canUpdateMessage(originalMessage, newMessageText)) { + if (messagesService.updateMessage(mid, rid, newMessageText)) { telegramNotify(message.chat().id(), "Message updated", new com.juick.Message()); return; } @@ -257,6 +260,17 @@ public class TelegramBotManager implements NotificationListener { } } + /* + validate user input + */ + private boolean canUpdateMessage(com.juick.Message message, String newData) { + if (StringUtils.isEmpty(newData)) { + // allow empty text only when image is present + return StringUtils.isNotEmpty(message.getAttachmentType()); + } + return true; + } + private void executeCommand(Integer messageId, Long userId, User user_from, String text, URI attachment) { try { CommandResult result = commandsManager.processCommand(user_from, text, attachment); -- cgit v1.2.3