diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-09-03 03:38:19 +0200 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-09-03 03:38:19 +0200 |
commit | 3eac3a05ec822259a2aaba8dc661a8260d841eb8 (patch) | |
tree | e3805abc82a257a4a87fb53156ae0f5b7269da22 /util/src/entities | |
parent | :bug: fix member + member list (diff) | |
download | server-3eac3a05ec822259a2aaba8dc661a8260d841eb8.tar.xz |
:art: update entities
Diffstat (limited to 'util/src/entities')
-rw-r--r-- | util/src/entities/BaseClass.ts | 22 | ||||
-rw-r--r-- | util/src/entities/Member.ts | 40 | ||||
-rw-r--r-- | util/src/entities/Message.ts | 1 | ||||
-rw-r--r-- | util/src/entities/Recipient.ts | 6 | ||||
-rw-r--r-- | util/src/entities/User.ts | 2 |
5 files changed, 43 insertions, 28 deletions
diff --git a/util/src/entities/BaseClass.ts b/util/src/entities/BaseClass.ts index 403a6fc6..9b2ce058 100644 --- a/util/src/entities/BaseClass.ts +++ b/util/src/entities/BaseClass.ts @@ -6,11 +6,7 @@ import "missing-native-js-functions"; // TODO use class-validator https://typeorm.io/#/validation with class annotators (isPhone/isEmail) combined with types from typescript-json-schema // btw. we don't use class-validator for everything, because we need to explicitly set the type instead of deriving it from typescript also it doesn't easily support nested objects -export class BaseClass extends BaseEntity { - @PrimaryColumn() - id: string; - - // @ts-ignore +export class BaseClassWithoutId extends BaseEntity { constructor(private props?: any) { super(); this.assign(props); @@ -24,8 +20,7 @@ export class BaseClass extends BaseEntity { return this.construct.getRepository().metadata as EntityMetadata; } - assign(props: any) { - if (!props || typeof props !== "object") return; + assign(props: any = {}) { delete props.opts; delete props.props; @@ -48,8 +43,6 @@ export class BaseClass extends BaseEntity { this[key] = props[key]; } } - - if (!this.id) this.id = Snowflake.generate(); } @BeforeUpdate() @@ -77,3 +70,14 @@ export class BaseClass extends BaseEntity { return repository.decrement(conditions, propertyPath, value); } } + +export class BaseClass extends BaseClassWithoutId { + @PrimaryColumn() + id: string; + + assign(props: any = {}) { + super.assign(props); + if (!this.id) this.id = Snowflake.generate(); + return this; + } +} diff --git a/util/src/entities/Member.ts b/util/src/entities/Member.ts index 502f4813..42215c45 100644 --- a/util/src/entities/Member.ts +++ b/util/src/entities/Member.ts @@ -12,15 +12,16 @@ import { } from "../interfaces"; import { HTTPError } from "lambert-server"; import { Role } from "./Role"; +import { BaseClassWithoutId } from "./BaseClass"; @Entity("members") -@Index(["user_id", "guild_id"], { unique: true }) -export class Member extends BaseClass { - @Column() +@Index(["id", "guild_id"], { unique: true }) +export class Member extends BaseClassWithoutId { + @Column({ primary: true, unique: false }) @RelationId((member: Member) => member.user) - user_id: string; + id: string; - @JoinColumn({ name: "user_id" }) + @JoinColumn({ name: "id" }) @ManyToOne(() => User) user: User; @@ -35,7 +36,14 @@ export class Member extends BaseClass { @Column({ nullable: true }) nick?: string; - @JoinTable({ name: "member_roles" }) + @JoinTable({ + name: "member_roles", + joinColumn: { name: "id", referencedColumnName: "id" }, + inverseJoinColumn: { + name: "role_id", + referencedColumnName: "id", + }, + }) @ManyToMany(() => Role) roles: Role[]; @@ -62,19 +70,19 @@ export class Member extends BaseClass { // read_state: ReadState; static async IsInGuildOrFail(user_id: string, guild_id: string) { - if (await Member.count({ user_id: user_id, guild: { 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); } 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: { user_id, guild_id }, relations: ["user"] }); + const member = await Member.findOneOrFail({ where: { id: user_id, guild_id }, relations: ["user"] }); // use promise all to execute all promises at the same time -> save time return Promise.all([ Member.delete({ - user_id, + id: user_id, guild_id, }), Guild.decrement({ id: guild_id }, "member_count", -1), @@ -98,7 +106,7 @@ export class Member extends BaseClass { const [member] = await Promise.all([ // @ts-ignore Member.findOneOrFail({ - where: { user_id: user_id, guild_id }, + where: { id: user_id, guild_id }, relations: ["user", "roles"], // we don't want to load the role objects just the ids select: ["roles.id"], }), @@ -124,7 +132,7 @@ export class Member extends BaseClass { const [member] = await Promise.all([ // @ts-ignore Member.findOneOrFail({ - where: { user_id, guild_id }, + where: { id: user_id, guild_id }, relations: ["user", "roles"], // we don't want to load the role objects just the ids select: ["roles.id"], }), @@ -149,7 +157,7 @@ export class Member extends BaseClass { static async changeNickname(user_id: string, guild_id: string, nickname: string) { const member = await Member.findOneOrFail({ where: { - user_id: user_id, + id: user_id, guild_id, }, relations: ["user"], @@ -175,7 +183,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({ user_id: user_id }); + const guild_count = await Member.count({ id: user_id }); if (guild_count >= maxGuilds) { throw new HTTPError(`You are at the ${maxGuilds} server limit.`, 403); } @@ -187,11 +195,11 @@ export class Member extends BaseClass { relations: ["channels", "emojis", "members", "roles", "stickers"], }); - if (await Member.count({ user_id: user.id, guild: { 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 = { - user_id, + id: user_id, guild_id, nick: undefined, roles: [guild_id], // @everyone role @@ -207,7 +215,7 @@ export class Member extends BaseClass { await Promise.all([ new Member({ ...member, - roles: undefined, + roles: [new Role({ id: guild_id })], // read_state: {}, settings: { channel_overrides: [], diff --git a/util/src/entities/Message.ts b/util/src/entities/Message.ts index aacca526..623c9168 100644 --- a/util/src/entities/Message.ts +++ b/util/src/entities/Message.ts @@ -103,7 +103,6 @@ export class Message extends BaseClass { timestamp: Date; @Column() - @UpdateDateColumn() edited_timestamp?: Date; @Column({ nullable: true }) diff --git a/util/src/entities/Recipient.ts b/util/src/entities/Recipient.ts index 75d5b94d..2a27b29f 100644 --- a/util/src/entities/Recipient.ts +++ b/util/src/entities/Recipient.ts @@ -11,7 +11,11 @@ export class Recipient extends BaseClass { @ManyToOne(() => require("./Channel").Channel) channel: import("./Channel").Channel; - @JoinColumn({ name: "id" }) + @Column() + @RelationId((recipient: Recipient) => recipient.user) + user_id: string; + + @JoinColumn({ name: "user_id" }) @ManyToOne(() => require("./User").User) user: import("./User").User; diff --git a/util/src/entities/User.ts b/util/src/entities/User.ts index 1b4ce351..63ef41a2 100644 --- a/util/src/entities/User.ts +++ b/util/src/entities/User.ts @@ -37,7 +37,7 @@ export const PublicUserProjection = Object.values(PublicUserEnum).filter( ) as PublicUserKeys[]; export const PrivateUserProjection = [ ...PublicUserProjection, - Object.values(PrivateUserEnum).filter((x) => typeof x === "string"), + ...Object.values(PrivateUserEnum).filter((x) => typeof x === "string"), ] as PrivateUserKeys[]; // Private user data that should never get sent to the client |