Skip to content

Commit

Permalink
implement authed pomelo routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Puyodead1 committed Dec 23, 2023
1 parent cf34ab1 commit 83fa03e
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/api/routes/users/@me/pomelo-attempt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { route } from "@spacebar/api";
import { Config, UniqueUsernameAttemptSchema, User } from "@spacebar/util";
import { Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";
const router = Router();

// https://discord-userdoccers.vercel.app/resources/user#get-pomelo-eligibility
router.post(
"/",
route({
requestBody: "UniqueUsernameAttemptSchema",
responses: {
200: { body: "UniqueUsernameAttemptResponse" },
400: { body: "APIErrorResponse" },
},
description:
"Checks whether a unique username is available for the user to claim.",
}),
async (req: Request, res: Response) => {
const body = req.body as UniqueUsernameAttemptSchema;
const { uniqueUsernames } = Config.get().general;
if (!uniqueUsernames) {
throw new HTTPError(
"Unique Usernames feature is not enabled on this instance.",
400,
);
}

res.json({
taken: !(await User.isUsernameAvailable(body.username)),
});
},
);

export default router;
37 changes: 37 additions & 0 deletions src/api/routes/users/@me/pomelo-suggestions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { route } from "@spacebar/api";
import { Config, User } from "@spacebar/util";
import { Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";
const router = Router();

// https://discord-userdoccers.vercel.app/resources/user#get-pomelo-suggestions
router.get(
"/",
route({
description:
"Returns a suggested unique username string based on the current user's username.",
responses: {
400: { body: "APIErrorResponse" },
},
}),
async (req: Request, res: Response) => {
const { uniqueUsernames } = Config.get().general;
if (!uniqueUsernames) {
throw new HTTPError(
"Unique Usernames feature is not enabled on this instance.",
400,
);
}

const user = await User.findOneOrFail({
where: {
id: req.user_id,
},
});

// TODO: return a suggestion based on the users current username
return res.json({ username: user.username.toString() });
},
);

export default router;
61 changes: 61 additions & 0 deletions src/api/routes/users/@me/pomelo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { route } from "@spacebar/api";
import {
Config,
FieldErrors,
UniqueUsernameAttemptSchema,
User,
} from "@spacebar/util";
import { Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";
const router = Router();

// https://discord-userdoccers.vercel.app/resources/user#create-pomelo-migration
router.post(
"/",
route({
description:
"Claims a unique username for the user. Returns the updated user object on success. Fires a User Update Gateway event.",
requestBody: "UniqueUsernameAttemptSchema",
responses: {
200: { body: "PrivateUserResponse" },
400: { body: "APIErrorResponse" },
},
}),
async (req: Request, res: Response) => {
const body = req.body as UniqueUsernameAttemptSchema;
const { uniqueUsernames } = Config.get().general;
if (!uniqueUsernames) {
throw new HTTPError(
"Unique Usernames feature is not enabled on this instance.",
400,
);
}

const isAvailable = await User.isUsernameAvailable(body.username);

if (!isAvailable) {
throw FieldErrors({
username: {
code: "USERNAME_TOO_MANY_USERS",
message:
req?.t("auth:register.USERNAME_TOO_MANY_USERS") || "",
},
});
}

const user = await User.findOneOrFail({
where: {
id: req.user_id,
},
});

user.legacy_username = user.username;
user.username = body.username;
user.discriminator = "0";
const newUser = await user.save();

res.json(newUser.toPrivateUser());
},
);

export default router;

0 comments on commit 83fa03e

Please sign in to comment.