[AB-84] adding profanity filter with different feature modes

react command: adding additional mapping for character r
This commit is contained in:
Sheldan
2021-05-16 23:44:50 +02:00
parent eca9e6ebf7
commit 04a7cfafd7
53 changed files with 2875 additions and 34 deletions

View File

@@ -68,17 +68,22 @@ public class ProfanityServiceBean implements ProfanityService {
@Override
public boolean containsProfanity(String input, Long serverId) {
return getProfanityRegex(input, serverId).isPresent();
}
@Override
public Optional<ProfanityRegex> getProfanityRegex(String input, Long serverId) {
if(regex.containsKey(serverId)) {
List<PatternReplacement> regexes = regex.get(serverId);
log.debug("Checking existence of {} regexes for server {}.", regexes.size(), serverId);
for (PatternReplacement pattern: regexes) {
Matcher matcher = pattern.getPattern().matcher(input);
if(matcher.matches()) {
return true;
if(matcher.find()) {
return profanityRegexManagementService.getProfanityRegexViaIdOptional(pattern.profanityRegexId);
}
}
}
return false;
return Optional.empty();
}
@Override
@@ -132,18 +137,28 @@ public class ProfanityServiceBean implements ProfanityService {
regex = new HashMap<>();
List<ProfanityGroup> allGroups = profanityGroupManagementService.getAllGroups();
allGroups.forEach(profanityGroup -> profanityGroup.getProfanities().forEach(profanityRegex -> {
Pattern pattern = Pattern.compile(profanityRegex.getRegex());
List<PatternReplacement> newPatterns = new ArrayList<>();
Long serverId = profanityGroup.getServer().getId();
if(regex.containsKey(serverId)) {
regex.get(serverId).add(PatternReplacement.builder().pattern(pattern).replacement(profanityRegex.getReplacement()).build());
} else {
newPatterns.add(PatternReplacement.builder().pattern(pattern).replacement(profanityRegex.getReplacement()).build());
regex.put(serverId, newPatterns);
}
loadProfanityRegex(profanityRegex, serverId);
}));
}
private void loadProfanityRegex(ProfanityRegex profanityRegex, Long serverId) {
Pattern pattern = Pattern.compile(profanityRegex.getRegex());
List<PatternReplacement> newPatterns = new ArrayList<>();
PatternReplacement patternReplacement = PatternReplacement
.builder()
.pattern(pattern)
.replacement(profanityRegex.getReplacement())
.profanityRegexId(profanityRegex.getId())
.build();
if (regex.containsKey(serverId)) {
regex.get(serverId).add(patternReplacement);
} else {
newPatterns.add(patternReplacement);
regex.put(serverId, newPatterns);
}
}
@Override
public void reloadRegex(Long serverId) {
log.info("Reloading regex for server {}.", serverId);
@@ -152,22 +167,16 @@ public class ProfanityServiceBean implements ProfanityService {
}
regex.remove(serverId);
List<ProfanityGroup> allGroups = profanityGroupManagementService.getAllForServer(serverId);
allGroups.forEach(profanityGroup -> profanityGroup.getProfanities().forEach(profanityRegex -> {
Pattern pattern = Pattern.compile(profanityRegex.getRegex());
List<PatternReplacement> newPatterns = new ArrayList<>();
if(regex.containsKey(serverId)) {
regex.get(serverId).add(PatternReplacement.builder().pattern(pattern).replacement(profanityRegex.getReplacement()).build());
} else {
newPatterns.add(PatternReplacement.builder().pattern(pattern).replacement(profanityRegex.getReplacement()).build());
regex.put(serverId, newPatterns);
}
}));
allGroups
.forEach(profanityGroup -> profanityGroup.getProfanities()
.forEach(profanityRegex -> loadProfanityRegex(profanityRegex, serverId)));
}
@Getter
@Builder
private static class PatternReplacement {
private final Long profanityRegexId;
private final Pattern pattern;
private final String replacement;
}

View File

@@ -44,6 +44,16 @@ public class ProfanityGroupManagementServiceBean implements ProfanityGroupManage
return getProfanityGroupOptional(server, name).isPresent();
}
@Override
public Optional<ProfanityGroup> getProfanityGroupByIdOptional(Long profanityGroupId) {
return repository.findById(profanityGroupId);
}
@Override
public ProfanityGroup getProfanityGroupById(Long profanityGroupId) {
return getProfanityGroupByIdOptional(profanityGroupId).orElseThrow(ProfanityGroupNotFoundException::new);
}
@Override
public Optional<ProfanityGroup> getProfanityGroupOptional(AServer server, String name) {
return repository.findByServerAndGroupNameIgnoreCase(server, name);

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.core.service.management;
import dev.sheldan.abstracto.core.exception.ProfanityRegexNotFoundException;
import dev.sheldan.abstracto.core.models.database.ProfanityGroup;
import dev.sheldan.abstracto.core.models.database.ProfanityRegex;
import dev.sheldan.abstracto.core.repository.ProfanityRegexRepository;
@@ -54,4 +55,14 @@ public class ProfanityRegexManagementServiceBean implements ProfanityRegexManag
public Optional<ProfanityRegex> getProfanityRegexOptional(ProfanityGroup profanityGroup, String name) {
return repository.findByGroupAndRegexNameIgnoreCase(profanityGroup, name);
}
@Override
public Optional<ProfanityRegex> getProfanityRegexViaIdOptional(Long profanityRegexId) {
return repository.findById(profanityRegexId);
}
@Override
public ProfanityRegex getProfanityRegexViaId(Long profanityRegexId) {
return getProfanityRegexViaIdOptional(profanityRegexId).orElseThrow(ProfanityRegexNotFoundException::new);
}
}