Добавил возможность устанавливать получателя в BoxAnswer

This commit is contained in:
Struchkov Mark 2022-12-13 21:52:22 +03:00
parent ad69ea3702
commit 6f64494504
10 changed files with 63 additions and 91 deletions

View File

@ -13,12 +13,11 @@ import org.jetbrains.annotations.NotNull;
public interface Sending {
/**
* Отрпавляет ответ пользователю
* Отправляет сообщение пользователю
*
* @param personId Идентификатор пользователя
* @param boxAnswer Объект с данными, которые необходимо отправить
*/
Uni<Void> send(@NotNull String personId, @NotNull BoxAnswer boxAnswer);
Uni<Void> send(@NotNull BoxAnswer boxAnswer);
void addPreSendProcess(@NotNull PreSendProcessing processing);

View File

@ -12,12 +12,11 @@ import org.jetbrains.annotations.NotNull;
public interface Sending {
/**
* Отрпавляет ответ пользователю
* Отравляет сообщение пользователю.
*
* @param personId Идентификатор пользователя
* @param boxAnswer Объект с данными, которые необходимо отправить
*/
void send(@NotNull String personId, @NotNull BoxAnswer boxAnswer);
void send(@NotNull BoxAnswer boxAnswer);
void addPreSendProcess(@NotNull PreSendProcessing processing);

View File

@ -1,5 +1,6 @@
package dev.struchkov.godfather.quarkus.core.action;
import dev.struchkov.godfather.main.domain.BoxAnswer;
import dev.struchkov.godfather.main.domain.content.Message;
import dev.struchkov.godfather.quarkus.context.service.Sending;
import dev.struchkov.godfather.quarkus.core.unit.AnswerCheck;
@ -9,6 +10,7 @@ import io.smallrye.mutiny.Uni;
import java.util.Objects;
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
import static dev.struchkov.haiti.utils.Checker.checkTrue;
/**
@ -32,10 +34,19 @@ public class AnswerCheckAction<M extends Message> implements ActionUnit<AnswerCh
return unit.getCheck().checked(message)
.onItem().call(checkValue -> {
if (checkTrue(checkValue)) {
return sending.send(message.getPersonId(), unit.getIntermediateAnswerIfTrue());
final BoxAnswer answerIfTrue = unit.getIntermediateAnswerIfTrue();
if (checkNotNull(answerIfTrue)) {
answerIfTrue.setRecipientIfNull(message.getPersonId());
return sending.send(answerIfTrue);
}
} else {
return sending.send(message.getPersonId(), unit.getIntermediateAnswerIfFalse());
final BoxAnswer answerIfFalse = unit.getIntermediateAnswerIfFalse();
if (checkNotNull(answerIfFalse)) {
answerIfFalse.setRecipientIfNull(message.getPersonId());
return sending.send(answerIfFalse);
}
}
return Uni.createFrom().voidItem();
})
.onItem().transform(
checkValue -> {

View File

@ -7,7 +7,6 @@ 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.utils.Sender;
import io.smallrye.mutiny.Uni;
import static dev.struchkov.haiti.utils.Checker.checkNotEmpty;
@ -35,10 +34,11 @@ public class AnswerTextAction implements ActionUnit<AnswerText<Message>, Message
.onItem().ifNotNull().transformToUni(boxAnswer -> replaceMarkers(unit, message, boxAnswer))
.onItem().ifNotNull().transformToUni(boxAnswer -> {
final Sending answerTextSending = unit.getSending();
boxAnswer.setRecipientIfNull(message.getPersonId());
if (checkNotNull(answerTextSending)) {
return Sender.sends(message, boxAnswer, answerTextSending);
return answerTextSending.send(boxAnswer);
} else {
return Sender.sends(message, boxAnswer, this.sending);
return sending.send(boxAnswer);
}
}).replaceWith(UnitRequest.of(unit, message));
}

View File

@ -1,40 +0,0 @@
package dev.struchkov.godfather.quarkus.core.utils;
import dev.struchkov.godfather.main.domain.BoxAnswer;
import dev.struchkov.godfather.main.domain.content.Message;
import dev.struchkov.godfather.quarkus.context.service.Sending;
import io.smallrye.mutiny.Uni;
import static dev.struchkov.haiti.utils.Exceptions.utilityClass;
/**
* Используется для отправки сообщений определенного типа.
*
* @author upagge
*/
public class Sender {
private Sender() {
utilityClass();
}
public static Uni<Void> sends(Message message, BoxAnswer boxAnswer, Sending sending) {
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<Void> privateSend(Message message, BoxAnswer boxAnswer, Sending sending) {
return sending.send(message.getPersonId(), boxAnswer);
}
}

View File

@ -39,12 +39,18 @@ public class AnswerCheckAction<M extends Message> implements ActionUnit<AnswerCh
if (unit.getCheck().checked(message)) {
log.debug("Unit: {}. Проверка пройдена", unit.getName());
final BoxAnswer answerIfTrue = unit.getIntermediateAnswerIfTrue();
if (checkNotNull(answerIfTrue)) sending.send(message.getPersonId(), answerIfTrue);
if (checkNotNull(answerIfTrue)) {
answerIfTrue.setRecipientIfNull(message.getPersonId());
sending.send(answerIfTrue);
}
unitAnswer = unit.getUnitTrue();
} else {
log.debug("Unit: {}. Проверка НЕ пройдена", unit.getName());
final BoxAnswer answerIfFalse = unit.getIntermediateAnswerIfFalse();
if (checkNotNull(answerIfFalse)) sending.send(message.getPersonId(), answerIfFalse);
if (checkNotNull(answerIfFalse)) {
answerIfFalse.setRecipientIfNull(message.getPersonId());
sending.send(answerIfFalse);
}
unitAnswer = unit.getUnitFalse();
}
log.debug("Завершилась обработка unit: {}.", unit.getName());

View File

@ -7,7 +7,6 @@ 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.utils.Sender;
import java.util.List;
import java.util.Optional;
@ -36,10 +35,11 @@ public class AnswerTextAction implements ActionUnit<AnswerText<Message>, Message
replaceMarkers(unit, message, answer);
final Sending answerTextSending = unit.getSending();
answer.setRecipientIfNull(message.getPersonId());
if (answerTextSending != null) {
Sender.sends(message, answer, answerTextSending);
answerTextSending.send(answer);
} else {
Sender.sends(message, answer, this.sending);
sending.send(answer);
}
}

View File

@ -1,5 +1,6 @@
package dev.struchkov.godfather.simple.core.pusher;
import dev.struchkov.godfather.main.domain.BoxAnswer;
import dev.struchkov.godfather.simple.context.service.Pusher;
import dev.struchkov.godfather.simple.context.service.Sending;
@ -25,7 +26,9 @@ public class UserSanderPusher implements Pusher<String> {
stringBuilder.append("========= ").append(nameForm).append(" =========\n");
saveElement.forEach((key, value) -> stringBuilder.append(key).append(": ").append(value).append("\n"));
stringBuilder.append("====================");
sending.send(this.personId, boxAnswer(stringBuilder.toString()));
final BoxAnswer boxAnswer = boxAnswer(stringBuilder.toString());
boxAnswer.setRecipientIfNull(this.personId);
sending.send(boxAnswer);
}
}

View File

@ -1,34 +0,0 @@
package dev.struchkov.godfather.simple.core.utils;
import dev.struchkov.godfather.main.domain.BoxAnswer;
import dev.struchkov.godfather.main.domain.content.Message;
import dev.struchkov.godfather.simple.context.service.Sending;
import static dev.struchkov.haiti.utils.Exceptions.utilityClass;
/**
* Используется для отправки сообщений определенного типа.
*
* @author upagge
*/
public class Sender {
private Sender() {
utilityClass();
}
public static void sends(Message message, BoxAnswer boxAnswer, Sending sending) {
switch (sending.getType()) {
case PUBLIC:
break;
case PRIVATE:
privateSend(message, boxAnswer, sending);
break;
}
}
private static void privateSend(Message message, BoxAnswer boxAnswer, Sending sending) {
sending.send(message.getPersonId(), boxAnswer);
}
}

View File

@ -2,6 +2,8 @@ package dev.struchkov.godfather.main.domain;
import dev.struchkov.godfather.main.domain.keyboard.KeyBoard;
import static dev.struchkov.haiti.utils.Checker.checkNull;
/**
* Контейнер, которые содержит данные, которые будут отправлены пользователю как ответ на его запрос.
*
@ -24,10 +26,16 @@ public class BoxAnswer {
*/
private String message;
/**
* <p>Получатель сообщения</p>
*/
private String recipientPersonId;
private BoxAnswer(Builder builder) {
message = builder.message;
keyBoard = builder.keyBoard;
replace = builder.replace;
recipientPersonId = builder.recipientPersonId;
}
public static BoxAnswer boxAnswer(boolean replace, String message) {
@ -78,6 +86,20 @@ public class BoxAnswer {
return replace;
}
public String getRecipientPersonId() {
return recipientPersonId;
}
public void setRecipientPersonId(String recipientPersonId) {
this.recipientPersonId = recipientPersonId;
}
public void setRecipientIfNull(String recipientPersonId) {
if (checkNull(this.recipientPersonId)) {
this.recipientPersonId = recipientPersonId;
}
}
@Override
public String toString() {
return "BoxAnswer{" +
@ -91,6 +113,7 @@ public class BoxAnswer {
private String message;
private KeyBoard keyBoard;
private boolean replace;
private String recipientPersonId;
private Builder() {
}
@ -110,6 +133,11 @@ public class BoxAnswer {
return this;
}
public Builder recipientPersonId(String val) {
recipientPersonId = val;
return this;
}
public BoxAnswer build() {
return new BoxAnswer(this);
}