summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-30 01:44:15 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-30 01:44:15 +0200
commit3254c31e028ca32a8b00753dabde3f984f8a1524 (patch)
tree1cce08ad3d7c168a0af2354f021687fe6ee2795b /src
parent:sparkles: message attachments (diff)
downloadserver-3254c31e028ca32a8b00753dabde3f984f8a1524.tar.xz
:sparkles: user avatar
Diffstat (limited to 'src')
-rw-r--r--src/Server.ts9
-rw-r--r--src/routes/auth/register.ts1
-rw-r--r--src/routes/users/#id/index.ts4
-rw-r--r--src/routes/users/@me/index.ts30
-rw-r--r--src/util/instanceOf.ts3
5 files changed, 27 insertions, 20 deletions
diff --git a/src/Server.ts b/src/Server.ts

index 79a8bba3..0ff4df8c 100644 --- a/src/Server.ts +++ b/src/Server.ts
@@ -97,7 +97,7 @@ export class FosscordServer extends Server { app.use("/api/v8", prefix); this.app = app; this.app.use(ErrorHandler); - const indexHTML = await fs.readFile(path.join(__dirname, "..", "client_test", "index.html")); + const indexHTML = await fs.readFile(path.join(__dirname, "..", "client_test", "index.html"), { encoding: "utf8" }); this.app.use("/assets", express.static(path.join(__dirname, "..", "assets"))); @@ -143,7 +143,12 @@ export class FosscordServer extends Server { this.app.get("*", (req, res) => { res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24); res.set("content-type", "text/html"); - res.send(indexHTML); + res.send( + indexHTML.replace( + /CDN_HOST: ".+"/, + `CDN_HOST: "${(Config.get().cdn.endpoint || "http://localhost:3003").replace(/https?:/, "")}"` + ) + ); }); return super.start(); } diff --git a/src/routes/auth/register.ts b/src/routes/auth/register.ts
index e24485da..50bac43a 100644 --- a/src/routes/auth/register.ts +++ b/src/routes/auth/register.ts
@@ -181,6 +181,7 @@ router.post( premium: false, premium_type: 0, phone: null, + bio: "", mfa_enabled: false, verified: false, disabled: false, diff --git a/src/routes/users/#id/index.ts b/src/routes/users/#id/index.ts
index 185b2e5f..a2ad3ae6 100644 --- a/src/routes/users/#id/index.ts +++ b/src/routes/users/#id/index.ts
@@ -6,10 +6,8 @@ const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const { id } = req.params; - const user = await getPublicUser(id); - if (!user) throw new HTTPError("User not found", 404); - res.json(user); + res.json(await getPublicUser(id)); }); export default router; diff --git a/src/routes/users/@me/index.ts b/src/routes/users/@me/index.ts
index d139203d..4f17fbee 100644 --- a/src/routes/users/@me/index.ts +++ b/src/routes/users/@me/index.ts
@@ -2,31 +2,35 @@ import { Router, Request, Response } from "express"; import { UserModel, toObject } from "@fosscord/server-util"; import { HTTPError } from "lambert-server"; import { getPublicUser } from "../../../util/User"; -import { UserModifySchema } from "../../../schema/User" +import { UserModifySchema } from "../../../schema/User"; import { check } from "../../../util/instanceOf"; +import { uploadFile } from "../../../util/cdn"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { - const user = await UserModel.findOne({ id: req.user_id }).exec(); - if (!user) throw new HTTPError("User not found", 404); - - var publicUser = await getPublicUser(user.id); - - res.json(publicUser); + res.json(await getPublicUser(req.user_id)); }); router.patch("/", check(UserModifySchema), async (req: Request, res: Response) => { const body = req.body as UserModifySchema; - const user = await UserModel.findOne({ id: req.user_id }).exec(); - if (!user) throw new HTTPError("User not found", 404); + if (body.avatar) { + try { + const mimetype = body.avatar.split(":")[1].split(";")[0]; + const buffer = Buffer.from(body.avatar.split(",")[1], "base64"); + + // @ts-ignore + const { id } = await uploadFile(`/avatars/${req.user_id}`, { buffer, mimetype, originalname: "avatar" }); + body.avatar = id; + } catch (error) { + throw new HTTPError("Invalid avatar"); + } + } - var newuser = await UserModel.findOneAndUpdate({ id: req.user_id }, { - ...body - }).exec(); + const user = await UserModel.findOneAndUpdate({ id: req.user_id }, body).exec(); - res.json(newuser); + res.json(toObject(user)); }); export default router; diff --git a/src/util/instanceOf.ts b/src/util/instanceOf.ts
index b67bde27..93a92805 100644 --- a/src/util/instanceOf.ts +++ b/src/util/instanceOf.ts
@@ -74,10 +74,9 @@ export function instanceOf( ): Boolean { if (!ref) ref = { obj: null, key: "" }; if (!path) path = "body"; + if (!type) return true; // no type was specified try { - if (!type) return true; // no type was specified - if (value == null) { if (optional) return true; throw new FieldError("BASE_TYPE_REQUIRED", req.t("common:field.BASE_TYPE_REQUIRED"));