mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-22 10:22:39 +01:00
implement authed pomelo routes
This commit is contained in:
parent
cf34ab1674
commit
83fa03e392
35
src/api/routes/users/@me/pomelo-attempt.ts
Normal file
35
src/api/routes/users/@me/pomelo-attempt.ts
Normal file
@ -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
src/api/routes/users/@me/pomelo-suggestions.ts
Normal file
37
src/api/routes/users/@me/pomelo-suggestions.ts
Normal file
@ -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
src/api/routes/users/@me/pomelo.ts
Normal file
61
src/api/routes/users/@me/pomelo.ts
Normal file
@ -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;
|
Loading…
Reference in New Issue
Block a user