diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2022-08-07 02:52:09 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2022-08-09 23:28:27 +0200 |
commit | 365abc36dd3b41680527bcf613013bb4999b613d (patch) | |
tree | f46b9c12a0c89de24af1a862db414c44d30e8b3e /util/src/entities | |
parent | Resolve changes undone by rebasing from master (diff) | |
download | server-365abc36dd3b41680527bcf613013bb4999b613d.tar.xz |
Switch to migrations fully
Diffstat (limited to 'util/src/entities')
-rw-r--r-- | util/src/entities/BackupCodes.ts | 16 | ||||
-rw-r--r-- | util/src/entities/BaseClass.ts | 26 | ||||
-rw-r--r-- | util/src/entities/Member.ts | 11 |
3 files changed, 23 insertions, 30 deletions
diff --git a/util/src/entities/BackupCodes.ts b/util/src/entities/BackupCodes.ts index d532a39a..9092c14e 100644 --- a/util/src/entities/BackupCodes.ts +++ b/util/src/entities/BackupCodes.ts @@ -1,7 +1,6 @@ import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; import { BaseClass } from "./BaseClass"; import { User } from "./User"; -import crypto from "crypto"; @Entity("backup_codes") export class BackupCode extends BaseClass { @@ -17,19 +16,4 @@ export class BackupCode extends BaseClass { @Column() expired: boolean; -} - -export function generateMfaBackupCodes(user_id: string) { - let backup_codes: BackupCode[] = []; - for (let i = 0; i < 10; i++) { - const code = BackupCode.create({ - user: { id: user_id }, - code: crypto.randomBytes(4).toString("hex"), // 8 characters - consumed: false, - expired: false, - }); - backup_codes.push(code); - } - - return backup_codes; } \ No newline at end of file diff --git a/util/src/entities/BaseClass.ts b/util/src/entities/BaseClass.ts index 770824ba..e72d09e9 100644 --- a/util/src/entities/BaseClass.ts +++ b/util/src/entities/BaseClass.ts @@ -8,27 +8,30 @@ export class BaseClassWithoutId extends BaseEntity { this.assign(props); } - private get construct(): any { + /*private get construct(): any { return this.constructor; - } + }*/ - private get metadata() { - return this.construct.getRepository().metadata as EntityMetadata; - } + /*private get metadata() { + console.log("getMetadata") + return dataSource.getRepository(this.constructor).metadata as EntityMetadata; + //return this.construct.getRepository().metadata as EntityMetadata; + }*/ assign(props: any = {}) { + //console.log(`assign (${typeof this})...`) delete props.opts; delete props.props; - const properties = new Set( + /*const properties = new Set( this.metadata.columns .map((x: any) => x.propertyName) .concat(this.metadata.relations.map((x) => x.propertyName)) - ); + );*/ // will not include relational properties for (const key in props) { - if (!properties.has(key)) continue; + //if (!properties.has(key)) continue; // @ts-ignore const setter = this[`set${key.capitalize()}`]; // use setter function if it exists @@ -41,7 +44,8 @@ export class BaseClassWithoutId extends BaseEntity { } } - toJSON(): any { + /*toJSON(): any { + console.log("toJSON...") return Object.fromEntries( this.metadata.columns // @ts-ignore .map((x) => [x.propertyName, this[x.propertyName]]) // @ts-ignore @@ -54,6 +58,7 @@ export class BaseClassWithoutId extends BaseEntity { propertyPath: string, value: number | string ) { + console.log("increment...") const repository = this.getRepository(); return repository.increment(conditions, propertyPath, value); } @@ -63,9 +68,10 @@ export class BaseClassWithoutId extends BaseEntity { propertyPath: string, value: number | string ) { + console.log("increment...") const repository = this.getRepository(); return repository.decrement(conditions, propertyPath, value); - } + }*/ } export const PrimaryIdColumn = process.env.DATABASE?.startsWith("mongodb") ? ObjectIdColumn : PrimaryColumn; diff --git a/util/src/entities/Member.ts b/util/src/entities/Member.ts index e7e97b7c..7ca50992 100644 --- a/util/src/entities/Member.ts +++ b/util/src/entities/Member.ts @@ -122,17 +122,19 @@ export class Member extends BaseClassWithoutId { } static async removeFromGuild(user_id: string, guild_id: string) { - const guild = await Guild.findOneOrFail({ select: ["owner_id"], where: { id: guild_id } }); + const guild = await Guild.findOneOrFail({ select: ["owner_id", "member_count"], 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"] }); // use promise all to execute all promises at the same time -> save time + //TODO: check for bugs + if(guild.member_count) guild.member_count--; return Promise.all([ Member.delete({ id: user_id, guild_id, }), - Guild.decrement({ id: guild_id }, "member_count", -1), + //Guild.decrement({ id: guild_id }, "member_count", -1), emitEvent({ event: "GUILD_DELETE", @@ -259,7 +261,8 @@ export class Member extends BaseClassWithoutId { mute: false, pending: false, }; - + //TODO: check for bugs + if(guild.member_count) guild.member_count++; await Promise.all([ new Member({ ...member, @@ -276,7 +279,7 @@ export class Member extends BaseClassWithoutId { }, // Member.save is needed because else the roles relations wouldn't be updated }).save(), - Guild.increment({ id: guild_id }, "member_count", 1), + //Guild.increment({ id: guild_id }, "member_count", 1), emitEvent({ event: "GUILD_MEMBER_ADD", data: { |