Refactor localized messages retrievement
This commit is contained in:
parent
288a4ab17a
commit
31f5c64058
@ -303,7 +303,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
})
|
})
|
||||||
.sorted()
|
.sorted()
|
||||||
.reduce((a, b) -> format("%s%n%s", a, b))
|
.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());
|
silent.send(commands, ctx.chatId());
|
||||||
})
|
})
|
||||||
@ -359,7 +359,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
.privacy(CREATOR)
|
.privacy(CREATOR)
|
||||||
.input(0)
|
.input(0)
|
||||||
.action(ctx -> silent.forceReply(
|
.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 -> {
|
.reply(update -> {
|
||||||
Long chatId = update.getMessage().getChatId();
|
Long chatId = update.getMessage().getChatId();
|
||||||
String fileId = update.getMessage().getDocument().getFileId();
|
String fileId = update.getMessage().getDocument().getFileId();
|
||||||
@ -413,10 +413,10 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
|
|
||||||
Set<Integer> blacklist = blacklist();
|
Set<Integer> blacklist = blacklist();
|
||||||
if (blacklist.contains(userId))
|
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 {
|
else {
|
||||||
blacklist.add(userId);
|
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))
|
.post(commitTo(db))
|
||||||
@ -441,9 +441,9 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
Set<Integer> blacklist = blacklist();
|
Set<Integer> blacklist = blacklist();
|
||||||
|
|
||||||
if (!blacklist.remove(userId))
|
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 {
|
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))
|
.post(commitTo(db))
|
||||||
@ -465,10 +465,10 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
|
|
||||||
Set<Integer> admins = admins();
|
Set<Integer> admins = admins();
|
||||||
if (admins.contains(userId))
|
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 {
|
else {
|
||||||
admins.add(userId);
|
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))
|
}).post(commitTo(db))
|
||||||
.build();
|
.build();
|
||||||
@ -489,9 +489,9 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
|
|
||||||
Set<Integer> admins = admins();
|
Set<Integer> admins = admins();
|
||||||
if (admins.remove(userId)) {
|
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 {
|
} 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))
|
.post(commitTo(db))
|
||||||
@ -516,10 +516,10 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
long chatId = ctx.chatId();
|
long chatId = ctx.chatId();
|
||||||
|
|
||||||
if (admins.contains(id))
|
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 {
|
else {
|
||||||
admins.add(id);
|
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 {
|
} else {
|
||||||
// This is not a joke
|
// This is not a joke
|
||||||
|
@ -3,11 +3,9 @@ package org.telegram.abilitybots.api.objects;
|
|||||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import com.google.common.base.MoreObjects;
|
import com.google.common.base.MoreObjects;
|
||||||
import com.google.common.base.Strings;
|
|
||||||
import org.telegram.telegrambots.api.objects.User;
|
import org.telegram.telegrambots.api.objects.User;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.StringJoiner;
|
import java.util.StringJoiner;
|
||||||
|
|
||||||
@ -29,24 +27,20 @@ public final class EndUser implements Serializable {
|
|||||||
private final String lastName;
|
private final String lastName;
|
||||||
@JsonProperty("username")
|
@JsonProperty("username")
|
||||||
private final String 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.id = id;
|
||||||
this.firstName = firstName;
|
this.firstName = firstName;
|
||||||
this.lastName = lastName;
|
this.lastName = lastName;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.locale = locale;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonCreator
|
@JsonCreator
|
||||||
public static EndUser endUser(@JsonProperty("id") Integer id,
|
public static EndUser endUser(@JsonProperty("id") Integer id,
|
||||||
@JsonProperty("firstName") String firstName,
|
@JsonProperty("firstName") String firstName,
|
||||||
@JsonProperty("lastName") String lastName,
|
@JsonProperty("lastName") String lastName,
|
||||||
@JsonProperty("username") String username,
|
@JsonProperty("username") String username) {
|
||||||
@JsonProperty("locale") Locale locale) {
|
return new EndUser(id, firstName, lastName, username);
|
||||||
return new EndUser(id, firstName, lastName, username, locale);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -56,8 +50,7 @@ public final class EndUser implements Serializable {
|
|||||||
* @return an augmented end-user
|
* @return an augmented end-user
|
||||||
*/
|
*/
|
||||||
public static EndUser fromUser(User 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());
|
||||||
return new EndUser(user.getId(), user.getFirstName(), user.getLastName(), user.getUserName(), locale);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int id() {
|
public int id() {
|
||||||
@ -76,8 +69,6 @@ public final class EndUser implements Serializable {
|
|||||||
return username;
|
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.
|
* 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.
|
* 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) &&
|
return Objects.equals(id, endUser.id) &&
|
||||||
Objects.equals(firstName, endUser.firstName) &&
|
Objects.equals(firstName, endUser.firstName) &&
|
||||||
Objects.equals(lastName, endUser.lastName) &&
|
Objects.equals(lastName, endUser.lastName) &&
|
||||||
Objects.equals(username, endUser.username) &&
|
Objects.equals(username, endUser.username);
|
||||||
Objects.equals(locale, endUser.locale);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(id, firstName, lastName, username, locale);
|
return Objects.hash(id, firstName, lastName, username);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -143,7 +133,6 @@ public final class EndUser implements Serializable {
|
|||||||
.add("firstName", firstName)
|
.add("firstName", firstName)
|
||||||
.add("lastName", lastName)
|
.add("lastName", lastName)
|
||||||
.add("username", username)
|
.add("username", username)
|
||||||
.add("locale", locale)
|
|
||||||
.toString();
|
.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,6 @@ import org.telegram.abilitybots.api.sender.MessageSender;
|
|||||||
import org.telegram.abilitybots.api.sender.SilentSender;
|
import org.telegram.abilitybots.api.sender.SilentSender;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
import static org.apache.commons.lang3.StringUtils.EMPTY;
|
import static org.apache.commons.lang3.StringUtils.EMPTY;
|
||||||
import static org.mockito.Mockito.*;
|
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;
|
import static org.telegram.abilitybots.api.objects.EndUser.endUser;
|
||||||
|
|
||||||
public class AbilityBotI18nTest {
|
public class AbilityBotI18nTest {
|
||||||
private static final EndUser NO_LANGUAGE_USER = endUser(1, "first", "last", "username", null);
|
private static final EndUser NO_LANGUAGE_USER = endUser(1, "first", "last", "username");
|
||||||
private static final EndUser ITALIAN_USER = endUser(2, "first", "last", "username", Locale.ITALY);
|
private static final EndUser ITALIAN_USER = endUser(2, "first", "last", "username");
|
||||||
|
|
||||||
private DBContext db;
|
private DBContext db;
|
||||||
private DefaultBot bot;
|
private DefaultBot bot;
|
||||||
|
@ -55,8 +55,8 @@ public class AbilityBotTest {
|
|||||||
private static final long GROUP_ID = 10L;
|
private static final long GROUP_ID = 10L;
|
||||||
private static final String TEST = "test";
|
private static final String TEST = "test";
|
||||||
private static final String[] TEXT = {TEST};
|
private static final String[] TEXT = {TEST};
|
||||||
public static final EndUser MUSER = endUser(1, "first", "last", "username", null);
|
public static final EndUser MUSER = endUser(1, "first", "last", "username");
|
||||||
public static final EndUser CREATOR = endUser(1337, "creatorFirst", "creatorLast", "creatorUsername", null);
|
public static final EndUser CREATOR = endUser(1337, "creatorFirst", "creatorLast", "creatorUsername");
|
||||||
|
|
||||||
private DefaultBot bot;
|
private DefaultBot bot;
|
||||||
private DBContext db;
|
private DBContext db;
|
||||||
@ -295,7 +295,7 @@ public class AbilityBotTest {
|
|||||||
String newFirstName = MUSER.firstName() + "-test";
|
String newFirstName = MUSER.firstName() + "-test";
|
||||||
String newLastName = MUSER.lastName() + "-test";
|
String newLastName = MUSER.lastName() + "-test";
|
||||||
int sameId = MUSER.id();
|
int sameId = MUSER.id();
|
||||||
EndUser changedUser = endUser(sameId, newFirstName, newLastName, newUsername, null);
|
EndUser changedUser = endUser(sameId, newFirstName, newLastName, newUsername);
|
||||||
|
|
||||||
mockAlternateUser(update, message, user, changedUser);
|
mockAlternateUser(update, message, user, changedUser);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user