From bf8f13488cc69bbf6aaf14e9e1884585ff5d5139 Mon Sep 17 00:00:00 2001 From: Pedro Mendes Date: Sun, 16 Jun 2024 17:09:29 -0300 Subject: [PATCH 1/5] fix: Check if values is null when saving chats objects --- .../altart/telegrambridge/config/Config.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/altart/telegrambridge/config/Config.java b/src/main/java/org/altart/telegrambridge/config/Config.java index ae8c546..658b631 100644 --- a/src/main/java/org/altart/telegrambridge/config/Config.java +++ b/src/main/java/org/altart/telegrambridge/config/Config.java @@ -157,13 +157,15 @@ public static List chatsFrom(List> chats_map) { List chats = new ArrayList<>(); for (Map chat : chats_map) { String id = (String) chat.get("id"); + Object threadObj = chat.get("thread"); Integer thread = null; - if (chat.get("thread") != null) { - thread = Integer.valueOf(String.valueOf(chat.get("thread"))); + if (threadObj != null) { + thread = Integer.valueOf(String.valueOf(threadObj)); } + Object pinnedMessageIdObj = chat.get("pinnedMessageId"); Integer pinnedMessageId = null; - if (chat.get("pinnedMessageId") != null) { - pinnedMessageId = Integer.valueOf(String.valueOf(chat.get("pinnedMessageId"))); + if (pinnedMessageIdObj != null) { + pinnedMessageId = Integer.valueOf(String.valueOf(pinnedMessageIdObj)); } chats.add(new Chat(id, thread, pinnedMessageId)); } @@ -175,8 +177,12 @@ public static List> chatsToMaps(List chats) { for (Chat chat : chats) { Map chat_map = new HashMap<>(); chat_map.put("id", chat.id); - chat_map.put("thread", String.valueOf(chat.thread)); - chat_map.put("pinnedMessageId", String.valueOf(chat.pinnedMessageId)); + if (chat.thread != null) { + chat_map.put("thread", String.valueOf(chat.thread)); + } + if (chat.pinnedMessageId != null) { + chat_map.put("pinnedMessageId", String.valueOf(chat.pinnedMessageId)); + } chats_map.add(chat_map); } return chats_map; From 25340249636889a3fd43dd06c53b081affe98534 Mon Sep 17 00:00:00 2001 From: Pedro Mendes Date: Sun, 16 Jun 2024 17:10:45 -0300 Subject: [PATCH 2/5] fix: If username is null get the first name of user Closes: #17 --- .../org/altart/telegrambridge/bot/feature/MessageListener.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java b/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java index 9334e35..cd9379f 100644 --- a/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java +++ b/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java @@ -65,6 +65,9 @@ public void onUpdateReceived(@NotNull Update update) { String text = message.getText(); if (text != null && !text.startsWith("/") && TelegramBridge.config.sendToChat) { String username = message.getFrom().getUserName(); + if (username == null) { + username = message.getFrom().getFirstName(); + } TelegramBridge.log.info("Telegram message received from " + username + ": " + text); for (Player player : Bukkit.getOnlinePlayers()) { if (player.hasPermission(Permissions.RECEIVE.getString())) { From 7fe29e469ded145039e40a420f537aa56a551aed Mon Sep 17 00:00:00 2001 From: Pedro Mendes Date: Sun, 16 Jun 2024 17:17:15 -0300 Subject: [PATCH 3/5] feat: Get first and last names --- .../org/altart/telegrambridge/bot/feature/MessageListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java b/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java index cd9379f..fe4b2c2 100644 --- a/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java +++ b/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java @@ -66,7 +66,7 @@ public void onUpdateReceived(@NotNull Update update) { if (text != null && !text.startsWith("/") && TelegramBridge.config.sendToChat) { String username = message.getFrom().getUserName(); if (username == null) { - username = message.getFrom().getFirstName(); + username = String.format("%s %s", message.getFrom().getFirstName(), message.getFrom().getLastName()); } TelegramBridge.log.info("Telegram message received from " + username + ": " + text); for (Player player : Bukkit.getOnlinePlayers()) { From 86987dd41b0a60c4e0b031ec5a642c38469ab6c7 Mon Sep 17 00:00:00 2001 From: Pedro Mendes Date: Sun, 16 Jun 2024 17:19:50 -0300 Subject: [PATCH 4/5] fix: Add a fallback if nothing is displayed --- .../org/altart/telegrambridge/bot/feature/MessageListener.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java b/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java index fe4b2c2..165f471 100644 --- a/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java +++ b/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java @@ -68,6 +68,9 @@ public void onUpdateReceived(@NotNull Update update) { if (username == null) { username = String.format("%s %s", message.getFrom().getFirstName(), message.getFrom().getLastName()); } + if (username.trim().isEmpty()) { + username = "Anonymous"; + } TelegramBridge.log.info("Telegram message received from " + username + ": " + text); for (Player player : Bukkit.getOnlinePlayers()) { if (player.hasPermission(Permissions.RECEIVE.getString())) { From 9887b59a09578b023c3f3417a78a4676c709168b Mon Sep 17 00:00:00 2001 From: Pedro Mendes Date: Sun, 16 Jun 2024 18:05:38 -0300 Subject: [PATCH 5/5] refactor: Make a function to get username --- .../bot/feature/MessageListener.java | 26 ++++++++++++++----- .../altart/telegrambridge/utils/Format.java | 12 ++++++--- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java b/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java index 165f471..7927694 100644 --- a/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java +++ b/src/main/java/org/altart/telegrambridge/bot/feature/MessageListener.java @@ -64,13 +64,7 @@ public void onUpdateReceived(@NotNull Update update) { Message message = update.getMessage(); String text = message.getText(); if (text != null && !text.startsWith("/") && TelegramBridge.config.sendToChat) { - String username = message.getFrom().getUserName(); - if (username == null) { - username = String.format("%s %s", message.getFrom().getFirstName(), message.getFrom().getLastName()); - } - if (username.trim().isEmpty()) { - username = "Anonymous"; - } + String username = getUserName(message); TelegramBridge.log.info("Telegram message received from " + username + ": " + text); for (Player player : Bukkit.getOnlinePlayers()) { if (player.hasPermission(Permissions.RECEIVE.getString())) { @@ -118,6 +112,24 @@ public void onUpdateReceived(@NotNull Update update) { } } + private static @NotNull String getUserName(Message message) { + String username = message.getFrom().getUserName(); + if (username == null) { + String firstName = message.getFrom().getFirstName(); + String lastName = message.getFrom().getLastName(); + username = firstName; + if (lastName != null) { + username += " " + message.getFrom().getLastName(); + } + } + + if (username.trim().isEmpty()) { + username = "Anonymous"; + } + + return username; + } + @SuppressWarnings("deprecation") private static TextComponent replyComponent(String username, String message, @Nullable String lang) { HashMap replyValues = makeMessageMap(username, shrinkReplyText(message)); diff --git a/src/main/java/org/altart/telegrambridge/utils/Format.java b/src/main/java/org/altart/telegrambridge/utils/Format.java index a9f9fef..55f9456 100644 --- a/src/main/java/org/altart/telegrambridge/utils/Format.java +++ b/src/main/java/org/altart/telegrambridge/utils/Format.java @@ -1,21 +1,25 @@ package org.altart.telegrambridge.utils; -import org.altart.telegrambridge.TelegramBridge; +import org.jetbrains.annotations.NotNull; import java.util.HashMap; import java.util.Map; public class Format { - public static String string(String format, HashMap values) { + public static String string(@NotNull String format, @NotNull HashMap values) { String result = format; result = result.replace("\\n", "\n"); for (Map.Entry entry : values.entrySet()) { - result = result.replace("%" + entry.getKey() + "%", entry.getValue()); + String value = entry.getValue(); + if (value == null) { + value = ""; + } + result = result.replace("%" + entry.getKey() + "%", value); } return result; } - public static String string(String format, String key, String value) { + public static String string(@NotNull String format, @NotNull String key, @NotNull String value) { String result = format; result = result.replace("\\n", "\n"); result = result.replace("%" + key + "%", value);