Skip to content

Commit d075b86

Browse files
committed
Added way to get all whitelisted players
1 parent d94679a commit d075b86

6 files changed

Lines changed: 211 additions & 4 deletions

File tree

logs\.269f6f2c978942fcfa2d6d23072f04a23690649c-audit.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"auditLog": "logs\\.269f6f2c978942fcfa2d6d23072f04a23690649c-audit.json",
77
"files": [
88
{
9-
"date": 1764778586440,
10-
"name": "logs/combined-2025-12-03.log",
11-
"hash": "abe52c937c8d8a0f2fc2a72d234cc26411185fb28b8e9f5795e0601f1eaeb0c6"
9+
"date": 1765031080915,
10+
"name": "logs/combined-2025-12-06.log",
11+
"hash": "5842b0342d1099e5a0f3d262d11dc7357393432464285ee995ec567005c23d99"
1212
}
1313
],
1414
"hashType": "sha256"

package-lock.json

Lines changed: 147 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"dotenv": "^17.2.3",
3535
"express": "^5.2.0",
3636
"express-rate-limit": "^8.2.1",
37+
"minecraft-api-wrapper": "^1.7.1",
3738
"mongoose": "^9.0.0",
3839
"sanitize-filename": "^1.6.3",
3940
"winston": "^3.18.3",

src/models/userSchema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ export const fetchAllUsers = async (filter: object, maxUser = -1) => {
157157
}
158158
};
159159

160+
export const fetchAllUsersWithoutSanitized = async (filter: object) => {
161+
return await userModel.find(filter);
162+
};
163+
160164
/**
161165
* Updates a user in the database.
162166
* Uses dot notation to ensure nested fields are updated without overwriting siblings.

src/routes/user.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { Request, Response, Router, json } from "express";
2-
import { fetchAllUsers, fetchUser, updateUser } from "../models/userSchema";
2+
import {
3+
fetchAllUsers,
4+
fetchAllUsersWithoutSanitized,
5+
fetchUser,
6+
updateUser,
7+
} from "../models/userSchema";
8+
import { formatUsersToWhitelist } from "../utils/formatUsersToWhitelist";
39

410
const userRouter = Router();
511
userRouter.use(json());
@@ -44,4 +50,15 @@ userRouter.post("/:userid", async (req: Request, res: Response) => {
4450
res.json(userData.toJSON());
4551
});
4652

53+
userRouter.get("/minecraft/data", async (req: Request, res: Response) => {
54+
const users = await fetchAllUsersWithoutSanitized({
55+
// eslint-disable-next-line @typescript-eslint/naming-convention
56+
"minecraftData.username": { $ne: null },
57+
});
58+
59+
const minecraftUsers = await formatUsersToWhitelist(users);
60+
61+
res.json(minecraftUsers);
62+
});
63+
4764
export { userRouter };
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { getProfileFromUsername, uuidToFullUuid } from "minecraft-api-wrapper";
2+
import { UserDocument } from "../models/userSchema";
3+
4+
export async function formatUsersToWhitelist(users: UserDocument[]) {
5+
const minecraftUsers = await Promise.all(
6+
users.map(async (user) => {
7+
if (user.minecraftData.uuid == null) {
8+
const profile = await getProfileFromUsername(
9+
user.minecraftData.username!,
10+
);
11+
if (profile) {
12+
user.minecraftData.uuid = profile.getFullUUID();
13+
await user.save();
14+
}
15+
}
16+
return user;
17+
}),
18+
);
19+
20+
for (const user of users) {
21+
if (user.minecraftData.uuid == null) {
22+
user.minecraftData.username = null;
23+
user.minecraftData.uuid = null;
24+
minecraftUsers.splice(minecraftUsers.indexOf(user), 1);
25+
await user.save();
26+
continue;
27+
}
28+
29+
if (!user.minecraftData.uuid?.includes("-")) {
30+
user.minecraftData.uuid = uuidToFullUuid(user.minecraftData.uuid!);
31+
}
32+
}
33+
34+
return minecraftUsers.map((user) => ({
35+
name: user.minecraftData.username!,
36+
uuid: user.minecraftData.uuid!,
37+
}));
38+
}

0 commit comments

Comments
 (0)