summary refs log tree commit diff
path: root/util/src/entities/Member.ts
diff options
context:
space:
mode:
Diffstat (limited to 'util/src/entities/Member.ts')
-rw-r--r--util/src/entities/Member.ts86
1 files changed, 46 insertions, 40 deletions
diff --git a/util/src/entities/Member.ts b/util/src/entities/Member.ts
index c5d289ef..d2d78bb9 100644
--- a/util/src/entities/Member.ts
+++ b/util/src/entities/Member.ts
@@ -15,27 +15,22 @@ import { Role } from "./Role";
 
 @Entity("members")
 export class Member extends BaseClass {
-	@RelationId((member: Member) => member.user)
-	user_id: string;
-
-	@JoinColumn({ name: "user_id" })
-	@ManyToOne(() => User, (user: User) => user.id)
+	@JoinColumn({ name: "id" })
+	@ManyToOne(() => User)
 	user: User;
 
+	@Column({ nullable: true })
 	@RelationId((member: Member) => member.guild)
 	guild_id: string;
 
 	@JoinColumn({ name: "guild_id" })
-	@ManyToOne(() => Guild, (guild: Guild) => guild.members)
+	@ManyToOne(() => Guild)
 	guild: Guild;
 
 	@Column({ nullable: true })
 	nick?: string;
 
-	@RelationId((member: Member) => member.roles)
-	role_ids: string[];
-
-	@JoinTable()
+	@JoinTable({ name: "member_roles" })
 	@ManyToMany(() => Role)
 	roles: Role[];
 
@@ -62,7 +57,7 @@ export class Member extends BaseClass {
 	read_state: Record<string, string | null>;
 
 	static async IsInGuildOrFail(user_id: string, guild_id: string) {
-		if (await Member.count({ id: user_id, guild_id })) return true;
+		if (await Member.count({ id: user_id, guild: { id: guild_id } })) return true;
 		throw new HTTPError("You are not member of this guild", 403);
 	}
 
@@ -99,46 +94,54 @@ export class Member extends BaseClass {
 
 	static async addRole(user_id: string, guild_id: string, role_id: string) {
 		const [member] = await Promise.all([
+			// @ts-ignore
 			Member.findOneOrFail({
 				where: { id: user_id, guild_id: guild_id },
-				relations: ["user"], // we don't want to load  the role objects just the ids
+				relations: ["user", "roles"], // we don't want to load  the role objects just the ids
+				select: ["roles.id"],
 			}),
 			await Role.findOneOrFail({ id: role_id, guild_id: guild_id }),
 		]);
-		member.role_ids.push(role_id);
-		member.save();
+		member.roles.push(new Role({ id: role_id }));
 
-		await emitEvent({
-			event: "GUILD_MEMBER_UPDATE",
-			data: {
+		await Promise.all([
+			member.save(),
+			emitEvent({
+				event: "GUILD_MEMBER_UPDATE",
+				data: {
+					guild_id: guild_id,
+					user: member.user,
+					roles: member.roles.map((x) => x.id),
+				},
 				guild_id: guild_id,
-				user: member.user,
-				roles: member.role_ids,
-			},
-			guild_id: guild_id,
-		} as GuildMemberUpdateEvent);
+			} as GuildMemberUpdateEvent),
+		]);
 	}
 
 	static async removeRole(user_id: string, guild_id: string, role_id: string) {
 		const [member] = await Promise.all([
+			// @ts-ignore
 			Member.findOneOrFail({
 				where: { id: user_id, guild_id: guild_id },
-				relations: ["user"], // we don't want to load  the role objects just the ids
+				relations: ["user", "roles"], // we don't want to load  the role objects just the ids
+				select: ["roles.id"],
 			}),
 			await Role.findOneOrFail({ id: role_id, guild_id: guild_id }),
 		]);
-		member.role_ids.remove(role_id);
-		member.save();
+		member.roles = member.roles.filter((x) => x.id == role_id);
 
-		await emitEvent({
-			event: "GUILD_MEMBER_UPDATE",
-			data: {
+		await Promise.all([
+			member.save(),
+			emitEvent({
+				event: "GUILD_MEMBER_UPDATE",
+				data: {
+					guild_id: guild_id,
+					user: member.user,
+					roles: member.roles.map((x) => x.id),
+				},
 				guild_id: guild_id,
-				user: member.user,
-				roles: member.role_ids,
-			},
-			guild_id: guild_id,
-		} as GuildMemberUpdateEvent);
+			} as GuildMemberUpdateEvent),
+		]);
 	}
 
 	static async changeNickname(user_id: string, guild_id: string, nickname: string) {
@@ -175,11 +178,14 @@ export class Member extends BaseClass {
 			throw new HTTPError(`You are at the ${maxGuilds} server limit.`, 403);
 		}
 
-		const guild = await Guild.findOneOrFail(guild_id, {
+		const guild = await Guild.findOneOrFail({
+			where: {
+				id: guild_id,
+			},
 			relations: ["channels", "emojis", "members", "roles", "stickers"],
 		});
 
-		if (await Member.count({ id: user.id, guild_id }))
+		if (await Member.count({ id: user.id, guild: { id: guild_id } }))
 			throw new HTTPError("You are already a member of this guild", 400);
 
 		const member = {
@@ -194,23 +200,23 @@ export class Member extends BaseClass {
 			pending: false,
 		};
 		// @ts-ignore
-		guild.joined_at = member.joined_at;
+		guild.joined_at = member.joined_at.toISOString();
 
 		await Promise.all([
-			new Member({
+			Member.insert({
 				...member,
+				roles: undefined,
 				read_state: {},
 				settings: {
 					channel_overrides: [],
 					message_notifications: 0,
 					mobile_push: true,
-					mute_config: null,
 					muted: false,
 					suppress_everyone: false,
 					suppress_roles: false,
 					version: 0,
 				},
-			}).save(),
+			}),
 			Guild.increment({ id: guild_id }, "member_count", 1),
 			emitEvent({
 				event: "GUILD_MEMBER_ADD",
@@ -223,7 +229,7 @@ export class Member extends BaseClass {
 			} as GuildMemberAddEvent),
 			emitEvent({
 				event: "GUILD_CREATE",
-				data: guild,
+				data: { ...guild, members: [...guild.members, member] },
 				user_id,
 			} as GuildCreateEvent),
 		]);