From 288a4ab17a09f5820c6e8ed67bcb83b46c28018f Mon Sep 17 00:00:00 2001 From: davioooh Date: Tue, 8 May 2018 11:10:37 +0200 Subject: [PATCH] Fix issues - export message codes - standardize message codes - format/spacing --- .../abilitybots/api/bot/AbilityBot.java | 41 ++++++++++--------- .../abilitybots/api/objects/EndUser.java | 4 +- .../api/util/AbilityMessageCodes.java | 31 ++++++++++++++ .../abilitybots/api/util/AbilityUtils.java | 15 ++++--- .../src/main/resources/messages.properties | 22 +++++----- .../api/bot/AbilityBotI18nTest.java | 6 +-- .../abilitybots/api/bot/AbilityBotTest.java | 4 +- .../api/bot/NoPublicCommandsBot.java | 1 - 8 files changed, 78 insertions(+), 46 deletions(-) create mode 100644 telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityMessageCodes.java diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/AbilityBot.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/AbilityBot.java index d4b4cdc9..070a0fb1 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/AbilityBot.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/bot/AbilityBot.java @@ -49,6 +49,7 @@ import static org.telegram.abilitybots.api.objects.Flag.*; import static org.telegram.abilitybots.api.objects.Locality.*; import static org.telegram.abilitybots.api.objects.MessageContext.newContext; import static org.telegram.abilitybots.api.objects.Privacy.*; +import static org.telegram.abilitybots.api.util.AbilityMessageCodes.*; import static org.telegram.abilitybots.api.util.AbilityUtils.*; /** @@ -265,7 +266,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot { try { return getUser(username).id(); } catch (IllegalStateException ex) { - silent.send(getLocalizedMessage("userNotFound","", username), chatId); // TODO how to retrieve language? + silent.send(getLocalizedMessage(USER_NOT_FOUND,"", username), chatId); // TODO how to retrieve language? throw propagate(ex); } } @@ -302,7 +303,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot { }) .sorted() .reduce((a, b) -> format("%s%n%s", a, b)) - .orElse(getLocalizedMessage("ability.commands.notFound", ctx.user().locale())); + .orElse(getLocalizedMessage(ABILITY_COMMANDS_NOT_FOUND, ctx.user().locale())); silent.send(commands, ctx.chatId()); }) @@ -358,7 +359,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot { .privacy(CREATOR) .input(0) .action(ctx -> silent.forceReply( - getLocalizedMessage("ability.recover.message", ctx.user().locale()), ctx.chatId())) + getLocalizedMessage(ABILITY_RECOVER_MESSAGE, ctx.user().locale()), ctx.chatId())) .reply(update -> { Long chatId = update.getMessage().getChatId(); String fileId = update.getMessage().getDocument().getFileId(); @@ -366,19 +367,19 @@ public abstract class AbilityBot extends TelegramLongPollingBot { try (FileReader reader = new FileReader(downloadFileWithId(fileId))) { String backupData = IOUtils.toString(reader); if (db.recover(backupData)) { - silent.send(getLocalizedMessage("ability.recover.success", + silent.send(getLocalizedMessage(ABILITY_RECOVER_SUCCESS, ""), chatId); // TODO how to retrieve language? Getting java.lang.IllegalStateException: Could not retrieve originating user from update } else { - silent.send(getLocalizedMessage("ability.recover.fail", + silent.send(getLocalizedMessage(ABILITY_RECOVER_FAIL, AbilityUtils.getUser(update).getLanguageCode()), chatId); } } catch (Exception e) { BotLogger.error("Could not recover DB from backup", TAG, e); - silent.send(getLocalizedMessage("ability.recover.error", + silent.send(getLocalizedMessage(ABILITY_RECOVER_ERROR, AbilityUtils.getUser(update).getLanguageCode()), chatId); } - }, MESSAGE, DOCUMENT, REPLY, isReplyTo(getLocalizedMessage("ability.recover.success", ""))) // TODO how to retrieve language? + }, MESSAGE, DOCUMENT, REPLY, isReplyTo(getLocalizedMessage(ABILITY_RECOVER_SUCCESS, ""))) // TODO how to retrieve language? .build(); } @@ -412,10 +413,10 @@ public abstract class AbilityBot extends TelegramLongPollingBot { Set blacklist = blacklist(); if (blacklist.contains(userId)) - silent.sendMd(getLocalizedMessage("ability.ban.alreadyBanned", ctx.user().locale(), escape(bannedUser)), ctx.chatId()); + silent.sendMd(getLocalizedMessage(ABILITY_BAN_FAIL, ctx.user().locale(), escape(bannedUser)), ctx.chatId()); else { blacklist.add(userId); - silent.sendMd(getLocalizedMessage("ability.ban.banned", ctx.user().locale(), escape(bannedUser)), ctx.chatId()); + silent.sendMd(getLocalizedMessage(ABILITY_BAN_SUCCESS, ctx.user().locale(), escape(bannedUser)), ctx.chatId()); } }) .post(commitTo(db)) @@ -440,9 +441,9 @@ public abstract class AbilityBot extends TelegramLongPollingBot { Set blacklist = blacklist(); if (!blacklist.remove(userId)) - silent.sendMd(getLocalizedMessage("ability.unban.notBanned", ctx.user().locale(), escape(username)), ctx.chatId()); + silent.sendMd(getLocalizedMessage(ABILITY_UNBAN_FAIL, ctx.user().locale(), escape(username)), ctx.chatId()); else { - silent.sendMd(getLocalizedMessage("ability.unban.lifted", ctx.user().locale(), escape(username)), ctx.chatId()); + silent.sendMd(getLocalizedMessage(ABILITY_UNBAN_SUCCESS, ctx.user().locale(), escape(username)), ctx.chatId()); } }) .post(commitTo(db)) @@ -464,10 +465,10 @@ public abstract class AbilityBot extends TelegramLongPollingBot { Set admins = admins(); if (admins.contains(userId)) - silent.sendMd(getLocalizedMessage("ability.promote.alreadyPromoted", ctx.user().locale(), escape(username)), ctx.chatId()); + silent.sendMd(getLocalizedMessage(ABILITY_PROMOTE_FAIL, ctx.user().locale(), escape(username)), ctx.chatId()); else { admins.add(userId); - silent.sendMd(getLocalizedMessage("ability.promote.promoted", ctx.user().locale(), escape(username)), ctx.chatId()); + silent.sendMd(getLocalizedMessage(ABILITY_PROMOTE_SUCCESS, ctx.user().locale(), escape(username)), ctx.chatId()); } }).post(commitTo(db)) .build(); @@ -488,9 +489,9 @@ public abstract class AbilityBot extends TelegramLongPollingBot { Set admins = admins(); if (admins.remove(userId)) { - silent.sendMd(getLocalizedMessage("ability.demote.demoted", ctx.user().locale(), escape(username)), ctx.chatId()); + silent.sendMd(getLocalizedMessage(ABILITY_DEMOTE_SUCCESS, ctx.user().locale(), escape(username)), ctx.chatId()); } else { - silent.sendMd(getLocalizedMessage("ability.demote.alreadyDemoted", ctx.user().locale(), escape(username)), ctx.chatId()); + silent.sendMd(getLocalizedMessage(ABILITY_DEMOTE_FAIL, ctx.user().locale(), escape(username)), ctx.chatId()); } }) .post(commitTo(db)) @@ -515,10 +516,10 @@ public abstract class AbilityBot extends TelegramLongPollingBot { long chatId = ctx.chatId(); if (admins.contains(id)) - silent.send(getLocalizedMessage("ability.claim.alreadyClaimed", ctx.user().locale()), chatId); + silent.send(getLocalizedMessage(ABILITY_CLAIM_FAIL, ctx.user().locale()), chatId); else { admins.add(id); - silent.send(getLocalizedMessage("ability.claim.claimed", ctx.user().locale()), chatId); + silent.send(getLocalizedMessage(ABILITY_CLAIM_SUCCESS, ctx.user().locale()), chatId); } } else { // This is not a joke @@ -618,7 +619,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot { if (!isOk) silent.send( getLocalizedMessage( - "checkInput.fail", + CHECK_INPUT_FAIL, AbilityUtils.getUser(trio.a()).getLanguageCode(), abilityTokens, abilityTokens == 1 ? "input" : "inputs"), getChatId(trio.a())); @@ -635,7 +636,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot { if (!isOk) silent.send( getLocalizedMessage( - "checkLocality.fail", + CHECK_LOCALITY_FAIL, AbilityUtils.getUser(trio.a()).getLanguageCode(), abilityLocality.toString().toLowerCase()), getChatId(trio.a())); @@ -655,7 +656,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot { if (!isOk) silent.send( getLocalizedMessage( - "checkPrivacy.fail", + CHECK_PRIVACY_FAIL, AbilityUtils.getUser(trio.a()).getLanguageCode()), getChatId(trio.a())); return isOk; diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/EndUser.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/EndUser.java index 0ea7919d..0f65e39b 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/EndUser.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/objects/EndUser.java @@ -37,7 +37,7 @@ public final class EndUser implements Serializable { this.firstName = firstName; this.lastName = lastName; this.username = username; - this.locale = locale != null? locale : Locale.ENGLISH; + this.locale = locale; } @JsonCreator @@ -143,7 +143,7 @@ public final class EndUser implements Serializable { .add("firstName", firstName) .add("lastName", lastName) .add("username", username) - .add("locale", locale.toString()) + .add("locale", locale) .toString(); } } diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityMessageCodes.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityMessageCodes.java new file mode 100644 index 00000000..4f21f937 --- /dev/null +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityMessageCodes.java @@ -0,0 +1,31 @@ +package org.telegram.abilitybots.api.util; + +public final class AbilityMessageCodes { + public static String USER_NOT_FOUND = "userNotFound"; + public static String CHECK_INPUT_FAIL = "checkInput.fail"; + public static String CHECK_LOCALITY_FAIL = "checkLocality.fail"; + public static String CHECK_PRIVACY_FAIL = "checkPrivacy.fail"; + + public static String ABILITY_COMMANDS_NOT_FOUND = "ability.commands.notFound"; + + public static String ABILITY_RECOVER_SUCCESS = "ability.recover.success"; + public static String ABILITY_RECOVER_FAIL = "ability.recover.fail"; + public static String ABILITY_RECOVER_MESSAGE = "ability.recover.message"; + public static String ABILITY_RECOVER_ERROR = "ability.recover.error"; + + public static String ABILITY_BAN_SUCCESS = "ability.ban.success"; + public static String ABILITY_BAN_FAIL = "ability.ban.fail"; + + public static String ABILITY_UNBAN_SUCCESS = "ability.unban.success"; + public static String ABILITY_UNBAN_FAIL = "ability.unban.fail"; + + public static String ABILITY_PROMOTE_SUCCESS = "ability.promote.success"; + public static String ABILITY_PROMOTE_FAIL = "ability.promote.fail"; + + public static String ABILITY_DEMOTE_SUCCESS = "ability.demote.success"; + public static String ABILITY_DEMOTE_FAIL = "ability.demote.fail"; + + public static String ABILITY_CLAIM_SUCCESS = "ability.claim.success"; + public static String ABILITY_CLAIM_FAIL = "ability.claim.fail"; + +} diff --git a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java index b64e70bb..973d6ea6 100644 --- a/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java +++ b/telegrambots-abilities/src/main/java/org/telegram/abilitybots/api/util/AbilityUtils.java @@ -13,6 +13,9 @@ import java.util.ResourceBundle; import java.util.function.Consumer; import java.util.function.Predicate; +import static java.util.ResourceBundle.Control.FORMAT_PROPERTIES; +import static java.util.ResourceBundle.Control.getNoFallbackControl; +import static java.util.ResourceBundle.getBundle; import static org.telegram.abilitybots.api.objects.Flag.*; /** @@ -158,16 +161,16 @@ public final class AbilityUtils { public static String getLocalizedMessage(String messageCode, Locale locale, Object...arguments) { ResourceBundle bundle; - if(locale == null){ - bundle = ResourceBundle.getBundle("messages", Locale.ROOT); - }else { + if (locale == null) { + bundle = getBundle("messages", Locale.ROOT); + } else { try { - bundle = ResourceBundle.getBundle( + bundle = getBundle( "messages", locale, - ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_PROPERTIES)); + getNoFallbackControl(FORMAT_PROPERTIES)); } catch (MissingResourceException e) { - bundle = ResourceBundle.getBundle("messages", Locale.ROOT); + bundle = getBundle("messages", Locale.ROOT); } } String message = bundle.getString(messageCode); diff --git a/telegrambots-abilities/src/main/resources/messages.properties b/telegrambots-abilities/src/main/resources/messages.properties index efd03c44..a8495e84 100644 --- a/telegrambots-abilities/src/main/resources/messages.properties +++ b/telegrambots-abilities/src/main/resources/messages.properties @@ -1,24 +1,24 @@ ability.commands.notFound=No public commands found. -ability.recover.message=I am ready to receive the backup file. Please reply to this message with the backup file attached. ability.recover.success=I have successfully recovered. ability.recover.fail=Oops, something went wrong during recovery. +ability.recover.message=I am ready to receive the backup file. Please reply to this message with the backup file attached. ability.recover.error=I have failed to recover. -ability.ban.alreadyBanned={0} is already *banned*. -ability.ban.banned={0} is now *banned*. +ability.ban.success={0} is now *banned*. +ability.ban.fail={0} is already *banned*. -ability.unban.notBanned=@{0} is *not* on the *blacklist*. -ability.unban.lifted=@{0}, your ban has been *lifted*. +ability.unban.success=@{0}, your ban has been *lifted*. +ability.unban.fail=@{0} is *not* on the *blacklist*. -ability.promote.alreadyPromoted=@{0} is already an *admin*. -ability.promote.promoted=@{0} has been *promoted*. +ability.promote.success=@{0} has been *promoted*. +ability.promote.fail=@{0} is already an *admin*. -ability.demote.alreadyDemoted=@{0} is *not* an *admin*. -ability.demote.demoted=@{0} has been *demoted*. +ability.demote.success=@{0} has been *demoted*. +ability.demote.fail=@{0} is *not* an *admin*. -ability.claim.alreadyClaimed=You''re already my master. -ability.claim.claimed=You''re now my master. +ability.claim.success=You''re now my master. +ability.claim.fail=You''re already my master. checkInput.fail=Sorry, this feature requires {0,number,integer} additional {1}. checkLocality.fail=Sorry, {0}-only feature. diff --git a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotI18nTest.java b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotI18nTest.java index b8799004..d64cfe14 100644 --- a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotI18nTest.java +++ b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotI18nTest.java @@ -19,8 +19,8 @@ import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance; import static org.telegram.abilitybots.api.objects.EndUser.endUser; public class AbilityBotI18nTest { - public static final EndUser NO_LANGUAGE_USER = endUser(1, "first", "last", "username", null); - public static final EndUser ITALIAN_USER = endUser(2, "first", "last", "username", Locale.ITALY); + private static final EndUser NO_LANGUAGE_USER = endUser(1, "first", "last", "username", null); + private static final EndUser ITALIAN_USER = endUser(2, "first", "last", "username", Locale.ITALY); private DBContext db; private DefaultBot bot; @@ -55,8 +55,6 @@ public class AbilityBotI18nTest { verify(silent, times(1)) .send("No public commands found.", NO_LANGUAGE_USER.id()); - // - MessageContext context1 = mock(MessageContext.class); when(context1.chatId()).thenReturn(Long.valueOf(ITALIAN_USER.id())); when(context1.user()).thenReturn(ITALIAN_USER); diff --git a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotTest.java b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotTest.java index 51f26353..dd896295 100644 --- a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotTest.java +++ b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/AbilityBotTest.java @@ -48,8 +48,8 @@ import static org.telegram.abilitybots.api.objects.Privacy.*; public class AbilityBotTest { // Messages - protected static final String RECOVERY_MESSAGE = "I am ready to receive the backup file. Please reply to this message with the backup file attached."; - protected static final String RECOVER_SUCCESS = "I have successfully recovered."; + private static final String RECOVERY_MESSAGE = "I am ready to receive the backup file. Please reply to this message with the backup file attached."; + private static final String RECOVER_SUCCESS = "I have successfully recovered."; private static final String[] EMPTY_ARRAY = {}; private static final long GROUP_ID = 10L; diff --git a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/NoPublicCommandsBot.java b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/NoPublicCommandsBot.java index fa7b70e5..b4e49973 100644 --- a/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/NoPublicCommandsBot.java +++ b/telegrambots-abilities/src/test/java/org/telegram/abilitybots/api/bot/NoPublicCommandsBot.java @@ -4,7 +4,6 @@ import org.telegram.abilitybots.api.db.DBContext; public class NoPublicCommandsBot extends AbilityBot { - protected NoPublicCommandsBot(String botToken, String botUsername, DBContext db) { super(botToken, botUsername, db); }