aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2019-05-15 12:16:44 +0300
committerGravatar Vitaly Takmazov2019-05-15 12:16:44 +0300
commit4ad62bb9635f7177ecd8de21d255396b54c04985 (patch)
treee0da760d4a94e59c0b78697d11778d1e709af063 /src/main/java/com/juick
parentbaf420b738723c519fb8c06281bfc9c72d478a30 (diff)
allow to empty message only when image present
Diffstat (limited to 'src/main/java/com/juick')
-rw-r--r--src/main/java/com/juick/server/TelegramBotManager.java28
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);