Refactor localized messages retrievement

This commit is contained in:
davioooh 2018-05-13 11:16:51 +02:00
parent 288a4ab17a
commit 31f5c64058
4 changed files with 23 additions and 35 deletions

View File

@ -303,7 +303,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
})
.sorted()
.reduce((a, b) -> format("%s%n%s", a, b))
.orElse(getLocalizedMessage(ABILITY_COMMANDS_NOT_FOUND, ctx.user().locale()));
.orElse(getLocalizedMessage(ABILITY_COMMANDS_NOT_FOUND, AbilityUtils.getUser(ctx.update()).getLanguageCode()));
silent.send(commands, ctx.chatId());
})
@ -359,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, AbilityUtils.getUser(ctx.update()).getLanguageCode()), ctx.chatId()))
.reply(update -> {
Long chatId = update.getMessage().getChatId();
String fileId = update.getMessage().getDocument().getFileId();
@ -413,10 +413,10 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
Set<Integer> blacklist = blacklist();
if (blacklist.contains(userId))
silent.sendMd(getLocalizedMessage(ABILITY_BAN_FAIL, ctx.user().locale(), escape(bannedUser)), ctx.chatId());
silent.sendMd(getLocalizedMessage(ABILITY_BAN_FAIL, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(bannedUser)), ctx.chatId());
else {
blacklist.add(userId);
silent.sendMd(getLocalizedMessage(ABILITY_BAN_SUCCESS, ctx.user().locale(), escape(bannedUser)), ctx.chatId());
silent.sendMd(getLocalizedMessage(ABILITY_BAN_SUCCESS, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(bannedUser)), ctx.chatId());
}
})
.post(commitTo(db))
@ -441,9 +441,9 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
Set<Integer> blacklist = blacklist();
if (!blacklist.remove(userId))
silent.sendMd(getLocalizedMessage(ABILITY_UNBAN_FAIL, ctx.user().locale(), escape(username)), ctx.chatId());
silent.sendMd(getLocalizedMessage(ABILITY_UNBAN_FAIL, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(username)), ctx.chatId());
else {
silent.sendMd(getLocalizedMessage(ABILITY_UNBAN_SUCCESS, ctx.user().locale(), escape(username)), ctx.chatId());
silent.sendMd(getLocalizedMessage(ABILITY_UNBAN_SUCCESS, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(username)), ctx.chatId());
}
})
.post(commitTo(db))
@ -465,10 +465,10 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
Set<Integer> admins = admins();
if (admins.contains(userId))
silent.sendMd(getLocalizedMessage(ABILITY_PROMOTE_FAIL, ctx.user().locale(), escape(username)), ctx.chatId());
silent.sendMd(getLocalizedMessage(ABILITY_PROMOTE_FAIL, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(username)), ctx.chatId());
else {
admins.add(userId);
silent.sendMd(getLocalizedMessage(ABILITY_PROMOTE_SUCCESS, ctx.user().locale(), escape(username)), ctx.chatId());
silent.sendMd(getLocalizedMessage(ABILITY_PROMOTE_SUCCESS, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(username)), ctx.chatId());
}
}).post(commitTo(db))
.build();
@ -489,9 +489,9 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
Set<Integer> admins = admins();
if (admins.remove(userId)) {
silent.sendMd(getLocalizedMessage(ABILITY_DEMOTE_SUCCESS, ctx.user().locale(), escape(username)), ctx.chatId());
silent.sendMd(getLocalizedMessage(ABILITY_DEMOTE_SUCCESS, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(username)), ctx.chatId());
} else {
silent.sendMd(getLocalizedMessage(ABILITY_DEMOTE_FAIL, ctx.user().locale(), escape(username)), ctx.chatId());
silent.sendMd(getLocalizedMessage(ABILITY_DEMOTE_FAIL, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(username)), ctx.chatId());
}
})
.post(commitTo(db))
@ -516,10 +516,10 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
long chatId = ctx.chatId();
if (admins.contains(id))
silent.send(getLocalizedMessage(ABILITY_CLAIM_FAIL, ctx.user().locale()), chatId);
silent.send(getLocalizedMessage(ABILITY_CLAIM_FAIL, AbilityUtils.getUser(ctx.update()).getLanguageCode()), chatId);
else {
admins.add(id);
silent.send(getLocalizedMessage(ABILITY_CLAIM_SUCCESS, ctx.user().locale()), chatId);
silent.send(getLocalizedMessage(ABILITY_CLAIM_SUCCESS, AbilityUtils.getUser(ctx.update()).getLanguageCode()), chatId);
}
} else {
// This is not a joke

View File

@ -3,11 +3,9 @@ package org.telegram.abilitybots.api.objects;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import org.telegram.telegrambots.api.objects.User;
import java.io.Serializable;
import java.util.Locale;
import java.util.Objects;
import java.util.StringJoiner;
@ -29,24 +27,20 @@ public final class EndUser implements Serializable {
private final String lastName;
@JsonProperty("username")
private final String username;
@JsonProperty("locale")
private Locale locale;
private EndUser(Integer id, String firstName, String lastName, String username, Locale locale) {
private EndUser(Integer id, String firstName, String lastName, String username) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.username = username;
this.locale = locale;
}
@JsonCreator
public static EndUser endUser(@JsonProperty("id") Integer id,
@JsonProperty("firstName") String firstName,
@JsonProperty("lastName") String lastName,
@JsonProperty("username") String username,
@JsonProperty("locale") Locale locale) {
return new EndUser(id, firstName, lastName, username, locale);
@JsonProperty("username") String username) {
return new EndUser(id, firstName, lastName, username);
}
/**
@ -56,8 +50,7 @@ public final class EndUser implements Serializable {
* @return an augmented end-user
*/
public static EndUser fromUser(User user) {
Locale locale = Strings.isNullOrEmpty(user.getLanguageCode()) ? null : Locale.forLanguageTag(user.getLanguageCode());
return new EndUser(user.getId(), user.getFirstName(), user.getLastName(), user.getUserName(), locale);
return new EndUser(user.getId(), user.getFirstName(), user.getLastName(), user.getUserName());
}
public int id() {
@ -76,8 +69,6 @@ public final class EndUser implements Serializable {
return username;
}
public Locale locale() { return locale; }
/**
* The full name is identified as the concatenation of the first and last name, separated by a space.
* This method can return an empty name if both first and last name are empty.
@ -127,13 +118,12 @@ public final class EndUser implements Serializable {
return Objects.equals(id, endUser.id) &&
Objects.equals(firstName, endUser.firstName) &&
Objects.equals(lastName, endUser.lastName) &&
Objects.equals(username, endUser.username) &&
Objects.equals(locale, endUser.locale);
Objects.equals(username, endUser.username);
}
@Override
public int hashCode() {
return Objects.hash(id, firstName, lastName, username, locale);
return Objects.hash(id, firstName, lastName, username);
}
@Override
@ -143,7 +133,6 @@ public final class EndUser implements Serializable {
.add("firstName", firstName)
.add("lastName", lastName)
.add("username", username)
.add("locale", locale)
.toString();
}
}

View File

@ -10,7 +10,6 @@ import org.telegram.abilitybots.api.sender.MessageSender;
import org.telegram.abilitybots.api.sender.SilentSender;
import java.io.IOException;
import java.util.Locale;
import static org.apache.commons.lang3.StringUtils.EMPTY;
import static org.mockito.Mockito.*;
@ -19,8 +18,8 @@ import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance;
import static org.telegram.abilitybots.api.objects.EndUser.endUser;
public class AbilityBotI18nTest {
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 static final EndUser NO_LANGUAGE_USER = endUser(1, "first", "last", "username");
private static final EndUser ITALIAN_USER = endUser(2, "first", "last", "username");
private DBContext db;
private DefaultBot bot;

View File

@ -55,8 +55,8 @@ public class AbilityBotTest {
private static final long GROUP_ID = 10L;
private static final String TEST = "test";
private static final String[] TEXT = {TEST};
public static final EndUser MUSER = endUser(1, "first", "last", "username", null);
public static final EndUser CREATOR = endUser(1337, "creatorFirst", "creatorLast", "creatorUsername", null);
public static final EndUser MUSER = endUser(1, "first", "last", "username");
public static final EndUser CREATOR = endUser(1337, "creatorFirst", "creatorLast", "creatorUsername");
private DefaultBot bot;
private DBContext db;
@ -295,7 +295,7 @@ public class AbilityBotTest {
String newFirstName = MUSER.firstName() + "-test";
String newLastName = MUSER.lastName() + "-test";
int sameId = MUSER.id();
EndUser changedUser = endUser(sameId, newFirstName, newLastName, newUsername, null);
EndUser changedUser = endUser(sameId, newFirstName, newLastName, newUsername);
mockAlternateUser(update, message, user, changedUser);