diff options
author | Vitaly Takmazov | 2018-04-16 12:34:37 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-04-16 12:34:37 +0300 |
commit | 73afea037564c2565744d9e12a078dcf0a230a71 (patch) | |
tree | f7a752ae1ee50a490bc3ee70745dfba525972dd0 /juick-server/src/main | |
parent | 4bcb69153187f73d5a981db4a9c403622768af1b (diff) |
server: mark reply as read when sent over telegram
Diffstat (limited to 'juick-server/src/main')
-rw-r--r-- | juick-server/src/main/java/com/juick/server/TelegramBotManager.java | 43 |
1 files changed, 24 insertions, 19 deletions
diff --git a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java index 95d91679..7a377d5a 100644 --- a/juick-server/src/main/java/com/juick/server/TelegramBotManager.java +++ b/juick-server/src/main/java/com/juick/server/TelegramBotManager.java @@ -25,7 +25,6 @@ import com.juick.server.component.SubscribeEvent; import com.juick.server.helpers.CommandResult; import com.juick.server.util.HttpUtils; import com.juick.service.MessagesService; -import com.juick.service.SubscriptionService; import com.juick.service.TelegramService; import com.juick.service.UserService; import com.juick.util.MessageUtils; @@ -51,6 +50,7 @@ import org.springframework.stereotype.Component; import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; +import javax.annotation.Nonnull; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; @@ -163,7 +163,7 @@ public class TelegramBotManager implements NotificationListener { String msgUrl = "http://juick.com/login?hash=" + userService.getHashByUID(user_from.getUid()); String msg = String.format("Hi, %s!\nYou can post messages and images to Juick there.\n" + "Tap to [log into website](%s) to get more info", user_from.getName(), msgUrl); - telegramNotify(message.from().id().longValue(), msg, StringUtils.EMPTY); + telegramNotify(message.from().id().longValue(), msg, new com.juick.Message()); } else { Message replyMessage = message.replyToMessage(); if (replyMessage != null) { @@ -192,7 +192,7 @@ public class TelegramBotManager implements NotificationListener { result = commandsManager.processCommand(user_from, prefix + text, attachment); String messageTxt = StringUtils.isNotEmpty(result.getMarkdown()) ? result.getMarkdown() : "Unknown error or unsupported command"; - telegramNotify(message.from().id().longValue(), messageTxt, StringUtils.EMPTY); + telegramNotify(message.from().id().longValue(), messageTxt, new com.juick.Message()); } catch (Exception e) { logger.warn("telegram exception", e); } @@ -204,19 +204,18 @@ public class TelegramBotManager implements NotificationListener { } } else { telegramNotify(message.from().id().longValue(), - "Can not reply to this message", replyMessage.messageId(), StringUtils.EMPTY); + "Can not reply to this message", replyMessage.messageId(), new com.juick.Message()); } } else { telegramNotify(message.from().id().longValue(), - "Can not reply to this message", replyMessage.messageId(), StringUtils.EMPTY); + "Can not reply to this message", replyMessage.messageId(), new com.juick.Message()); } } else { - CommandResult result = null; try { - result = commandsManager.processCommand(user_from, text, attachment); + CommandResult result = commandsManager.processCommand(user_from, text, attachment); String messageTxt = StringUtils.isNotEmpty(result.getMarkdown()) ? result.getMarkdown() : "Unknown error or unsupported command"; - telegramNotify(message.from().id().longValue(), messageTxt, StringUtils.EMPTY); + telegramNotify(message.from().id().longValue(), messageTxt, new com.juick.Message()); } catch (Exception e) { logger.warn("telegram reply exception", e); } @@ -231,11 +230,12 @@ public class TelegramBotManager implements NotificationListener { return e.offset() == 0 && e.type().equals(MessageEntity.Type.text_link) && e.length() == 2; } - public void telegramNotify(Long chatId, String msg, String attachment) { - telegramNotify(chatId, msg, 0, attachment); + public void telegramNotify(Long chatId, String msg, @Nonnull com.juick.Message source) { + telegramNotify(chatId, msg, 0, source); } - public void telegramNotify(Long chatId, String msg, Integer replyTo, String attachment) { + public void telegramNotify(Long chatId, String msg, Integer replyTo, @Nonnull com.juick.Message source) { + String attachment = MessageUtils.attachmentUrl(source); if (StringUtils.isEmpty(attachment)) { SendMessage telegramMessage = new SendMessage(chatId, msg); if (replyTo > 0) { @@ -245,7 +245,7 @@ public class TelegramBotManager implements NotificationListener { bot.execute(telegramMessage, new Callback<SendMessage, SendResponse>() { @Override public void onResponse(SendMessage request, SendResponse response) { - processTelegramResponse(chatId, response); + processTelegramResponse(chatId, response, source); } @Override public void onFailure(SendMessage request, IOException e) { @@ -263,7 +263,7 @@ public class TelegramBotManager implements NotificationListener { bot.execute(telegramPhoto, new Callback<SendPhoto, SendResponse>() { @Override public void onResponse(SendPhoto request, SendResponse response) { - processTelegramResponse(chatId, response); + processTelegramResponse(chatId, response, source); } @Override public void onFailure(SendPhoto request, IOException e) { @@ -273,7 +273,8 @@ public class TelegramBotManager implements NotificationListener { } } - private void processTelegramResponse(Long chatId, SendResponse response) { + private void processTelegramResponse(Long chatId, SendResponse response, com.juick.Message source) { + int userId = telegramService.getUser(chatId); if (!response.isOk()) { if (response.errorCode() == 403) { // remove from anonymous chat @@ -283,10 +284,9 @@ public class TelegramBotManager implements NotificationListener { logger.info("deleted {} chat", d); } ); - int userId = telegramService.getUser(chatId); if (userId > 0) { User userToDelete = userService.getUserByUID(userId) - .orElse(new User()); + .orElseThrow(IllegalStateException::new); boolean status = telegramService.deleteTelegramUser(userToDelete.getUid()); logger.info("deleting telegram id of @{} : {}", userToDelete.getName(), status); boolean chatStatus = telegramService.deleteChat(chatId); @@ -296,6 +296,11 @@ public class TelegramBotManager implements NotificationListener { logger.warn("error response, isOk: {}, errorCode: {}, description: {}", response.isOk(), response.errorCode(), response.description()); } + } else { + if (MessageUtils.isReply(source)) { + messagesService.setLastReadComment(userService.getUserByUID(userId) + .orElseThrow(IllegalStateException::new), source.getMid(), source.getRid()); + } } } @@ -329,7 +334,7 @@ public class TelegramBotManager implements NotificationListener { String fmsg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg, true)); telegramService.getTelegramIdentifiers( subscribedUsers - ).forEach(c -> telegramNotify(c, fmsg, MessageUtils.attachmentUrl(jmsg))); + ).forEach(c -> telegramNotify(c, fmsg, jmsg)); } else { String msg = String.format("[%s](%s) %s", MSG_LINK, msgUrl, formatPost(jmsg, true)); @@ -337,9 +342,9 @@ public class TelegramBotManager implements NotificationListener { List<Long> chats = telegramService.getChats(); // registered subscribed users - users.forEach(c -> telegramNotify(c, msg, MessageUtils.attachmentUrl(jmsg))); + users.forEach(c -> telegramNotify(c, msg, jmsg)); // anonymous - chats.stream().filter(u -> telegramService.getUser(u) == 0).forEach(c -> telegramNotify(c, msg, MessageUtils.attachmentUrl(jmsg))); + chats.stream().filter(u -> telegramService.getUser(u) == 0).forEach(c -> telegramNotify(c, msg, jmsg)); } } @Override |