diff --git a/bot-context/bot-context-main/pom.xml b/bot-context/bot-context-main/pom.xml index 0ea62f6..19fc346 100644 --- a/bot-context/bot-context-main/pom.xml +++ b/bot-context/bot-context-main/pom.xml @@ -3,10 +3,9 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - godfather-bot dev.struchkov.godfather - 0.0.25 - ../../pom.xml + bot-context + 0.0.27 4.0.0 diff --git a/bot-context/bot-context-quarkus/pom.xml b/bot-context/bot-context-quarkus/pom.xml index b3a3c76..b429ef1 100644 --- a/bot-context/bot-context-quarkus/pom.xml +++ b/bot-context/bot-context-quarkus/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather bot-context - 0.0.25 + 0.0.27 4.0.0 diff --git a/bot-context/bot-context-quarkus/src/main/java/dev/struchkov/godfather/quarkus/context/service/ErrorHandler.java b/bot-context/bot-context-quarkus/src/main/java/dev/struchkov/godfather/quarkus/context/service/ErrorHandler.java index 8a4b986..af9c6ee 100644 --- a/bot-context/bot-context-quarkus/src/main/java/dev/struchkov/godfather/quarkus/context/service/ErrorHandler.java +++ b/bot-context/bot-context-quarkus/src/main/java/dev/struchkov/godfather/quarkus/context/service/ErrorHandler.java @@ -1,6 +1,7 @@ package dev.struchkov.godfather.quarkus.context.service; import dev.struchkov.godfather.main.domain.content.Message; +import io.smallrye.mutiny.Uni; /** * Используется для перехвата исключений, которые возникают при обработке юнитов. @@ -13,6 +14,6 @@ public interface ErrorHandler { * @param message Сообщение, после которого возникло исключение. * @param e Объект исключения. */ - void handle(Message message, Throwable e); + Uni handle(Message message, Throwable e); } diff --git a/bot-context/bot-context-simple/pom.xml b/bot-context/bot-context-simple/pom.xml index a651bf3..b2457b8 100644 --- a/bot-context/bot-context-simple/pom.xml +++ b/bot-context/bot-context-simple/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather bot-context - 0.0.25 + 0.0.27 4.0.0 diff --git a/bot-context/pom.xml b/bot-context/pom.xml index 80608ce..144f5c5 100644 --- a/bot-context/pom.xml +++ b/bot-context/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.25 + 0.0.27 bot-context diff --git a/bot-core/bot-core-main/pom.xml b/bot-core/bot-core-main/pom.xml index 23cae53..e1fe13b 100644 --- a/bot-core/bot-core-main/pom.xml +++ b/bot-core/bot-core-main/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather bot-core - 0.0.25 + 0.0.27 4.0.0 diff --git a/bot-core/bot-core-quarkus/pom.xml b/bot-core/bot-core-quarkus/pom.xml index c13f6bf..13d0f4c 100644 --- a/bot-core/bot-core-quarkus/pom.xml +++ b/bot-core/bot-core-quarkus/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather bot-core - 0.0.25 + 0.0.27 4.0.0 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 65d2d23..7f98eb8 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 @@ -28,9 +28,11 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; -import static dev.struchkov.haiti.utils.Checker.checkNotEmpty; +import static dev.struchkov.haiti.utils.Checker.checkEmpty; import static dev.struchkov.haiti.utils.Checker.checkNotNull; -import static dev.struchkov.haiti.utils.Checker.checkNull; +import static java.lang.Boolean.TRUE; +import static java.util.Collections.emptyList; +import static java.util.Collections.emptySet; public class GeneralAutoResponder { @@ -84,87 +86,72 @@ public class GeneralAutoResponder { } public Uni processingNewMessage(M newMessage) { - if (newMessage != null) { - return personSettingService.getStateProcessingByPersonId(newMessage.getPersonId()) - .onItem().transformToUni( - state -> { - if (checkNull(state) || state) { - return processing(newMessage); - } - return Uni.createFrom().voidItem(); - } - ); - } - return Uni.createFrom().voidItem(); + return Uni.createFrom().item(newMessage) + .onItem().ifNotNull().transformToUni( + message -> personSettingService.getStateProcessingByPersonId(newMessage.getPersonId()) + .replaceIfNullWith(TRUE) + .chain( + state -> { + if (TRUE.equals(state)) { + return processing(newMessage); + } + return Uni.createFrom().voidItem(); + } + ) + ).replaceWithVoid(); + } public Uni processingNewMessages(List newMessages) { - if (newMessages != null && !newMessages.isEmpty()) { - final Set personIds = newMessages.stream() - .map(Message::getPersonId) - .collect(Collectors.toSet()); - return personSettingService.getAllPersonIdDisableMessages(personIds) - .onItem().transformToMulti( - disableIds -> { - final List allowedMessages = newMessages.stream() - .filter(message -> !disableIds.contains(message.getPersonId())) - .toList(); - return Multi.createFrom().iterable(allowedMessages); - } - ) - .onItem().transform(this::processing) - .toUni().replaceWithVoid(); - } - return Uni.createFrom().voidItem(); - } + return Uni.createFrom().item(newMessages) + .onItem().ifNotNull().transformToUni( + messages -> { + if (checkEmpty(newMessages)) return Uni.createFrom().voidItem(); - private Uni processing(M message) { - return Uni.createFrom().item(message) - .onItem().transformToUni( - mail -> { - if (checkNotEmpty(modifiable)) { - return Multi.createFrom().iterable(modifiable) - .onItem().transformToUni(m -> m.change(mail)) - .concatenate().toUni().replaceWith(mail); - } - return Uni.createFrom().item(mail); - } - ).onItem().transformToUni( - mail -> { - final Uni>> uniUnits = storyLineService.getNextUnitByPersonId(mail.getPersonId()); - return Uni.combine().all().unis(uniUnits, Uni.createFrom().item(mail)).asTuple(); - } - ).onItem().transformToUni( - t -> { - final Set> units = t.getItem1(); - final M mail = t.getItem2(); - final Optional> optAnswer = Responder.nextUnit(mail, units).or(storyLineService::getDefaultUnit); - if (optAnswer.isPresent()) { - final MainUnit answer = optAnswer.get(); - //TODO [05.08.2022]: нужно ли проверку встраивать в поток? -// if (checkPermission(answer.getAccessibility(), message)) { - return answer(UnitRequest.of(answer, message)); -// } - } - return Uni.createFrom().voidItem(); + final Set personIds = newMessages.stream() + .map(Message::getPersonId) + .collect(Collectors.toSet()); + return personSettingService.getAllPersonIdDisableMessages(personIds) + .replaceIfNullWith(emptySet()) + .onItem().transformToMulti( + disableIds -> { + final List allowedMessages = newMessages.stream() + .filter(message -> !disableIds.contains(message.getPersonId())) + .toList(); + return Multi.createFrom().iterable(allowedMessages); + } + ) + .onItem().transform(this::processing) + .toUni().replaceWithVoid(); } ); } -// private boolean checkPermission(Optional accessibility, M message) { -// return accessibility.isEmpty() || accessibility.get().check(message); -// } + private Uni processing(M message) { + return Uni.createFrom().item(message) + .onItem().ifNotNull().transform(m -> modifiable) + .replaceIfNullWith(emptyList()) + .onItem().transformToMulti(modifiables -> Multi.createFrom().iterable(modifiables)) + .onItem().transformToUni(mModifiable -> mModifiable.change(message)) + .concatenate().toUni().replaceWith( + storyLineService.getNextUnitByPersonId(message.getPersonId()) + .onItem().ifNotNull().transformToUni( + nextUnits -> Uni.createFrom().optional( + Responder.nextUnit(message, nextUnits).or(storyLineService::getDefaultUnit) + ) + ).onItem().ifNotNull().transformToUni(answerUnit -> answer(UnitRequest.of(answerUnit, message))) + ); + } public Uni answer(UnitRequest unitRequest) { return getAction(unitRequest) - .onItem().transformToUni( - request -> activeUnitAfter(unitRequest) - ) - .onFailure().invoke( + .chain(request -> activeUnitAfter(unitRequest)) + .onFailure().call( throwable -> { - if (errorHandler != null) { - errorHandler.handle(unitRequest.getMessage(), throwable); + if (checkNotNull(errorHandler)) { + return errorHandler.handle(unitRequest.getMessage(), throwable); } + return Uni.createFrom().voidItem(); } ) .replaceWithVoid(); @@ -178,7 +165,7 @@ public class GeneralAutoResponder { .findFirst(); if (first.isPresent()) { return Uni.createFrom().voidItem().onItem().transformToUni( - v-> getAction(UnitRequest.of(first.get(), unitRequest.getMessage())) + v -> getAction(UnitRequest.of(first.get(), unitRequest.getMessage())) ) .onItem().transformToUni( uR -> activeUnitAfter(UnitRequest.of(first.get(), unitRequest.getMessage())) 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 e1a9adc..74c8609 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 @@ -28,48 +28,32 @@ public class AnswerTextAction implements ActionUnit, Message @Override public Uni> action(UnitRequest, Message> unitRequest) { - final AnswerText unit = unitRequest.getUnit(); final Message message = unitRequest.getMessage(); - - return unit.getAnswer().processing(message) - .onItem().transformToUni( - boxAnswer -> { - if (checkNotNull(boxAnswer)) { - return replaceMarkers(unit, message, boxAnswer); - } - return Uni.createFrom().nullItem(); - } - ).onItem().transformToUni( - boxAnswer -> { - if (checkNotNull(boxAnswer)) { - final Sending answerTextSending = unit.getSending(); - if (answerTextSending != null) { - return Sender.sends(message, boxAnswer, answerTextSending); - } else { - return Sender.sends(message, boxAnswer, this.sending); - } - } - return Uni.createFrom().nullItem(); - } - ).onItem().transform( - v -> UnitRequest.of(unit, message) - ); + final AnswerText unit = unitRequest.getUnit(); + return Uni.createFrom().voidItem() + .chain(request -> unit.getAnswer().processing(message)) + .onItem().ifNotNull().transformToUni(boxAnswer -> replaceMarkers(unit, message, boxAnswer)) + .onItem().ifNotNull().transformToUni(boxAnswer -> { + final Sending answerTextSending = unit.getSending(); + if (checkNotNull(answerTextSending)) { + return Sender.sends(message, boxAnswer, answerTextSending); + } else { + return Sender.sends(message, boxAnswer, this.sending); + } + }).replaceWith(UnitRequest.of(unit, message)); } private Uni replaceMarkers(AnswerText answerText, Message message, BoxAnswer boxAnswer) { - if (answerText.getInsert() != null) { - return answerText.getInsert().insert(message.getPersonId()) - .onItem().transformToUni( - words -> { - if (checkNotEmpty(words)) { - final String newMessage = InsertWords.insert(boxAnswer.getMessage(), words); - boxAnswer.setMessage(newMessage); - } - return Uni.createFrom().item(boxAnswer); - } - ); - } - return Uni.createFrom().item(boxAnswer); + return Uni.createFrom().item(answerText.getInsert()) + .onItem().ifNotNull().transformToUni(insert -> insert.insert(message.getPersonId())) + .onItem().ifNotNull().transform(words -> { + if (checkNotEmpty(words)) { + final String newMessage = InsertWords.insert(boxAnswer.getMessage(), words); + boxAnswer.setMessage(newMessage); + } + return boxAnswer; + }) + .replaceIfNullWith(boxAnswer); } } diff --git a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/unit/AnswerCheck.java b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/unit/AnswerCheck.java index dbc4f4c..244e8b2 100644 --- a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/unit/AnswerCheck.java +++ b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/unit/AnswerCheck.java @@ -167,7 +167,7 @@ public class AnswerCheck extends MainUnit { return this; } - private Builder triggerCheck(Predicate trigger) { + public Builder triggerCheck(Predicate trigger) { triggerCheck = trigger; return this; } diff --git a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/utils/Sender.java b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/utils/Sender.java index 4c862c5..5293528 100644 --- a/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/utils/Sender.java +++ b/bot-core/bot-core-quarkus/src/main/java/dev/struchkov/godfather/quarkus/core/utils/Sender.java @@ -19,13 +19,18 @@ public class Sender { } public static Uni sends(Message message, BoxAnswer boxAnswer, Sending sending) { - switch (sending.getType()) { - case PUBLIC: - break; - case PRIVATE: - return privateSend(message, boxAnswer, sending); - } - return Uni.createFrom().voidItem(); + return Uni.createFrom().item(sending) + .onItem().ifNotNull().transformToUni( + sender -> { + switch (sending.getType()) { + case PUBLIC: + break; + case PRIVATE: + return privateSend(message, boxAnswer, sending); + } + return Uni.createFrom().nullItem(); + } + ).replaceWithVoid(); } private static Uni privateSend(Message message, BoxAnswer boxAnswer, Sending sending) { diff --git a/bot-core/bot-core-simple/pom.xml b/bot-core/bot-core-simple/pom.xml index 27821d8..8a6e998 100644 --- a/bot-core/bot-core-simple/pom.xml +++ b/bot-core/bot-core-simple/pom.xml @@ -5,7 +5,7 @@ bot-core dev.struchkov.godfather - 0.0.25 + 0.0.27 4.0.0 diff --git a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/unit/AnswerCheck.java b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/unit/AnswerCheck.java index b25f309..d5fbe6c 100644 --- a/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/unit/AnswerCheck.java +++ b/bot-core/bot-core-simple/src/main/java/dev/struchkov/godfather/simple/core/unit/AnswerCheck.java @@ -164,7 +164,7 @@ public class AnswerCheck extends MainUnit { return this; } - private Builder triggerCheck(Predicate trigger) { + public Builder triggerCheck(Predicate trigger) { triggerCheck = trigger; return this; } diff --git a/bot-core/pom.xml b/bot-core/pom.xml index 16cfb0d..d0b0665 100644 --- a/bot-core/pom.xml +++ b/bot-core/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.25 + 0.0.27 bot-core diff --git a/bot-data/bot-data-main/pom.xml b/bot-data/bot-data-main/pom.xml index 502d9ca..d6cc961 100644 --- a/bot-data/bot-data-main/pom.xml +++ b/bot-data/bot-data-main/pom.xml @@ -5,7 +5,7 @@ bot-data dev.struchkov.godfather - 0.0.25 + 0.0.27 4.0.0 diff --git a/bot-data/bot-data-quarkus/pom.xml b/bot-data/bot-data-quarkus/pom.xml index 16f4883..48b923a 100644 --- a/bot-data/bot-data-quarkus/pom.xml +++ b/bot-data/bot-data-quarkus/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather bot-data - 0.0.25 + 0.0.27 4.0.0 diff --git a/bot-data/bot-data-simple/pom.xml b/bot-data/bot-data-simple/pom.xml index 717b945..bdc74ae 100644 --- a/bot-data/bot-data-simple/pom.xml +++ b/bot-data/bot-data-simple/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather bot-data - 0.0.25 + 0.0.27 4.0.0 diff --git a/bot-data/pom.xml b/bot-data/pom.xml index d371d92..7923d6f 100644 --- a/bot-data/pom.xml +++ b/bot-data/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather godfather-bot - 0.0.25 + 0.0.27 4.0.0 diff --git a/bot-domain/bot-domain-main/pom.xml b/bot-domain/bot-domain-main/pom.xml index 2d117e3..fa00651 100644 --- a/bot-domain/bot-domain-main/pom.xml +++ b/bot-domain/bot-domain-main/pom.xml @@ -5,7 +5,7 @@ bot-domain dev.struchkov.godfather - 0.0.25 + 0.0.27 4.0.0 diff --git a/bot-domain/pom.xml b/bot-domain/pom.xml index f44a2ff..e4d5134 100644 --- a/bot-domain/pom.xml +++ b/bot-domain/pom.xml @@ -5,7 +5,7 @@ godfather-bot dev.struchkov.godfather - 0.0.25 + 0.0.27 4.0.0 diff --git a/bot-exception/pom.xml b/bot-exception/pom.xml index c10ac7c..8dde2cf 100644 --- a/bot-exception/pom.xml +++ b/bot-exception/pom.xml @@ -5,7 +5,7 @@ dev.struchkov.godfather godfather-bot - 0.0.25 + 0.0.27 4.0.0 diff --git a/pom.xml b/pom.xml index 809961f..b07d3c9 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ dev.struchkov.godfather godfather-bot - 0.0.25 + 0.0.27 pom