diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/com/juick/server/TelegramBotManager.java | 28 |
1 files 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<Pair<Integer, Integer>> originalMessage = messagesService.findMessageByProperty("durovId", + Optional<Pair<Integer, Integer>> 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); |