Cleanup and refactoring
This commit is contained in:
parent
308aec54c2
commit
a819d7f178
@ -14,6 +14,7 @@ import org.telegram.telegrambots.api.methods.groupadministration.GetChatAdminist
|
|||||||
import org.telegram.telegrambots.api.methods.send.SendDocument;
|
import org.telegram.telegrambots.api.methods.send.SendDocument;
|
||||||
import org.telegram.telegrambots.api.objects.Message;
|
import org.telegram.telegrambots.api.objects.Message;
|
||||||
import org.telegram.telegrambots.api.objects.Update;
|
import org.telegram.telegrambots.api.objects.Update;
|
||||||
|
import org.telegram.telegrambots.api.objects.User;
|
||||||
import org.telegram.telegrambots.bots.DefaultBotOptions;
|
import org.telegram.telegrambots.bots.DefaultBotOptions;
|
||||||
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
||||||
import org.telegram.telegrambots.exceptions.TelegramApiException;
|
import org.telegram.telegrambots.exceptions.TelegramApiException;
|
||||||
@ -44,7 +45,6 @@ import static java.util.stream.Collectors.toMap;
|
|||||||
import static jersey.repackaged.com.google.common.base.Throwables.propagate;
|
import static jersey.repackaged.com.google.common.base.Throwables.propagate;
|
||||||
import static org.telegram.abilitybots.api.db.MapDBContext.onlineInstance;
|
import static org.telegram.abilitybots.api.db.MapDBContext.onlineInstance;
|
||||||
import static org.telegram.abilitybots.api.objects.Ability.builder;
|
import static org.telegram.abilitybots.api.objects.Ability.builder;
|
||||||
import static org.telegram.abilitybots.api.objects.EndUser.fromUser;
|
|
||||||
import static org.telegram.abilitybots.api.objects.Flag.*;
|
import static org.telegram.abilitybots.api.objects.Flag.*;
|
||||||
import static org.telegram.abilitybots.api.objects.Locality.*;
|
import static org.telegram.abilitybots.api.objects.Locality.*;
|
||||||
import static org.telegram.abilitybots.api.objects.MessageContext.newContext;
|
import static org.telegram.abilitybots.api.objects.MessageContext.newContext;
|
||||||
@ -145,9 +145,9 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the map of ID -> EndUser
|
* @return the map of ID -> User
|
||||||
*/
|
*/
|
||||||
protected Map<Integer, EndUser> users() {
|
protected Map<Integer, User> users() {
|
||||||
return db.getMap(USERS);
|
return db.getMap(USERS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -232,7 +232,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
* @param username the username of the required user
|
* @param username the username of the required user
|
||||||
* @return the user
|
* @return the user
|
||||||
*/
|
*/
|
||||||
protected EndUser getUser(String username) {
|
protected User getUser(String username) {
|
||||||
Integer id = userIds().get(username.toLowerCase());
|
Integer id = userIds().get(username.toLowerCase());
|
||||||
if (id == null) {
|
if (id == null) {
|
||||||
throw new IllegalStateException(format("Could not find ID corresponding to username [%s]", username));
|
throw new IllegalStateException(format("Could not find ID corresponding to username [%s]", username));
|
||||||
@ -247,13 +247,13 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
* @param id the id of the required user
|
* @param id the id of the required user
|
||||||
* @return the user
|
* @return the user
|
||||||
*/
|
*/
|
||||||
protected EndUser getUser(int id) {
|
protected User getUser(int id) {
|
||||||
EndUser endUser = users().get(id);
|
User user = users().get(id);
|
||||||
if (endUser == null) {
|
if (user == null) {
|
||||||
throw new IllegalStateException(format("Could not find user corresponding to id [%d]", id));
|
throw new IllegalStateException(format("Could not find user corresponding to id [%d]", id));
|
||||||
}
|
}
|
||||||
|
|
||||||
return endUser;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -264,7 +264,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
*/
|
*/
|
||||||
protected int getUserIdSendError(String username, long chatId) {
|
protected int getUserIdSendError(String username, long chatId) {
|
||||||
try {
|
try {
|
||||||
return getUser(username).id();
|
return getUser(username).getId();
|
||||||
} catch (IllegalStateException ex) {
|
} catch (IllegalStateException ex) {
|
||||||
silent.send(getLocalizedMessage(USER_NOT_FOUND, "", username), chatId); // TODO how to retrieve language?
|
silent.send(getLocalizedMessage(USER_NOT_FOUND, "", username), chatId); // TODO how to retrieve language?
|
||||||
throw propagate(ex);
|
throw propagate(ex);
|
||||||
@ -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, AbilityUtils.getUser(ctx.update()).getLanguageCode()));
|
.orElse(getLocalizedMessage(ABILITY_COMMANDS_NOT_FOUND, ctx.user().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, AbilityUtils.getUser(ctx.update()).getLanguageCode()), ctx.chatId()))
|
getLocalizedMessage(ABILITY_RECOVER_MESSAGE, ctx.user().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();
|
||||||
@ -367,17 +367,13 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
try (FileReader reader = new FileReader(downloadFileWithId(fileId))) {
|
try (FileReader reader = new FileReader(downloadFileWithId(fileId))) {
|
||||||
String backupData = IOUtils.toString(reader);
|
String backupData = IOUtils.toString(reader);
|
||||||
if (db.recover(backupData)) {
|
if (db.recover(backupData)) {
|
||||||
silent.send(getLocalizedMessage(ABILITY_RECOVER_SUCCESS,
|
send(ABILITY_RECOVER_SUCCESS, update, chatId);
|
||||||
""), chatId);
|
|
||||||
// TODO how to retrieve language? Getting java.lang.IllegalStateException: Could not retrieve originating user from update
|
|
||||||
} else {
|
} else {
|
||||||
silent.send(getLocalizedMessage(ABILITY_RECOVER_FAIL,
|
send(ABILITY_RECOVER_FAIL, update, chatId);
|
||||||
AbilityUtils.getUser(update).getLanguageCode()), chatId);
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
BotLogger.error("Could not recover DB from backup", TAG, e);
|
BotLogger.error("Could not recover DB from backup", TAG, e);
|
||||||
silent.send(getLocalizedMessage(ABILITY_RECOVER_ERROR,
|
send(ABILITY_RECOVER_ERROR, update, chatId);
|
||||||
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();
|
.build();
|
||||||
@ -405,18 +401,18 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
|
|
||||||
// Protection from abuse
|
// Protection from abuse
|
||||||
if (userId == creatorId()) {
|
if (userId == creatorId()) {
|
||||||
userId = ctx.user().id();
|
userId = ctx.user().getId();
|
||||||
bannedUser = isNullOrEmpty(ctx.user().username()) ? addTag(ctx.user().username()) : ctx.user().shortName();
|
bannedUser = isNullOrEmpty(ctx.user().getUserName()) ? addTag(ctx.user().getUserName()) : shortName(ctx.user());
|
||||||
} else {
|
} else {
|
||||||
bannedUser = addTag(username);
|
bannedUser = addTag(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
Set<Integer> blacklist = blacklist();
|
Set<Integer> blacklist = blacklist();
|
||||||
if (blacklist.contains(userId))
|
if (blacklist.contains(userId))
|
||||||
silent.sendMd(getLocalizedMessage(ABILITY_BAN_FAIL, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(bannedUser)), ctx.chatId());
|
sendMd(ABILITY_BAN_FAIL, ctx, escape(bannedUser));
|
||||||
else {
|
else {
|
||||||
blacklist.add(userId);
|
blacklist.add(userId);
|
||||||
silent.sendMd(getLocalizedMessage(ABILITY_BAN_SUCCESS, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(bannedUser)), ctx.chatId());
|
sendMd(ABILITY_BAN_SUCCESS, ctx, escape(bannedUser));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.post(commitTo(db))
|
.post(commitTo(db))
|
||||||
@ -441,9 +437,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, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(username)), ctx.chatId());
|
silent.sendMd(getLocalizedMessage(ABILITY_UNBAN_FAIL, ctx.user().getLanguageCode(), escape(username)), ctx.chatId());
|
||||||
else {
|
else {
|
||||||
silent.sendMd(getLocalizedMessage(ABILITY_UNBAN_SUCCESS, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(username)), ctx.chatId());
|
silent.sendMd(getLocalizedMessage(ABILITY_UNBAN_SUCCESS, ctx.user().getLanguageCode(), escape(username)), ctx.chatId());
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.post(commitTo(db))
|
.post(commitTo(db))
|
||||||
@ -465,10 +461,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, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(username)), ctx.chatId());
|
sendMd(ABILITY_PROMOTE_FAIL, ctx, escape(username));
|
||||||
else {
|
else {
|
||||||
admins.add(userId);
|
admins.add(userId);
|
||||||
silent.sendMd(getLocalizedMessage(ABILITY_PROMOTE_SUCCESS, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(username)), ctx.chatId());
|
sendMd(ABILITY_PROMOTE_SUCCESS, ctx, escape(username));
|
||||||
}
|
}
|
||||||
}).post(commitTo(db))
|
}).post(commitTo(db))
|
||||||
.build();
|
.build();
|
||||||
@ -489,9 +485,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, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(username)), ctx.chatId());
|
sendMd(ABILITY_DEMOTE_SUCCESS, ctx, escape(username));
|
||||||
} else {
|
} else {
|
||||||
silent.sendMd(getLocalizedMessage(ABILITY_DEMOTE_FAIL, AbilityUtils.getUser(ctx.update()).getLanguageCode(), escape(username)), ctx.chatId());
|
sendMd(ABILITY_DEMOTE_FAIL, ctx, escape(username));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.post(commitTo(db))
|
.post(commitTo(db))
|
||||||
@ -510,26 +506,37 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
.privacy(PUBLIC)
|
.privacy(PUBLIC)
|
||||||
.input(0)
|
.input(0)
|
||||||
.action(ctx -> {
|
.action(ctx -> {
|
||||||
if (ctx.user().id() == creatorId()) {
|
if (ctx.user().getId() == creatorId()) {
|
||||||
Set<Integer> admins = admins();
|
Set<Integer> admins = admins();
|
||||||
int id = creatorId();
|
int id = creatorId();
|
||||||
long chatId = ctx.chatId();
|
|
||||||
|
|
||||||
if (admins.contains(id))
|
if (admins.contains(id))
|
||||||
silent.send(getLocalizedMessage(ABILITY_CLAIM_FAIL, AbilityUtils.getUser(ctx.update()).getLanguageCode()), chatId);
|
send(ABILITY_CLAIM_FAIL, ctx);
|
||||||
else {
|
else {
|
||||||
admins.add(id);
|
admins.add(id);
|
||||||
silent.send(getLocalizedMessage(ABILITY_CLAIM_SUCCESS, AbilityUtils.getUser(ctx.update()).getLanguageCode()), chatId);
|
send(ABILITY_CLAIM_SUCCESS, ctx);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// This is not a joke
|
// This is not a joke
|
||||||
abilities.get(BAN).action().accept(newContext(ctx.update(), ctx.user(), ctx.chatId(), ctx.user().username()));
|
abilities.get(BAN).action().accept(newContext(ctx.update(), ctx.user(), ctx.chatId(), ctx.user().getUserName()));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.post(commitTo(db))
|
.post(commitTo(db))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Optional<Message> send(String message, MessageContext ctx, String... args) {
|
||||||
|
return silent.send(getLocalizedMessage(message, ctx.user().getLanguageCode(), args), ctx.chatId());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Optional<Message> sendMd(String message, MessageContext ctx, String... args) {
|
||||||
|
return silent.sendMd(getLocalizedMessage(message, ctx.user().getLanguageCode(), args), ctx.chatId());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Optional<Message> send(String message, Update upd, Long chatId) {
|
||||||
|
return silent.send(getLocalizedMessage(message, AbilityUtils.getUser(upd).getLanguageCode()), chatId);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers the declared abilities using method reflection. Also, replies are accumulated using the built abilities and standalone methods that return a Reply.
|
* Registers the declared abilities using method reflection. Also, replies are accumulated using the built abilities and standalone methods that return a Reply.
|
||||||
* <p>
|
* <p>
|
||||||
@ -599,7 +606,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
|
|
||||||
Pair<MessageContext, Ability> getContext(Trio<Update, Ability, String[]> trio) {
|
Pair<MessageContext, Ability> getContext(Trio<Update, Ability, String[]> trio) {
|
||||||
Update update = trio.a();
|
Update update = trio.a();
|
||||||
EndUser user = fromUser(AbilityUtils.getUser(update));
|
User user = AbilityUtils.getUser(update);
|
||||||
|
|
||||||
return Pair.of(newContext(update, user, getChatId(update), trio.c()), trio.b());
|
return Pair.of(newContext(update, user, getChatId(update), trio.c()), trio.b());
|
||||||
}
|
}
|
||||||
@ -645,9 +652,9 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
|
|
||||||
boolean checkPrivacy(Trio<Update, Ability, String[]> trio) {
|
boolean checkPrivacy(Trio<Update, Ability, String[]> trio) {
|
||||||
Update update = trio.a();
|
Update update = trio.a();
|
||||||
EndUser user = fromUser(AbilityUtils.getUser(update));
|
User user = AbilityUtils.getUser(update);
|
||||||
Privacy privacy;
|
Privacy privacy;
|
||||||
int id = user.id();
|
int id = user.getId();
|
||||||
|
|
||||||
privacy = isCreator(id) ? CREATOR : isAdmin(id) ? ADMIN : (isGroupUpdate(update) || isSuperGroupUpdate(update)) && isGroupAdmin(update, id) ? GROUP_ADMIN : PUBLIC;
|
privacy = isCreator(id) ? CREATOR : isAdmin(id) ? ADMIN : (isGroupUpdate(update) || isSuperGroupUpdate(update)) && isGroupAdmin(update, id) ? GROUP_ADMIN : PUBLIC;
|
||||||
|
|
||||||
@ -709,9 +716,9 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Update addUser(Update update) {
|
Update addUser(Update update) {
|
||||||
EndUser endUser = fromUser(AbilityUtils.getUser(update));
|
User endUser = AbilityUtils.getUser(update);
|
||||||
|
|
||||||
users().compute(endUser.id(), (id, user) -> {
|
users().compute(endUser.getId(), (id, user) -> {
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
updateUserId(user, endUser);
|
updateUserId(user, endUser);
|
||||||
return endUser;
|
return endUser;
|
||||||
@ -729,15 +736,15 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
return update;
|
return update;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateUserId(EndUser oldUser, EndUser newUser) {
|
private void updateUserId(User oldUser, User newUser) {
|
||||||
if (oldUser != null && oldUser.username() != null) {
|
if (oldUser != null && oldUser.getUserName() != null) {
|
||||||
// Remove old username -> ID
|
// Remove old username -> ID
|
||||||
userIds().remove(oldUser.username());
|
userIds().remove(oldUser.getUserName());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newUser.username() != null) {
|
if (newUser.getUserName() != null) {
|
||||||
// Add new mapping with the new username
|
// Add new mapping with the new username
|
||||||
userIds().put(newUser.username().toLowerCase(), newUser.id());
|
userIds().put(newUser.getUserName().toLowerCase(), newUser.getId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -765,6 +772,7 @@ public abstract class AbilityBot extends TelegramLongPollingBot {
|
|||||||
return sender.downloadFile(sender.execute(new GetFile().setFileId(fileId)));
|
return sender.downloadFile(sender.execute(new GetFile().setFileId(fileId)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String escape(String username) {
|
private String escape(String username) {
|
||||||
return username.replace("_", "\\_");
|
return username.replace("_", "\\_");
|
||||||
}
|
}
|
||||||
|
@ -1,138 +0,0 @@
|
|||||||
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 org.telegram.telegrambots.api.objects.User;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.StringJoiner;
|
|
||||||
|
|
||||||
import static org.apache.commons.lang3.StringUtils.isEmpty;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class serves the purpose of separating the basic Telegram {@link User} and the augmented {@link EndUser}.
|
|
||||||
* <p>
|
|
||||||
* It adds proper hashCode, equals, toString as well as useful utility methods such as {@link EndUser#shortName} and {@link EndUser#fullName}.
|
|
||||||
*
|
|
||||||
* @author Abbas Abou Daya
|
|
||||||
*/
|
|
||||||
public final class EndUser implements Serializable {
|
|
||||||
@JsonProperty("id")
|
|
||||||
private final Integer id;
|
|
||||||
@JsonProperty("firstName")
|
|
||||||
private final String firstName;
|
|
||||||
@JsonProperty("lastName")
|
|
||||||
private final String lastName;
|
|
||||||
@JsonProperty("username")
|
|
||||||
private final String username;
|
|
||||||
|
|
||||||
private EndUser(Integer id, String firstName, String lastName, String username) {
|
|
||||||
this.id = id;
|
|
||||||
this.firstName = firstName;
|
|
||||||
this.lastName = lastName;
|
|
||||||
this.username = username;
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonCreator
|
|
||||||
public static EndUser endUser(@JsonProperty("id") Integer id,
|
|
||||||
@JsonProperty("firstName") String firstName,
|
|
||||||
@JsonProperty("lastName") String lastName,
|
|
||||||
@JsonProperty("username") String username) {
|
|
||||||
return new EndUser(id, firstName, lastName, username);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs an {@link EndUser} from a {@link User}.
|
|
||||||
*
|
|
||||||
* @param user the Telegram user
|
|
||||||
* @return an augmented end-user
|
|
||||||
*/
|
|
||||||
public static EndUser fromUser(User user) {
|
|
||||||
return new EndUser(user.getId(), user.getFirstName(), user.getLastName(), user.getUserName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public int id() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String firstName() {
|
|
||||||
return firstName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String lastName() {
|
|
||||||
return lastName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String username() {
|
|
||||||
return username;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
*
|
|
||||||
* @return the full name of the user
|
|
||||||
*/
|
|
||||||
public String fullName() {
|
|
||||||
StringJoiner name = new StringJoiner(" ");
|
|
||||||
|
|
||||||
if (!isEmpty(firstName))
|
|
||||||
name.add(firstName);
|
|
||||||
if (!isEmpty(lastName))
|
|
||||||
name.add(lastName);
|
|
||||||
|
|
||||||
return name.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The short name is one of the following:
|
|
||||||
* <ol>
|
|
||||||
* <li>First name</li>
|
|
||||||
* <li>Last name</li>
|
|
||||||
* <li>Username</li>
|
|
||||||
* </ol>
|
|
||||||
* The method will try to return the first valid name in the specified order.
|
|
||||||
*
|
|
||||||
* @return the short name of the user
|
|
||||||
*/
|
|
||||||
public String shortName() {
|
|
||||||
if (!isEmpty(firstName))
|
|
||||||
return firstName;
|
|
||||||
|
|
||||||
if (!isEmpty(lastName))
|
|
||||||
return lastName;
|
|
||||||
|
|
||||||
return username;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object o) {
|
|
||||||
if (this == o)
|
|
||||||
return true;
|
|
||||||
if (o == null || getClass() != o.getClass())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
EndUser endUser = (EndUser) o;
|
|
||||||
return Objects.equals(id, endUser.id) &&
|
|
||||||
Objects.equals(firstName, endUser.firstName) &&
|
|
||||||
Objects.equals(lastName, endUser.lastName) &&
|
|
||||||
Objects.equals(username, endUser.username);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return Objects.hash(id, firstName, lastName, username);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return MoreObjects.toStringHelper(this)
|
|
||||||
.add("id", id)
|
|
||||||
.add("firstName", firstName)
|
|
||||||
.add("lastName", lastName)
|
|
||||||
.add("username", username)
|
|
||||||
.toString();
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,6 +3,7 @@ package org.telegram.abilitybots.api.objects;
|
|||||||
import com.google.common.base.MoreObjects;
|
import com.google.common.base.MoreObjects;
|
||||||
import com.google.common.base.Objects;
|
import com.google.common.base.Objects;
|
||||||
import org.telegram.telegrambots.api.objects.Update;
|
import org.telegram.telegrambots.api.objects.Update;
|
||||||
|
import org.telegram.telegrambots.api.objects.User;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
@ -14,26 +15,26 @@ import java.util.Arrays;
|
|||||||
* @author Abbas Abou Daya
|
* @author Abbas Abou Daya
|
||||||
*/
|
*/
|
||||||
public class MessageContext {
|
public class MessageContext {
|
||||||
private final EndUser user;
|
private final User user;
|
||||||
private final Long chatId;
|
private final Long chatId;
|
||||||
private final String[] arguments;
|
private final String[] arguments;
|
||||||
private final Update update;
|
private final Update update;
|
||||||
|
|
||||||
private MessageContext(Update update, EndUser user, Long chatId, String[] arguments) {
|
private MessageContext(Update update, User user, Long chatId, String[] arguments) {
|
||||||
this.user = user;
|
this.user = user;
|
||||||
this.chatId = chatId;
|
this.chatId = chatId;
|
||||||
this.update = update;
|
this.update = update;
|
||||||
this.arguments = arguments;
|
this.arguments = arguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MessageContext newContext(Update update, EndUser user, Long chatId, String... arguments) {
|
public static MessageContext newContext(Update update, User user, Long chatId, String... arguments) {
|
||||||
return new MessageContext(update, user, chatId, arguments);
|
return new MessageContext(update, user, chatId, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the originating Telegram user of this update
|
* @return the originating Telegram user of this update
|
||||||
*/
|
*/
|
||||||
public EndUser user() {
|
public User user() {
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,12 +10,14 @@ import java.text.MessageFormat;
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.MissingResourceException;
|
import java.util.MissingResourceException;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
|
import java.util.StringJoiner;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
import static java.util.ResourceBundle.Control.FORMAT_PROPERTIES;
|
import static java.util.ResourceBundle.Control.FORMAT_PROPERTIES;
|
||||||
import static java.util.ResourceBundle.Control.getNoFallbackControl;
|
import static java.util.ResourceBundle.Control.getNoFallbackControl;
|
||||||
import static java.util.ResourceBundle.getBundle;
|
import static java.util.ResourceBundle.getBundle;
|
||||||
|
import static org.apache.commons.lang3.StringUtils.isEmpty;
|
||||||
import static org.telegram.abilitybots.api.objects.Flag.*;
|
import static org.telegram.abilitybots.api.objects.Flag.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -204,4 +206,42 @@ public final class AbilityUtils {
|
|||||||
return getLocalizedMessage(messageCode, locale, arguments);
|
return getLocalizedMessage(messageCode, locale, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The short name is one of the following:
|
||||||
|
* <ol>
|
||||||
|
* <li>First name</li>
|
||||||
|
* <li>Last name</li>
|
||||||
|
* <li>Username</li>
|
||||||
|
* </ol>
|
||||||
|
* The method will try to return the first valid name in the specified order.
|
||||||
|
*
|
||||||
|
* @return the short name of the user
|
||||||
|
*/
|
||||||
|
public static String shortName(User user) {
|
||||||
|
if (!isEmpty(user.getFirstName()))
|
||||||
|
return user.getFirstName();
|
||||||
|
|
||||||
|
if (!isEmpty(user.getLastName()))
|
||||||
|
return user.getLastName();
|
||||||
|
|
||||||
|
return user.getUserName();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @return the full name of the user
|
||||||
|
* @param user
|
||||||
|
*/
|
||||||
|
public static String fullName(User user) {
|
||||||
|
StringJoiner name = new StringJoiner(" ");
|
||||||
|
|
||||||
|
if (!isEmpty(user.getFirstName()))
|
||||||
|
name.add(user.getFirstName());
|
||||||
|
if (!isEmpty(user.getLastName()))
|
||||||
|
name.add(user.getLastName());
|
||||||
|
|
||||||
|
return name.toString();
|
||||||
|
}
|
||||||
}
|
}
|
@ -11,17 +11,15 @@ import org.telegram.telegrambots.api.objects.User;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static java.lang.Long.valueOf;
|
|
||||||
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.*;
|
||||||
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||||
import static org.telegram.abilitybots.api.bot.AbilityBotTest.mockContext;
|
import static org.telegram.abilitybots.api.bot.AbilityBotTest.mockContext;
|
||||||
import static org.telegram.abilitybots.api.bot.AbilityBotTest.newUser;
|
|
||||||
import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance;
|
import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance;
|
||||||
|
|
||||||
public class AbilityBotI18nTest {
|
public class AbilityBotI18nTest {
|
||||||
private static final User NO_LANGUAGE_USER = newUser(1, "first", "last", "username", null);
|
private static final User NO_LANGUAGE_USER = new User(1, "first", false, "last", "username", null);
|
||||||
private static final User ITALIAN_USER = newUser(2, "first", "last", "username", "it-IT");
|
private static final User ITALIAN_USER = new User(2, "first", false, "last", "username", "it-IT");
|
||||||
|
|
||||||
private DBContext db;
|
private DBContext db;
|
||||||
private NoPublicCommandsBot bot;
|
private NoPublicCommandsBot bot;
|
||||||
@ -53,9 +51,9 @@ public class AbilityBotI18nTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void missingPublicCommandsLocalizedCorrectly2() {
|
public void missingPublicCommandsLocalizedCorrectly2() {
|
||||||
MessageContext context1 = mockContext(ITALIAN_USER);
|
MessageContext context = mockContext(ITALIAN_USER);
|
||||||
|
|
||||||
bot.reportCommands().action().accept(context1);
|
bot.reportCommands().action().accept(context);
|
||||||
|
|
||||||
verify(silent, times(1))
|
verify(silent, times(1))
|
||||||
.send("Non sono presenti comandi pubblici.", ITALIAN_USER.getId());
|
.send("Non sono presenti comandi pubblici.", ITALIAN_USER.getId());
|
||||||
|
@ -38,8 +38,6 @@ import static org.mockito.Mockito.*;
|
|||||||
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
import static org.mockito.internal.verification.VerificationModeFactory.times;
|
||||||
import static org.telegram.abilitybots.api.bot.DefaultBot.getDefaultBuilder;
|
import static org.telegram.abilitybots.api.bot.DefaultBot.getDefaultBuilder;
|
||||||
import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance;
|
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.fromUser;
|
|
||||||
import static org.telegram.abilitybots.api.objects.Flag.DOCUMENT;
|
import static org.telegram.abilitybots.api.objects.Flag.DOCUMENT;
|
||||||
import static org.telegram.abilitybots.api.objects.Flag.MESSAGE;
|
import static org.telegram.abilitybots.api.objects.Flag.MESSAGE;
|
||||||
import static org.telegram.abilitybots.api.objects.Locality.ALL;
|
import static org.telegram.abilitybots.api.objects.Locality.ALL;
|
||||||
@ -56,10 +54,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");
|
public static final User USER = new User(1, "first", false, "last", "username", null);
|
||||||
public static final User TG_USER = newUser(1, "first", "last", "username", null);
|
public static final User CREATOR = new User(1337, "creatorFirst", false, "creatorLast", "creatorUsername", null);
|
||||||
public static final EndUser CREATOR = endUser(1337, "creatorFirst", "creatorLast", "creatorUsername");
|
|
||||||
public static final User TG_CREATOR = newUser(1337, "creatorFirst", "creatorLast", "creatorUsername", null);
|
|
||||||
|
|
||||||
private DefaultBot bot;
|
private DefaultBot bot;
|
||||||
private DBContext db;
|
private DBContext db;
|
||||||
@ -80,39 +76,39 @@ public class AbilityBotTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void sendsPrivacyViolation() {
|
public void sendsPrivacyViolation() {
|
||||||
Update update = mockFullUpdate(MUSER, "/admin");
|
Update update = mockFullUpdate(USER, "/admin");
|
||||||
|
|
||||||
bot.onUpdateReceived(update);
|
bot.onUpdateReceived(update);
|
||||||
|
|
||||||
verify(silent, times(1)).send("Sorry, you don't have the required access level to do that.", MUSER.id());
|
verify(silent, times(1)).send("Sorry, you don't have the required access level to do that.", USER.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void sendsLocalityViolation() {
|
public void sendsLocalityViolation() {
|
||||||
Update update = mockFullUpdate(MUSER, "/group");
|
Update update = mockFullUpdate(USER, "/group");
|
||||||
|
|
||||||
bot.onUpdateReceived(update);
|
bot.onUpdateReceived(update);
|
||||||
|
|
||||||
verify(silent, times(1)).send(format("Sorry, %s-only feature.", "group"), MUSER.id());
|
verify(silent, times(1)).send(format("Sorry, %s-only feature.", "group"), USER.getId());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void sendsInputArgsViolation() {
|
public void sendsInputArgsViolation() {
|
||||||
Update update = mockFullUpdate(MUSER, "/count 1 2 3");
|
Update update = mockFullUpdate(USER, "/count 1 2 3");
|
||||||
|
|
||||||
bot.onUpdateReceived(update);
|
bot.onUpdateReceived(update);
|
||||||
|
|
||||||
verify(silent, times(1)).send(format("Sorry, this feature requires %d additional inputs.", 4), MUSER.id());
|
verify(silent, times(1)).send(format("Sorry, this feature requires %d additional inputs.", 4), USER.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canProcessRepliesIfSatisfyRequirements() {
|
public void canProcessRepliesIfSatisfyRequirements() {
|
||||||
Update update = mockFullUpdate(MUSER, "must reply");
|
Update update = mockFullUpdate(USER, "must reply");
|
||||||
|
|
||||||
// False means the update was not pushed down the stream since it has been consumed by the reply
|
// False means the update was not pushed down the stream since it has been consumed by the reply
|
||||||
assertFalse(bot.filterReply(update));
|
assertFalse(bot.filterReply(update));
|
||||||
verify(silent, times(1)).send("reply", MUSER.id());
|
verify(silent, times(1)).send("reply", USER.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -149,8 +145,8 @@ public class AbilityBotTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canDemote() {
|
public void canDemote() {
|
||||||
addUsers(MUSER);
|
addUsers(USER);
|
||||||
bot.admins().add(MUSER.id());
|
bot.admins().add(USER.getId());
|
||||||
|
|
||||||
MessageContext context = defaultContext();
|
MessageContext context = defaultContext();
|
||||||
|
|
||||||
@ -163,33 +159,33 @@ public class AbilityBotTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canPromote() {
|
public void canPromote() {
|
||||||
addUsers(MUSER);
|
addUsers(USER);
|
||||||
|
|
||||||
MessageContext context = defaultContext();
|
MessageContext context = defaultContext();
|
||||||
|
|
||||||
bot.promoteAdmin().action().accept(context);
|
bot.promoteAdmin().action().accept(context);
|
||||||
|
|
||||||
Set<Integer> actual = bot.admins();
|
Set<Integer> actual = bot.admins();
|
||||||
Set<Integer> expected = newHashSet(MUSER.id());
|
Set<Integer> expected = newHashSet(USER.getId());
|
||||||
assertEquals("Could not sudo user", expected, actual);
|
assertEquals("Could not sudo user", expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canBanUser() {
|
public void canBanUser() {
|
||||||
addUsers(MUSER);
|
addUsers(USER);
|
||||||
MessageContext context = defaultContext();
|
MessageContext context = defaultContext();
|
||||||
|
|
||||||
bot.banUser().action().accept(context);
|
bot.banUser().action().accept(context);
|
||||||
|
|
||||||
Set<Integer> actual = bot.blacklist();
|
Set<Integer> actual = bot.blacklist();
|
||||||
Set<Integer> expected = newHashSet(MUSER.id());
|
Set<Integer> expected = newHashSet(USER.getId());
|
||||||
assertEquals("The ban was not emplaced", expected, actual);
|
assertEquals("The ban was not emplaced", expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canUnbanUser() {
|
public void canUnbanUser() {
|
||||||
addUsers(MUSER);
|
addUsers(USER);
|
||||||
bot.blacklist().add(MUSER.id());
|
bot.blacklist().add(USER.getId());
|
||||||
|
|
||||||
MessageContext context = defaultContext();
|
MessageContext context = defaultContext();
|
||||||
|
|
||||||
@ -202,51 +198,49 @@ public class AbilityBotTest {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private MessageContext defaultContext() {
|
private MessageContext defaultContext() {
|
||||||
MessageContext context = mockContext(TG_CREATOR, GROUP_ID);
|
MessageContext context = mockContext(CREATOR, GROUP_ID, USER.getUserName());
|
||||||
when(context.firstArg()).thenReturn(MUSER.username());
|
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void cannotBanCreator() {
|
public void cannotBanCreator() {
|
||||||
addUsers(MUSER, CREATOR);
|
addUsers(USER, CREATOR);
|
||||||
MessageContext context = mockContext(TG_USER, GROUP_ID);
|
MessageContext context = mockContext(USER, GROUP_ID, CREATOR.getUserName());
|
||||||
when(context.firstArg()).thenReturn(CREATOR.username());
|
|
||||||
|
|
||||||
bot.banUser().action().accept(context);
|
bot.banUser().action().accept(context);
|
||||||
|
|
||||||
Set<Integer> actual = bot.blacklist();
|
Set<Integer> actual = bot.blacklist();
|
||||||
Set<Integer> expected = newHashSet(MUSER.id());
|
Set<Integer> expected = newHashSet(USER.getId());
|
||||||
assertEquals("Impostor was not added to the blacklist", expected, actual);
|
assertEquals("Impostor was not added to the blacklist", expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addUsers(EndUser... users) {
|
private void addUsers(User... users) {
|
||||||
Arrays.stream(users).forEach(user -> {
|
Arrays.stream(users).forEach(user -> {
|
||||||
bot.users().put(user.id(), user);
|
bot.users().put(user.getId(), user);
|
||||||
bot.userIds().put(user.username().toLowerCase(), user.id());
|
bot.userIds().put(user.getUserName().toLowerCase(), user.getId());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void creatorCanClaimBot() {
|
public void creatorCanClaimBot() {
|
||||||
MessageContext context = mockContext(TG_CREATOR, GROUP_ID);
|
MessageContext context = mockContext(CREATOR, GROUP_ID);
|
||||||
|
|
||||||
bot.claimCreator().action().accept(context);
|
bot.claimCreator().action().accept(context);
|
||||||
|
|
||||||
Set<Integer> actual = bot.admins();
|
Set<Integer> actual = bot.admins();
|
||||||
Set<Integer> expected = newHashSet(CREATOR.id());
|
Set<Integer> expected = newHashSet(CREATOR.getId());
|
||||||
assertEquals("Creator was not properly added to the super admins set", expected, actual);
|
assertEquals("Creator was not properly added to the super admins set", expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void userGetsBannedIfClaimsBot() {
|
public void userGetsBannedIfClaimsBot() {
|
||||||
addUsers(MUSER);
|
addUsers(USER);
|
||||||
MessageContext context = mockContext(TG_USER, GROUP_ID);
|
MessageContext context = mockContext(USER, GROUP_ID);
|
||||||
|
|
||||||
bot.claimCreator().action().accept(context);
|
bot.claimCreator().action().accept(context);
|
||||||
|
|
||||||
Set<Integer> actual = bot.blacklist();
|
Set<Integer> actual = bot.blacklist();
|
||||||
Set<Integer> expected = newHashSet(MUSER.id());
|
Set<Integer> expected = newHashSet(USER.getId());
|
||||||
assertEquals("Could not find user on the blacklist", expected, actual);
|
assertEquals("Could not find user on the blacklist", expected, actual);
|
||||||
|
|
||||||
actual = bot.admins();
|
actual = bot.admins();
|
||||||
@ -256,7 +250,7 @@ public class AbilityBotTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void bannedCreatorPassesBlacklistCheck() {
|
public void bannedCreatorPassesBlacklistCheck() {
|
||||||
bot.blacklist().add(CREATOR.id());
|
bot.blacklist().add(CREATOR.getId());
|
||||||
Update update = mock(Update.class);
|
Update update = mock(Update.class);
|
||||||
Message message = mock(Message.class);
|
Message message = mock(Message.class);
|
||||||
User user = mock(User.class);
|
User user = mock(User.class);
|
||||||
@ -273,35 +267,35 @@ public class AbilityBotTest {
|
|||||||
Message message = mock(Message.class);
|
Message message = mock(Message.class);
|
||||||
User user = mock(User.class);
|
User user = mock(User.class);
|
||||||
|
|
||||||
mockAlternateUser(update, message, user, MUSER);
|
mockAlternateUser(update, message, USER);
|
||||||
|
|
||||||
bot.addUser(update);
|
bot.addUser(update);
|
||||||
|
|
||||||
Map<String, Integer> expectedUserIds = ImmutableMap.of(MUSER.username(), MUSER.id());
|
Map<String, Integer> expectedUserIds = ImmutableMap.of(USER.getUserName(), USER.getId());
|
||||||
Map<Integer, EndUser> expectedUsers = ImmutableMap.of(MUSER.id(), MUSER);
|
Map<Integer, User> expectedUsers = ImmutableMap.of(USER.getId(), USER);
|
||||||
assertEquals("User was not added", expectedUserIds, bot.userIds());
|
assertEquals("User was not added", expectedUserIds, bot.userIds());
|
||||||
assertEquals("User was not added", expectedUsers, bot.users());
|
assertEquals("User was not added", expectedUsers, bot.users());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canEditUser() {
|
public void canEditUser() {
|
||||||
addUsers(MUSER);
|
addUsers(USER);
|
||||||
Update update = mock(Update.class);
|
Update update = mock(Update.class);
|
||||||
Message message = mock(Message.class);
|
Message message = mock(Message.class);
|
||||||
User user = mock(User.class);
|
User user = mock(User.class);
|
||||||
|
|
||||||
String newUsername = MUSER.username() + "-test";
|
String newUsername = USER.getUserName() + "-test";
|
||||||
String newFirstName = MUSER.firstName() + "-test";
|
String newFirstName = USER.getFirstName() + "-test";
|
||||||
String newLastName = MUSER.lastName() + "-test";
|
String newLastName = USER.getLastName() + "-test";
|
||||||
int sameId = MUSER.id();
|
int sameId = USER.getId();
|
||||||
EndUser changedUser = endUser(sameId, newFirstName, newLastName, newUsername);
|
User changedUser = new User(sameId, newFirstName, false, newLastName, newUsername, null);
|
||||||
|
|
||||||
mockAlternateUser(update, message, user, changedUser);
|
mockAlternateUser(update, message, changedUser);
|
||||||
|
|
||||||
bot.addUser(update);
|
bot.addUser(update);
|
||||||
|
|
||||||
Map<String, Integer> expectedUserIds = ImmutableMap.of(changedUser.username(), changedUser.id());
|
Map<String, Integer> expectedUserIds = ImmutableMap.of(changedUser.getUserName(), changedUser.getId());
|
||||||
Map<Integer, EndUser> expectedUsers = ImmutableMap.of(changedUser.id(), changedUser);
|
Map<Integer, User> expectedUsers = ImmutableMap.of(changedUser.getId(), changedUser);
|
||||||
assertEquals("User was not properly edited", bot.userIds(), expectedUserIds);
|
assertEquals("User was not properly edited", bot.userIds(), expectedUserIds);
|
||||||
assertEquals("User was not properly edited", expectedUsers, expectedUsers);
|
assertEquals("User was not properly edited", expectedUsers, expectedUsers);
|
||||||
}
|
}
|
||||||
@ -318,7 +312,7 @@ public class AbilityBotTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canCheckInput() {
|
public void canCheckInput() {
|
||||||
Update update = mockFullUpdate(MUSER, "/something");
|
Update update = mockFullUpdate(USER, "/something");
|
||||||
Ability abilityWithOneInput = getDefaultBuilder()
|
Ability abilityWithOneInput = getDefaultBuilder()
|
||||||
.build();
|
.build();
|
||||||
Ability abilityWithZeroInput = getDefaultBuilder()
|
Ability abilityWithZeroInput = getDefaultBuilder()
|
||||||
@ -409,7 +403,7 @@ public class AbilityBotTest {
|
|||||||
|
|
||||||
Trio<Update, Ability, String[]> creatorTrio = Trio.of(update, creatorAbility, TEXT);
|
Trio<Update, Ability, String[]> creatorTrio = Trio.of(update, creatorAbility, TEXT);
|
||||||
|
|
||||||
bot.admins().add(MUSER.id());
|
bot.admins().add(USER.getId());
|
||||||
mockUser(update, message, user);
|
mockUser(update, message, user);
|
||||||
|
|
||||||
assertEquals("Unexpected result when checking for privacy", false, bot.checkPrivacy(creatorTrio));
|
assertEquals("Unexpected result when checking for privacy", false, bot.checkPrivacy(creatorTrio));
|
||||||
@ -440,15 +434,14 @@ public class AbilityBotTest {
|
|||||||
public void canRetrieveContext() {
|
public void canRetrieveContext() {
|
||||||
Update update = mock(Update.class);
|
Update update = mock(Update.class);
|
||||||
Message message = mock(Message.class);
|
Message message = mock(Message.class);
|
||||||
User user = mock(User.class);
|
|
||||||
Ability ability = getDefaultBuilder().build();
|
Ability ability = getDefaultBuilder().build();
|
||||||
Trio<Update, Ability, String[]> trio = Trio.of(update, ability, TEXT);
|
Trio<Update, Ability, String[]> trio = Trio.of(update, ability, TEXT);
|
||||||
|
|
||||||
when(message.getChatId()).thenReturn(GROUP_ID);
|
when(message.getChatId()).thenReturn(GROUP_ID);
|
||||||
mockUser(update, message, user);
|
mockUser(update, message, USER);
|
||||||
|
|
||||||
Pair<MessageContext, Ability> actualPair = bot.getContext(trio);
|
Pair<MessageContext, Ability> actualPair = bot.getContext(trio);
|
||||||
Pair<MessageContext, Ability> expectedPair = Pair.of(newContext(update, MUSER, GROUP_ID, TEXT), ability);
|
Pair<MessageContext, Ability> expectedPair = Pair.of(newContext(update, USER, GROUP_ID, TEXT), ability);
|
||||||
|
|
||||||
assertEquals("Unexpected result when fetching for context", expectedPair, actualPair);
|
assertEquals("Unexpected result when fetching for context", expectedPair, actualPair);
|
||||||
}
|
}
|
||||||
@ -549,7 +542,7 @@ public class AbilityBotTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canReportCommands() {
|
public void canReportCommands() {
|
||||||
MessageContext context = mockContext(TG_USER, GROUP_ID);
|
MessageContext context = mockContext(USER, GROUP_ID);
|
||||||
|
|
||||||
bot.reportCommands().action().accept(context);
|
bot.reportCommands().action().accept(context);
|
||||||
|
|
||||||
@ -562,10 +555,9 @@ public class AbilityBotTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static MessageContext mockContext(User user, long groupId) {
|
public static MessageContext mockContext(User user, long groupId, String... args) {
|
||||||
Update update = mock(Update.class);
|
Update update = mock(Update.class);
|
||||||
Message message = mock(Message.class);
|
Message message = mock(Message.class);
|
||||||
EndUser endUser = fromUser(user);
|
|
||||||
|
|
||||||
when(update.hasMessage()).thenReturn(true);
|
when(update.hasMessage()).thenReturn(true);
|
||||||
when(update.getMessage()).thenReturn(message);
|
when(update.getMessage()).thenReturn(message);
|
||||||
@ -573,12 +565,7 @@ public class AbilityBotTest {
|
|||||||
when(message.getFrom()).thenReturn(user);
|
when(message.getFrom()).thenReturn(user);
|
||||||
when(message.hasText()).thenReturn(true);
|
when(message.hasText()).thenReturn(true);
|
||||||
|
|
||||||
MessageContext context = mock(MessageContext.class);
|
return newContext(update, user, groupId, args);
|
||||||
when(context.update()).thenReturn(update);
|
|
||||||
when(context.chatId()).thenReturn(groupId);
|
|
||||||
when(context.user()).thenReturn(endUser);
|
|
||||||
|
|
||||||
return context;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
@ -587,26 +574,14 @@ public class AbilityBotTest {
|
|||||||
db.close();
|
db.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private User mockUser(EndUser fromUser) {
|
|
||||||
User user = mock(User.class);
|
|
||||||
when(user.getId()).thenReturn(fromUser.id());
|
|
||||||
when(user.getUserName()).thenReturn(fromUser.username());
|
|
||||||
when(user.getFirstName()).thenReturn(fromUser.firstName());
|
|
||||||
when(user.getLastName()).thenReturn(fromUser.lastName());
|
|
||||||
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private Update mockFullUpdate(EndUser fromUser, String args) {
|
private Update mockFullUpdate(User user, String args) {
|
||||||
bot.users().put(MUSER.id(), MUSER);
|
bot.users().put(USER.getId(), USER);
|
||||||
bot.users().put(CREATOR.id(), CREATOR);
|
bot.users().put(CREATOR.getId(), CREATOR);
|
||||||
bot.userIds().put(CREATOR.username(), CREATOR.id());
|
bot.userIds().put(CREATOR.getUserName(), CREATOR.getId());
|
||||||
bot.userIds().put(MUSER.username(), MUSER.id());
|
bot.userIds().put(USER.getUserName(), USER.getId());
|
||||||
|
|
||||||
bot.admins().add(CREATOR.id());
|
bot.admins().add(CREATOR.getId());
|
||||||
|
|
||||||
User user = mockUser(fromUser);
|
|
||||||
|
|
||||||
Update update = mock(Update.class);
|
Update update = mock(Update.class);
|
||||||
when(update.hasMessage()).thenReturn(true);
|
when(update.hasMessage()).thenReturn(true);
|
||||||
@ -615,7 +590,7 @@ public class AbilityBotTest {
|
|||||||
when(message.getText()).thenReturn(args);
|
when(message.getText()).thenReturn(args);
|
||||||
when(message.hasText()).thenReturn(true);
|
when(message.hasText()).thenReturn(true);
|
||||||
when(message.isUserMessage()).thenReturn(true);
|
when(message.isUserMessage()).thenReturn(true);
|
||||||
when(message.getChatId()).thenReturn((long) fromUser.id());
|
when(message.getChatId()).thenReturn((long) user.getId());
|
||||||
when(update.getMessage()).thenReturn(message);
|
when(update.getMessage()).thenReturn(message);
|
||||||
return update;
|
return update;
|
||||||
}
|
}
|
||||||
@ -624,17 +599,9 @@ public class AbilityBotTest {
|
|||||||
when(update.hasMessage()).thenReturn(true);
|
when(update.hasMessage()).thenReturn(true);
|
||||||
when(update.getMessage()).thenReturn(message);
|
when(update.getMessage()).thenReturn(message);
|
||||||
when(message.getFrom()).thenReturn(user);
|
when(message.getFrom()).thenReturn(user);
|
||||||
when(user.getFirstName()).thenReturn(MUSER.firstName());
|
|
||||||
when(user.getLastName()).thenReturn(MUSER.lastName());
|
|
||||||
when(user.getId()).thenReturn(MUSER.id());
|
|
||||||
when(user.getUserName()).thenReturn(MUSER.username());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void mockAlternateUser(Update update, Message message, User user, EndUser changedUser) {
|
private void mockAlternateUser(Update update, Message message, User user) {
|
||||||
when(user.getId()).thenReturn(changedUser.id());
|
|
||||||
when(user.getFirstName()).thenReturn(changedUser.firstName());
|
|
||||||
when(user.getLastName()).thenReturn(changedUser.lastName());
|
|
||||||
when(user.getUserName()).thenReturn(changedUser.username());
|
|
||||||
when(message.getFrom()).thenReturn(user);
|
when(message.getFrom()).thenReturn(user);
|
||||||
when(update.hasMessage()).thenReturn(true);
|
when(update.hasMessage()).thenReturn(true);
|
||||||
when(update.getMessage()).thenReturn(message);
|
when(update.getMessage()).thenReturn(message);
|
||||||
@ -646,10 +613,12 @@ public class AbilityBotTest {
|
|||||||
Message botMessage = mock(Message.class);
|
Message botMessage = mock(Message.class);
|
||||||
Document document = mock(Document.class);
|
Document document = mock(Document.class);
|
||||||
|
|
||||||
|
when(message.getFrom()).thenReturn(CREATOR);
|
||||||
when(update.getMessage()).thenReturn(message);
|
when(update.getMessage()).thenReturn(message);
|
||||||
when(message.getDocument()).thenReturn(document);
|
when(message.getDocument()).thenReturn(document);
|
||||||
when(botMessage.getText()).thenReturn(RECOVERY_MESSAGE);
|
when(botMessage.getText()).thenReturn(RECOVERY_MESSAGE);
|
||||||
when(message.isReply()).thenReturn(true);
|
when(message.isReply()).thenReturn(true);
|
||||||
|
when(update.hasMessage()).thenReturn(true);
|
||||||
when(message.hasDocument()).thenReturn(true);
|
when(message.hasDocument()).thenReturn(true);
|
||||||
when(message.getReplyToMessage()).thenReturn(botMessage);
|
when(message.getReplyToMessage()).thenReturn(botMessage);
|
||||||
when(message.getChatId()).thenReturn(GROUP_ID);
|
when(message.getChatId()).thenReturn(GROUP_ID);
|
||||||
|
@ -3,7 +3,7 @@ package org.telegram.abilitybots.api.db;
|
|||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.telegram.abilitybots.api.objects.EndUser;
|
import org.telegram.telegrambots.api.objects.User;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -12,12 +12,11 @@ import java.util.Set;
|
|||||||
import static com.google.common.collect.Maps.newHashMap;
|
import static com.google.common.collect.Maps.newHashMap;
|
||||||
import static com.google.common.collect.Sets.newHashSet;
|
import static com.google.common.collect.Sets.newHashSet;
|
||||||
import static java.lang.String.format;
|
import static java.lang.String.format;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.*;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.telegram.abilitybots.api.bot.AbilityBot.USERS;
|
import static org.telegram.abilitybots.api.bot.AbilityBot.USERS;
|
||||||
import static org.telegram.abilitybots.api.bot.AbilityBot.USER_ID;
|
import static org.telegram.abilitybots.api.bot.AbilityBot.USER_ID;
|
||||||
import static org.telegram.abilitybots.api.bot.AbilityBotTest.CREATOR;
|
import static org.telegram.abilitybots.api.bot.AbilityBotTest.CREATOR;
|
||||||
import static org.telegram.abilitybots.api.bot.AbilityBotTest.MUSER;
|
import static org.telegram.abilitybots.api.bot.AbilityBotTest.USER;
|
||||||
import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance;
|
import static org.telegram.abilitybots.api.db.MapDBContext.offlineInstance;
|
||||||
|
|
||||||
public class MapDBContextTest {
|
public class MapDBContextTest {
|
||||||
@ -32,22 +31,22 @@ public class MapDBContextTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canRecoverDB() {
|
public void canRecoverDB() {
|
||||||
Map<Integer, EndUser> users = db.getMap(USERS);
|
Map<Integer, User> users = db.getMap(USERS);
|
||||||
Map<String, Integer> userIds = db.getMap(USER_ID);
|
Map<String, Integer> userIds = db.getMap(USER_ID);
|
||||||
users.put(CREATOR.id(), CREATOR);
|
users.put(CREATOR.getId(), CREATOR);
|
||||||
users.put(MUSER.id(), MUSER);
|
users.put(USER.getId(), USER);
|
||||||
userIds.put(CREATOR.username(), CREATOR.id());
|
userIds.put(CREATOR.getUserName(), CREATOR.getId());
|
||||||
userIds.put(MUSER.username(), MUSER.id());
|
userIds.put(USER.getUserName(), USER.getId());
|
||||||
|
|
||||||
db.getSet("AYRE").add(123123);
|
db.getSet("AYRE").add(123123);
|
||||||
Map<Integer, EndUser> originalUsers = newHashMap(users);
|
Map<Integer, User> originalUsers = newHashMap(users);
|
||||||
String beforeBackupInfo = db.info(USERS);
|
String beforeBackupInfo = db.info(USERS);
|
||||||
|
|
||||||
Object jsonBackup = db.backup();
|
Object jsonBackup = db.backup();
|
||||||
db.clear();
|
db.clear();
|
||||||
boolean recovered = db.recover(jsonBackup);
|
boolean recovered = db.recover(jsonBackup);
|
||||||
|
|
||||||
Map<Integer, EndUser> recoveredUsers = db.getMap(USERS);
|
Map<Integer, User> recoveredUsers = db.getMap(USERS);
|
||||||
String afterRecoveryInfo = db.info(USERS);
|
String afterRecoveryInfo = db.info(USERS);
|
||||||
|
|
||||||
assertTrue("Could not recover database successfully", recovered);
|
assertTrue("Could not recover database successfully", recovered);
|
||||||
@ -56,24 +55,24 @@ public class MapDBContextTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canFallbackDBIfRecoveryFails() throws IOException {
|
public void canFallbackDBIfRecoveryFails() {
|
||||||
Set<EndUser> users = db.getSet(USERS);
|
Set<User> users = db.getSet(USERS);
|
||||||
users.add(CREATOR);
|
users.add(CREATOR);
|
||||||
users.add(MUSER);
|
users.add(USER);
|
||||||
|
|
||||||
Set<EndUser> originalSet = newHashSet(users);
|
Set<User> originalSet = newHashSet(users);
|
||||||
Object jsonBackup = db.backup();
|
Object jsonBackup = db.backup();
|
||||||
String corruptBackup = "!@#$" + String.valueOf(jsonBackup);
|
String corruptBackup = "!@#$" + String.valueOf(jsonBackup);
|
||||||
boolean recovered = db.recover(corruptBackup);
|
boolean recovered = db.recover(corruptBackup);
|
||||||
|
|
||||||
Set<EndUser> recoveredSet = db.getSet(USERS);
|
Set<User> recoveredSet = db.getSet(USERS);
|
||||||
|
|
||||||
assertEquals("Recovery was successful from a CORRUPT backup", false, recovered);
|
assertFalse("Recovery was successful from a CORRUPT backup", recovered);
|
||||||
assertEquals("Set before and after corrupt recovery are not equal", originalSet, recoveredSet);
|
assertEquals("Set before and after corrupt recovery are not equal", originalSet, recoveredSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canGetSummary() throws IOException {
|
public void canGetSummary() {
|
||||||
String anotherTest = TEST + 1;
|
String anotherTest = TEST + 1;
|
||||||
db.getSet(TEST).add(TEST);
|
db.getSet(TEST).add(TEST);
|
||||||
db.getSet(anotherTest).add(anotherTest);
|
db.getSet(anotherTest).add(anotherTest);
|
||||||
@ -86,7 +85,7 @@ public class MapDBContextTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void canGetInfo() throws IOException {
|
public void canGetInfo() {
|
||||||
db.getSet(TEST).add(TEST);
|
db.getSet(TEST).add(TEST);
|
||||||
|
|
||||||
String actualInfo = db.info(TEST);
|
String actualInfo = db.info(TEST);
|
||||||
@ -97,7 +96,7 @@ public class MapDBContextTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalStateException.class)
|
@Test(expected = IllegalStateException.class)
|
||||||
public void cantGetInfoFromNonexistentDBStructureName() throws IOException {
|
public void cantGetInfoFromNonexistentDBStructureName() {
|
||||||
db.info(TEST);
|
db.info(TEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user