✨ user avatar
This commit is contained in:
		
							parent
							
								
									d61bbe8293
								
							
						
					
					
						commit
						3254c31e02
					
				| @ -13,12 +13,11 @@ | ||||
| 			window.GLOBAL_ENV = { | ||||
| 				API_ENDPOINT: "/api", | ||||
| 				WEBAPP_ENDPOINT: "", | ||||
| 				CDN_HOST: "cdn.discordapp.com", | ||||
| 				CDN_HOST: "//localhost:3003", | ||||
| 				ASSET_ENDPOINT: "", | ||||
| 				MEDIA_PROXY_ENDPOINT: "https://media.discordapp.net", | ||||
| 				WIDGET_ENDPOINT: "//discord.com/widget", | ||||
| 				INVITE_HOST: "discord.gg", | ||||
| 
 | ||||
| 				GUILD_TEMPLATE_HOST: "discord.new", | ||||
| 				GIFT_CODE_HOST: "discord.gift", | ||||
| 				RELEASE_CHANNEL: "stable", | ||||
|  | ||||
| @ -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(); | ||||
| 	} | ||||
|  | ||||
| @ -181,6 +181,7 @@ router.post( | ||||
| 			premium: false, | ||||
| 			premium_type: 0, | ||||
| 			phone: null, | ||||
| 			bio: "", | ||||
| 			mfa_enabled: false, | ||||
| 			verified: false, | ||||
| 			disabled: false, | ||||
|  | ||||
| @ -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; | ||||
|  | ||||
| @ -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"); | ||||
| 
 | ||||
| 	var newuser = await UserModel.findOneAndUpdate({ id: req.user_id }, { | ||||
| 		...body | ||||
| 	}).exec(); | ||||
| 			// @ts-ignore
 | ||||
| 			const { id } = await uploadFile(`/avatars/${req.user_id}`, { buffer, mimetype, originalname: "avatar" }); | ||||
| 			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; | ||||
|  | ||||
| @ -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")); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Flam3rboy
						Flam3rboy