diff --git a/src/main/java/net/frozenorb/apiv3/APIv3.java b/src/main/java/net/frozenorb/apiv3/APIv3.java index 8338fdf..bfe0cfd 100644 --- a/src/main/java/net/frozenorb/apiv3/APIv3.java +++ b/src/main/java/net/frozenorb/apiv3/APIv3.java @@ -238,6 +238,7 @@ public final class APIv3 extends AbstractVerticle { }); database.getCollection("users").createIndexes(ImmutableList.of( new IndexModel(new Document("lastUsername", 1)), + new IndexModel(new Document("lastUsernameLower", 1)), new IndexModel(new Document("emailToken", 1)) ), (a, b) -> { }); diff --git a/src/main/java/net/frozenorb/apiv3/model/User.java b/src/main/java/net/frozenorb/apiv3/model/User.java index d3a7a82..abae471 100644 --- a/src/main/java/net/frozenorb/apiv3/model/User.java +++ b/src/main/java/net/frozenorb/apiv3/model/User.java @@ -41,6 +41,7 @@ public final class User { @Getter @Id @JsonSerialize(using = UuidJsonSerializer.class) @JsonDeserialize(using = UuidJsonDeserializer.class) private UUID id; @Getter private String lastUsername; + @Getter private String lastUsernameLower; @Getter @ExcludeFromReplies private Map aliases = new HashMap<>(); @Getter @ExcludeFromReplies @Setter private String totpSecret; @Getter @ExcludeFromReplies private String password; @@ -148,6 +149,10 @@ public final class User { usersCollection.find(new Document("lastUsername", lastUsername)).first(SyncUtils.vertxWrap(callback)); } + public static void findByLastUsernameLower(String lastUsernameLower, SingleResultCallback callback) { + usersCollection.find(new Document("lastUsernameLower", lastUsernameLower)).first(SyncUtils.vertxWrap(callback)); + } + public static void findOrCreateByIdGrouped(Map search, SingleResultCallback> callback) { usersCollection.find(new Document("_id", new Document("$in", search.keySet()))).into(new LinkedList<>(), SyncUtils.vertxWrap((users, error) -> { if (error != null) { @@ -205,6 +210,7 @@ public final class User { public User(UUID id, String lastUsername) { this.id = id; this.lastUsername = lastUsername; + this.lastUsernameLower = lastUsername.toLowerCase(); this.aliases = new HashMap<>(); this.lastSeenAt = Instant.now(); this.firstSeenAt = Instant.now(); @@ -215,6 +221,7 @@ public final class User { public void updateUsername(String newUsername) { this.aliases.put(newUsername, Instant.now()); this.lastUsername = newUsername; + this.lastUsernameLower = newUsername.toLowerCase(); } public void checkNameCollisions(SingleResultCallback callback) { diff --git a/src/main/java/net/frozenorb/apiv3/route/users/GETUsersIdVerifyPassword.java b/src/main/java/net/frozenorb/apiv3/route/users/GETUsersIdVerifyPassword.java index 31883ed..5669101 100644 --- a/src/main/java/net/frozenorb/apiv3/route/users/GETUsersIdVerifyPassword.java +++ b/src/main/java/net/frozenorb/apiv3/route/users/GETUsersIdVerifyPassword.java @@ -22,7 +22,7 @@ public final class GETUsersIdVerifyPassword implements Handler { User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v)); if (user == null) { - user = SyncUtils.runBlocking(v -> User.findByLastUsername(ctx.request().getParam("userId"), v)); + user = SyncUtils.runBlocking(v -> User.findByLastUsernameLower(ctx.request().getParam("userId"), v)); } if (user == null) {