This commit is contained in:
parent
dc567ad410
commit
d723460d31
@ -12,7 +12,7 @@ public interface StorylineContext {
|
||||
|
||||
<T> Uni<T> getByKey(@NotNull String personId, @NotNull ContextKey<T> key);
|
||||
|
||||
<T> Uni<T> removeKey(@NotNull String personId, @NotNull ContextKey<?> key);
|
||||
<T> Uni<T> removeKey(@NotNull String personId, @NotNull ContextKey<T> key);
|
||||
|
||||
<T> Uni<T> getByKeyOrThrow(@NotNull String personId, @NotNull ContextKey<T> key);
|
||||
|
||||
|
@ -1,21 +0,0 @@
|
||||
package dev.struchkov.godfather.main.core.utils;
|
||||
|
||||
import dev.struchkov.godfather.main.domain.content.EmptyMessage;
|
||||
import dev.struchkov.godfather.main.domain.content.Message;
|
||||
|
||||
import static dev.struchkov.haiti.utils.Exceptions.utilityClass;
|
||||
|
||||
/**
|
||||
* Класс для хранения объекта заглушки для {@link Message}.
|
||||
*
|
||||
* @author upagge [08/07/2019]
|
||||
*/
|
||||
public final class MessageUtils {
|
||||
|
||||
public static final EmptyMessage EMPTY_MESSAGE = new EmptyMessage();
|
||||
|
||||
private MessageUtils() {
|
||||
utilityClass();
|
||||
}
|
||||
|
||||
}
|
@ -77,7 +77,6 @@ public class GeneralAutoResponder<M extends Message> {
|
||||
}
|
||||
)
|
||||
).replaceWithVoid();
|
||||
|
||||
}
|
||||
|
||||
public Uni<Void> processingNewMessages(List<M> newMessages) {
|
||||
@ -121,7 +120,7 @@ public class GeneralAutoResponder<M extends Message> {
|
||||
|
||||
public Uni<Void> answer(UnitRequest<MainUnit, M> unitRequest) {
|
||||
return getAction(unitRequest)
|
||||
.chain(request -> activeUnitAfter(unitRequest))
|
||||
.chain(this::activeUnitAfter)
|
||||
.onFailure().call(
|
||||
throwable -> {
|
||||
if (checkNotNull(errorHandler)) {
|
||||
@ -144,7 +143,7 @@ public class GeneralAutoResponder<M extends Message> {
|
||||
v -> getAction(UnitRequest.of(first.get(), unitRequest.getMessage()))
|
||||
)
|
||||
.onItem().transformToUni(
|
||||
uR -> activeUnitAfter(UnitRequest.of(first.get(), unitRequest.getMessage()))
|
||||
newRequest -> activeUnitAfter(UnitRequest.of(first.get(), unitRequest.getMessage()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ public class StorylineContextMapImpl implements StorylineContext {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Uni<T> removeKey(@NotNull String personId, @NotNull ContextKey<?> key) {
|
||||
public <T> Uni<T> removeKey(@NotNull String personId, @NotNull ContextKey<T> key) {
|
||||
isNotNull(personId, key);
|
||||
if (map.containsKey(personId)) {
|
||||
final Map<String, Object> storage = map.get(personId);
|
||||
|
@ -119,6 +119,12 @@ public class StorylineMailService implements StorylineService<Mail> {
|
||||
defaultUnitName = defaultUnit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uni<Void> cleanHistoryByPersonId(@NotNull String personId) {
|
||||
return storylineRepository.cleanHistoryByPersonId(personId)
|
||||
.call(() -> unitPointerService.removeByPersonId(personId));
|
||||
}
|
||||
|
||||
//TODO [22.06.2022]: Временное решение для ленивой инициализации
|
||||
@Override
|
||||
public void lazyLink(String firstName, String secondName) {
|
||||
|
@ -34,4 +34,6 @@ public interface StorylineService<M extends Message> {
|
||||
|
||||
void setDefaultUnit(String unitName);
|
||||
|
||||
Uni<Void> cleanHistoryByPersonId(@NotNull String personId);
|
||||
|
||||
}
|
||||
|
@ -6,8 +6,8 @@ import dev.struchkov.godfather.simple.domain.BoxAnswer;
|
||||
import dev.struchkov.godfather.simple.domain.unit.AnswerCheck;
|
||||
import dev.struchkov.godfather.simple.domain.unit.MainUnit;
|
||||
import dev.struchkov.godfather.simple.domain.unit.UnitRequest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@ -18,20 +18,16 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull;
|
||||
*
|
||||
* @author upagge [11/07/2019]
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class AnswerCheckAction implements ActionUnit<AnswerCheck<Message>, Message> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AnswerCheckAction.class);
|
||||
|
||||
private final Sending sending;
|
||||
|
||||
public AnswerCheckAction(Sending sending) {
|
||||
this.sending = sending;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UnitRequest<MainUnit, Message> action(UnitRequest<AnswerCheck<Message>, Message> unitRequest) {
|
||||
final AnswerCheck<Message> unit = unitRequest.getUnit();
|
||||
log.debug("Началась обработка unit: {}.", unit.getName());
|
||||
log.debug("Началась обработка юнита: {}.", unit.getName());
|
||||
|
||||
final Message message = unitRequest.getMessage();
|
||||
|
||||
@ -41,6 +37,7 @@ public class AnswerCheckAction implements ActionUnit<AnswerCheck<Message>, Messa
|
||||
final BoxAnswer answerIfTrue = unit.getIntermediateAnswerIfTrue();
|
||||
if (checkNotNull(answerIfTrue)) {
|
||||
answerIfTrue.setRecipientIfNull(message.getFromPersonId());
|
||||
log.trace("Будет отправлен промежуточный ответ: {}", answerIfTrue);
|
||||
sending.send(answerIfTrue);
|
||||
}
|
||||
unitAnswer = unit.getUnitTrue();
|
||||
@ -49,6 +46,7 @@ public class AnswerCheckAction implements ActionUnit<AnswerCheck<Message>, Messa
|
||||
final BoxAnswer answerIfFalse = unit.getIntermediateAnswerIfFalse();
|
||||
if (checkNotNull(answerIfFalse)) {
|
||||
answerIfFalse.setRecipientIfNull(message.getFromPersonId());
|
||||
log.trace("Будет отправлен промежуточный ответ: {}", answerIfFalse);
|
||||
sending.send(answerIfFalse);
|
||||
}
|
||||
unitAnswer = unit.getUnitFalse();
|
||||
|
@ -8,6 +8,7 @@ import dev.struchkov.godfather.simple.domain.unit.func.CheckSave;
|
||||
import dev.struchkov.godfather.simple.domain.unit.func.PreservableData;
|
||||
import dev.struchkov.godfather.simple.domain.unit.func.Pusher;
|
||||
import dev.struchkov.godfather.simple.domain.unit.func.preser.AnswerSavePreservable;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
|
||||
|
||||
@ -16,11 +17,15 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull;
|
||||
*
|
||||
* @author upagge [11/07/2019]
|
||||
*/
|
||||
@Slf4j
|
||||
public class AnswerSaveAction<D> implements ActionUnit<AnswerSave<Message, D>, Message> {
|
||||
|
||||
@Override
|
||||
public UnitRequest<MainUnit, Message> action(UnitRequest<AnswerSave<Message, D>, Message> unitRequest) {
|
||||
final AnswerSave<Message, D> answerSave = unitRequest.getUnit();
|
||||
|
||||
log.debug("Началась обработка юнита: {}.", answerSave.getName());
|
||||
|
||||
final Message message = unitRequest.getMessage();
|
||||
|
||||
final AnswerSavePreservable<D> preservable = answerSave.getPreservable();
|
||||
@ -28,7 +33,7 @@ public class AnswerSaveAction<D> implements ActionUnit<AnswerSave<Message, D>, M
|
||||
|
||||
final CheckSave<Message> checkSave = answerSave.getCheckSave();
|
||||
if (checkNotNull(checkSave)) {
|
||||
MainUnit<Message> unit = checkSave.check(message);
|
||||
final MainUnit<Message> unit = checkSave.check(message);
|
||||
if (checkNotNull(unit)) {
|
||||
return UnitRequest.of(unit, message);
|
||||
}
|
||||
@ -46,6 +51,8 @@ public class AnswerSaveAction<D> implements ActionUnit<AnswerSave<Message, D>, M
|
||||
if (checkNotNull(pusher)) {
|
||||
preservable.push(personId, pusher);
|
||||
}
|
||||
|
||||
log.debug("Завершилась обработка unit: {}.", answerSave.getName());
|
||||
return UnitRequest.of(answerSave, message);
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,8 @@ 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 lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@ -19,14 +21,12 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull;
|
||||
*
|
||||
* @author upagge [11/07/2019]
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class AnswerTextChatMailAction implements ActionUnit<AnswerText<ChatMail>, ChatMail> {
|
||||
|
||||
private final Sending sending;
|
||||
|
||||
public AnswerTextChatMailAction(Sending sending) {
|
||||
this.sending = sending;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UnitRequest<MainUnit, ChatMail> action(UnitRequest<AnswerText<ChatMail>, ChatMail> unitRequest) {
|
||||
final ChatMail message = unitRequest.getMessage();
|
||||
|
@ -9,6 +9,8 @@ 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 lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@ -19,36 +21,45 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull;
|
||||
*
|
||||
* @author upagge [11/07/2019]
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class AnswerTextMailAction implements ActionUnit<AnswerText<Mail>, Mail> {
|
||||
|
||||
private final Sending sending;
|
||||
|
||||
public AnswerTextMailAction(Sending sending) {
|
||||
this.sending = sending;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UnitRequest<MainUnit, Mail> action(UnitRequest<AnswerText<Mail>, Mail> unitRequest) {
|
||||
final Mail message = unitRequest.getMessage();
|
||||
final AnswerText<Mail> unit = unitRequest.getUnit();
|
||||
final Mail message = unitRequest.getMessage();
|
||||
|
||||
log.debug("Началась обработка юнита: {}.", unit.getName());
|
||||
|
||||
final ProcessingData<Mail> answerProcessing = unit.getAnswer();
|
||||
if (checkNotNull(answerProcessing)) {
|
||||
log.trace("Начало выполнения клиентской логики");
|
||||
final Optional<BoxAnswer> optAnswer = answerProcessing.processing(message);
|
||||
log.trace("Конец выполнения клиентской логики");
|
||||
if (optAnswer.isPresent()) {
|
||||
final BoxAnswer answer = optAnswer.get();
|
||||
|
||||
answer.setRecipientIfNull(message.getFromPersonId());
|
||||
|
||||
log.debug("AnswerText предоставил ответ для отправки.", answer);
|
||||
|
||||
final Optional<? extends SentBox> optSentBox = sending.send(answer);
|
||||
final CallBackConsumer callBack = unit.getCallBack();
|
||||
if (checkNotNull(callBack) && optAnswer.isPresent()) {
|
||||
final SentBox sentBox = optSentBox.get();
|
||||
log.debug("Вызывается callBack юнита. Название юнита: {}", unit.getName());
|
||||
callBack.processing(sentBox);
|
||||
}
|
||||
} else {
|
||||
log.debug("AnswerText не предоставил ответ для отправки. Название юнита: {}", unit.getName());
|
||||
}
|
||||
} else {
|
||||
log.warn("У AnswerText отсутствовал answer. Обработка не выполнялась. Название юнита: {}", unit.getName());
|
||||
}
|
||||
|
||||
log.debug("Завершилась обработка unit: {}.", unit.getName());
|
||||
return UnitRequest.of(unit, message);
|
||||
}
|
||||
|
||||
|
@ -95,6 +95,12 @@ public class StorylineMailService<T extends Message> implements StorylineService
|
||||
defaultUnitName = defaultUnit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanHistoryByPersonId(@NotNull String personId) {
|
||||
storylineRepository.cleanHistoryByPersonId(personId);
|
||||
unitPointerService.removeByPersonId(personId);
|
||||
}
|
||||
|
||||
//TODO [22.06.2022]: Временное решение для ленивой инициализации
|
||||
@Override
|
||||
public void lazyLink(String firstName, String secondName) {
|
||||
|
@ -33,4 +33,6 @@ public interface StorylineService<M extends Message> {
|
||||
|
||||
void setDefaultUnit(String unitName);
|
||||
|
||||
void cleanHistoryByPersonId(@NotNull String personId);
|
||||
|
||||
}
|
||||
|
@ -6,11 +6,13 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonDeserialize(using = TelegramEventContainerDeserializer.class)
|
||||
public class EventContainer<T> {
|
||||
|
||||
|
@ -1,8 +1,13 @@
|
||||
package dev.struchkov.godfather.main.domain.unit;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* Ключевое слово для юнитов.
|
||||
*/
|
||||
@Getter
|
||||
@ToString
|
||||
public class KeyWord {
|
||||
|
||||
private final Integer important;
|
||||
@ -24,12 +29,4 @@ public class KeyWord {
|
||||
return new KeyWord(1, word);
|
||||
}
|
||||
|
||||
public Integer getImportant() {
|
||||
return important;
|
||||
}
|
||||
|
||||
public String getWord() {
|
||||
return word;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ public class BoxAnswer {
|
||||
|
||||
public <T> Optional<T> getPayLoad(ContextKey<T> contextKey) {
|
||||
if (checkNotNull(payload)) {
|
||||
return Optional.ofNullable(payload.get(contextKey.getValue()))
|
||||
return Optional.ofNullable(payload.get(contextKey))
|
||||
.map(value -> (T) value);
|
||||
}
|
||||
return Optional.empty();
|
||||
|
@ -10,6 +10,7 @@ import dev.struchkov.haiti.utils.container.ContextKey;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -25,6 +26,7 @@ import static dev.struchkov.haiti.utils.Checker.checkNull;
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
public class BoxAnswer {
|
||||
|
||||
@ -51,6 +53,7 @@ public class BoxAnswer {
|
||||
/**
|
||||
* Обычное текстовое сообщение.
|
||||
*/
|
||||
@ToString.Exclude
|
||||
private String message;
|
||||
|
||||
/**
|
||||
|
@ -6,11 +6,13 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Getter
|
||||
@Builder
|
||||
@ToString
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class SentBox {
|
||||
|
@ -6,6 +6,7 @@ import dev.struchkov.godfather.main.domain.unit.UnitActiveType;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
@ -20,6 +21,7 @@ import java.util.regex.Pattern;
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
|
||||
public abstract class MainUnit<M extends Message> {
|
||||
|
||||
@ -62,6 +64,7 @@ public abstract class MainUnit<M extends Message> {
|
||||
/**
|
||||
* Множество следующих Unit в сценарии.
|
||||
*/
|
||||
@ToString.Exclude
|
||||
protected Set<MainUnit<M>> nextUnits;
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,5 @@
|
||||
package dev.struchkov.godfather.simple.domain.unit.func;
|
||||
|
||||
|
||||
import dev.struchkov.godfather.simple.domain.BoxAnswer;
|
||||
|
||||
import java.util.Optional;
|
||||
|
Loading…
Reference in New Issue
Block a user