From 1c43f1d797fff8a73edd8795cc3ab10fb8994340 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Fri, 15 Jul 2022 17:48:49 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B4=D0=B2=D0=B0=20eventproviders=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=B4=D0=BF=D0=B8=D1=81=D0=BA=D0=B8=20=D0=B8=20?= =?UTF-8?q?=D0=BE=D1=82=D0=BF=D0=B8=D1=81=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 +- telegram-core/pom.xml | 2 +- .../telegram/convert/SubscribeConvert.java | 28 ++++++++++ .../telegram/convert/UnsubscribeConvert.java | 28 ++++++++++ .../telegram/domain/event/Command.java | 14 +++++ .../telegram/domain/event/Subscribe.java | 53 +++++++++++++++++++ .../telegram/domain/event/Unsubscribe.java | 53 +++++++++++++++++++ .../listen/EventDistributorService.java | 43 +++++++++++---- .../service/provider/CommandProvider.java | 18 +++++++ 9 files changed, 231 insertions(+), 12 deletions(-) create mode 100644 telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/SubscribeConvert.java create mode 100644 telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/UnsubscribeConvert.java create mode 100644 telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/event/Command.java create mode 100644 telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/event/Subscribe.java create mode 100644 telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/event/Unsubscribe.java create mode 100644 telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/provider/CommandProvider.java diff --git a/pom.xml b/pom.xml index bef1523..1930614 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather telegram-bot - 0.0.13 + 0.0.14 pom @@ -33,7 +33,7 @@ UTF-8 UTF-8 - 0.0.11 + 0.0.12 6.1.0 3.10.1 diff --git a/telegram-core/pom.xml b/telegram-core/pom.xml index 8ef57bb..7af7772 100644 --- a/telegram-core/pom.xml +++ b/telegram-core/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather telegram-bot - 0.0.13 + 0.0.14 telegram-core diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/SubscribeConvert.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/SubscribeConvert.java new file mode 100644 index 0000000..99d73f6 --- /dev/null +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/SubscribeConvert.java @@ -0,0 +1,28 @@ +package dev.struchkov.godfather.telegram.convert; + +import dev.struchkov.godfather.telegram.domain.event.Subscribe; +import org.telegram.telegrambots.meta.api.objects.Chat; +import org.telegram.telegrambots.meta.api.objects.ChatMemberUpdated; + +import java.time.LocalDateTime; + +import static dev.struchkov.haiti.utils.Exceptions.utilityClass; + +public final class SubscribeConvert { + + private SubscribeConvert() { + utilityClass(); + } + + public static Subscribe apply(ChatMemberUpdated updated) { + final Chat chat = updated.getChat(); + + final Subscribe subscribe = new Subscribe(); + subscribe.setTelegramId(chat.getId()); + subscribe.setLastName(chat.getLastName()); + subscribe.setFirstName(chat.getFirstName()); + subscribe.setSubscriptionDate(LocalDateTime.now()); + return subscribe; + } + +} diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/UnsubscribeConvert.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/UnsubscribeConvert.java new file mode 100644 index 0000000..388ad76 --- /dev/null +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/convert/UnsubscribeConvert.java @@ -0,0 +1,28 @@ +package dev.struchkov.godfather.telegram.convert; + +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 java.time.LocalDateTime; + +import static dev.struchkov.haiti.utils.Exceptions.utilityClass; + +public final class UnsubscribeConvert { + + private UnsubscribeConvert() { + utilityClass(); + } + + public static Unsubscribe apply(ChatMemberUpdated updated) { + final Chat chat = updated.getChat(); + + final Unsubscribe unsubscribe = new Unsubscribe(); + unsubscribe.setTelegramId(chat.getId()); + unsubscribe.setLastName(chat.getLastName()); + unsubscribe.setFirstName(chat.getFirstName()); + unsubscribe.setSubscriptionDate(LocalDateTime.now()); + return unsubscribe; + } + +} diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/event/Command.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/event/Command.java new file mode 100644 index 0000000..ed71139 --- /dev/null +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/event/Command.java @@ -0,0 +1,14 @@ +package dev.struchkov.godfather.telegram.domain.event; + +import dev.struchkov.godfather.context.domain.event.Event; + +public class Command implements Event { + + public static final String TYPE = "CMD"; + + @Override + public String getType() { + return TYPE; + } + +} diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/event/Subscribe.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/event/Subscribe.java new file mode 100644 index 0000000..3db70f6 --- /dev/null +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/event/Subscribe.java @@ -0,0 +1,53 @@ +package dev.struchkov.godfather.telegram.domain.event; + +import dev.struchkov.godfather.context.domain.event.Event; + +import java.time.LocalDateTime; + +public class Subscribe implements Event { + + public static final String TYPE = "SUBSCRIBE"; + + private Long telegramId; + private String firstName; + private String lastName; + private LocalDateTime subscriptionDate; + + public Long getTelegramId() { + return telegramId; + } + + public void setTelegramId(Long telegramId) { + this.telegramId = telegramId; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public LocalDateTime getSubscriptionDate() { + return subscriptionDate; + } + + public void setSubscriptionDate(LocalDateTime subscriptionDate) { + this.subscriptionDate = subscriptionDate; + } + + @Override + public String getType() { + return TYPE; + } + +} diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/event/Unsubscribe.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/event/Unsubscribe.java new file mode 100644 index 0000000..fee2d2e --- /dev/null +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/domain/event/Unsubscribe.java @@ -0,0 +1,53 @@ +package dev.struchkov.godfather.telegram.domain.event; + +import dev.struchkov.godfather.context.domain.event.Event; + +import java.time.LocalDateTime; + +public class Unsubscribe implements Event { + + public static final String TYPE = "UNSUBSCRIBE"; + + private Long telegramId; + private String firstName; + private String lastName; + private LocalDateTime subscriptionDate; + + public Long getTelegramId() { + return telegramId; + } + + public void setTelegramId(Long telegramId) { + this.telegramId = telegramId; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public LocalDateTime getSubscriptionDate() { + return subscriptionDate; + } + + public void setSubscriptionDate(LocalDateTime subscriptionDate) { + this.subscriptionDate = subscriptionDate; + } + + @Override + public String getType() { + return TYPE; + } + +} diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/EventDistributorService.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/EventDistributorService.java index 46e8f93..81e2d95 100644 --- a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/EventDistributorService.java +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/listen/EventDistributorService.java @@ -5,12 +5,20 @@ import dev.struchkov.godfather.context.service.EventProvider; import dev.struchkov.godfather.telegram.TelegramConnect; import dev.struchkov.godfather.telegram.convert.CallbackQueryConvert; import dev.struchkov.godfather.telegram.convert.MessageMailConvert; +import dev.struchkov.godfather.telegram.convert.SubscribeConvert; +import dev.struchkov.godfather.telegram.convert.UnsubscribeConvert; +import dev.struchkov.godfather.telegram.domain.event.Subscribe; +import dev.struchkov.godfather.telegram.domain.event.Unsubscribe; import org.jetbrains.annotations.NotNull; import org.telegram.telegrambots.meta.api.objects.CallbackQuery; +import org.telegram.telegrambots.meta.api.objects.ChatMemberUpdated; import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Update; import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; /** * TODO: Добавить описание класса. @@ -19,23 +27,40 @@ import java.util.List; */ public class EventDistributorService implements EventDistributor { - private final List> eventProviders; + private final Map> eventProviderMap; - public EventDistributorService(TelegramConnect telegramConnect, List> eventProviders) { - this.eventProviders = eventProviders; + public EventDistributorService(TelegramConnect telegramConnect, List eventProviders) { + this.eventProviderMap = eventProviders.stream().collect(Collectors.groupingBy(EventProvider::getEventType)); telegramConnect.initEventDistributor(this); } @Override public void processing(@NotNull Update update) { - final Message message = update.getMessage(); - final CallbackQuery callbackQuery = update.getCallbackQuery(); - if (message != null) { - eventProviders.forEach(provider -> provider.sendEvent(MessageMailConvert.apply(message))); + if (update.getMessage() != null) { + final Message message = update.getMessage(); + getEventProvider(Mail.TYPE) + .ifPresent(eventProviders -> eventProviders.forEach(eventProvider -> eventProvider.sendEvent(MessageMailConvert.apply(message)))); } - if (callbackQuery != null) { - eventProviders.forEach(provider -> provider.sendEvent(CallbackQueryConvert.apply(callbackQuery))); + if (update.getCallbackQuery() != null) { + final CallbackQuery callbackQuery = update.getCallbackQuery(); + getEventProvider(Mail.TYPE) + .ifPresent(eventProviders -> eventProviders.forEach(eventProvider -> eventProvider.sendEvent(CallbackQueryConvert.apply(callbackQuery)))); + } + if (update.getMyChatMember() != null) { + final ChatMemberUpdated chatMember = update.getMyChatMember(); + if ("kicked".equals(chatMember.getNewChatMember().getStatus())) { + getEventProvider(Unsubscribe.TYPE) + .ifPresent(providers -> providers.forEach(provider -> provider.sendEvent(UnsubscribeConvert.apply(chatMember)))); + } + if ("member".equals(chatMember.getNewChatMember().getStatus())) { + getEventProvider(Subscribe.TYPE) + .ifPresent(eventProviders -> eventProviders.forEach(eventProvider -> eventProvider.sendEvent(SubscribeConvert.apply(chatMember)))); + } } } + private Optional> getEventProvider(String type) { + return Optional.ofNullable(eventProviderMap.get(type)); + } + } diff --git a/telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/provider/CommandProvider.java b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/provider/CommandProvider.java new file mode 100644 index 0000000..3b7a77a --- /dev/null +++ b/telegram-core/src/main/java/dev/struchkov/godfather/telegram/service/provider/CommandProvider.java @@ -0,0 +1,18 @@ +package dev.struchkov.godfather.telegram.service.provider; + +import dev.struchkov.godfather.context.service.EventProvider; +import dev.struchkov.godfather.telegram.domain.event.Command; + +public class CommandProvider implements EventProvider { + + @Override + public void sendEvent(Command event) { + + } + + @Override + public String getEventType() { + return Command.TYPE; + } + +}