Рефакторинг
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Struchkov Mark 2023-04-01 21:45:24 +03:00
parent dc567ad410
commit d723460d31
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
19 changed files with 73 additions and 57 deletions

View File

@ -12,7 +12,7 @@ public interface StorylineContext {
<T> Uni<T> getByKey(@NotNull String personId, @NotNull ContextKey<T> key); <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); <T> Uni<T> getByKeyOrThrow(@NotNull String personId, @NotNull ContextKey<T> key);

View File

@ -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();
}
}

View File

@ -77,7 +77,6 @@ public class GeneralAutoResponder<M extends Message> {
} }
) )
).replaceWithVoid(); ).replaceWithVoid();
} }
public Uni<Void> processingNewMessages(List<M> newMessages) { 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) { public Uni<Void> answer(UnitRequest<MainUnit, M> unitRequest) {
return getAction(unitRequest) return getAction(unitRequest)
.chain(request -> activeUnitAfter(unitRequest)) .chain(this::activeUnitAfter)
.onFailure().call( .onFailure().call(
throwable -> { throwable -> {
if (checkNotNull(errorHandler)) { if (checkNotNull(errorHandler)) {
@ -144,7 +143,7 @@ public class GeneralAutoResponder<M extends Message> {
v -> getAction(UnitRequest.of(first.get(), unitRequest.getMessage())) v -> getAction(UnitRequest.of(first.get(), unitRequest.getMessage()))
) )
.onItem().transformToUni( .onItem().transformToUni(
uR -> activeUnitAfter(UnitRequest.of(first.get(), unitRequest.getMessage())) newRequest -> activeUnitAfter(UnitRequest.of(first.get(), unitRequest.getMessage()))
); );
} }
} }

View File

@ -35,7 +35,7 @@ public class StorylineContextMapImpl implements StorylineContext {
} }
@Override @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); isNotNull(personId, key);
if (map.containsKey(personId)) { if (map.containsKey(personId)) {
final Map<String, Object> storage = map.get(personId); final Map<String, Object> storage = map.get(personId);

View File

@ -119,6 +119,12 @@ public class StorylineMailService implements StorylineService<Mail> {
defaultUnitName = defaultUnit; defaultUnitName = defaultUnit;
} }
@Override
public Uni<Void> cleanHistoryByPersonId(@NotNull String personId) {
return storylineRepository.cleanHistoryByPersonId(personId)
.call(() -> unitPointerService.removeByPersonId(personId));
}
//TODO [22.06.2022]: Временное решение для ленивой инициализации //TODO [22.06.2022]: Временное решение для ленивой инициализации
@Override @Override
public void lazyLink(String firstName, String secondName) { public void lazyLink(String firstName, String secondName) {

View File

@ -34,4 +34,6 @@ public interface StorylineService<M extends Message> {
void setDefaultUnit(String unitName); void setDefaultUnit(String unitName);
Uni<Void> cleanHistoryByPersonId(@NotNull String personId);
} }

View File

@ -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.AnswerCheck;
import dev.struchkov.godfather.simple.domain.unit.MainUnit; import dev.struchkov.godfather.simple.domain.unit.MainUnit;
import dev.struchkov.godfather.simple.domain.unit.UnitRequest; import dev.struchkov.godfather.simple.domain.unit.UnitRequest;
import org.slf4j.Logger; import lombok.RequiredArgsConstructor;
import org.slf4j.LoggerFactory; import lombok.extern.slf4j.Slf4j;
import java.util.Objects; import java.util.Objects;
@ -18,20 +18,16 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull;
* *
* @author upagge [11/07/2019] * @author upagge [11/07/2019]
*/ */
@Slf4j
@RequiredArgsConstructor
public class AnswerCheckAction implements ActionUnit<AnswerCheck<Message>, Message> { public class AnswerCheckAction implements ActionUnit<AnswerCheck<Message>, Message> {
private static final Logger log = LoggerFactory.getLogger(AnswerCheckAction.class);
private final Sending sending; private final Sending sending;
public AnswerCheckAction(Sending sending) {
this.sending = sending;
}
@Override @Override
public UnitRequest<MainUnit, Message> action(UnitRequest<AnswerCheck<Message>, Message> unitRequest) { public UnitRequest<MainUnit, Message> action(UnitRequest<AnswerCheck<Message>, Message> unitRequest) {
final AnswerCheck<Message> unit = unitRequest.getUnit(); final AnswerCheck<Message> unit = unitRequest.getUnit();
log.debug("Началась обработка unit: {}.", unit.getName()); log.debug("Началась обработка юнита: {}.", unit.getName());
final Message message = unitRequest.getMessage(); final Message message = unitRequest.getMessage();
@ -41,6 +37,7 @@ public class AnswerCheckAction implements ActionUnit<AnswerCheck<Message>, Messa
final BoxAnswer answerIfTrue = unit.getIntermediateAnswerIfTrue(); final BoxAnswer answerIfTrue = unit.getIntermediateAnswerIfTrue();
if (checkNotNull(answerIfTrue)) { if (checkNotNull(answerIfTrue)) {
answerIfTrue.setRecipientIfNull(message.getFromPersonId()); answerIfTrue.setRecipientIfNull(message.getFromPersonId());
log.trace("Будет отправлен промежуточный ответ: {}", answerIfTrue);
sending.send(answerIfTrue); sending.send(answerIfTrue);
} }
unitAnswer = unit.getUnitTrue(); unitAnswer = unit.getUnitTrue();
@ -49,6 +46,7 @@ public class AnswerCheckAction implements ActionUnit<AnswerCheck<Message>, Messa
final BoxAnswer answerIfFalse = unit.getIntermediateAnswerIfFalse(); final BoxAnswer answerIfFalse = unit.getIntermediateAnswerIfFalse();
if (checkNotNull(answerIfFalse)) { if (checkNotNull(answerIfFalse)) {
answerIfFalse.setRecipientIfNull(message.getFromPersonId()); answerIfFalse.setRecipientIfNull(message.getFromPersonId());
log.trace("Будет отправлен промежуточный ответ: {}", answerIfFalse);
sending.send(answerIfFalse); sending.send(answerIfFalse);
} }
unitAnswer = unit.getUnitFalse(); unitAnswer = unit.getUnitFalse();

View File

@ -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.PreservableData;
import dev.struchkov.godfather.simple.domain.unit.func.Pusher; import dev.struchkov.godfather.simple.domain.unit.func.Pusher;
import dev.struchkov.godfather.simple.domain.unit.func.preser.AnswerSavePreservable; import dev.struchkov.godfather.simple.domain.unit.func.preser.AnswerSavePreservable;
import lombok.extern.slf4j.Slf4j;
import static dev.struchkov.haiti.utils.Checker.checkNotNull; 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] * @author upagge [11/07/2019]
*/ */
@Slf4j
public class AnswerSaveAction<D> implements ActionUnit<AnswerSave<Message, D>, Message> { public class AnswerSaveAction<D> implements ActionUnit<AnswerSave<Message, D>, Message> {
@Override @Override
public UnitRequest<MainUnit, Message> action(UnitRequest<AnswerSave<Message, D>, Message> unitRequest) { public UnitRequest<MainUnit, Message> action(UnitRequest<AnswerSave<Message, D>, Message> unitRequest) {
final AnswerSave<Message, D> answerSave = unitRequest.getUnit(); final AnswerSave<Message, D> answerSave = unitRequest.getUnit();
log.debug("Началась обработка юнита: {}.", answerSave.getName());
final Message message = unitRequest.getMessage(); final Message message = unitRequest.getMessage();
final AnswerSavePreservable<D> preservable = answerSave.getPreservable(); 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(); final CheckSave<Message> checkSave = answerSave.getCheckSave();
if (checkNotNull(checkSave)) { if (checkNotNull(checkSave)) {
MainUnit<Message> unit = checkSave.check(message); final MainUnit<Message> unit = checkSave.check(message);
if (checkNotNull(unit)) { if (checkNotNull(unit)) {
return UnitRequest.of(unit, message); return UnitRequest.of(unit, message);
} }
@ -46,6 +51,8 @@ public class AnswerSaveAction<D> implements ActionUnit<AnswerSave<Message, D>, M
if (checkNotNull(pusher)) { if (checkNotNull(pusher)) {
preservable.push(personId, pusher); preservable.push(personId, pusher);
} }
log.debug("Завершилась обработка unit: {}.", answerSave.getName());
return UnitRequest.of(answerSave, message); return UnitRequest.of(answerSave, message);
} }

View File

@ -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.UnitRequest;
import dev.struchkov.godfather.simple.domain.unit.func.CallBackConsumer; import dev.struchkov.godfather.simple.domain.unit.func.CallBackConsumer;
import dev.struchkov.godfather.simple.domain.unit.func.ProcessingData; import dev.struchkov.godfather.simple.domain.unit.func.ProcessingData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.Optional; import java.util.Optional;
@ -19,14 +21,12 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull;
* *
* @author upagge [11/07/2019] * @author upagge [11/07/2019]
*/ */
@Slf4j
@RequiredArgsConstructor
public class AnswerTextChatMailAction implements ActionUnit<AnswerText<ChatMail>, ChatMail> { public class AnswerTextChatMailAction implements ActionUnit<AnswerText<ChatMail>, ChatMail> {
private final Sending sending; private final Sending sending;
public AnswerTextChatMailAction(Sending sending) {
this.sending = sending;
}
@Override @Override
public UnitRequest<MainUnit, ChatMail> action(UnitRequest<AnswerText<ChatMail>, ChatMail> unitRequest) { public UnitRequest<MainUnit, ChatMail> action(UnitRequest<AnswerText<ChatMail>, ChatMail> unitRequest) {
final ChatMail message = unitRequest.getMessage(); final ChatMail message = unitRequest.getMessage();

View File

@ -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.UnitRequest;
import dev.struchkov.godfather.simple.domain.unit.func.CallBackConsumer; import dev.struchkov.godfather.simple.domain.unit.func.CallBackConsumer;
import dev.struchkov.godfather.simple.domain.unit.func.ProcessingData; import dev.struchkov.godfather.simple.domain.unit.func.ProcessingData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.Optional; import java.util.Optional;
@ -19,36 +21,45 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull;
* *
* @author upagge [11/07/2019] * @author upagge [11/07/2019]
*/ */
@Slf4j
@RequiredArgsConstructor
public class AnswerTextMailAction implements ActionUnit<AnswerText<Mail>, Mail> { public class AnswerTextMailAction implements ActionUnit<AnswerText<Mail>, Mail> {
private final Sending sending; private final Sending sending;
public AnswerTextMailAction(Sending sending) {
this.sending = sending;
}
@Override @Override
public UnitRequest<MainUnit, Mail> action(UnitRequest<AnswerText<Mail>, Mail> unitRequest) { public UnitRequest<MainUnit, Mail> action(UnitRequest<AnswerText<Mail>, Mail> unitRequest) {
final Mail message = unitRequest.getMessage();
final AnswerText<Mail> unit = unitRequest.getUnit(); final AnswerText<Mail> unit = unitRequest.getUnit();
final Mail message = unitRequest.getMessage();
log.debug("Началась обработка юнита: {}.", unit.getName());
final ProcessingData<Mail> answerProcessing = unit.getAnswer(); final ProcessingData<Mail> answerProcessing = unit.getAnswer();
if (checkNotNull(answerProcessing)) { if (checkNotNull(answerProcessing)) {
log.trace("Начало выполнения клиентской логики");
final Optional<BoxAnswer> optAnswer = answerProcessing.processing(message); final Optional<BoxAnswer> optAnswer = answerProcessing.processing(message);
log.trace("Конец выполнения клиентской логики");
if (optAnswer.isPresent()) { if (optAnswer.isPresent()) {
final BoxAnswer answer = optAnswer.get(); final BoxAnswer answer = optAnswer.get();
answer.setRecipientIfNull(message.getFromPersonId()); answer.setRecipientIfNull(message.getFromPersonId());
log.debug("AnswerText предоставил ответ для отправки.", answer);
final Optional<? extends SentBox> optSentBox = sending.send(answer); final Optional<? extends SentBox> optSentBox = sending.send(answer);
final CallBackConsumer callBack = unit.getCallBack(); final CallBackConsumer callBack = unit.getCallBack();
if (checkNotNull(callBack) && optAnswer.isPresent()) { if (checkNotNull(callBack) && optAnswer.isPresent()) {
final SentBox sentBox = optSentBox.get(); final SentBox sentBox = optSentBox.get();
log.debug("Вызывается callBack юнита. Название юнита: {}", unit.getName());
callBack.processing(sentBox); 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); return UnitRequest.of(unit, message);
} }

View File

@ -95,6 +95,12 @@ public class StorylineMailService<T extends Message> implements StorylineService
defaultUnitName = defaultUnit; defaultUnitName = defaultUnit;
} }
@Override
public void cleanHistoryByPersonId(@NotNull String personId) {
storylineRepository.cleanHistoryByPersonId(personId);
unitPointerService.removeByPersonId(personId);
}
//TODO [22.06.2022]: Временное решение для ленивой инициализации //TODO [22.06.2022]: Временное решение для ленивой инициализации
@Override @Override
public void lazyLink(String firstName, String secondName) { public void lazyLink(String firstName, String secondName) {

View File

@ -33,4 +33,6 @@ public interface StorylineService<M extends Message> {
void setDefaultUnit(String unitName); void setDefaultUnit(String unitName);
void cleanHistoryByPersonId(@NotNull String personId);
} }

View File

@ -6,11 +6,13 @@ import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.ToString;
@Getter @Getter
@Setter @Setter
@AllArgsConstructor @ToString
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor
@JsonDeserialize(using = TelegramEventContainerDeserializer.class) @JsonDeserialize(using = TelegramEventContainerDeserializer.class)
public class EventContainer<T> { public class EventContainer<T> {

View File

@ -1,8 +1,13 @@
package dev.struchkov.godfather.main.domain.unit; package dev.struchkov.godfather.main.domain.unit;
import lombok.Getter;
import lombok.ToString;
/** /**
* Ключевое слово для юнитов. * Ключевое слово для юнитов.
*/ */
@Getter
@ToString
public class KeyWord { public class KeyWord {
private final Integer important; private final Integer important;
@ -24,12 +29,4 @@ public class KeyWord {
return new KeyWord(1, word); return new KeyWord(1, word);
} }
public Integer getImportant() {
return important;
}
public String getWord() {
return word;
}
} }

View File

@ -130,7 +130,7 @@ public class BoxAnswer {
public <T> Optional<T> getPayLoad(ContextKey<T> contextKey) { public <T> Optional<T> getPayLoad(ContextKey<T> contextKey) {
if (checkNotNull(payload)) { if (checkNotNull(payload)) {
return Optional.ofNullable(payload.get(contextKey.getValue())) return Optional.ofNullable(payload.get(contextKey))
.map(value -> (T) value); .map(value -> (T) value);
} }
return Optional.empty(); return Optional.empty();

View File

@ -10,6 +10,7 @@ import dev.struchkov.haiti.utils.container.ContextKey;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.ToString;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -25,6 +26,7 @@ import static dev.struchkov.haiti.utils.Checker.checkNull;
*/ */
@Getter @Getter
@Setter @Setter
@ToString
@NoArgsConstructor @NoArgsConstructor
public class BoxAnswer { public class BoxAnswer {
@ -51,6 +53,7 @@ public class BoxAnswer {
/** /**
* Обычное текстовое сообщение. * Обычное текстовое сообщение.
*/ */
@ToString.Exclude
private String message; private String message;
/** /**

View File

@ -6,11 +6,13 @@ import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.ToString;
import java.util.Optional; import java.util.Optional;
@Getter @Getter
@Builder @Builder
@ToString
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE) @AllArgsConstructor(access = AccessLevel.PRIVATE)
public class SentBox { public class SentBox {

View File

@ -6,6 +6,7 @@ import dev.struchkov.godfather.main.domain.unit.UnitActiveType;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.ToString;
import java.util.Collection; import java.util.Collection;
import java.util.Optional; import java.util.Optional;
@ -20,6 +21,7 @@ import java.util.regex.Pattern;
*/ */
@Getter @Getter
@Setter @Setter
@ToString
@EqualsAndHashCode(onlyExplicitlyIncluded = true) @EqualsAndHashCode(onlyExplicitlyIncluded = true)
public abstract class MainUnit<M extends Message> { public abstract class MainUnit<M extends Message> {
@ -62,6 +64,7 @@ public abstract class MainUnit<M extends Message> {
/** /**
* Множество следующих Unit в сценарии. * Множество следующих Unit в сценарии.
*/ */
@ToString.Exclude
protected Set<MainUnit<M>> nextUnits; protected Set<MainUnit<M>> nextUnits;
/** /**

View File

@ -1,6 +1,5 @@
package dev.struchkov.godfather.simple.domain.unit.func; package dev.struchkov.godfather.simple.domain.unit.func;
import dev.struchkov.godfather.simple.domain.BoxAnswer; import dev.struchkov.godfather.simple.domain.BoxAnswer;
import java.util.Optional; import java.util.Optional;