Skip to content

Commit cd287a3

Browse files
authored
Add regex support for filtered join addresses setting (#4342)
1 parent e461c7b commit cd287a3

File tree

7 files changed

+39
-21
lines changed

7 files changed

+39
-21
lines changed

Plan/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ subprojects {
9090
placeholderapiVersion = "2.11.5"
9191
nkPlaceholderapiVersion = "1.4-SNAPSHOT"
9292

93-
junitVersion = "5.13.4"
93+
junitVersion = "6.0.1"
9494
mockitoVersion = "5.19.0"
9595
seleniumVersion = "4.35.0"
9696
testContainersVersion = "1.21.3"
@@ -121,7 +121,7 @@ subprojects {
121121
}
122122

123123
// Required for JUnit to discover tests in modules.
124-
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
124+
testRuntimeOnly("org.junit.platform:junit-platform-launcher:$junitVersion")
125125
}
126126

127127
test {

Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/html/Contributors.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ public class Contributors {
118118
new Contributor("Zaemong", LANG),
119119
new Contributor("TWJohnJohn20116", LANG),
120120
new Contributor("YannicHock", CODE),
121-
new Contributor("SaolGhra", CODE)
121+
new Contributor("SaolGhra", CODE),
122+
new Contributor("Jsinco", CODE)
122123
};
123124

124125
private Contributors() {

Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/JSONFactory.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@
5252
import com.djrapitops.plan.storage.database.sql.tables.JoinAddressTable;
5353
import com.djrapitops.plan.utilities.comparators.SessionStartComparator;
5454
import com.djrapitops.plan.utilities.dev.Untrusted;
55+
import com.djrapitops.plan.utilities.java.Lists;
5556
import com.djrapitops.plan.utilities.java.Maps;
5657

5758
import javax.inject.Inject;
5859
import javax.inject.Singleton;
5960
import java.util.*;
6061
import java.util.concurrent.TimeUnit;
62+
import java.util.regex.Pattern;
6163
import java.util.stream.Collectors;
6264

6365
/**
@@ -159,13 +161,13 @@ public List<RetentionData> networkPlayerRetentionAsJSONMap() {
159161
return db.query(PlayerRetentionQueries.fetchRetentionData());
160162
}
161163

162-
private static void removeFiltered(Map<UUID, String> addressByPlayerUUID, List<String> filteredJoinAddresses) {
163-
if (filteredJoinAddresses.isEmpty() || filteredJoinAddresses.equals(List.of("play.example.com"))) return;
164+
private static void removeFiltered(Map<UUID, String> addressByPlayerUUID, List<Pattern> filteredJoinAddresses) {
165+
if (filteredJoinAddresses.isEmpty() || filteredJoinAddresses.equals(List.of(Pattern.compile("play\\.example\\.com")))) return;
164166

165167
Set<UUID> toRemove = new HashSet<>();
166168
// Remove filtered addresses from the data
167169
for (Map.Entry<UUID, String> entry : addressByPlayerUUID.entrySet()) {
168-
if (filteredJoinAddresses.contains(entry.getValue())) {
170+
if (filteredJoinAddresses.stream().anyMatch(pattern -> pattern.matcher(entry.getValue()).matches())) {
169171
toRemove.add(entry.getKey());
170172
}
171173
}
@@ -176,7 +178,7 @@ private static void removeFiltered(Map<UUID, String> addressByPlayerUUID, List<S
176178

177179
public PlayerJoinAddresses playerJoinAddresses(ServerUUID serverUUID, boolean includeByPlayerMap) {
178180
Database db = dbSystem.getDatabase();
179-
List<String> filteredJoinAddresses = config.get(DataGatheringSettings.FILTER_JOIN_ADDRESSES);
181+
List<Pattern> filteredJoinAddresses = Lists.map(config.get(DataGatheringSettings.FILTER_JOIN_ADDRESSES), Pattern::compile);
180182
if (includeByPlayerMap) {
181183
Map<UUID, String> addresses = db.query(JoinAddressQueries.latestJoinAddressesOfPlayers(serverUUID));
182184

@@ -188,16 +190,20 @@ public PlayerJoinAddresses playerJoinAddresses(ServerUUID serverUUID, boolean in
188190
);
189191
} else {
190192
List<String> addresses = db.query(JoinAddressQueries.uniqueJoinAddresses(serverUUID));
191-
addresses.removeAll(filteredJoinAddresses);
193+
addresses.removeIf(address ->
194+
filteredJoinAddresses.stream().anyMatch(pattern -> pattern.matcher(address).matches())
195+
);
192196
return new PlayerJoinAddresses(addresses, null);
193197
}
194198
}
195199

196200
public PlayerJoinAddresses playerJoinAddresses(boolean includeByPlayerMap) {
197201
Database db = dbSystem.getDatabase();
198-
List<String> filteredJoinAddresses = config.get(DataGatheringSettings.FILTER_JOIN_ADDRESSES);
202+
List<Pattern> filteredJoinAddresses = Lists.map(config.get(DataGatheringSettings.FILTER_JOIN_ADDRESSES), Pattern::compile);
199203
List<String> unique = db.query(JoinAddressQueries.uniqueJoinAddresses());
200-
unique.removeAll(filteredJoinAddresses);
204+
unique.removeIf(address ->
205+
filteredJoinAddresses.stream().anyMatch(pattern -> pattern.matcher(address).matches())
206+
);
201207
if (includeByPlayerMap) {
202208
Map<UUID, String> latest = db.query(JoinAddressQueries.latestJoinAddressesOfPlayers());
203209
removeFiltered(latest, filteredJoinAddresses);

Plan/common/src/main/java/com/djrapitops/plan/delivery/rendering/json/graphs/GraphJSONCreator.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import javax.inject.Singleton;
6565
import java.util.*;
6666
import java.util.concurrent.TimeUnit;
67+
import java.util.regex.Pattern;
6768
import java.util.stream.Collectors;
6869

6970
/**
@@ -474,11 +475,13 @@ public Map<String, Object> joinAddressesByDay(long after, long before, @Untruste
474475
return mapToJson(pieColors, joinAddresses);
475476
}
476477

477-
private static void removeFilteredAddresses(List<JoinAddressCount> addresses, List<String> filteredJoinAddresses) {
478-
if (filteredJoinAddresses.isEmpty() || filteredJoinAddresses.equals(List.of("play.example.com"))) return;
478+
private static void removeFilteredAddresses(List<JoinAddressCount> addresses, List<Pattern> filteredJoinAddresses) {
479+
if (filteredJoinAddresses.isEmpty() || filteredJoinAddresses.equals(List.of(Pattern.compile("play\\.example\\.com")))) return;
479480

480481
List<JoinAddressCount> addressesToRemove = addresses.stream()
481-
.filter(address -> filteredJoinAddresses.contains(address.getJoinAddress()))
482+
.filter(address ->
483+
filteredJoinAddresses.stream().anyMatch(p -> p.matcher(address.getJoinAddress()).matches())
484+
)
482485
.collect(Collectors.toList());
483486

484487
if (!addressesToRemove.isEmpty()) {
@@ -505,7 +508,7 @@ private Map<String, Object> mapToJson(String[] pieColors, List<DateObj<Map<Strin
505508
translateUnknown(addressesByDate.getValue());
506509
}
507510

508-
List<String> filteredJoinAddresses = config.get(DataGatheringSettings.FILTER_JOIN_ADDRESSES);
511+
List<Pattern> filteredJoinAddresses = Lists.map(config.get(DataGatheringSettings.FILTER_JOIN_ADDRESSES), Pattern::compile);
509512

510513
List<JoinAddressCounts> joinAddressCounts = joinAddresses.stream()
511514
.map(addressesOnDay -> {

Plan/common/src/main/java/com/djrapitops/plan/gathering/JoinAddressValidator.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@
1919
import com.djrapitops.plan.settings.config.PlanConfig;
2020
import com.djrapitops.plan.settings.config.paths.DataGatheringSettings;
2121
import com.djrapitops.plan.utilities.dev.Untrusted;
22+
import com.djrapitops.plan.utilities.java.Lists;
2223
import org.apache.commons.lang3.StringUtils;
2324

2425
import javax.inject.Inject;
2526
import javax.inject.Singleton;
2627
import java.net.URI;
2728
import java.net.URISyntaxException;
2829
import java.util.ArrayList;
30+
import java.util.Collections;
2931
import java.util.List;
32+
import java.util.regex.Pattern;
3033

3134
/**
3235
* Utility for validating and sanitizing join addresses.
@@ -37,7 +40,7 @@
3740
public class JoinAddressValidator {
3841

3942
private final PlanConfig config;
40-
private List<String> filteredAddresses;
43+
private List<Pattern> filteredAddresses;
4144

4245
@Inject
4346
public JoinAddressValidator(PlanConfig config) {
@@ -46,9 +49,13 @@ public JoinAddressValidator(PlanConfig config) {
4649
}
4750

4851
private void prepareFilteredAddresses() {
49-
if (filteredAddresses == null) {
50-
filteredAddresses = config.get(DataGatheringSettings.FILTER_JOIN_ADDRESSES);
51-
if (filteredAddresses == null) filteredAddresses = new ArrayList<>();
52+
if (filteredAddresses != null) return;
53+
54+
List<String> unCompiledFilteredAddresses = config.get(DataGatheringSettings.FILTER_JOIN_ADDRESSES);
55+
if (unCompiledFilteredAddresses != null) {
56+
filteredAddresses = Lists.map(unCompiledFilteredAddresses, Pattern::compile);
57+
} else {
58+
filteredAddresses = new ArrayList<>();
5259
}
5360
}
5461

@@ -72,7 +79,8 @@ public String sanitize(@Untrusted String address) {
7279
address = StringUtils.lowerCase(address);
7380
}
7481
prepareFilteredAddresses();
75-
if (filteredAddresses.contains(address)) {
82+
@Untrusted final String finalAddress = address;
83+
if (filteredAddresses.stream().anyMatch(pattern -> pattern.matcher(finalAddress).matches())) {
7684
address = "";
7785
}
7886
}

Plan/common/src/main/resources/assets/plan/bungeeconfig.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Data_gathering:
119119
# Does not affect already gathered data
120120
Preserve_case: false
121121
Preserve_invalid: false
122-
# Replaces these join addresses with unknown
122+
# Replaces these join addresses with unknown, supports regex
123123
Filter_out_from_data:
124124
- "play.example.com"
125125
# -----------------------------------------------------

Plan/common/src/main/resources/assets/plan/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Data_gathering:
123123
# Does not affect already gathered data
124124
Preserve_case: false
125125
Preserve_invalid: false
126-
# Replaces these join addresses with unknown
126+
# Replaces these join addresses with unknown, supports regex
127127
Filter_out_from_data:
128128
- "play.example.com"
129129
# -----------------------------------------------------

0 commit comments

Comments
 (0)