Рефакторинг

This commit is contained in:
Struchkov Mark 2023-03-15 20:07:30 +03:00
parent c379904d26
commit 245254628a
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
9 changed files with 233 additions and 37 deletions

View File

@ -24,7 +24,7 @@ public class CallbackQueryConvert {
mail.addAttachment(convertToButtonClick(callbackData, callbackQuery.getMessage().getMessageId())); mail.addAttachment(convertToButtonClick(callbackData, callbackQuery.getMessage().getMessageId()));
final Long chatId = callbackQuery.getMessage().getChatId(); final Long chatId = callbackQuery.getMessage().getChatId();
mail.setPersonId(chatId != null ? chatId.toString() : null); mail.setFromPersonId(chatId != null ? chatId.toString() : null);
final User user = callbackQuery.getFrom(); final User user = callbackQuery.getFrom();
mail.setFirstName(user.getFirstName()); mail.setFirstName(user.getFirstName());

View File

@ -0,0 +1,163 @@
package dev.struchkov.godfather.telegram.main.consumer;
import dev.struchkov.godfather.main.domain.content.Attachment;
import dev.struchkov.godfather.main.domain.content.ChatMail;
import dev.struchkov.godfather.telegram.domain.attachment.CommandAttachment;
import dev.struchkov.godfather.telegram.domain.attachment.ContactAttachment;
import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment;
import dev.struchkov.godfather.telegram.domain.attachment.LinkAttachment;
import dev.struchkov.godfather.telegram.domain.attachment.Picture;
import dev.struchkov.godfather.telegram.domain.attachment.PictureGroupAttachment;
import dev.struchkov.godfather.telegram.main.context.MailPayload;
import dev.struchkov.haiti.utils.Checker;
import dev.struchkov.haiti.utils.Strings;
import org.telegram.telegrambots.meta.api.objects.Contact;
import org.telegram.telegrambots.meta.api.objects.Document;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.MessageEntity;
import org.telegram.telegrambots.meta.api.objects.PhotoSize;
import org.telegram.telegrambots.meta.api.objects.User;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import static dev.struchkov.haiti.utils.Exceptions.utilityClass;
/**
* TODO: Добавить описание класса.
*
* @author upagge [18.08.2019]
*/
public final class MessageChatMailConvert {
private MessageChatMailConvert() {
utilityClass();
}
public static ChatMail apply(Message message) {
final ChatMail mail = new ChatMail();
final Long chatId = message.getChatId();
mail.setId(message.getMessageId().toString());
mail.setChatId(chatId.toString());
mail.setText(message.getText());
mail.setCreateDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), ZoneId.systemDefault()));
final User fromUser = message.getFrom();
mail.setFirstName(fromUser.getFirstName());
mail.setLastName(fromUser.getLastName());
mail.setPayload(MailPayload.USERNAME, fromUser.getUserName());
mail.setFromPersonId(fromUser.getId().toString());
convertDocument(message.getDocument()).ifPresent(mail::addAttachment);
convertContact(message.getContact()).ifPresent(mail::addAttachment);
convertPhoto(message.getPhoto()).ifPresent(mail::addAttachment);
final List<MessageEntity> entities = message.getEntities();
if (entities != null) {
mail.addAttachments(convertAttachments(message));
}
if (message.getReplyToMessage() != null) {
mail.setForwardMail(Collections.singletonList(apply(message.getReplyToMessage())));
}
return mail;
}
private static Optional<Attachment> convertPhoto(List<PhotoSize> photoSizes) {
if (photoSizes != null && !photoSizes.isEmpty()) {
final PictureGroupAttachment attachment = new PictureGroupAttachment();
final List<Picture> pictures = photoSizes.stream()
.map(photoSize -> {
final Picture picture = new Picture();
picture.setFileSize(photoSize.getFileSize());
picture.setFileId(photoSize.getFileId());
picture.setHeight(photoSize.getHeight());
picture.setWeight(photoSize.getWidth());
picture.setFileUniqueId(photoSize.getFileUniqueId());
return picture;
}).toList();
attachment.setPictureSizes(pictures);
return Optional.of(attachment);
}
return Optional.empty();
}
private static Optional<ContactAttachment> convertContact(Contact contact) {
if (contact != null) {
final ContactAttachment attachment = new ContactAttachment();
attachment.setPhoneNumber(contact.getPhoneNumber());
attachment.setUserId(contact.getUserId());
attachment.setFirstName(contact.getFirstName());
attachment.setLastName(contact.getLastName());
if (contact.getVCard() != null) {
attachment.setOwner(false);
attachment.setVCard(contact.getVCard());
} else {
attachment.setOwner(true);
}
return Optional.of(attachment);
}
return Optional.empty();
}
private static Optional<DocumentAttachment> convertDocument(Document document) {
if (document != null) {
final DocumentAttachment attachment = new DocumentAttachment();
attachment.setFileId(document.getFileId());
attachment.setFileSize(document.getFileSize());
attachment.setFileName(document.getFileName());
attachment.setMimeType(document.getMimeType());
return Optional.of(attachment);
}
return Optional.empty();
}
private static List<Attachment> convertAttachments(Message message) {
final List<MessageEntity> entities = message.getEntities();
if (Checker.checkNotEmpty(entities)) {
return entities.stream()
.map(entity -> convertEntity(message, entity))
.filter(Optional::isPresent)
.map(Optional::get)
.toList();
}
return Collections.emptyList();
}
private static Optional<Attachment> convertEntity(Message message, MessageEntity entity) {
switch (entity.getType()) {
case "text_link" -> {
return Optional.of(entity.getUrl())
.map(LinkAttachment::new);
}
case "url" -> {
return Optional.of(entity.getText())
.map(LinkAttachment::new);
}
case "bot_command" -> {
final String commandValue = entity.getText();
String commandArg = message.getText().replace(commandValue, "");
if (Checker.checkNotEmpty(commandArg)) {
commandArg = commandArg.substring(1);
}
final CommandAttachment commandAttachment = new CommandAttachment();
commandAttachment.setValue(commandValue);
commandAttachment.setCommandType(commandValue.replace("/", ""));
commandAttachment.setArg(Strings.EMPTY.equals(commandArg) ? null : commandArg);
commandAttachment.setRawValue(message.getText());
return Optional.of(commandAttachment);
}
}
return Optional.empty();
}
}

