Добавил возможность выполнить какое-нибудь действие перед отправкой AnswerText

This commit is contained in:
Struchkov Mark 2023-02-17 21:21:29 +03:00
parent 3b9ebf28c6
commit dc2051d85a
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
6 changed files with 63 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import dev.struchkov.godfather.quarkus.context.service.Sending;
import dev.struchkov.godfather.quarkus.core.unit.AnswerText;
import dev.struchkov.godfather.quarkus.core.unit.MainUnit;
import dev.struchkov.godfather.quarkus.core.unit.UnitRequest;
import dev.struchkov.godfather.quarkus.core.unit.func.ActionBeforeSending;
import dev.struchkov.godfather.quarkus.core.unit.func.CallBackConsumer;
import io.smallrye.mutiny.Uni;
@ -29,6 +30,13 @@ public class AnswerTextAction implements ActionUnit<AnswerText<Message>, Message
final AnswerText<Message> unit = unitRequest.getUnit();
return unit.getAnswer().processing(message)
.onItem().ifNotNull().call(v -> {
final ActionBeforeSending actionBeforeSending = unit.getActionBeforeSending();
if (checkNotNull(actionBeforeSending)) {
return actionBeforeSending.execute();
}
return Uni.createFrom().nullItem();
})
.onItem().ifNotNull().transformToUni(boxAnswer -> {
boxAnswer.setRecipientIfNull(message.getPersonId());
return sending.send(boxAnswer);

View File

@ -5,6 +5,7 @@ import dev.struchkov.godfather.main.core.unit.TypeUnit;
import dev.struchkov.godfather.main.core.unit.UnitActiveType;
import dev.struchkov.godfather.main.domain.content.Message;
import dev.struchkov.godfather.quarkus.context.service.Accessibility;
import dev.struchkov.godfather.quarkus.core.unit.func.ActionBeforeSending;
import dev.struchkov.godfather.quarkus.core.unit.func.CallBackConsumer;
import dev.struchkov.godfather.quarkus.core.unit.func.ProcessingData;
import dev.struchkov.godfather.quarkus.domain.BoxAnswer;
@ -38,6 +39,8 @@ public class AnswerText<M extends Message> extends MainUnit<M> {
*/
private final CallBackConsumer callBack;
private final ActionBeforeSending actionBeforeSending;
private AnswerText(Builder<M> builder) {
super(
builder.name,
@ -56,6 +59,7 @@ public class AnswerText<M extends Message> extends MainUnit<M> {
);
answer = builder.boxAnswer;
callBack = builder.callBack;
actionBeforeSending = builder.actionBeforeSending;
}
public static <M extends Message> AnswerText<M> of(String message) {
@ -78,6 +82,10 @@ public class AnswerText<M extends Message> extends MainUnit<M> {
return callBack;
}
public ActionBeforeSending getActionBeforeSending() {
return actionBeforeSending;
}
public static final class Builder<M extends Message> {
private String name = UUID.randomUUID().toString();
@ -98,6 +106,7 @@ public class AnswerText<M extends Message> extends MainUnit<M> {
private ProcessingData<M> boxAnswer;
private CallBackConsumer callBack;
private ActionBeforeSending actionBeforeSending;
private Builder() {
}
@ -140,6 +149,11 @@ public class AnswerText<M extends Message> extends MainUnit<M> {
return this;
}
public Builder<M> actionBeforeSending(ActionBeforeSending action) {
this.actionBeforeSending = action;
return this;
}
public Builder<M> triggerWords(Set<KeyWord> val) {
if (triggerWords == null) {
triggerWords = new HashSet<>();

View File

@ -0,0 +1,10 @@
package dev.struchkov.godfather.quarkus.core.unit.func;
import io.smallrye.mutiny.Uni;
@FunctionalInterface
public interface ActionBeforeSending {
Uni<Void> execute();
}

View File

@ -5,6 +5,7 @@ import dev.struchkov.godfather.simple.context.service.Sending;
import dev.struchkov.godfather.simple.core.unit.AnswerText;
import dev.struchkov.godfather.simple.core.unit.MainUnit;
import dev.struchkov.godfather.simple.core.unit.UnitRequest;
import dev.struchkov.godfather.simple.core.unit.func.ActionBeforeSending;
import dev.struchkov.godfather.simple.core.unit.func.CallBackConsumer;
import dev.struchkov.godfather.simple.core.unit.func.ProcessingData;
import dev.struchkov.godfather.simple.domain.BoxAnswer;
@ -39,6 +40,12 @@ public class AnswerTextAction implements ActionUnit<AnswerText<Message>, Message
final BoxAnswer answer = optAnswer.get();
answer.setRecipientIfNull(message.getPersonId());
final ActionBeforeSending actionBeforeSending = unit.getActionBeforeSending();
if (checkNotNull(actionBeforeSending)) {
actionBeforeSending.execute(message.getPersonId());
}
final Optional<? extends SentBox> optSentBox = sending.send(answer);
final CallBackConsumer callBack = unit.getCallBack();
if (checkNotNull(callBack) && optAnswer.isPresent()) {

View File

@ -5,6 +5,7 @@ import dev.struchkov.godfather.main.core.unit.TypeUnit;
import dev.struchkov.godfather.main.core.unit.UnitActiveType;
import dev.struchkov.godfather.main.domain.content.Message;
import dev.struchkov.godfather.simple.context.service.Accessibility;
import dev.struchkov.godfather.simple.core.unit.func.ActionBeforeSending;
import dev.struchkov.godfather.simple.core.unit.func.CallBackConsumer;
import dev.struchkov.godfather.simple.core.unit.func.ProcessingData;
import dev.struchkov.godfather.simple.domain.BoxAnswer;
@ -35,6 +36,8 @@ public class AnswerText<M extends Message> extends MainUnit<M> {
private final CallBackConsumer callBack;
private final ActionBeforeSending actionBeforeSending;
private AnswerText(Builder<M> builder) {
super(
builder.name,
@ -53,6 +56,7 @@ public class AnswerText<M extends Message> extends MainUnit<M> {
);
answer = builder.boxAnswer;
callBack = builder.callBack;
actionBeforeSending = builder.actionBeforeSending;
}
public static <M extends Message> AnswerText<M> of(String message) {
@ -75,6 +79,10 @@ public class AnswerText<M extends Message> extends MainUnit<M> {
return callBack;
}
public ActionBeforeSending getActionBeforeSending() {
return actionBeforeSending;
}
public static final class Builder<M extends Message> {
private String name = UUID.randomUUID().toString();
@ -95,6 +103,7 @@ public class AnswerText<M extends Message> extends MainUnit<M> {
private ProcessingData<M> boxAnswer;
private CallBackConsumer callBack;
private ActionBeforeSending actionBeforeSending;
private Builder() {
}
@ -132,11 +141,16 @@ public class AnswerText<M extends Message> extends MainUnit<M> {
return this;
}
public <T> Builder<M> callBack(CallBackConsumer callBack) {
public Builder<M> callBack(CallBackConsumer callBack) {
this.callBack = callBack;
return this;
}
public Builder<M> actionBeforeSending(ActionBeforeSending action) {
this.actionBeforeSending = action;
return this;
}
public Builder<M> triggerWords(Set<KeyWord> val) {
if (triggerWords == null) {
triggerWords = new HashSet<>();
@ -239,4 +253,5 @@ public class AnswerText<M extends Message> extends MainUnit<M> {
}
}
}

View File

@ -0,0 +1,8 @@
package dev.struchkov.godfather.simple.core.unit.func;
@FunctionalInterface
public interface ActionBeforeSending {
void execute(String personId);
}