diff --git a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/GeneralAutoResponder.java b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/GeneralAutoResponder.java index e95bb4c..0205972 100644 --- a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/GeneralAutoResponder.java +++ b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/GeneralAutoResponder.java @@ -1,19 +1,12 @@ package dev.struchkov.godfather.quarkus.core; import dev.struchkov.autoresponder.Responder; -import dev.struchkov.godfather.exception.ConfigAppException; import dev.struchkov.godfather.main.domain.content.Message; -import dev.struchkov.godfather.main.domain.unit.TypeUnit; import dev.struchkov.godfather.main.domain.unit.UnitActiveType; import dev.struchkov.godfather.quarkus.context.service.ErrorHandler; import dev.struchkov.godfather.quarkus.context.service.Modifiable; import dev.struchkov.godfather.quarkus.context.service.PersonSettingService; -import dev.struchkov.godfather.quarkus.context.service.Sending; import dev.struchkov.godfather.quarkus.core.action.ActionUnit; -import dev.struchkov.godfather.quarkus.core.action.AnswerCheckAction; -import dev.struchkov.godfather.quarkus.core.action.AnswerSaveAction; -import dev.struchkov.godfather.quarkus.core.action.AnswerTextAction; -import dev.struchkov.godfather.quarkus.core.action.cmd.ReplaceCmdAction; import dev.struchkov.godfather.quarkus.core.service.StorylineService; import dev.struchkov.godfather.quarkus.domain.unit.MainUnit; import dev.struchkov.godfather.quarkus.domain.unit.UnitRequest; @@ -38,46 +31,26 @@ public class GeneralAutoResponder { protected final PersonSettingService personSettingService; protected final StorylineService storyLineService; - protected Map actionUnitMap = new HashMap<>(); + protected Map> actionUnitMap = new HashMap<>(); protected List> modifiable; protected ErrorHandler errorHandler; protected GeneralAutoResponder( - Sending sending, PersonSettingService personSettingService, StorylineService storyLineService ) { this.personSettingService = personSettingService; this.storyLineService = storyLineService; - init(sending); } - private void init(Sending sending) { - actionUnitMap.put(TypeUnit.CHECK, new AnswerCheckAction<>(sending)); - actionUnitMap.put(TypeUnit.TEXT, new AnswerTextAction(sending)); - actionUnitMap.put(TypeUnit.REPLACE_CMD, new ReplaceCmdAction()); + public void registrationActionUnit(ActionUnit actionUnit) { + actionUnitMap.computeIfAbsent(actionUnit.getUnitType(), k -> new HashMap<>()).putIfAbsent(actionUnit.getMessageType(), actionUnit); } public void initModifiable(List> modifiable) { this.modifiable = modifiable; } - public void initTextAnswerActionUnit(AnswerTextAction action) { - actionUnitMap.put(TypeUnit.TEXT, action); - } - - public void initActionUnit(String typeUnit, ActionUnit, M> actionUnit) { - if (!actionUnitMap.containsKey(typeUnit)) { - actionUnitMap.put(typeUnit, actionUnit); - } else { - throw new ConfigAppException("Обработка такого типа юнита уже зарегистрирована"); - } - } - - public void initSaveAction(AnswerSaveAction answerSaveAction) { - actionUnitMap.put(TypeUnit.SAVE, answerSaveAction); - } - /** * Позволяет установить перехватчик и обработчик исключений, возникающих при обработке юнитов. */ @@ -92,7 +65,7 @@ public class GeneralAutoResponder { public Uni processingNewMessage(M newMessage) { return Uni.createFrom().item(newMessage) .onItem().ifNotNull().transformToUni( - message -> personSettingService.getStateProcessingByPersonId(newMessage.getPersonId()) + message -> personSettingService.getStateProcessingByPersonId(newMessage.getFromPersonId()) .replaceIfNullWith(TRUE) .chain( state -> { @@ -113,14 +86,14 @@ public class GeneralAutoResponder { if (checkEmpty(newMessages)) return Uni.createFrom().voidItem(); final Set personIds = newMessages.stream() - .map(Message::getPersonId) + .map(Message::getFromPersonId) .collect(Collectors.toSet()); return personSettingService.getAllPersonIdDisableMessages(personIds) .replaceIfNullWith(emptySet()) .onItem().transformToMulti( disableIds -> { final List allowedMessages = newMessages.stream() - .filter(message -> !disableIds.contains(message.getPersonId())) + .filter(message -> !disableIds.contains(message.getFromPersonId())) .toList(); return Multi.createFrom().iterable(allowedMessages); } @@ -138,7 +111,7 @@ public class GeneralAutoResponder { .onItem().transformToMulti(modifiables -> Multi.createFrom().iterable(modifiables)) .onItem().transformToUni(mModifiable -> mModifiable.change(message)) .concatenate().toUni().replaceWith( - storyLineService.getNextUnitByPersonId(message.getPersonId()) + storyLineService.getNextUnitByPersonId(message.getFromPersonId()) .onItem().ifNotNull().transformToUni( nextUnits -> Uni.createFrom().optional( Responder.nextUnit(message, nextUnits).or(storyLineService::getDefaultUnit) @@ -184,29 +157,31 @@ public class GeneralAutoResponder { final M message = unitRequest.getMessage(); final String typeUnit = unit.getType(); if (actionUnitMap.containsKey(typeUnit)) { - ActionUnit actionUnit = actionUnitMap.get(typeUnit); - return actionUnit.action(unitRequest) - .onItem().transformToUni( - newUnitRequest -> { - final Optional> optDefaultUnit = storyLineService.getDefaultUnit(); - if (!unit.isNotSaveHistory() && (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unit))) { - return Uni.combine().all().unis( - Uni.createFrom().item(newUnitRequest), - storyLineService.save(message.getPersonId(), unit.getName(), message) - ).asTuple(); + Map actionMap = actionUnitMap.get(typeUnit); + if (actionMap.containsKey(message.getClass())) { + final ActionUnit actionUnit = actionMap.get(message.getClass()); + return actionUnit.action(unitRequest) + .flatMap( + newUnitRequest -> { + final Optional> optDefaultUnit = storyLineService.getDefaultUnit(); + if (!unit.isNotSaveHistory() && (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unit))) { + return Uni.combine().all().unis( + Uni.createFrom().item(newUnitRequest), + storyLineService.save(message.getFromPersonId(), unit.getName(), message) + ).asTuple(); + } + return Uni.combine().all().unis(Uni.createFrom().item(newUnitRequest), Uni.createFrom().voidItem()).asTuple(); } - return Uni.combine().all().unis(Uni.createFrom().item(newUnitRequest), Uni.createFrom().voidItem()).asTuple(); - } - ).onItem().transformToUni( - t -> { - final UnitRequest newUnitRequest = t.getItem1(); - final MainUnit newUnit = newUnitRequest.getUnit(); - return !unit.equals(newUnit) ? getAction(newUnitRequest) : Uni.createFrom().item(unitRequest); - } - ); - } else { - throw new NotFoundException("ActionUnit для типа {0} не зарегистрирован", unit.getType()); + ).onItem().transformToUni( + t -> { + final UnitRequest newUnitRequest = t.getItem1(); + final MainUnit newUnit = newUnitRequest.getUnit(); + return !unit.equals(newUnit) ? getAction(newUnitRequest) : Uni.createFrom().item(unitRequest); + } + ); + } } + throw new NotFoundException("ActionUnit для типа {0} не зарегистрирован", unit.getType()); } } diff --git a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/ActionUnit.java b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/ActionUnit.java index a2a20e8..a122b10 100644 --- a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/ActionUnit.java +++ b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/ActionUnit.java @@ -10,7 +10,6 @@ import io.smallrye.mutiny.Uni; * * @author upagge [11/07/2019] */ -@FunctionalInterface public interface ActionUnit { /** @@ -20,4 +19,8 @@ public interface ActionUnit { */ Uni> action(UnitRequest unitRequest); + String getUnitType(); + + Class getMessageType(); + } diff --git a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerCheckAction.java b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerCheckAction.java index e27311a..9d75eeb 100644 --- a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerCheckAction.java +++ b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerCheckAction.java @@ -4,6 +4,7 @@ import dev.struchkov.godfather.main.domain.content.Message; import dev.struchkov.godfather.quarkus.context.service.Sending; import dev.struchkov.godfather.quarkus.domain.BoxAnswer; import dev.struchkov.godfather.quarkus.domain.unit.AnswerCheck; +import dev.struchkov.godfather.quarkus.domain.unit.AnswerText; import dev.struchkov.godfather.quarkus.domain.unit.MainUnit; import dev.struchkov.godfather.quarkus.domain.unit.UnitRequest; import io.smallrye.mutiny.Uni; @@ -18,7 +19,7 @@ import static dev.struchkov.haiti.utils.Checker.checkTrue; * * @author upagge [11/07/2019] */ -public class AnswerCheckAction implements ActionUnit, M> { +public class AnswerCheckAction implements ActionUnit, Message> { private final Sending sending; @@ -27,22 +28,22 @@ public class AnswerCheckAction implements ActionUnit> action(UnitRequest, M> unitRequest) { - final AnswerCheck unit = unitRequest.getUnit(); - final M message = unitRequest.getMessage(); + public Uni> action(UnitRequest, Message> unitRequest) { + final AnswerCheck unit = unitRequest.getUnit(); + final Message message = unitRequest.getMessage(); return unit.getCheck().checked(message) .onItem().call(checkValue -> { if (checkTrue(checkValue)) { final BoxAnswer answerIfTrue = unit.getIntermediateAnswerIfTrue(); if (checkNotNull(answerIfTrue)) { - answerIfTrue.setRecipientIfNull(message.getPersonId()); + answerIfTrue.setRecipientIfNull(message.getFromPersonId()); return sending.send(answerIfTrue); } } else { final BoxAnswer answerIfFalse = unit.getIntermediateAnswerIfFalse(); if (checkNotNull(answerIfFalse)) { - answerIfFalse.setRecipientIfNull(message.getPersonId()); + answerIfFalse.setRecipientIfNull(message.getFromPersonId()); return sending.send(answerIfFalse); } } @@ -59,4 +60,14 @@ public class AnswerCheckAction implements ActionUnit getMessageType() { + return Message.class; + } + } diff --git a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerSaveAction.java b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerSaveAction.java index 189e603..4b18a09 100644 --- a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerSaveAction.java +++ b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerSaveAction.java @@ -17,17 +17,17 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull; * * @author upagge [11/07/2019] */ -public class AnswerSaveAction implements ActionUnit, M> { +public class AnswerSaveAction implements ActionUnit, Message> { @Override - public Uni> action(UnitRequest, M> unitRequest) { - final AnswerSave answerSave = unitRequest.getUnit(); - final M message = unitRequest.getMessage(); + public Uni> action(UnitRequest, Message> unitRequest) { + final AnswerSave answerSave = unitRequest.getUnit(); + final Message message = unitRequest.getMessage(); final AnswerSavePreservable preservable = answerSave.getPreservable(); - final String personId = message.getPersonId(); + final String personId = message.getFromPersonId(); - final CheckSave checkSave = answerSave.getCheckSave(); + final CheckSave checkSave = answerSave.getCheckSave(); if (checkNotNull(checkSave)) { return Uni.createFrom().voidItem() .onItem().transformToUni( @@ -43,7 +43,7 @@ public class AnswerSaveAction implements ActionUnit preservableData = answerSave.getPreservableData(); + final PreservableData preservableData = answerSave.getPreservableData(); final Pusher pusher = answerSave.getPusher(); return Uni.createFrom().voidItem() @@ -72,4 +72,15 @@ public class AnswerSaveAction implements ActionUnit UnitRequest.of(answerSave, message) ); } + + @Override + public String getUnitType() { + return AnswerSave.TYPE; + } + + @Override + public Class getMessageType() { + return Message.class; + } + } diff --git a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerTextAction.java b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerTextAction.java index ddb3c6b..0d7fbf4 100644 --- a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerTextAction.java +++ b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerTextAction.java @@ -1,6 +1,6 @@ package dev.struchkov.godfather.quarkus.core.action; -import dev.struchkov.godfather.main.domain.content.Message; +import dev.struchkov.godfather.main.domain.content.Mail; import dev.struchkov.godfather.quarkus.context.service.Sending; import dev.struchkov.godfather.quarkus.domain.unit.AnswerText; import dev.struchkov.godfather.quarkus.domain.unit.MainUnit; @@ -15,7 +15,7 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull; * * @author upagge [11/07/2019] */ -public class AnswerTextAction implements ActionUnit, Message> { +public class AnswerTextAction implements ActionUnit, Mail> { private final Sending sending; @@ -24,13 +24,13 @@ public class AnswerTextAction implements ActionUnit, Message } @Override - public Uni> action(UnitRequest, Message> unitRequest) { - final Message message = unitRequest.getMessage(); - final AnswerText unit = unitRequest.getUnit(); + public Uni> action(UnitRequest, Mail> unitRequest) { + final Mail message = unitRequest.getMessage(); + final AnswerText unit = unitRequest.getUnit(); return unit.getAnswer().processing(message) .onItem().ifNotNull().transformToUni(boxAnswer -> { - boxAnswer.setRecipientIfNull(message.getPersonId()); + boxAnswer.setRecipientIfNull(message.getFromPersonId()); return sending.send(boxAnswer); }) .onItem().ifNotNull().transformToUni(sentBox -> { @@ -43,4 +43,14 @@ public class AnswerTextAction implements ActionUnit, Message .replaceWith(UnitRequest.of(unit, message)); } + @Override + public String getUnitType() { + return AnswerText.TYPE; + } + + @Override + public Class getMessageType() { + return Mail.class; + } + } diff --git a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerTextChatMailAction.java b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerTextChatMailAction.java new file mode 100644 index 0000000..d32f62b --- /dev/null +++ b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/AnswerTextChatMailAction.java @@ -0,0 +1,56 @@ +package dev.struchkov.godfather.quarkus.core.action; + +import dev.struchkov.godfather.main.domain.content.ChatMail; +import dev.struchkov.godfather.quarkus.context.service.Sending; +import dev.struchkov.godfather.quarkus.domain.unit.AnswerText; +import dev.struchkov.godfather.quarkus.domain.unit.MainUnit; +import dev.struchkov.godfather.quarkus.domain.unit.UnitRequest; +import dev.struchkov.godfather.quarkus.domain.unit.func.CallBackConsumer; +import io.smallrye.mutiny.Uni; + +import static dev.struchkov.haiti.utils.Checker.checkNotNull; + +/** + * Обработчик Unit-а {@link AnswerText}. + * + * @author upagge [11/07/2019] + */ +public class AnswerTextChatMailAction implements ActionUnit, ChatMail> { + + private final Sending sending; + + public AnswerTextChatMailAction(Sending sending) { + this.sending = sending; + } + + @Override + public Uni> action(UnitRequest, ChatMail> unitRequest) { + final ChatMail message = unitRequest.getMessage(); + final AnswerText unit = unitRequest.getUnit(); + + return unit.getAnswer().processing(message) + .onItem().ifNotNull().transformToUni(boxAnswer -> { + boxAnswer.setRecipientIfNull(message.getChatId()); + return sending.send(boxAnswer); + }) + .onItem().ifNotNull().transformToUni(sentBox -> { + final CallBackConsumer callBack = unit.getCallBack(); + if (checkNotNull(callBack)) { + return callBack.processing(sentBox); + } + return Uni.createFrom().voidItem(); + }) + .replaceWith(UnitRequest.of(unit, message)); + } + + @Override + public String getUnitType() { + return AnswerText.TYPE; + } + + @Override + public Class getMessageType() { + return ChatMail.class; + } + +} diff --git a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/cmd/ReplaceCmdAction.java b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/cmd/ReplaceCmdAction.java index 2445c5c..baf9923 100644 --- a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/cmd/ReplaceCmdAction.java +++ b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/cmd/ReplaceCmdAction.java @@ -16,4 +16,14 @@ public class ReplaceCmdAction implements ActionUnit, Message return Uni.createFrom().item(UnitRequest.of(unit.getThisUnit(), message)); } + @Override + public String getUnitType() { + return ReplaceCmd.TYPE; + } + + @Override + public Class getMessageType() { + return Message.class; + } + } diff --git a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/cmd/RollBackCmdAction.java b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/cmd/RollBackCmdAction.java index da29931..714bf58 100644 --- a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/cmd/RollBackCmdAction.java +++ b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/action/cmd/RollBackCmdAction.java @@ -11,35 +11,45 @@ import io.smallrye.mutiny.Uni; import static dev.struchkov.godfather.exception.RollBackException.rollBackException; -public class RollBackCmdAction implements ActionUnit, M> { +public class RollBackCmdAction implements ActionUnit, Message> { - private final StorylineService storyLineService; + private final StorylineService storyLineService; - public RollBackCmdAction(StorylineService storyLineService) { + public RollBackCmdAction(StorylineService storyLineService) { this.storyLineService = storyLineService; } @Override - public Uni> action(UnitRequest, M> unitRequest) { - final RollBackCmd unit = unitRequest.getUnit(); - final M message = unitRequest.getMessage(); + public Uni> action(UnitRequest, Message> unitRequest) { + final RollBackCmd unit = unitRequest.getUnit(); + final Message message = unitRequest.getMessage(); final int countToBack = unit.getCountBack(); final String rollbackUnitName = unit.getRollbackUnitName(); final Uni uniHistory = (rollbackUnitName != null) - ? storyLineService.replaceUserToBack(message.getPersonId(), rollbackUnitName).onItem().ifNull().failWith(rollBackException("Юнит для возвращения не был найден")) - : storyLineService.replaceUserToBack(message.getPersonId(), countToBack).onItem().ifNull().failWith(rollBackException("Юнит для возвращения не был найден")); + ? storyLineService.replaceUserToBack(message.getFromPersonId(), rollbackUnitName).onItem().ifNull().failWith(rollBackException("Юнит для возвращения не был найден")) + : storyLineService.replaceUserToBack(message.getFromPersonId(), countToBack).onItem().ifNull().failWith(rollBackException("Юнит для возвращения не был найден")); return uniHistory .onItem().transform( history -> { final String unitName = history.getUnitName(); - final MainUnit nextUnit = storyLineService.getUnitByName(unitName).orElse(unit); - final M oldMessage = (M) history.getMessage(); + final MainUnit nextUnit = storyLineService.getUnitByName(unitName).orElse(unit); + final Message oldMessage = history.getMessage(); return UnitRequest.of(nextUnit, oldMessage); } ); } + @Override + public String getUnitType() { + return RollBackCmd.TYPE; + } + + @Override + public Class getMessageType() { + return Message.class; + } + } diff --git a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/GeneralAutoResponder.java b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/GeneralAutoResponder.java index 8a3a297..4774930 100644 --- a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/GeneralAutoResponder.java +++ b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/GeneralAutoResponder.java @@ -1,30 +1,26 @@ package dev.struchkov.godfather.simple.core; import dev.struchkov.autoresponder.Responder; -import dev.struchkov.godfather.exception.ConfigAppException; import dev.struchkov.godfather.main.domain.content.Message; -import dev.struchkov.godfather.main.domain.unit.TypeUnit; import dev.struchkov.godfather.main.domain.unit.UnitActiveType; import dev.struchkov.godfather.simple.context.service.Accessibility; import dev.struchkov.godfather.simple.context.service.ErrorHandler; import dev.struchkov.godfather.simple.context.service.Modifiable; import dev.struchkov.godfather.simple.context.service.PersonSettingService; -import dev.struchkov.godfather.simple.context.service.Sending; import dev.struchkov.godfather.simple.core.action.ActionUnit; -import dev.struchkov.godfather.simple.core.action.AnswerCheckAction; -import dev.struchkov.godfather.simple.core.action.AnswerSaveAction; -import dev.struchkov.godfather.simple.core.action.AnswerTextAction; -import dev.struchkov.godfather.simple.core.action.cmd.ReplaceCmdAction; import dev.struchkov.godfather.simple.core.service.StorylineService; import dev.struchkov.godfather.simple.domain.unit.MainUnit; import dev.struchkov.godfather.simple.domain.unit.UnitRequest; import dev.struchkov.haiti.context.exception.NotFoundException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; @@ -33,48 +29,30 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull; public class GeneralAutoResponder { + private static final Logger log = LoggerFactory.getLogger(GeneralAutoResponder.class); + protected final PersonSettingService personSettingService; protected final StorylineService storyLineService; - protected Map actionUnitMap = new HashMap<>(); + protected Map> actionUnitMap = new HashMap<>(); protected List> modifiable; protected ErrorHandler errorHandler; protected ExecutorService executorService; protected GeneralAutoResponder( - Sending sending, PersonSettingService personSettingService, StorylineService storyLineService ) { this.personSettingService = personSettingService; this.storyLineService = storyLineService; - init(sending); - } - - private void init(Sending sending) { - actionUnitMap.put(TypeUnit.CHECK, new AnswerCheckAction<>(sending)); - actionUnitMap.put(TypeUnit.TEXT, new AnswerTextAction(sending)); - actionUnitMap.put(TypeUnit.REPLACE_CMD, new ReplaceCmdAction()); } public void initModifiable(List> modifiable) { this.modifiable = modifiable; } - public void initTextAnswerActionUnit(AnswerTextAction action) { - actionUnitMap.put(TypeUnit.TEXT, action); - } - - public void initActionUnit(String typeUnit, ActionUnit, M> actionUnit) { - if (!actionUnitMap.containsKey(typeUnit)) { - actionUnitMap.put(typeUnit, actionUnit); - } else { - throw new ConfigAppException("Обработка такого типа юнита уже зарегистрирована"); - } - } - - public void initSaveAction(AnswerSaveAction answerSaveAction) { - actionUnitMap.put(TypeUnit.SAVE, answerSaveAction); + public void registrationActionUnit(ActionUnit actionUnit) { + actionUnitMap.computeIfAbsent(actionUnit.getUnitType(), k -> new HashMap<>()).putIfAbsent(actionUnit.getMessageType(), actionUnit); } /** @@ -95,15 +73,20 @@ public class GeneralAutoResponder { public void processingNewMessage(M newMessage) { if (checkNotNull(newMessage)) { if (checkNotNull(executorService)) { - executorService.submit(() -> { - final boolean state = personSettingService.getStateProcessingByPersonId(newMessage.getPersonId()) - .orElse(true); - if (state) { - processing(newMessage); - } + CompletableFuture.runAsync( + () -> { + final boolean state = personSettingService.getStateProcessingByPersonId(newMessage.getFromPersonId()) + .orElse(true); + if (state) { + processing(newMessage); + } + }, executorService + ).exceptionally(ex -> { + log.error(ex.getMessage(), ex); + return null; }); } else { - final boolean state = personSettingService.getStateProcessingByPersonId(newMessage.getPersonId()) + final boolean state = personSettingService.getStateProcessingByPersonId(newMessage.getFromPersonId()) .orElse(true); if (state) { processing(newMessage); @@ -115,11 +98,11 @@ public class GeneralAutoResponder { public void processingNewMessages(List newMessages) { if (checkNotEmpty(newMessages)) { final Set personIds = newMessages.stream() - .map(Message::getPersonId) + .map(Message::getFromPersonId) .collect(Collectors.toSet()); final Set disableIds = personSettingService.getAllPersonIdDisableMessages(personIds); final List allowedMessages = newMessages.stream() - .filter(message -> !disableIds.contains(message.getPersonId())) + .filter(message -> !disableIds.contains(message.getFromPersonId())) .toList(); if (checkNotNull(executorService)) { for (M allowedMessage : allowedMessages) { @@ -137,7 +120,7 @@ public class GeneralAutoResponder { if (checkNotEmpty(modifiable)) { modifiable.forEach(m -> m.change(message)); } - final Set> units = storyLineService.getNextUnitByPersonId(message.getPersonId()); + final Set> units = storyLineService.getNextUnitByPersonId(message.getFromPersonId()); final Optional> optAnswer = Responder.nextUnit(message, units) .or(storyLineService::getDefaultUnit); if (optAnswer.isPresent()) { @@ -183,17 +166,19 @@ public class GeneralAutoResponder { final M message = unitRequest.getMessage(); final String typeUnit = unit.getType(); if (actionUnitMap.containsKey(typeUnit)) { - ActionUnit actionUnit = actionUnitMap.get(typeUnit); - UnitRequest, M> newUnitRequest = actionUnit.action(unitRequest); - final Optional> optDefaultUnit = storyLineService.getDefaultUnit(); - if (!unit.isNotSaveHistory() && (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unit))) { - storyLineService.save(message.getPersonId(), unit.getName(), message); + Map actionMap = actionUnitMap.get(typeUnit); + if (actionMap.containsKey(message.getClass())) { + final ActionUnit actionUnit = actionMap.get(message.getClass()); + UnitRequest, M> newUnitRequest = actionUnit.action(unitRequest); + final Optional> optDefaultUnit = storyLineService.getDefaultUnit(); + if (!unit.isNotSaveHistory() && (optDefaultUnit.isEmpty() || !optDefaultUnit.get().equals(unit))) { + storyLineService.save(message.getFromPersonId(), unit.getName(), message); + } + final MainUnit newUnit = newUnitRequest.getUnit(); + return !unit.equals(newUnit) ? getAction(newUnitRequest) : unitRequest; } - final MainUnit newUnit = newUnitRequest.getUnit(); - return !unit.equals(newUnit) ? getAction(newUnitRequest) : unitRequest; - } else { - throw new NotFoundException("ActionUnit для типа {0} не зарегистрирован", unit.getType()); } + throw new NotFoundException("ActionUnit для типа {0} не зарегистрирован", unit.getType()); } } diff --git a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/ActionUnit.java b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/ActionUnit.java index fdc55f4..478ec64 100644 --- a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/ActionUnit.java +++ b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/ActionUnit.java @@ -9,7 +9,6 @@ import dev.struchkov.godfather.simple.domain.unit.UnitRequest; * * @author upagge [11/07/2019] */ -@FunctionalInterface public interface ActionUnit { /** @@ -19,4 +18,8 @@ public interface ActionUnit { */ UnitRequest action(UnitRequest unitRequest); + String getUnitType(); + + Class getMessageType(); + } diff --git a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerCheckAction.java b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerCheckAction.java index ac448e6..8b477bc 100644 --- a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerCheckAction.java +++ b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerCheckAction.java @@ -18,7 +18,7 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull; * * @author upagge [11/07/2019] */ -public class AnswerCheckAction implements ActionUnit, M> { +public class AnswerCheckAction implements ActionUnit, Message> { private static final Logger log = LoggerFactory.getLogger(AnswerCheckAction.class); @@ -29,18 +29,18 @@ public class AnswerCheckAction implements ActionUnit action(UnitRequest, M> unitRequest) { - final AnswerCheck unit = unitRequest.getUnit(); + public UnitRequest action(UnitRequest, Message> unitRequest) { + final AnswerCheck unit = unitRequest.getUnit(); log.debug("Началась обработка unit: {}.", unit.getName()); - final M message = unitRequest.getMessage(); + final Message message = unitRequest.getMessage(); - MainUnit unitAnswer; + MainUnit unitAnswer; if (unit.getCheck().checked(message)) { log.debug("Unit: {}. Проверка пройдена", unit.getName()); final BoxAnswer answerIfTrue = unit.getIntermediateAnswerIfTrue(); if (checkNotNull(answerIfTrue)) { - answerIfTrue.setRecipientIfNull(message.getPersonId()); + answerIfTrue.setRecipientIfNull(message.getFromPersonId()); sending.send(answerIfTrue); } unitAnswer = unit.getUnitTrue(); @@ -48,7 +48,7 @@ public class AnswerCheckAction implements ActionUnit implements ActionUnit getMessageType() { + return Message.class; + } + } diff --git a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerSaveAction.java b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerSaveAction.java index 10f8fe8..6796471 100644 --- a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerSaveAction.java +++ b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerSaveAction.java @@ -16,25 +16,25 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull; * * @author upagge [11/07/2019] */ -public class AnswerSaveAction implements ActionUnit, M> { +public class AnswerSaveAction implements ActionUnit, Message> { @Override - public UnitRequest action(UnitRequest, M> unitRequest) { - final AnswerSave answerSave = unitRequest.getUnit(); - final M message = unitRequest.getMessage(); + public UnitRequest action(UnitRequest, Message> unitRequest) { + final AnswerSave answerSave = unitRequest.getUnit(); + final Message message = unitRequest.getMessage(); final AnswerSavePreservable preservable = answerSave.getPreservable(); - final String personId = message.getPersonId(); + final String personId = message.getFromPersonId(); - final CheckSave checkSave = answerSave.getCheckSave(); + final CheckSave checkSave = answerSave.getCheckSave(); if (checkNotNull(checkSave)) { - MainUnit unit = checkSave.check(message); + MainUnit unit = checkSave.check(message); if (checkNotNull(unit)) { return UnitRequest.of(unit, message); } } - final PreservableData preservableData = answerSave.getPreservableData(); + final PreservableData preservableData = answerSave.getPreservableData(); if (checkNotNull(preservableData)) { D data = preservableData.getData(message); if (checkNotNull(data)) { @@ -49,4 +49,14 @@ public class AnswerSaveAction implements ActionUnit getMessageType() { + return Message.class; + } + } diff --git a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerTextChatMailAction.java b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerTextChatMailAction.java new file mode 100644 index 0000000..05cbb62 --- /dev/null +++ b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerTextChatMailAction.java @@ -0,0 +1,65 @@ +package dev.struchkov.godfather.simple.core.action; + +import dev.struchkov.godfather.main.domain.content.ChatMail; +import dev.struchkov.godfather.simple.context.service.Sending; +import dev.struchkov.godfather.simple.domain.BoxAnswer; +import dev.struchkov.godfather.simple.domain.SentBox; +import dev.struchkov.godfather.simple.domain.unit.AnswerText; +import dev.struchkov.godfather.simple.domain.unit.MainUnit; +import dev.struchkov.godfather.simple.domain.unit.UnitRequest; +import dev.struchkov.godfather.simple.domain.unit.func.CallBackConsumer; +import dev.struchkov.godfather.simple.domain.unit.func.ProcessingData; + +import java.util.Optional; + +import static dev.struchkov.haiti.utils.Checker.checkNotNull; + +/** + * Обработчик Unit-а {@link AnswerText}. + * + * @author upagge [11/07/2019] + */ +public class AnswerTextChatMailAction implements ActionUnit, ChatMail> { + + private final Sending sending; + + public AnswerTextChatMailAction(Sending sending) { + this.sending = sending; + } + + @Override + public UnitRequest action(UnitRequest, ChatMail> unitRequest) { + final ChatMail message = unitRequest.getMessage(); + final AnswerText unit = unitRequest.getUnit(); + + final ProcessingData answerProcessing = unit.getAnswer(); + if (checkNotNull(answerProcessing)) { + final Optional optAnswer = answerProcessing.processing(message); + if (optAnswer.isPresent()) { + final BoxAnswer answer = optAnswer.get(); + + answer.setRecipientIfNull(message.getChatId()); + + final Optional optSentBox = sending.send(answer); + final CallBackConsumer callBack = unit.getCallBack(); + if (checkNotNull(callBack) && optAnswer.isPresent()) { + final SentBox sentBox = optSentBox.get(); + callBack.processing(sentBox); + } + } + } + + return UnitRequest.of(unit, message); + } + + @Override + public String getUnitType() { + return AnswerText.TYPE; + } + + @Override + public Class getMessageType() { + return ChatMail.class; + } + +} diff --git a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerTextAction.java b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerTextMailAction.java similarity index 67% rename from bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerTextAction.java rename to bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerTextMailAction.java index 4818d69..cab6df2 100644 --- a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerTextAction.java +++ b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/AnswerTextMailAction.java @@ -1,6 +1,6 @@ package dev.struchkov.godfather.simple.core.action; -import dev.struchkov.godfather.main.domain.content.Message; +import dev.struchkov.godfather.main.domain.content.Mail; import dev.struchkov.godfather.simple.context.service.Sending; import dev.struchkov.godfather.simple.domain.BoxAnswer; import dev.struchkov.godfather.simple.domain.SentBox; @@ -19,26 +19,26 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull; * * @author upagge [11/07/2019] */ -public class AnswerTextAction implements ActionUnit, Message> { +public class AnswerTextMailAction implements ActionUnit, Mail> { private final Sending sending; - public AnswerTextAction(Sending sending) { + public AnswerTextMailAction(Sending sending) { this.sending = sending; } @Override - public UnitRequest action(UnitRequest, Message> unitRequest) { - final Message message = unitRequest.getMessage(); - final AnswerText unit = unitRequest.getUnit(); + public UnitRequest action(UnitRequest, Mail> unitRequest) { + final Mail message = unitRequest.getMessage(); + final AnswerText unit = unitRequest.getUnit(); - final ProcessingData answerProcessing = unit.getAnswer(); + final ProcessingData answerProcessing = unit.getAnswer(); if (checkNotNull(answerProcessing)) { final Optional optAnswer = answerProcessing.processing(message); if (optAnswer.isPresent()) { final BoxAnswer answer = optAnswer.get(); - answer.setRecipientIfNull(message.getPersonId()); + answer.setRecipientIfNull(message.getFromPersonId()); final Optional optSentBox = sending.send(answer); final CallBackConsumer callBack = unit.getCallBack(); @@ -52,4 +52,14 @@ public class AnswerTextAction implements ActionUnit, Message return UnitRequest.of(unit, message); } + @Override + public String getUnitType() { + return AnswerText.TYPE; + } + + @Override + public Class getMessageType() { + return Mail.class; + } + } diff --git a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/cmd/ReplaceCmdAction.java b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/cmd/ReplaceCmdAction.java index 8e5ddc4..03c3cf2 100644 --- a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/cmd/ReplaceCmdAction.java +++ b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/cmd/ReplaceCmdAction.java @@ -15,4 +15,14 @@ public class ReplaceCmdAction implements ActionUnit, Message return UnitRequest.of(unit.getThisUnit(), message); } + @Override + public String getUnitType() { + return ReplaceCmd.TYPE; + } + + @Override + public Class getMessageType() { + return Message.class; + } + } diff --git a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/cmd/RollBackCmdAction.java b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/cmd/RollBackCmdAction.java index fa656c5..3375953 100644 --- a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/cmd/RollBackCmdAction.java +++ b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/action/cmd/RollBackCmdAction.java @@ -10,29 +10,39 @@ import dev.struchkov.godfather.simple.domain.unit.cmd.RollBackCmd; import static dev.struchkov.godfather.exception.RollBackException.rollBackException; -public class RollBackCmdAction implements ActionUnit, M> { +public class RollBackCmdAction implements ActionUnit, Message> { - private final StorylineService storyLineService; + private final StorylineService storyLineService; - public RollBackCmdAction(StorylineService storyLineService) { + public RollBackCmdAction(StorylineService storyLineService) { this.storyLineService = storyLineService; } @Override - public UnitRequest action(UnitRequest, M> unitRequest) { - final RollBackCmd unit = unitRequest.getUnit(); - final M message = unitRequest.getMessage(); + public UnitRequest action(UnitRequest, Message> unitRequest) { + final RollBackCmd unit = unitRequest.getUnit(); + final Message message = unitRequest.getMessage(); final int countToBack = unit.getCountBack(); final String rollbackUnitName = unit.getRollbackUnitName(); StorylineHistory history = rollbackUnitName != null - ? storyLineService.replaceUserToBack(message.getPersonId(), rollbackUnitName).orElseThrow(rollBackException("Юнит для возвращения не был найден")) - : storyLineService.replaceUserToBack(message.getPersonId(), countToBack).orElseThrow(rollBackException("Юнит для возвращения не был найден")); + ? storyLineService.replaceUserToBack(message.getFromPersonId(), rollbackUnitName).orElseThrow(rollBackException("Юнит для возвращения не был найден")) + : storyLineService.replaceUserToBack(message.getFromPersonId(), countToBack).orElseThrow(rollBackException("Юнит для возвращения не был найден")); final String unitName = history.getUnitName(); - final MainUnit nextUnit = storyLineService.getUnitByName(unitName).orElse(unit); - final M oldMessage = (M) history.getMessage(); + final MainUnit nextUnit = storyLineService.getUnitByName(unitName).orElse(unit); + final Message oldMessage = history.getMessage(); return UnitRequest.of(nextUnit, oldMessage); } + @Override + public String getUnitType() { + return RollBackCmd.TYPE; + } + + @Override + public Class getMessageType() { + return Message.class; + } + } diff --git a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/provider/ChatStoryLineHandler.java b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/provider/ChatStoryLineHandler.java new file mode 100644 index 0000000..70400b8 --- /dev/null +++ b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/provider/ChatStoryLineHandler.java @@ -0,0 +1,25 @@ +package dev.struchkov.godfather.simple.core.provider; + +import dev.struchkov.godfather.main.domain.content.ChatMail; +import dev.struchkov.godfather.simple.context.service.EventHandler; +import dev.struchkov.godfather.simple.core.GeneralAutoResponder; + +public class ChatStoryLineHandler implements EventHandler { + + private final GeneralAutoResponder generalAutoResponder; + + public ChatStoryLineHandler(GeneralAutoResponder generalAutoResponder) { + this.generalAutoResponder = generalAutoResponder; + } + + @Override + public void handle(ChatMail message) { + generalAutoResponder.processingNewMessage(message); + } + + @Override + public String getEventType() { + return ChatMail.TYPE; + } + +} diff --git a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/provider/StoryLineHandler.java b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/provider/PersonStoryLineHandler.java similarity index 79% rename from bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/provider/StoryLineHandler.java rename to bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/provider/PersonStoryLineHandler.java index 8fa3423..8476abd 100644 --- a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/provider/StoryLineHandler.java +++ b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/provider/PersonStoryLineHandler.java @@ -4,11 +4,11 @@ import dev.struchkov.godfather.main.domain.content.Mail; import dev.struchkov.godfather.simple.context.service.EventHandler; import dev.struchkov.godfather.simple.core.GeneralAutoResponder; -public class StoryLineHandler implements EventHandler { +public class PersonStoryLineHandler implements EventHandler { private final GeneralAutoResponder generalAutoResponder; - public StoryLineHandler(GeneralAutoResponder generalAutoResponder) { + public PersonStoryLineHandler(GeneralAutoResponder generalAutoResponder) { this.generalAutoResponder = generalAutoResponder; } diff --git a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/service/StorylineMailService.java b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/service/StorylineMailService.java index 6d7b414..6fd6821 100644 --- a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/service/StorylineMailService.java +++ b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/service/StorylineMailService.java @@ -3,7 +3,7 @@ package dev.struchkov.godfather.simple.core.service; import dev.struchkov.autoresponder.entity.Unit; import dev.struchkov.godfather.main.domain.StorylineHistory; import dev.struchkov.godfather.main.domain.UnitPointer; -import dev.struchkov.godfather.main.domain.content.Mail; +import dev.struchkov.godfather.main.domain.content.Message; import dev.struchkov.godfather.simple.context.repository.StorylineRepository; import dev.struchkov.godfather.simple.context.service.UnitPointerService; import dev.struchkov.godfather.simple.core.Storyline; @@ -20,11 +20,11 @@ import static dev.struchkov.haiti.utils.Inspector.isNotNull; /** * Отвечает за работу со сценарием в личных сообщениях с пользователем. */ -public class StorylineMailService implements StorylineService { +public class StorylineMailService implements StorylineService { private final UnitPointerService unitPointerService; private final StorylineRepository storylineRepository; - private final Storyline storyLine; + private final Storyline storyLine; private String defaultUnitName; public StorylineMailService( @@ -32,7 +32,7 @@ public class StorylineMailService implements StorylineService { StorylineRepository storylineRepository, List unitConfigurations ) { - this.storyLine = new StorylineFactory(unitConfigurations).createStoryLine(); + this.storyLine = new StorylineFactory(unitConfigurations).createStoryLine(); this.unitPointerService = unitPointerService; this.storylineRepository = storylineRepository; } @@ -44,27 +44,27 @@ public class StorylineMailService implements StorylineService { } @Override - public Optional> getUnitNameByPersonId(@NotNull String personId) { + public Optional> getUnitNameByPersonId(@NotNull String personId) { isNotNull(personId); return unitPointerService.getUnitNameByPersonId(personId) .flatMap(storyLine::getUnit); } @Override - public Set> getNextUnitByPersonId(@NotNull String personId) { - final Optional>> optMainUnits = getUnitNameByPersonId(personId) + public Set> getNextUnitByPersonId(@NotNull String personId) { + final Optional>> optMainUnits = getUnitNameByPersonId(personId) .map(Unit::getNextUnits) .filter(mainUnits -> !mainUnits.isEmpty()); if (optMainUnits.isEmpty()) { storylineRepository.cleanHistoryByPersonId(personId); } - final Set> nextUnits = optMainUnits.orElse(storyLine.getStartingUnits()); + final Set> nextUnits = optMainUnits.orElse(storyLine.getStartingUnits()); nextUnits.addAll(storyLine.getGlobalUnits()); return nextUnits; } @Override - public void save(String personId, String unitName, Mail mail) { + public void save(String personId, String unitName, T mail) { isNotNull(personId, unitName, mail); unitPointerService.save(new UnitPointer(personId, unitName)); @@ -86,7 +86,7 @@ public class StorylineMailService implements StorylineService { } @Override - public Optional> getDefaultUnit() { + public Optional> getDefaultUnit() { if (defaultUnitName == null) return Optional.empty(); return storyLine.getUnit(defaultUnitName); } @@ -103,7 +103,7 @@ public class StorylineMailService implements StorylineService { } @Override - public Optional> getUnitByName(String unitName) { + public Optional> getUnitByName(String unitName) { return storyLine.getUnit(unitName); } diff --git a/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/content/ChatMail.java b/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/content/ChatMail.java new file mode 100644 index 0000000..77d3b31 --- /dev/null +++ b/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/content/ChatMail.java @@ -0,0 +1,52 @@ +package dev.struchkov.godfather.main.domain.content; + +import dev.struchkov.godfather.main.domain.event.Event; +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +@Getter +@Setter +public class ChatMail extends Message implements Event { + + public static final String TYPE = "CHAT_MAIL"; + + private String chatId; + + /** + * Имя отправителя. + */ + private String firstName; + + /** + * Фамилия отправителя. + */ + private String lastName; + + /** + * Вложения к сообщению. + */ + private List attachments = new ArrayList<>(); + + /** + * Пересланные сообщения. + */ + private List forwardMail; + + public void addAttachment(Attachment attachment) { + this.attachments.add(attachment); + } + + public void addAttachments(Collection attachments) { + this.attachments.addAll(attachments); + } + + @Override + public String getEventType() { + return TYPE; + } + +} diff --git a/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/content/Mail.java b/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/content/Mail.java index fde02b1..f7e6ea0 100644 --- a/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/content/Mail.java +++ b/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/content/Mail.java @@ -1,6 +1,8 @@ package dev.struchkov.godfather.main.domain.content; import dev.struchkov.godfather.main.domain.event.Event; +import lombok.Getter; +import lombok.Setter; import java.util.ArrayList; import java.util.Collection; @@ -12,6 +14,8 @@ import java.util.List; * @author upagge [08/07/2019] */ +@Getter +@Setter public class Mail extends Message implements Event { public static final String TYPE = "MAIL"; diff --git a/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/content/Message.java b/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/content/Message.java index 7b33137..e8176e9 100644 --- a/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/content/Message.java +++ b/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/content/Message.java @@ -2,6 +2,8 @@ package dev.struchkov.godfather.main.domain.content; import dev.struchkov.autoresponder.entity.DeliverableText; import dev.struchkov.haiti.utils.container.ContextKey; +import lombok.Getter; +import lombok.Setter; import java.time.LocalDateTime; import java.util.HashMap; @@ -16,6 +18,8 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull; * * @author upagge [08/07/2019] */ +@Getter +@Setter public abstract class Message implements DeliverableText { protected String id; @@ -33,7 +37,7 @@ public abstract class Message implements DeliverableText { /** * Идентификатор пользователя, отправившего сообщение. */ - protected String personId; + protected String fromPersonId; /** * Текстовое сообщение. @@ -44,7 +48,7 @@ public abstract class Message implements DeliverableText { protected Message(Message source) { this.id = source.getId(); - this.personId = source.getPersonId(); + this.fromPersonId = source.getFromPersonId(); this.text = source.getText(); this.createDate = source.getCreateDate(); this.contentType = source.getContentType(); @@ -54,46 +58,6 @@ public abstract class Message implements DeliverableText { protected Message() { } - public ContentType getContentType() { - return contentType; - } - - public void setContentType(ContentType type) { - this.contentType = type; - } - - public LocalDateTime getCreateDate() { - return createDate; - } - - public void setCreateDate(LocalDateTime createDate) { - this.createDate = createDate; - } - - public String getPersonId() { - return personId; - } - - public void setPersonId(String personId) { - this.personId = personId; - } - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - public Map getPayload() { return payload; } diff --git a/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/unit/TypeUnit.java b/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/unit/TypeUnit.java deleted file mode 100644 index 9a3c071..0000000 --- a/bot-domain/bot-domain-main/src/main/java/dev/struchkov/godfather/main/domain/unit/TypeUnit.java +++ /dev/null @@ -1,24 +0,0 @@ -package dev.struchkov.godfather.main.domain.unit; - -import static dev.struchkov.haiti.utils.Exceptions.utilityClass; - -/** - * Тип Unit-а. Обределяет способ обработки. - * - * @author upagge [11/07/2019] - */ -public class TypeUnit { - - public static final String TEXT = "TEXT"; - public static final String SAVE = "SAVE"; - public static final String CHECK = "CHECK"; - - public static final String BACK_CMD = "BACK_CMD"; - public static final String REPLACE_CMD = "REPLACE_CMD"; - - private TypeUnit() { - utilityClass(); - } - - -} diff --git a/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/AnswerCheck.java b/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/AnswerCheck.java index 156456d..a5958e8 100644 --- a/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/AnswerCheck.java +++ b/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/AnswerCheck.java @@ -2,7 +2,6 @@ package dev.struchkov.godfather.quarkus.domain.unit; import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.main.domain.content.Message; -import dev.struchkov.godfather.main.domain.unit.TypeUnit; import dev.struchkov.godfather.main.domain.unit.UnitActiveType; import dev.struchkov.godfather.quarkus.domain.BoxAnswer; import dev.struchkov.godfather.quarkus.domain.unit.func.CheckData; @@ -21,6 +20,8 @@ import java.util.stream.Collectors; */ public class AnswerCheck extends MainUnit { + public static final String TYPE = "ANSWER_CHECK"; + /** * Unit для true. */ @@ -59,7 +60,7 @@ public class AnswerCheck extends MainUnit { new HashSet<>(), builder.activeType, builder.notSaveHistory, - TypeUnit.CHECK + TYPE ); unitTrue = builder.unitTrue; unitFalse = builder.unitFalse; diff --git a/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/AnswerSave.java b/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/AnswerSave.java index 3fdc40a..fa08e89 100644 --- a/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/AnswerSave.java +++ b/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/AnswerSave.java @@ -2,7 +2,6 @@ package dev.struchkov.godfather.quarkus.domain.unit; import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.main.domain.content.Message; -import dev.struchkov.godfather.main.domain.unit.TypeUnit; import dev.struchkov.godfather.main.domain.unit.UnitActiveType; import dev.struchkov.godfather.quarkus.domain.unit.func.CheckSave; import dev.struchkov.godfather.quarkus.domain.unit.func.PreservableData; @@ -26,6 +25,8 @@ import static dev.struchkov.haiti.utils.Checker.checkNull; */ public class AnswerSave extends MainUnit { + public static final String TYPE = "ANSWER_SAVE"; + /** * Объект отвечающий за сохранение - репозиторий. */ @@ -66,7 +67,7 @@ public class AnswerSave extends MainUnit { builder.nextUnits, (builder.hidden) ? UnitActiveType.AFTER : UnitActiveType.DEFAULT, builder.notSaveHistory, - TypeUnit.SAVE + TYPE ); maintenanceNextUnit(nextUnits); preservable = builder.preservable; @@ -112,6 +113,7 @@ public class AnswerSave extends MainUnit { } public static final class Builder { + private String name = UUID.randomUUID().toString(); private String description; private Set> nextUnits; diff --git a/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/AnswerText.java b/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/AnswerText.java index 223ea6b..1553e6a 100644 --- a/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/AnswerText.java +++ b/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/AnswerText.java @@ -2,7 +2,6 @@ package dev.struchkov.godfather.quarkus.domain.unit; import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.main.domain.content.Message; -import dev.struchkov.godfather.main.domain.unit.TypeUnit; import dev.struchkov.godfather.main.domain.unit.UnitActiveType; import dev.struchkov.godfather.quarkus.domain.BoxAnswer; import dev.struchkov.godfather.quarkus.domain.unit.func.CallBackConsumer; @@ -27,6 +26,8 @@ import java.util.stream.Collectors; */ public class AnswerText extends MainUnit { + public static final String TYPE = "ANSWER_TEXT"; + /** * Объект, который необходимо отправить пользователю. */ @@ -50,7 +51,7 @@ public class AnswerText extends MainUnit { builder.nextUnits, builder.activeType, builder.notSaveHistory, - TypeUnit.TEXT + TYPE ); answer = builder.boxAnswer; callBack = builder.callBack; diff --git a/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/cmd/ReplaceCmd.java b/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/cmd/ReplaceCmd.java index f09d4c9..cc6e2fd 100644 --- a/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/cmd/ReplaceCmd.java +++ b/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/cmd/ReplaceCmd.java @@ -2,7 +2,6 @@ package dev.struchkov.godfather.quarkus.domain.unit.cmd; import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.main.domain.content.Message; -import dev.struchkov.godfather.main.domain.unit.TypeUnit; import dev.struchkov.godfather.main.domain.unit.UnitActiveType; import dev.struchkov.godfather.quarkus.domain.unit.MainUnit; import dev.struchkov.haiti.utils.Checker; @@ -16,6 +15,8 @@ import java.util.stream.Collectors; public class ReplaceCmd extends MainUnit { + public static final String TYPE = "REPLACE_CMD"; + private final MainUnit thisUnit; private ReplaceCmd(Builder builder) { @@ -31,7 +32,7 @@ public class ReplaceCmd extends MainUnit { new HashSet<>(), builder.activeType, true, - TypeUnit.REPLACE_CMD + TYPE ); this.thisUnit = builder.thisUnit; } diff --git a/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/cmd/RollBackCmd.java b/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/cmd/RollBackCmd.java index 76415f0..f3fa561 100644 --- a/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/cmd/RollBackCmd.java +++ b/bot-domain/bot-domain-quarkus/src/main/java/dev/struchkov/godfather/quarkus/domain/unit/cmd/RollBackCmd.java @@ -3,7 +3,6 @@ package dev.struchkov.godfather.quarkus.domain.unit.cmd; import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.exception.UnitConfigException; import dev.struchkov.godfather.main.domain.content.Message; -import dev.struchkov.godfather.main.domain.unit.TypeUnit; import dev.struchkov.godfather.main.domain.unit.UnitActiveType; import dev.struchkov.godfather.quarkus.domain.unit.MainUnit; @@ -21,6 +20,8 @@ import static dev.struchkov.haiti.utils.Checker.checkNull; */ public class RollBackCmd extends MainUnit { + public static final String TYPE = "ROLLBACK_CMD"; + /** * Количество юнитов, на которые можно откатиться назад. */ @@ -44,7 +45,7 @@ public class RollBackCmd extends MainUnit { new HashSet<>(), builder.activeType, true, - TypeUnit.BACK_CMD + TYPE ); this.countBack = builder.countBack; this.rollbackUnitName = builder.rollbackUnitName; @@ -83,6 +84,7 @@ public class RollBackCmd extends MainUnit { } public static final class Builder { + private String name = UUID.randomUUID().toString(); private String description; diff --git a/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/AnswerCheck.java b/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/AnswerCheck.java index 2bcbc7f..bc48672 100644 --- a/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/AnswerCheck.java +++ b/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/AnswerCheck.java @@ -2,7 +2,6 @@ package dev.struchkov.godfather.simple.domain.unit; import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.main.domain.content.Message; -import dev.struchkov.godfather.main.domain.unit.TypeUnit; import dev.struchkov.godfather.main.domain.unit.UnitActiveType; import dev.struchkov.godfather.simple.domain.BoxAnswer; import dev.struchkov.godfather.simple.domain.unit.func.CheckData; @@ -21,6 +20,8 @@ import java.util.stream.Collectors; */ public class AnswerCheck extends MainUnit { + public static final String TYPE = "ANSWER_CHECK"; + /** * Unit для true. */ @@ -59,7 +60,7 @@ public class AnswerCheck extends MainUnit { new HashSet<>(), builder.activeType, builder.notSaveHistory, - TypeUnit.CHECK + TYPE ); unitTrue = builder.unitTrue; unitFalse = builder.unitFalse; diff --git a/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/AnswerSave.java b/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/AnswerSave.java index 2c46251..55ffd28 100644 --- a/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/AnswerSave.java +++ b/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/AnswerSave.java @@ -2,7 +2,6 @@ package dev.struchkov.godfather.simple.domain.unit; import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.main.domain.content.Message; -import dev.struchkov.godfather.main.domain.unit.TypeUnit; import dev.struchkov.godfather.main.domain.unit.UnitActiveType; import dev.struchkov.godfather.simple.domain.unit.func.CheckSave; import dev.struchkov.godfather.simple.domain.unit.func.PreservableData; @@ -25,6 +24,8 @@ import java.util.stream.Collectors; */ public class AnswerSave extends MainUnit { + public static final String TYPE = "ANSWER_SAVE"; + /** * Объект отвечающий за сохранение - репозиторий. */ @@ -65,7 +66,7 @@ public class AnswerSave extends MainUnit { builder.nextUnits, (builder.hidden) ? UnitActiveType.AFTER : UnitActiveType.DEFAULT, builder.notSaveHistory, - TypeUnit.SAVE + TYPE ); maintenanceNextUnit(nextUnits); preservable = builder.preservable; diff --git a/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/AnswerText.java b/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/AnswerText.java index 0f1caf4..4d8c46d 100644 --- a/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/AnswerText.java +++ b/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/AnswerText.java @@ -2,7 +2,6 @@ package dev.struchkov.godfather.simple.domain.unit; import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.main.domain.content.Message; -import dev.struchkov.godfather.main.domain.unit.TypeUnit; import dev.struchkov.godfather.main.domain.unit.UnitActiveType; import dev.struchkov.godfather.simple.domain.BoxAnswer; import dev.struchkov.godfather.simple.domain.unit.func.CallBackConsumer; @@ -27,6 +26,8 @@ import java.util.stream.Collectors; */ public class AnswerText extends MainUnit { + public static final String TYPE = "ANSWER_TEXT"; + /** * Объект, который необходимо отправить пользователю. */ @@ -47,7 +48,7 @@ public class AnswerText extends MainUnit { builder.nextUnits, builder.activeType, builder.notSaveHistory, - TypeUnit.TEXT + TYPE ); answer = builder.boxAnswer; callBack = builder.callBack; diff --git a/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/cmd/ReplaceCmd.java b/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/cmd/ReplaceCmd.java index d1d960a..1ef8c95 100644 --- a/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/cmd/ReplaceCmd.java +++ b/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/cmd/ReplaceCmd.java @@ -2,7 +2,6 @@ package dev.struchkov.godfather.simple.domain.unit.cmd; import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.main.domain.content.Message; -import dev.struchkov.godfather.main.domain.unit.TypeUnit; import dev.struchkov.godfather.main.domain.unit.UnitActiveType; import dev.struchkov.godfather.simple.domain.unit.MainUnit; import dev.struchkov.haiti.utils.Checker; @@ -16,6 +15,8 @@ import java.util.stream.Collectors; public class ReplaceCmd extends MainUnit { + public static final String TYPE = "REPLACE_CMD"; + private final MainUnit thisUnit; private ReplaceCmd(Builder builder) { @@ -31,7 +32,7 @@ public class ReplaceCmd extends MainUnit { new HashSet<>(), builder.activeType, true, - TypeUnit.REPLACE_CMD + TYPE ); this.thisUnit = builder.thisUnit; } diff --git a/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/cmd/RollBackCmd.java b/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/cmd/RollBackCmd.java index 701cd35..5a6e9a6 100644 --- a/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/cmd/RollBackCmd.java +++ b/bot-domain/bot-domain-simple/src/main/java/dev/struchkov/godfather/simple/domain/unit/cmd/RollBackCmd.java @@ -3,7 +3,6 @@ package dev.struchkov.godfather.simple.domain.unit.cmd; import dev.struchkov.autoresponder.entity.KeyWord; import dev.struchkov.godfather.exception.UnitConfigException; import dev.struchkov.godfather.main.domain.content.Message; -import dev.struchkov.godfather.main.domain.unit.TypeUnit; import dev.struchkov.godfather.main.domain.unit.UnitActiveType; import dev.struchkov.godfather.simple.domain.unit.MainUnit; @@ -21,6 +20,8 @@ import static dev.struchkov.haiti.utils.Checker.checkNull; */ public class RollBackCmd extends MainUnit { + public static final String TYPE = "ROLLBACK_CMD"; + /** * Количество юнитов, на которые можно откатиться назад. */ @@ -44,7 +45,7 @@ public class RollBackCmd extends MainUnit { new HashSet<>(), builder.activeType, true, - TypeUnit.BACK_CMD + TYPE ); this.countBack = builder.countBack; this.rollbackUnitName = builder.rollbackUnitName; diff --git a/pom.xml b/pom.xml index 15dc9e1..79edb55 100644 --- a/pom.xml +++ b/pom.xml @@ -153,6 +153,15 @@ + + + org.projectlombok + lombok + 1.18.26 + true + + +