1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-09-22 02:31:36 +02:00

user avatar

This commit is contained in:
Flam3rboy 2021-05-30 01:44:15 +02:00
parent d61bbe8293
commit 3254c31e02
6 changed files with 28 additions and 22 deletions

View File

@ -13,12 +13,11 @@
window.GLOBAL_ENV = { window.GLOBAL_ENV = {
API_ENDPOINT: "/api", API_ENDPOINT: "/api",
WEBAPP_ENDPOINT: "", WEBAPP_ENDPOINT: "",
CDN_HOST: "cdn.discordapp.com", CDN_HOST: "//localhost:3003",
ASSET_ENDPOINT: "", ASSET_ENDPOINT: "",
MEDIA_PROXY_ENDPOINT: "https://media.discordapp.net", MEDIA_PROXY_ENDPOINT: "https://media.discordapp.net",
WIDGET_ENDPOINT: "//discord.com/widget", WIDGET_ENDPOINT: "//discord.com/widget",
INVITE_HOST: "discord.gg", INVITE_HOST: "discord.gg",
GUILD_TEMPLATE_HOST: "discord.new", GUILD_TEMPLATE_HOST: "discord.new",
GIFT_CODE_HOST: "discord.gift", GIFT_CODE_HOST: "discord.gift",
RELEASE_CHANNEL: "stable", RELEASE_CHANNEL: "stable",

View File

@ -97,7 +97,7 @@ export class FosscordServer extends Server {
app.use("/api/v8", prefix); app.use("/api/v8", prefix);
this.app = app; this.app = app;
this.app.use(ErrorHandler); 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"))); this.app.use("/assets", express.static(path.join(__dirname, "..", "assets")));
@ -143,7 +143,12 @@ export class FosscordServer extends Server {
this.app.get("*", (req, res) => { this.app.get("*", (req, res) => {
res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24); res.set("Cache-Control", "public, max-age=" + 60 * 60 * 24);
res.set("content-type", "text/html"); 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(); return super.start();
} }

View File

@ -181,6 +181,7 @@ router.post(
premium: false, premium: false,
premium_type: 0, premium_type: 0,
phone: null, phone: null,
bio: "",
mfa_enabled: false, mfa_enabled: false,
verified: false, verified: false,
disabled: false, disabled: false,

View File

@ -6,10 +6,8 @@ const router: Router = Router();
router.get("/", async (req: Request, res: Response) => { router.get("/", async (req: Request, res: Response) => {
const { id } = req.params; 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; export default router;

View File

@ -2,31 +2,35 @@ import { Router, Request, Response } from "express";
import { UserModel, toObject } from "@fosscord/server-util"; import { UserModel, toObject } from "@fosscord/server-util";
import { HTTPError } from "lambert-server"; import { HTTPError } from "lambert-server";
import { getPublicUser } from "../../../util/User"; import { getPublicUser } from "../../../util/User";
import { UserModifySchema } from "../../../schema/User" import { UserModifySchema } from "../../../schema/User";
import { check } from "../../../util/instanceOf"; import { check } from "../../../util/instanceOf";
import { uploadFile } from "../../../util/cdn";
const router: Router = Router(); const router: Router = Router();
router.get("/", async (req: Request, res: Response) => { router.get("/", async (req: Request, res: Response) => {
const user = await UserModel.findOne({ id: req.user_id }).exec(); res.json(await getPublicUser(req.user_id));
if (!user) throw new HTTPError("User not found", 404);
var publicUser = await getPublicUser(user.id);
res.json(publicUser);
}); });
router.patch("/", check(UserModifySchema), async (req: Request, res: Response) => { router.patch("/", check(UserModifySchema), async (req: Request, res: Response) => {
const body = req.body as UserModifySchema; const body = req.body as UserModifySchema;
const user = await UserModel.findOne({ id: req.user_id }).exec(); if (body.avatar) {
if (!user) throw new HTTPError("User not found", 404); try {
const mimetype = body.avatar.split(":")[1].split(";")[0];
const buffer = Buffer.from(body.avatar.split(",")[1], "base64");
var newuser = await UserModel.findOneAndUpdate({ id: req.user_id }, { // @ts-ignore
...body const { id } = await uploadFile(`/avatars/${req.user_id}`, { buffer, mimetype, originalname: "avatar" });
}).exec(); body.avatar = id;
} catch (error) {
throw new HTTPError("Invalid avatar");
}
}
res.json(newuser); const user = await UserModel.findOneAndUpdate({ id: req.user_id }, body).exec();
res.json(toObject(user));
}); });
export default router; export default router;

View File

@ -74,10 +74,9 @@ export function instanceOf(
): Boolean { ): Boolean {
if (!ref) ref = { obj: null, key: "" }; if (!ref) ref = { obj: null, key: "" };
if (!path) path = "body"; if (!path) path = "body";
if (!type) return true; // no type was specified
try { try {
if (!type) return true; // no type was specified
if (value == null) { if (value == null) {
if (optional) return true; if (optional) return true;
throw new FieldError("BASE_TYPE_REQUIRED", req.t("common:field.BASE_TYPE_REQUIRED")); throw new FieldError("BASE_TYPE_REQUIRED", req.t("common:field.BASE_TYPE_REQUIRED"));