summary refs log tree commit diff
path: root/api/src/routes/users/@me/index.ts
blob: 6ebc6634fa223b7693d6a1e0f35311a688d3171c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { Router, Request, Response } from "express";
import { UserModel, toObject, PublicUserProjection } from "@fosscord/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, new: true }).exec();
	// TODO: dispatch user update event

	res.json(toObject(user));
});

export default router;
// {"message": "Invalid two-factor code", "code": 60008}