View File

@ -40,9 +40,10 @@ public final class MessageMailConvert {
public static Mail apply(Message message) { public static Mail apply(Message message) {
final Mail mail = new Mail(); final Mail mail = new Mail();
final Long chatId = message.getChatId(); final Long chatId = message.getChatId();
mail.setId(message.getMessageId().toString()); mail.setId(message.getMessageId().toString());
mail.setPersonId(chatId != null ? chatId.toString() : null); mail.setFromPersonId(chatId != null ? chatId.toString() : null);
mail.setText(message.getText()); mail.setText(message.getText());
mail.setCreateDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), ZoneId.systemDefault())); mail.setCreateDate(LocalDateTime.ofInstant(Instant.ofEpochSecond(message.getDate()), ZoneId.systemDefault()));

View File

@ -1,10 +1,12 @@
package dev.struchkov.godfather.telegram.simple.consumer; package dev.struchkov.godfather.telegram.simple.consumer;
import dev.struchkov.godfather.main.domain.content.ChatMail;
import dev.struchkov.godfather.main.domain.content.Mail; import dev.struchkov.godfather.main.domain.content.Mail;
import dev.struchkov.godfather.simple.context.service.EventHandler; import dev.struchkov.godfather.simple.context.service.EventHandler;
import dev.struchkov.godfather.telegram.domain.event.Subscribe; import dev.struchkov.godfather.telegram.domain.event.Subscribe;
import dev.struchkov.godfather.telegram.domain.event.Unsubscribe; import dev.struchkov.godfather.telegram.domain.event.Unsubscribe;
import dev.struchkov.godfather.telegram.main.consumer.CallbackQueryConvert; import dev.struchkov.godfather.telegram.main.consumer.CallbackQueryConvert;
import dev.struchkov.godfather.telegram.main.consumer.MessageChatMailConvert;
import dev.struchkov.godfather.telegram.main.consumer.MessageMailConvert; import dev.struchkov.godfather.telegram.main.consumer.MessageMailConvert;
import dev.struchkov.godfather.telegram.main.consumer.SubscribeConvert; import dev.struchkov.godfather.telegram.main.consumer.SubscribeConvert;
import dev.struchkov.godfather.telegram.main.consumer.UnsubscribeConvert; import dev.struchkov.godfather.telegram.main.consumer.UnsubscribeConvert;
@ -42,27 +44,48 @@ public class EventDistributorService implements EventDistributor {
final CallbackQuery callbackQuery = update.getCallbackQuery(); final CallbackQuery callbackQuery = update.getCallbackQuery();
if (message != null) { if (message != null) {
if (!isEvent(message)) { if (!isEvent(message)) {
getHandler(Mail.TYPE).ifPresent(eventProviders -> eventProviders.forEach(eventProvider -> eventProvider.handle(MessageMailConvert.apply(message)))); processionMessage(message);
return; return;
} }
} }
if (callbackQuery != null) { if (callbackQuery != null) {
getHandler(Mail.TYPE).ifPresent(eventProviders -> eventProviders.forEach(eventProvider -> eventProvider.handle(CallbackQueryConvert.apply(callbackQuery)))); processionCallback(callbackQuery);
return; return;
} }
if (update.getMyChatMember() != null) { if (update.getMyChatMember() != null) {
final ChatMemberUpdated chatMember = update.getMyChatMember(); final ChatMemberUpdated chatMember = update.getMyChatMember();
if ("kicked".equals(chatMember.getNewChatMember().getStatus())) { if ("kicked".equals(chatMember.getNewChatMember().getStatus())) {
getHandler(Unsubscribe.TYPE).ifPresent(providers -> providers.forEach(provider -> provider.handle(UnsubscribeConvert.apply(chatMember)))); getHandler(Unsubscribe.TYPE).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(UnsubscribeConvert.apply(chatMember))));
return; return;
} }
if ("member".equals(chatMember.getNewChatMember().getStatus())) { if ("member".equals(chatMember.getNewChatMember().getStatus())) {
getHandler(Subscribe.TYPE).ifPresent(eventProviders -> eventProviders.forEach(eventProvider -> eventProvider.handle(SubscribeConvert.apply(chatMember)))); getHandler(Subscribe.TYPE).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(SubscribeConvert.apply(chatMember))));
return; return;
} }
} }
} }
private void processionCallback(CallbackQuery callbackQuery) {
final Long fromId = callbackQuery.getMessage().getChat().getId();
if (fromId < 0) {
} else {
final Mail mail = CallbackQueryConvert.apply(callbackQuery);
getHandler(Mail.TYPE).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(mail)));
}
}
private void processionMessage(Message message) {
final Long fromId = message.getChat().getId();
if (fromId < 0) {
final ChatMail chatMail = MessageChatMailConvert.apply(message);
getHandler(ChatMail.TYPE).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(chatMail)));
} else {
final Mail mail = MessageMailConvert.apply(message);
getHandler(Mail.TYPE).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(mail)));
}
}
private boolean isEvent(Message message) { private boolean isEvent(Message message) {
return message.getChannelChatCreated() != null return message.getChannelChatCreated() != null
|| message.getDeleteChatPhoto() != null || message.getDeleteChatPhoto() != null

View File

@ -4,7 +4,6 @@ import dev.struchkov.godfather.main.domain.content.Mail;
import dev.struchkov.godfather.quarkus.context.service.PersonSettingService; import dev.struchkov.godfather.quarkus.context.service.PersonSettingService;
import dev.struchkov.godfather.quarkus.core.GeneralAutoResponder; import dev.struchkov.godfather.quarkus.core.GeneralAutoResponder;
import dev.struchkov.godfather.quarkus.core.service.StorylineService; import dev.struchkov.godfather.quarkus.core.service.StorylineService;
import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramSending;
/** /**
* TODO: Добавить описание класса. * TODO: Добавить описание класса.
@ -14,11 +13,10 @@ import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramSending;
public class MailAutoresponderTelegram extends GeneralAutoResponder<Mail> { public class MailAutoresponderTelegram extends GeneralAutoResponder<Mail> {
public MailAutoresponderTelegram( public MailAutoresponderTelegram(
TelegramSending sending,
PersonSettingService personSettingService, PersonSettingService personSettingService,
StorylineService<Mail> storyLineService StorylineService<Mail> storyLineService
) { ) {
super(sending, personSettingService, storyLineService); super(personSettingService, storyLineService);
} }
} }

View File

@ -0,0 +1,22 @@
package dev.struchkov.godfather.telegram.simple.core;
import dev.struchkov.godfather.main.domain.content.ChatMail;
import dev.struchkov.godfather.simple.context.service.PersonSettingService;
import dev.struchkov.godfather.simple.core.GeneralAutoResponder;
import dev.struchkov.godfather.simple.core.service.StorylineService;
/**
* TODO: Добавить описание класса.
*
* @author upagge [18.08.2019]
*/
public class ChatMailAutoresponderTelegram extends GeneralAutoResponder<ChatMail> {
public ChatMailAutoresponderTelegram(
PersonSettingService personSettingService,
StorylineService<ChatMail> storyLineService
) {
super(personSettingService, storyLineService);
}
}

View File

@ -4,7 +4,6 @@ import dev.struchkov.godfather.main.domain.content.Mail;
import dev.struchkov.godfather.simple.context.service.PersonSettingService; import dev.struchkov.godfather.simple.context.service.PersonSettingService;
import dev.struchkov.godfather.simple.core.GeneralAutoResponder; import dev.struchkov.godfather.simple.core.GeneralAutoResponder;
import dev.struchkov.godfather.simple.core.service.StorylineService; import dev.struchkov.godfather.simple.core.service.StorylineService;
import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending;
/** /**
* TODO: Добавить описание класса. * TODO: Добавить описание класса.
@ -14,11 +13,10 @@ import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending;
public class MailAutoresponderTelegram extends GeneralAutoResponder<Mail> { public class MailAutoresponderTelegram extends GeneralAutoResponder<Mail> {
public MailAutoresponderTelegram( public MailAutoresponderTelegram(
TelegramSending sending,
PersonSettingService personSettingService, PersonSettingService personSettingService,
StorylineService<Mail> storyLineService StorylineService<Mail> storyLineService
) { ) {
super(sending, personSettingService, storyLineService); super(personSettingService, storyLineService);
} }
} }

View File

@ -94,7 +94,10 @@ public class TelegramServiceImpl implements TelegramService {
final List<@NotNull BotCommand> noLangCommands = botCommands.stream() final List<@NotNull BotCommand> noLangCommands = botCommands.stream()
.filter(command -> checkNull(command.getLang())) .filter(command -> checkNull(command.getLang()))
.map(clientCommand -> BotCommand.builder().command(clientCommand.getKey()).description(clientCommand.getDescription()).build()) .map(clientCommand -> BotCommand.builder()
.command(clientCommand.getKey())
.description(clientCommand.getDescription())
.build())
.toList(); .toList();
try { try {

View File

@ -1,33 +1,21 @@
package dev.struchkov.godfather.telegram.domain; package dev.struchkov.godfather.telegram.domain;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ClientBotCommand { public class ClientBotCommand {
private String key; private String key;
private String description; private String description;
private String lang; private String lang;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
} }