Compare commits
No commits in common. "2f2f3319a2880dd36754c74f1c8477e107ea3181" and "420c7005815ae8871a4319943b900e95513439ae" have entirely different histories.
2f2f3319a2
...
420c700581
@ -0,0 +1,17 @@
|
||||
package dev.struchkov.godfather.quarkus.context.repository;
|
||||
|
||||
import io.smallrye.mutiny.Uni;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface PersonSettingRepository {
|
||||
|
||||
Uni<Set<String>> findAllByAllowedProcessing(Set<String> personIds);
|
||||
|
||||
Uni<Void> disableMessageProcessing(String personId);
|
||||
|
||||
Uni<Void> enableMessageProcessing(String personId);
|
||||
|
||||
Uni<Boolean> findStateByPersonId(String personId);
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package dev.struchkov.godfather.quarkus.context.service;
|
||||
|
||||
import io.smallrye.mutiny.Uni;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface PersonSettingService {
|
||||
|
||||
Uni<Set<String>> getAllPersonIdDisableMessages(@NotNull Set<String> personIds);
|
||||
|
||||
Uni<Boolean> getStateProcessingByPersonId(@NotNull String personId);
|
||||
|
||||
Uni<Void> disableMessageProcessing(@NotNull String personId);
|
||||
|
||||
Uni<Void> enableMessageProcessing(@NotNull String personId);
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package dev.struchkov.godfather.simple.context.repository;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public interface PersonSettingRepository {
|
||||
|
||||
Set<String> findAllByAllowedProcessing(Set<String> personIds);
|
||||
|
||||
void disableMessageProcessing(String personId);
|
||||
|
||||
void enableMessageProcessing(String personId);
|
||||
|
||||
Optional<Boolean> findStateByPersonId(String personId);
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package dev.struchkov.godfather.simple.context.service;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public interface PersonSettingService {
|
||||
|
||||
Set<String> getAllPersonIdDisableMessages(@NotNull Set<String> personIds);
|
||||
|
||||
Optional<Boolean> getStateProcessingByPersonId(@NotNull String personId);
|
||||
|
||||
void disableMessageProcessing(@NotNull String personId);
|
||||
|
||||
void enableMessageProcessing(@NotNull String personId);
|
||||
|
||||
}
|
@ -4,6 +4,7 @@ import dev.struchkov.godfather.main.domain.content.Message;
|
||||
import dev.struchkov.godfather.main.domain.unit.UnitActiveType;
|
||||
import dev.struchkov.godfather.quarkus.context.service.ErrorHandler;
|
||||
import dev.struchkov.godfather.quarkus.context.service.Modifiable;
|
||||
import dev.struchkov.godfather.quarkus.context.service.PersonSettingService;
|
||||
import dev.struchkov.godfather.quarkus.core.action.ActionUnit;
|
||||
import dev.struchkov.godfather.quarkus.core.service.StorylineService;
|
||||
import dev.struchkov.godfather.quarkus.domain.unit.MainUnit;
|
||||
@ -19,19 +20,27 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static dev.struchkov.haiti.utils.Checker.checkEmpty;
|
||||
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
|
||||
import static java.lang.Boolean.TRUE;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.emptySet;
|
||||
|
||||
public class GeneralAutoResponder<M extends Message> {
|
||||
|
||||
protected final PersonSettingService personSettingService;
|
||||
protected final StorylineService<M> storyLineService;
|
||||
protected Map<String, List<ActionUnit>> actionUnitMap = new HashMap<>();
|
||||
protected List<Modifiable<M>> modifiable;
|
||||
protected ErrorHandler errorHandler;
|
||||
|
||||
protected GeneralAutoResponder(StorylineService<M> storyLineService) {
|
||||
protected GeneralAutoResponder(
|
||||
PersonSettingService personSettingService,
|
||||
StorylineService<M> storyLineService
|
||||
) {
|
||||
this.personSettingService = personSettingService;
|
||||
this.storyLineService = storyLineService;
|
||||
}
|
||||
|
||||
@ -58,9 +67,17 @@ public class GeneralAutoResponder<M extends Message> {
|
||||
public Uni<Void> processingNewMessage(M newMessage) {
|
||||
return Uni.createFrom().item(newMessage)
|
||||
.onItem().ifNotNull().transformToUni(
|
||||
message -> processing(newMessage)
|
||||
)
|
||||
.replaceWithVoid();
|
||||
message -> personSettingService.getStateProcessingByPersonId(newMessage.getFromPersonId())
|
||||
.replaceIfNullWith(TRUE)
|
||||
.chain(
|
||||
state -> {
|
||||
if (TRUE.equals(state)) {
|
||||
return processing(newMessage);
|
||||
}
|
||||
return Uni.createFrom().voidItem();
|
||||
}
|
||||
)
|
||||
).replaceWithVoid();
|
||||
}
|
||||
|
||||
public Uni<Void> processingNewMessages(List<M> newMessages) {
|
||||
@ -68,9 +85,22 @@ public class GeneralAutoResponder<M extends Message> {
|
||||
.onItem().ifNotNull().transformToUni(
|
||||
messages -> {
|
||||
if (checkEmpty(newMessages)) return Uni.createFrom().voidItem();
|
||||
return Multi.createFrom().iterable(newMessages)
|
||||
|
||||
final Set<String> personIds = newMessages.stream()
|
||||
.map(Message::getFromPersonId)
|
||||
.collect(Collectors.toSet());
|
||||
return personSettingService.getAllPersonIdDisableMessages(personIds)
|
||||
.replaceIfNullWith(emptySet())
|
||||
.onItem().transformToMulti(
|
||||
disableIds -> {
|
||||
final List<M> allowedMessages = newMessages.stream()
|
||||
.filter(message -> !disableIds.contains(message.getFromPersonId()))
|
||||
.toList();
|
||||
return Multi.createFrom().iterable(allowedMessages);
|
||||
}
|
||||
)
|
||||
.onItem().transform(this::processing)
|
||||
.collect().asList().replaceWithVoid();
|
||||
.toUni().replaceWithVoid();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
package dev.struchkov.godfather.quarkus.core.service;
|
||||
|
||||
import dev.struchkov.godfather.quarkus.context.repository.PersonSettingRepository;
|
||||
import dev.struchkov.godfather.quarkus.context.service.PersonSettingService;
|
||||
import dev.struchkov.haiti.utils.Inspector;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class PersonSettingServiceImpl implements PersonSettingService {
|
||||
|
||||
private final PersonSettingRepository personSettingRepository;
|
||||
|
||||
public PersonSettingServiceImpl(PersonSettingRepository personSettingRepository) {
|
||||
this.personSettingRepository = personSettingRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uni<Set<String>> getAllPersonIdDisableMessages(@NotNull Set<String> personIds) {
|
||||
Inspector.isNotNull(personIds);
|
||||
return personSettingRepository.findAllByAllowedProcessing(personIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uni<Boolean> getStateProcessingByPersonId(@NotNull String personId) {
|
||||
return personSettingRepository.findStateByPersonId(personId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uni<Void> disableMessageProcessing(@NotNull String personId) {
|
||||
Inspector.isNotNull(personId);
|
||||
return personSettingRepository.disableMessageProcessing(personId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uni<Void> enableMessageProcessing(@NotNull String personId) {
|
||||
Inspector.isNotNull(personId);
|
||||
return personSettingRepository.enableMessageProcessing(personId);
|
||||
}
|
||||
|
||||
}
|
@ -5,6 +5,7 @@ import dev.struchkov.godfather.main.domain.unit.UnitActiveType;
|
||||
import dev.struchkov.godfather.simple.context.service.Accessibility;
|
||||
import dev.struchkov.godfather.simple.context.service.ErrorHandler;
|
||||
import dev.struchkov.godfather.simple.context.service.Modifiable;
|
||||
import dev.struchkov.godfather.simple.context.service.PersonSettingService;
|
||||
import dev.struchkov.godfather.simple.core.action.ActionUnit;
|
||||
import dev.struchkov.godfather.simple.core.service.StorylineService;
|
||||
import dev.struchkov.godfather.simple.domain.unit.MainUnit;
|
||||
@ -21,6 +22,7 @@ import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static dev.struchkov.haiti.utils.Checker.checkNotEmpty;
|
||||
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
|
||||
@ -29,6 +31,7 @@ public class GeneralAutoResponder<M extends Message> {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(GeneralAutoResponder.class);
|
||||
|
||||
protected final PersonSettingService personSettingService;
|
||||
protected final StorylineService<M> storyLineService;
|
||||
|
||||
protected Map<String, List<ActionUnit>> actionUnitMap = new HashMap<>();
|
||||
@ -36,7 +39,11 @@ public class GeneralAutoResponder<M extends Message> {
|
||||
protected ErrorHandler errorHandler;
|
||||
protected ExecutorService executorService;
|
||||
|
||||
protected GeneralAutoResponder(StorylineService<M> storyLineService) {
|
||||
protected GeneralAutoResponder(
|
||||
PersonSettingService personSettingService,
|
||||
StorylineService<M> storyLineService
|
||||
) {
|
||||
this.personSettingService = personSettingService;
|
||||
this.storyLineService = storyLineService;
|
||||
}
|
||||
|
||||
@ -68,25 +75,42 @@ public class GeneralAutoResponder<M extends Message> {
|
||||
if (checkNotNull(newMessage)) {
|
||||
if (checkNotNull(executorService)) {
|
||||
CompletableFuture.runAsync(
|
||||
() -> processing(newMessage), executorService
|
||||
() -> {
|
||||
final boolean state = personSettingService.getStateProcessingByPersonId(newMessage.getFromPersonId())
|
||||
.orElse(true);
|
||||
if (state) {
|
||||
processing(newMessage);
|
||||
}
|
||||
}, executorService
|
||||
).exceptionally(ex -> {
|
||||
log.error(ex.getMessage(), ex);
|
||||
return null;
|
||||
});
|
||||
} else {
|
||||
processing(newMessage);
|
||||
final boolean state = personSettingService.getStateProcessingByPersonId(newMessage.getFromPersonId())
|
||||
.orElse(true);
|
||||
if (state) {
|
||||
processing(newMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void processingNewMessages(List<M> newMessages) {
|
||||
if (checkNotEmpty(newMessages)) {
|
||||
final Set<String> personIds = newMessages.stream()
|
||||
.map(Message::getFromPersonId)
|
||||
.collect(Collectors.toSet());
|
||||
final Set<String> disableIds = personSettingService.getAllPersonIdDisableMessages(personIds);
|
||||
final List<M> allowedMessages = newMessages.stream()
|
||||
.filter(message -> !disableIds.contains(message.getFromPersonId()))
|
||||
.toList();
|
||||
if (checkNotNull(executorService)) {
|
||||
for (M allowedMessage : newMessages) {
|
||||
for (M allowedMessage : allowedMessages) {
|
||||
executorService.submit(() -> processing(allowedMessage));
|
||||
}
|
||||
} else {
|
||||
for (M allowedMessage : newMessages) {
|
||||
for (M allowedMessage : allowedMessages) {
|
||||
processing(allowedMessage);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
package dev.struchkov.godfather.simple.core.service;
|
||||
|
||||
import dev.struchkov.godfather.simple.context.repository.PersonSettingRepository;
|
||||
import dev.struchkov.godfather.simple.context.service.PersonSettingService;
|
||||
import dev.struchkov.haiti.utils.Inspector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public class PersonSettingServiceImpl implements PersonSettingService {
|
||||
|
||||
private final PersonSettingRepository personSettingRepository;
|
||||
|
||||
public PersonSettingServiceImpl(PersonSettingRepository personSettingRepository) {
|
||||
this.personSettingRepository = personSettingRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getAllPersonIdDisableMessages(@NotNull Set<String> personIds) {
|
||||
Inspector.isNotNull(personIds);
|
||||
return personSettingRepository.findAllByAllowedProcessing(personIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Boolean> getStateProcessingByPersonId(@NotNull String personId) {
|
||||
return personSettingRepository.findStateByPersonId(personId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disableMessageProcessing(@NotNull String personId) {
|
||||
Inspector.isNotNull(personId);
|
||||
personSettingRepository.disableMessageProcessing(personId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enableMessageProcessing(@NotNull String personId) {
|
||||
Inspector.isNotNull(personId);
|
||||
personSettingRepository.enableMessageProcessing(personId);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package dev.struchkov.godfather.quarkus.data.repository.impl;
|
||||
|
||||
import dev.struchkov.godfather.quarkus.context.repository.PersonSettingRepository;
|
||||
import io.smallrye.mutiny.Uni;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PersonSettingLocalRepository implements PersonSettingRepository {
|
||||
|
||||
private final Map<String, Boolean> map = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public Uni<Set<String>> findAllByAllowedProcessing(Set<String> personIds) {
|
||||
return Uni.createFrom().item(
|
||||
personIds.stream()
|
||||
.filter(map::get)
|
||||
.collect(Collectors.toSet())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uni<Void> disableMessageProcessing(String personId) {
|
||||
map.put(personId, false);
|
||||
return Uni.createFrom().voidItem();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uni<Void> enableMessageProcessing(String personId) {
|
||||
map.put(personId, true);
|
||||
return Uni.createFrom().voidItem();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uni<Boolean> findStateByPersonId(String personId) {
|
||||
return Uni.createFrom().item(map.get(personId));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package dev.struchkov.godfather.simple.data.repository.impl;
|
||||
|
||||
import dev.struchkov.godfather.simple.context.repository.PersonSettingRepository;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PersonSettingLocalRepository implements PersonSettingRepository {
|
||||
|
||||
private final Map<String, Boolean> map = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public Set<String> findAllByAllowedProcessing(Set<String> personIds) {
|
||||
return personIds.stream()
|
||||
.filter(map::get)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disableMessageProcessing(String personId) {
|
||||
map.put(personId, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enableMessageProcessing(String personId) {
|
||||
map.put(personId, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Boolean> findStateByPersonId(String personId) {
|
||||
return Optional.ofNullable(map.get(personId));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user