diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-08-12 20:09:35 +0200 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-08-12 20:09:35 +0200 |
commit | 08e837bf5559e9680fc8cb99bd05b93f8eb2cac5 (patch) | |
tree | 1eadc038773b025275d7b751265f741b09ca92ab /api/src/routes/users/@me/index.ts | |
parent | npm i @fosscord/server-util@1.3.52 (diff) | |
download | server-08e837bf5559e9680fc8cb99bd05b93f8eb2cac5.tar.xz |
:sparkles: api
Diffstat (limited to 'api/src/routes/users/@me/index.ts')
-rw-r--r-- | api/src/routes/users/@me/index.ts | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/api/src/routes/users/@me/index.ts b/api/src/routes/users/@me/index.ts new file mode 100644 index 00000000..7bd4a486 --- /dev/null +++ b/api/src/routes/users/@me/index.ts @@ -0,0 +1,48 @@ +import { Router, Request, Response } from "express"; +import { UserModel, toObject, PublicUserProjection } from "@fosscord/server-util"; +import { getPublicUser } from "../../../util/User"; +import { UserModifySchema } from "../../../schema/User"; +import { check } from "../../../util/instanceOf"; +import { handleFile } from "../../../util/cdn"; + +const router: Router = Router(); + +router.get("/", async (req: Request, res: Response) => { + res.json(await getPublicUser(req.user_id)); +}); + +const UserUpdateProjection = { + accent_color: true, + avatar: true, + banner: true, + bio: true, + bot: true, + discriminator: true, + email: true, + flags: true, + id: true, + locale: true, + mfa_enabled: true, + nsfw_alllowed: true, + phone: true, + public_flags: true, + purchased_flags: true, + // token: true, // this isn't saved in the db and needs to be set manually + username: true, + verified: true +}; + +router.patch("/", check(UserModifySchema), async (req: Request, res: Response) => { + const body = req.body as UserModifySchema; + + if (body.avatar) body.avatar = await handleFile(`/avatars/${req.user_id}`, body.avatar as string); + if (body.banner) body.banner = await handleFile(`/banners/${req.user_id}`, body.banner as string); + + const user = await UserModel.findOneAndUpdate({ id: req.user_id }, body, { projection: UserUpdateProjection }).exec(); + // TODO: dispatch user update event + + res.json(toObject(user)); +}); + +export default router; +// {"message": "Invalid two-factor code", "code": 60008} |