summary refs log tree commit diff
path: root/api/src
diff options
context:
space:
mode:
Diffstat (limited to 'api/src')
-rw-r--r--api/src/routes/channels/#channel_id/messages/index.ts12
-rw-r--r--api/src/routes/users/#id/profile.ts17
-rw-r--r--api/src/routes/users/#id/relationships.ts41
-rw-r--r--api/src/routes/users/@me/index.ts7
4 files changed, 74 insertions, 3 deletions
diff --git a/api/src/routes/channels/#channel_id/messages/index.ts b/api/src/routes/channels/#channel_id/messages/index.ts
index 1ae9d676..2fd08b04 100644
--- a/api/src/routes/channels/#channel_id/messages/index.ts
+++ b/api/src/routes/channels/#channel_id/messages/index.ts
@@ -126,6 +126,13 @@ router.get("/", async (req: Request, res: Response) => {
 				y.proxy_url = `${endpoint == null ? "" : endpoint}${new URL(uri).pathname}`;
 			});
 
+			//Some clients ( discord.js ) only check if a property exists within the response,
+			//which causes erorrs when, say, the `application` property is `null`.
+			for (var curr in x) {
+				if (x[curr] === null)
+					delete x[curr];
+			}
+
 			return x;
 		})
 	);
@@ -212,7 +219,10 @@ router.post(
 				})
 			);
 		}
-
+	
+		//Fix for the client bug
+		delete message.member
+		
 		await Promise.all([
 			message.save(),
 			emitEvent({ event: "MESSAGE_CREATE", channel_id: channel_id, data: message } as MessageCreateEvent),
diff --git a/api/src/routes/users/#id/profile.ts b/api/src/routes/users/#id/profile.ts
index 15457547..9481451d 100644
--- a/api/src/routes/users/#id/profile.ts
+++ b/api/src/routes/users/#id/profile.ts
@@ -1,5 +1,5 @@
 import { Router, Request, Response } from "express";
-import { PublicConnectedAccount, PublicUser, User, UserPublic } from "@fosscord/util";
+import { PublicConnectedAccount, PublicUser, User, UserPublic, Member } from "@fosscord/util";
 import { route } from "@fosscord/api";
 
 const router: Router = Router();
@@ -15,11 +15,24 @@ router.get("/", route({ test: { response: { body: "UserProfileResponse" } } }),
 	if (req.params.id === "@me") req.params.id = req.user_id;
 	const user = await User.getPublicUser(req.params.id, { relations: ["connected_accounts"] });
 
+	var mutual_guilds: object[] = [];
+
+	const requested_member = await Member.find( { id: req.params.id,  })
+	const self_member = await Member.find( { id: req.user_id,  })
+
+	for(const rmem of requested_member) {
+		for(const smem of self_member) {
+			if (smem.guild_id === rmem.guild_id) {
+				mutual_guilds.push({id: rmem.guild_id, nick: rmem.nick})
+			}
+		}
+	}
+
 	res.json({
 		connected_accounts: user.connected_accounts,
 		premium_guild_since: null, // TODO
 		premium_since: null, // TODO
-		mutual_guilds: [], // TODO {id: "", nick: null} when ?with_mutual_guilds=true
+		mutual_guilds: mutual_guilds, // TODO {id: "", nick: null} when ?with_mutual_guilds=true
 		user: {
 			username: user.username,
 			discriminator: user.discriminator,
diff --git a/api/src/routes/users/#id/relationships.ts b/api/src/routes/users/#id/relationships.ts
new file mode 100644
index 00000000..de7cb9d3
--- /dev/null
+++ b/api/src/routes/users/#id/relationships.ts
@@ -0,0 +1,41 @@
+import { Router, Request, Response } from "express";
+import { User } from "@fosscord/util";
+import { route } from "@fosscord/api";
+
+const router: Router = Router();
+
+export interface UserRelationsResponse {
+	object: {
+		id?: string,
+		username?: string,
+		avatar?: string, 
+		discriminator?: string, 
+		public_flags?: number
+	}
+}
+
+
+router.get("/", route({ test: { response: { body: "UserRelationsResponse" } } }), async (req: Request, res: Response) => {
+	var mutual_relations: object[] = [];
+    const requested_relations = await User.findOneOrFail({
+		where: { id: req.params.id },
+		relations: ["relationships"]
+	});
+    const self_relations = await User.findOneOrFail({
+		where: { id: req.user_id },
+		relations: ["relationships"]
+	});
+	
+    for(const rmem of requested_relations.relationships) {
+		for(const smem of self_relations.relationships)
+		if (rmem.to_id === smem.to_id && rmem.type === 1 && rmem.to_id !== req.user_id) {
+			var relation_user = await User.getPublicUser(rmem.to_id)
+
+			mutual_relations.push({id: relation_user.id, username: relation_user.username, avatar: relation_user.avatar, discriminator: relation_user.discriminator, public_flags: relation_user.public_flags})
+		}
+	}
+
+	res.json(mutual_relations)
+});
+
+export default router;
diff --git a/api/src/routes/users/@me/index.ts b/api/src/routes/users/@me/index.ts
index 1959704a..acca8910 100644
--- a/api/src/routes/users/@me/index.ts
+++ b/api/src/routes/users/@me/index.ts
@@ -57,6 +57,13 @@ router.patch("/", route({ body: "UserModifySchema" }), async (req: Request, res:
 		user.data.hash = await bcrypt.hash(body.new_password, 12);
 	}
 
+	var check_username = body?.username?.replace(/\s/g, '');
+	if(!check_username) {
+		throw FieldErrors({
+			username: { code: "BASE_TYPE_REQUIRED", message: req.t("common:field.BASE_TYPE_REQUIRED") }
+		});
+	}
+
 	await user.save();
 
 	// @ts-ignore