Доработка
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Struchkov Mark 2023-03-24 01:26:11 +03:00
parent bef5f42eb4
commit b657d588bb
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
5 changed files with 31 additions and 23 deletions

View File

@ -15,7 +15,7 @@ public final class SubscribeConvert {
} }
public static Subscribe apply(ChatMemberUpdated updated) { public static Subscribe apply(ChatMemberUpdated updated) {
final User user = updated.getNewChatMember().getUser(); final User user = updated.getFrom();
final Subscribe subscribe = new Subscribe(); final Subscribe subscribe = new Subscribe();
subscribe.setTelegramId(user.getId().toString()); subscribe.setTelegramId(user.getId().toString());

View File

@ -1,8 +1,8 @@
package dev.struchkov.godfather.telegram.main.consumer; package dev.struchkov.godfather.telegram.main.consumer;
import dev.struchkov.godfather.telegram.domain.event.Unsubscribe; import dev.struchkov.godfather.telegram.domain.event.Unsubscribe;
import org.telegram.telegrambots.meta.api.objects.Chat;
import org.telegram.telegrambots.meta.api.objects.ChatMemberUpdated; import org.telegram.telegrambots.meta.api.objects.ChatMemberUpdated;
import org.telegram.telegrambots.meta.api.objects.User;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -15,13 +15,13 @@ public final class UnsubscribeConvert {
} }
public static Unsubscribe apply(ChatMemberUpdated updated) { public static Unsubscribe apply(ChatMemberUpdated updated) {
final Chat chat = updated.getChat(); final User user = updated.getFrom();
final Unsubscribe unsubscribe = new Unsubscribe(); final Unsubscribe unsubscribe = new Unsubscribe();
unsubscribe.setTelegramId(chat.getId().toString()); unsubscribe.setTelegramId(user.getId().toString());
unsubscribe.setLastName(chat.getLastName()); unsubscribe.setLastName(user.getLastName());
unsubscribe.setFirstName(chat.getFirstName()); unsubscribe.setFirstName(user.getFirstName());
unsubscribe.setSubscriptionDate(LocalDateTime.now()); unsubscribe.setUnsubscriptionDate(LocalDateTime.now());
return unsubscribe; return unsubscribe;
} }

View File

@ -53,7 +53,7 @@ public class EventDistributorService implements EventDistributor {
final InlineQuery inlineQuery = update.getInlineQuery(); final InlineQuery inlineQuery = update.getInlineQuery();
// запросы к боту из чатов: https://core.telegram.org/bots/inline // запросы к боту из чатов: https://core.telegram.org/bots/inline
if (checkNotNull(inlineQuery)) { if (update.hasInlineQuery()) {
final Optional<List<EventHandler>> optHandlers = getHandler(inlineQuery.getClass().getSimpleName()); final Optional<List<EventHandler>> optHandlers = getHandler(inlineQuery.getClass().getSimpleName());
if (optHandlers.isPresent()) { if (optHandlers.isPresent()) {
return Multi.createFrom().iterable(optHandlers.get()) return Multi.createFrom().iterable(optHandlers.get())
@ -64,7 +64,7 @@ public class EventDistributorService implements EventDistributor {
return Uni.createFrom().voidItem(); return Uni.createFrom().voidItem();
} }
if (checkNotNull(preCheckoutQuery)) { if (update.hasPreCheckoutQuery()) {
final Optional<List<EventHandler>> optHandlers = getHandler(preCheckoutQuery.getClass().getName()); final Optional<List<EventHandler>> optHandlers = getHandler(preCheckoutQuery.getClass().getName());
if (optHandlers.isPresent()) { if (optHandlers.isPresent()) {
return Multi.createFrom().iterable(optHandlers.get()) return Multi.createFrom().iterable(optHandlers.get())
@ -75,7 +75,7 @@ public class EventDistributorService implements EventDistributor {
return Uni.createFrom().voidItem(); return Uni.createFrom().voidItem();
} }
if (checkNotNull(message) && (!isEvent(message))) { if (update.hasMessage()) {
final Optional<List<EventHandler>> optHandlers = getHandler(Mail.class.getName()); final Optional<List<EventHandler>> optHandlers = getHandler(Mail.class.getName());
if (optHandlers.isPresent()) { if (optHandlers.isPresent()) {
return Multi.createFrom().iterable(optHandlers.get()) return Multi.createFrom().iterable(optHandlers.get())
@ -86,7 +86,7 @@ public class EventDistributorService implements EventDistributor {
return Uni.createFrom().voidItem(); return Uni.createFrom().voidItem();
} }
if (checkNotNull(callbackQuery)) { if (update.hasCallbackQuery()) {
final Optional<List<EventHandler>> optHandlers = getHandler(Mail.class.getName()); final Optional<List<EventHandler>> optHandlers = getHandler(Mail.class.getName());
if (optHandlers.isPresent()) { if (optHandlers.isPresent()) {
return Multi.createFrom().iterable(optHandlers.get()) return Multi.createFrom().iterable(optHandlers.get())
@ -97,7 +97,7 @@ public class EventDistributorService implements EventDistributor {
return Uni.createFrom().voidItem(); return Uni.createFrom().voidItem();
} }
if (checkNotNull(update.getMyChatMember())) { if (update.hasMyChatMember()) {
final ChatMemberUpdated chatMember = update.getMyChatMember(); final ChatMemberUpdated chatMember = update.getMyChatMember();
if ("kicked".equals(chatMember.getNewChatMember().getStatus())) { if ("kicked".equals(chatMember.getNewChatMember().getStatus())) {

View File

@ -26,6 +26,7 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static dev.struchkov.haiti.utils.Checker.checkNotBlank;
import static dev.struchkov.haiti.utils.Checker.checkNotNull; import static dev.struchkov.haiti.utils.Checker.checkNotNull;
/** /**
@ -49,25 +50,25 @@ public class EventDistributorService implements EventDistributor {
final PreCheckoutQuery preCheckoutQuery = update.getPreCheckoutQuery(); final PreCheckoutQuery preCheckoutQuery = update.getPreCheckoutQuery();
final InlineQuery inlineQuery = update.getInlineQuery(); final InlineQuery inlineQuery = update.getInlineQuery();
if (checkNotNull(inlineQuery)) { if (update.hasInlineQuery()) {
getHandler(inlineQuery.getClass().getSimpleName()).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(inlineQuery))); getHandler(inlineQuery.getClass().getSimpleName()).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(inlineQuery)));
return; return;
} }
if (checkNotNull(preCheckoutQuery)) { if (update.hasPreCheckoutQuery()) {
getHandler(preCheckoutQuery.getClass().getSimpleName()).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(preCheckoutQuery))); getHandler(preCheckoutQuery.getClass().getSimpleName()).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(preCheckoutQuery)));
return; return;
} }
if (message != null && (!isEvent(message))) { if (update.hasMessage()) {
processionMessage(message); processionMessage(message);
return; return;
} }
if (callbackQuery != null) { if (update.hasCallbackQuery()) {
processionCallback(callbackQuery); processionCallback(callbackQuery);
return; return;
} }
if (update.getMyChatMember() != null) { if (update.hasMyChatMember()) {
final ChatMemberUpdated chatMember = update.getMyChatMember(); final ChatMemberUpdated chatMember = update.getMyChatMember();
if ("kicked".equals(chatMember.getNewChatMember().getStatus())) { if ("kicked".equals(chatMember.getNewChatMember().getStatus())) {
getHandler(Unsubscribe.class.getSimpleName()).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(UnsubscribeConvert.apply(chatMember)))); getHandler(Unsubscribe.class.getSimpleName()).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(UnsubscribeConvert.apply(chatMember))));
@ -81,12 +82,19 @@ public class EventDistributorService implements EventDistributor {
} }
private void processionCallback(CallbackQuery callbackQuery) { private void processionCallback(CallbackQuery callbackQuery) {
final Long fromId = callbackQuery.getMessage().getChat().getId(); final Message message = callbackQuery.getMessage();
if (fromId < 0) { if (checkNotBlank(callbackQuery.getInlineMessageId())) {
} else { return;
final Mail mail = CallbackQueryConvert.apply(callbackQuery); }
getHandler(Mail.class.getSimpleName()).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(mail))); if (checkNotNull(message)) {
final Long fromId = message.getChat().getId();
if (fromId < 0) {
} else {
final Mail mail = CallbackQueryConvert.apply(callbackQuery);
getHandler(Mail.class.getSimpleName()).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(mail)));
}
} }
} }

View File

@ -19,6 +19,6 @@ public class Unsubscribe {
private String telegramId; private String telegramId;
private String firstName; private String firstName;
private String lastName; private String lastName;
private LocalDateTime subscriptionDate; private LocalDateTime unsubscriptionDate;
} }