diff options
Diffstat (limited to 'util/src/entities')
-rw-r--r-- | util/src/entities/Member.ts | 63 |
1 files changed, 32 insertions, 31 deletions
diff --git a/util/src/entities/Member.ts b/util/src/entities/Member.ts index e1773730..e0dd411a 100644 --- a/util/src/entities/Member.ts +++ b/util/src/entities/Member.ts @@ -1,6 +1,6 @@ import { PublicUser, User } from "./User"; import { BaseClass } from "./BaseClass"; -import { Column, Entity, JoinColumn, JoinTable, ManyToMany, ManyToOne, OneToMany, RelationId } from "typeorm"; +import { Column, Entity, Index, JoinColumn, JoinTable, ManyToMany, ManyToOne, OneToMany, RelationId } from "typeorm"; import { Guild } from "./Guild"; import { Config, emitEvent } from "../util"; import { @@ -12,15 +12,19 @@ import { } from "../interfaces"; import { HTTPError } from "lambert-server"; import { Role } from "./Role"; -import { ReadState } from "./ReadState"; @Entity("members") +@Index(["user_id", "guild_id"], { unique: true }) export class Member extends BaseClass { - @JoinColumn({ name: "id" }) + @Column() + @RelationId((member: Member) => member.user) + user_id: string; + + @JoinColumn({ name: "user_id" }) @ManyToOne(() => User) user: User; - @Column({ nullable: true }) + @Column() @RelationId((member: Member) => member.guild) guild_id: string; @@ -58,20 +62,20 @@ export class Member extends BaseClass { // read_state: ReadState; static async IsInGuildOrFail(user_id: string, guild_id: string) { - if (await Member.count({ id: user_id, guild: { id: guild_id } })) return true; + if (await Member.count({ user_id: user_id, guild: { id: guild_id } })) return true; throw new HTTPError("You are not member of this guild", 403); } static async removeFromGuild(user_id: string, guild_id: string) { const guild = await Guild.findOneOrFail({ select: ["owner_id"], where: { id: guild_id } }); if (guild.owner_id === user_id) throw new Error("The owner cannot be removed of the guild"); - const member = await Member.findOneOrFail({ where: { id: user_id, guild_id }, relations: ["user"] }); + const member = await Member.findOneOrFail({ where: { user_id, guild_id }, relations: ["user"] }); // use promise all to execute all promises at the same time -> save time return Promise.all([ Member.delete({ - id: user_id, - guild_id: guild_id, + user_id, + guild_id, }), Guild.decrement({ id: guild_id }, "member_count", -1), @@ -84,11 +88,8 @@ export class Member extends BaseClass { } as GuildDeleteEvent), emitEvent({ event: "GUILD_MEMBER_REMOVE", - data: { - guild_id: guild_id, - user: member.user, - }, - guild_id: guild_id, + data: { guild_id, user: member.user }, + guild_id, } as GuildMemberRemoveEvent), ]); } @@ -97,11 +98,11 @@ export class Member extends BaseClass { const [member] = await Promise.all([ // @ts-ignore Member.findOneOrFail({ - where: { id: user_id, guild_id: guild_id }, + where: { user_id: user_id, guild_id }, 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 }), + await Role.findOneOrFail({ id: role_id, guild_id }), ]); member.roles.push(new Role({ id: role_id })); @@ -110,11 +111,11 @@ export class Member extends BaseClass { emitEvent({ event: "GUILD_MEMBER_UPDATE", data: { - guild_id: guild_id, + guild_id, user: member.user, roles: member.roles.map((x) => x.id), }, - guild_id: guild_id, + guild_id, } as GuildMemberUpdateEvent), ]); } @@ -123,11 +124,11 @@ export class Member extends BaseClass { const [member] = await Promise.all([ // @ts-ignore Member.findOneOrFail({ - where: { id: user_id, guild_id: guild_id }, + where: { user_id, guild_id }, 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 }), + await Role.findOneOrFail({ id: role_id, guild_id }), ]); member.roles = member.roles.filter((x) => x.id == role_id); @@ -136,11 +137,11 @@ export class Member extends BaseClass { emitEvent({ event: "GUILD_MEMBER_UPDATE", data: { - guild_id: guild_id, + guild_id, user: member.user, roles: member.roles.map((x) => x.id), }, - guild_id: guild_id, + guild_id, } as GuildMemberUpdateEvent), ]); } @@ -148,8 +149,8 @@ export class Member extends BaseClass { static async changeNickname(user_id: string, guild_id: string, nickname: string) { const member = await Member.findOneOrFail({ where: { - id: user_id, - guild_id: guild_id, + user_id: user_id, + guild_id, }, relations: ["user"], }); @@ -161,11 +162,11 @@ export class Member extends BaseClass { emitEvent({ event: "GUILD_MEMBER_UPDATE", data: { - guild_id: guild_id, + guild_id, user: member.user, nick: nickname, }, - guild_id: guild_id, + guild_id, } as GuildMemberUpdateEvent), ]); } @@ -174,7 +175,7 @@ export class Member extends BaseClass { const user = await User.getPublicUser(user_id); const { maxGuilds } = Config.get().limits.user; - const guild_count = await Member.count({ id: user_id }); + const guild_count = await Member.count({ user_id: user_id }); if (guild_count >= maxGuilds) { throw new HTTPError(`You are at the ${maxGuilds} server limit.`, 403); } @@ -186,12 +187,12 @@ export class Member extends BaseClass { relations: ["channels", "emojis", "members", "roles", "stickers"], }); - if (await Member.count({ id: user.id, guild: { id: guild_id } })) + if (await Member.count({ user_id: user.id, guild: { id: guild_id } })) throw new HTTPError("You are already a member of this guild", 400); const member = { - id: user_id, - guild_id: guild_id, + user_id, + guild_id, nick: undefined, roles: [guild_id], // @everyone role joined_at: new Date(), @@ -224,9 +225,9 @@ export class Member extends BaseClass { data: { ...member, user, - guild_id: guild_id, + guild_id, }, - guild_id: guild_id, + guild_id, } as GuildMemberAddEvent), emitEvent({ event: "GUILD_CREATE", |