Skip to content

Commit 3b1feb2

Browse files
committed
fix bugs in /names
1 parent 698a48c commit 3b1feb2

File tree

4 files changed

+85
-62
lines changed

4 files changed

+85
-62
lines changed

src/main/java/vc/api/LabyRestClient.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.springframework.http.client.ClientHttpRequestFactory;
44
import org.springframework.stereotype.Component;
55
import org.springframework.web.client.RestClient;
6+
import vc.api.model.LabyProfileSearch;
67
import vc.api.model.LabyProfileSearchResponse;
78

89
@Component
@@ -17,10 +18,11 @@ public LabyRestClient(ClientHttpRequestFactory requestFactory) {
1718
.build();
1819
}
1920

20-
public LabyProfileSearchResponse searchProfiles(String username) {
21-
return restClient.get()
21+
public LabyProfileSearch searchProfiles(String username) {
22+
var response = restClient.get()
2223
.uri("/search/profiles/{username}", username)
2324
.retrieve()
2425
.body(LabyProfileSearchResponse.class);
26+
return new LabyProfileSearch(username, response);
2527
}
2628
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package vc.api.model;
2+
3+
import org.jspecify.annotations.Nullable;
4+
5+
import java.util.*;
6+
import java.util.stream.Collectors;
7+
8+
public record LabyProfileSearch(String username, LabyProfileSearchResponse response) {
9+
List<LabyProfile> users() {
10+
return response == null || response.users() == null ? Collections.emptyList() : response.users();
11+
}
12+
13+
public @Nullable LabyProfile currentProfile() {
14+
if (users().isEmpty()) return null;
15+
for (var user : users()) {
16+
if (user.name().equalsIgnoreCase(username)) {
17+
return user;
18+
}
19+
}
20+
return null;
21+
}
22+
23+
public List<LabyProfile> historicalProfiles() {
24+
var users = users();
25+
if (users.isEmpty()) return Collections.emptyList();
26+
var currentProfile = currentProfile();
27+
if (currentProfile != null && users.size() == 1) return Collections.emptyList();
28+
return users.stream()
29+
.filter(user -> !Objects.equals(currentProfile, user))
30+
.collect(Collectors.toList());
31+
}
32+
33+
public List<String> previousUsernames() {
34+
var currentProfile = currentProfile();
35+
if (currentProfile == null) return Collections.emptyList();
36+
return currentProfile.nameHistory().stream()
37+
.filter(name -> !name.equalsIgnoreCase(username))
38+
.distinct()
39+
.toList();
40+
}
41+
42+
public List<String> associatedUsernames() {
43+
var historicalProfiles = historicalProfiles();
44+
if (historicalProfiles.isEmpty()) return Collections.emptyList();
45+
46+
47+
Set<String> currentProfileNames = new HashSet<>();
48+
currentProfileNames.add(username);
49+
var currentProfile = currentProfile();
50+
if (currentProfile != null) {
51+
currentProfileNames.addAll(currentProfile.nameHistory());
52+
}
53+
Set<String> associatedUsernames = new HashSet<>();
54+
for (var profile : historicalProfiles) {
55+
var name = profile.name();
56+
if (!currentProfileNames.contains(name)) {
57+
associatedUsernames.add(name);
58+
}
59+
var historyList = profile.history();
60+
if (historyList != null) {
61+
for (var h : historyList) {
62+
var hName = h.name();
63+
if (!currentProfileNames.contains(hName)) {
64+
associatedUsernames.add(hName);
65+
}
66+
}
67+
}
68+
}
69+
return new ArrayList<>(associatedUsernames);
70+
}
71+
}
Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,5 @@
11
package vc.api.model;
22

3-
import org.jspecify.annotations.Nullable;
3+
import java.util.List;
44

5-
import java.util.*;
6-
7-
public record LabyProfileSearchResponse(List<LabyProfile> users) {
8-
public @Nullable ProfileData currentProfile() {
9-
return users != null && !users.isEmpty() ? users.getFirst() : null;
10-
}
11-
12-
public List<ProfileData> historicalProfiles() {
13-
if (users == null || users.size() < 2) return Collections.emptyList();
14-
return (List) users.subList(1, users.size());
15-
}
16-
17-
public List<String> previousUsernames() {
18-
if (users == null || users.isEmpty()) return Collections.emptyList();
19-
var currentProfile = currentProfile();
20-
if (currentProfile == null) return Collections.emptyList();
21-
LabyProfile currentLabyProfile = users.getFirst();
22-
return currentLabyProfile.history().stream()
23-
.map(LabyNameHistoryProfile::name)
24-
.distinct()
25-
.filter(name -> !name.equals(currentProfile.name()))
26-
.toList();
27-
}
28-
29-
public List<String> associatedUsernames() {
30-
if (users == null || users.size() < 2) return Collections.emptyList();
31-
var currentProfile = currentProfile();
32-
if (currentProfile == null) return Collections.emptyList();
33-
final Set<String> previousUsernames = new HashSet<>();
34-
previousUsernames().forEach(u -> {
35-
previousUsernames.add(u.toLowerCase());
36-
});
37-
previousUsernames.add(currentProfile.name().toLowerCase());
38-
List<String> usernames = new ArrayList<>();
39-
var historicalProfiles = users.subList(1, users.size());
40-
for (LabyProfile profile : historicalProfiles) {
41-
var name = profile.name();
42-
previousUsernames.add(name.toLowerCase());
43-
var historyList = profile.history();
44-
if (historyList != null) {
45-
for (var h : historyList) {
46-
var hName = h.name();
47-
if (!previousUsernames.contains(hName.toLowerCase())) {
48-
usernames.add(hName);
49-
}
50-
}
51-
}
52-
}
53-
return usernames.stream().distinct().toList();
54-
}
55-
}
5+
public record LabyProfileSearchResponse(List<LabyProfile> users) { }

src/main/java/vc/commands/NamesCommand.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.springframework.stereotype.Component;
99
import reactor.core.publisher.Mono;
1010
import vc.api.LabyRestClient;
11-
import vc.api.model.LabyProfileSearchResponse;
11+
import vc.api.model.LabyProfileSearch;
1212
import vc.openapi.handler.ApiException;
1313
import vc.util.DiscordMarkdownEscape;
1414
import vc.util.Validator;
@@ -40,9 +40,9 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
4040
if (!Validator.isValidPlayerName(player)) {
4141
return error(event, "Invalid player name");
4242
}
43-
LabyProfileSearchResponse searchResponse;
43+
LabyProfileSearch search;
4444
try {
45-
searchResponse = labyRestClient.searchProfiles(player);
45+
search = labyRestClient.searchProfiles(player);
4646
} catch (Exception e) {
4747
if (e instanceof ApiException apiException) {
4848
if (apiException.getCause() instanceof HttpTimeoutException httpTimeoutException) {
@@ -61,15 +61,15 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
6161
.addField("Source", "[LabyMod API](https://laby.net/@" + player + ")", true)
6262
.color(Color.CYAN);
6363
var sb = new StringBuilder();
64-
var currentProfile = searchResponse.currentProfile();
64+
var currentProfile = search.currentProfile();
6565
sb.append("**Current Profile**\n\n");
6666
if (currentProfile != null) {
6767
sb.append(currentProfile.toDiscordFieldValue()).append("\n");
6868
builder.thumbnail(currentProfile.getAvatarURL());
6969
} else {
7070
sb.append("(none)\n");
7171
}
72-
var prevUsernames = searchResponse.previousUsernames();
72+
var prevUsernames = search.previousUsernames();
7373
sb.append("\n**Previous Usernames**\n\n");
7474
if (!prevUsernames.isEmpty()) {
7575
for (var name : prevUsernames) {
@@ -78,7 +78,7 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
7878
} else {
7979
sb.append("(none)\n");
8080
}
81-
var historicalProfiles = searchResponse.historicalProfiles();
81+
var historicalProfiles = search.historicalProfiles();
8282
sb.append("\n**Previous Accounts**\n\n");
8383
if (!historicalProfiles.isEmpty()) {
8484
for (var historicalProfile : historicalProfiles) {
@@ -87,11 +87,11 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
8787
} else {
8888
sb.append("(none)\n");
8989
}
90-
var names = searchResponse.associatedUsernames();
90+
var names = search.associatedUsernames();
9191
sb.append("\n**Previous Account Usernames**\n\n");
9292
if (!names.isEmpty()) {
9393
for (var name : names) {
94-
sb.append(name).append("\n");
94+
sb.append(DiscordMarkdownEscape.escape(name)).append("\n");
9595
}
9696
} else {
9797
sb.append("(none)\n");

0 commit comments

Comments
 (0)