Skip to content

Commit

Permalink
Optimize dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON committed Jun 14, 2024
1 parent 8b88323 commit 04b7d7f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
15 changes: 3 additions & 12 deletions module/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,23 @@ repositories {
}

ext.telegramBotsVersion = '7.4.1'
ext.retrofitVersion = '2.9.0'
ext.jacksonVersion = '2.14.2'
ext.jacksonVersion = '2.17.1'
ext.junit5Version = '5.10.2'
ext.jaxbVersion = '2.3.1'

dependencies {
api "org.telegram:telegrambots-longpolling:$telegramBotsVersion"
api "org.telegram:telegrambots-webhook:$telegramBotsVersion"
api "org.telegram:telegrambots-client:$telegramBotsVersion"
api "com.google.guava:guava:33.0.0-jre"
api "com.squareup.retrofit2:retrofit:$retrofitVersion"
api "com.squareup.retrofit2:converter-jackson:$retrofitVersion"
api "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:$jacksonVersion"
api "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
api 'org.glassfish:javax.el:3.0.1-b12'
api "javax.xml.bind:jaxb-api:$jaxbVersion"
api "com.sun.xml.bind:jaxb-impl:$jaxbVersion"
api "org.glassfish.jaxb:jaxb-runtime:$jaxbVersion"
api 'javax.activation:activation:1.1.1'
api 'org.jetbrains:annotations:24.1.0'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.7.0'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.10.2'
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit5Version"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit5Version"
testImplementation "org.junit.jupiter:junit-jupiter-engine:$junit5Version"
testImplementation "org.junit.vintage:junit-vintage-engine:$junit5Version"
testImplementation "org.assertj:assertj-core:3.25.3"
testImplementation 'org.assertj:assertj-core:3.26.0'
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
import com.annimon.tgbotsmodule.commands.context.MessageContext;
import com.annimon.tgbotsmodule.commands.context.RegexMessageContext;
import com.annimon.tgbotsmodule.services.CommonAbsSender;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.meta.api.objects.Update;
Expand All @@ -24,21 +22,21 @@
public class CommandRegistry<TRole extends Enum<TRole>> implements UpdateHandler {

private final String botUsername;
private final ListMultimap<String, TextCommand> textCommands;
private final Map<String, List<TextCommand>> textCommands;
private final List<RegexCommand> regexCommands;
private final ListMultimap<String, CallbackQueryCommand> callbackCommands;
private final ListMultimap<String, InlineQueryCommand> inlineCommands;
private final Map<String, List<CallbackQueryCommand>> callbackCommands;
private final Map<String, List<InlineQueryCommand>> inlineCommands;
private final Authority<TRole> authority;

private String callbackCommandSplitPattern;

public CommandRegistry(@NotNull String botUsername, @NotNull Authority<TRole> authority) {
this.authority = authority;
this.botUsername = "@" + botUsername.toLowerCase(Locale.ENGLISH);
textCommands = ArrayListMultimap.create();
textCommands = new HashMap<>();
regexCommands = new ArrayList<>();
callbackCommands = ArrayListMultimap.create();
inlineCommands = ArrayListMultimap.create();
callbackCommands = new HashMap<>();
inlineCommands = new HashMap<>();

callbackCommandSplitPattern = ":";
}
Expand All @@ -47,7 +45,7 @@ public CommandRegistry<TRole> register(@NotNull TextCommand command) {
Objects.requireNonNull(command);
Stream.concat(Stream.of(command.command()), command.aliases().stream())
.map(this::stringToCommand)
.forEach(key -> textCommands.put(key, command));
.forEach(key -> addCommand(textCommands, key, command));
return this;
}

Expand All @@ -59,13 +57,13 @@ public CommandRegistry<TRole> register(@NotNull RegexCommand command) {

public CommandRegistry<TRole> register(@NotNull CallbackQueryCommand command) {
Objects.requireNonNull(command);
callbackCommands.put(command.command(), command);
addCommand(callbackCommands, command.command(), command);
return this;
}

public CommandRegistry<TRole> register(@NotNull InlineQueryCommand command) {
Objects.requireNonNull(command);
inlineCommands.put(command.command(), command);
addCommand(inlineCommands, command.command(), command);
return this;
}

Expand Down Expand Up @@ -140,7 +138,7 @@ protected boolean handleTextCommands(@NotNull CommonAbsSender sender, @NotNull U
final var commands = Stream.ofNullable(textCommands.get(command))
.flatMap(Collection::stream)
.filter(cmd -> authority.hasRights(sender, update, message.getFrom(), cmd.authority()))
.collect(Collectors.toList());
.toList();
if (commands.isEmpty()) {
return false;
}
Expand Down Expand Up @@ -176,7 +174,7 @@ protected boolean handleCallbackQueryCommands(@NotNull CommonAbsSender sender, @
final var commands = Stream.ofNullable(callbackCommands.get(command))
.flatMap(Collection::stream)
.filter(cmd -> authority.hasRights(sender, update, query.getFrom(), cmd.authority()))
.collect(Collectors.toList());
.toList();
if (commands.isEmpty()) {
return false;
}
Expand All @@ -197,7 +195,7 @@ private boolean handleInlineQueryCommands(@NotNull CommonAbsSender sender, @NotN
final var commands = Stream.ofNullable(inlineCommands.get(command))
.flatMap(Collection::stream)
.filter(cmd -> authority.hasRights(sender, update, inlineQuery.getFrom(), cmd.authority()))
.collect(Collectors.toList());
.toList();
if (commands.isEmpty()) {
return false;
}
Expand All @@ -213,4 +211,8 @@ private boolean handleInlineQueryCommands(@NotNull CommonAbsSender sender, @NotN
protected String stringToCommand(String str) {
return str.toLowerCase(Locale.ENGLISH).replace(botUsername, "");
}

private static <K, V> void addCommand(Map<K, List<V>> map, K key, V value) {
map.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
}
}

0 comments on commit 04b7d7f

Please sign in to comment.