🎨 update entities
This commit is contained in:
parent
acb5fba488
commit
a374b0b11a
@ -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
|
// 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
|
// 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 {
|
export class BaseClassWithoutId extends BaseEntity {
|
||||||
@PrimaryColumn()
|
|
||||||
id: string;
|
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
constructor(private props?: any) {
|
constructor(private props?: any) {
|
||||||
super();
|
super();
|
||||||
this.assign(props);
|
this.assign(props);
|
||||||
@ -24,8 +20,7 @@ export class BaseClass extends BaseEntity {
|
|||||||
return this.construct.getRepository().metadata as EntityMetadata;
|
return this.construct.getRepository().metadata as EntityMetadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
assign(props: any) {
|
assign(props: any = {}) {
|
||||||
if (!props || typeof props !== "object") return;
|
|
||||||
delete props.opts;
|
delete props.opts;
|
||||||
delete props.props;
|
delete props.props;
|
||||||
|
|
||||||
@ -48,8 +43,6 @@ export class BaseClass extends BaseEntity {
|
|||||||
this[key] = props[key];
|
this[key] = props[key];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.id) this.id = Snowflake.generate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeUpdate()
|
@BeforeUpdate()
|
||||||
@ -77,3 +70,14 @@ export class BaseClass extends BaseEntity {
|
|||||||
return repository.decrement(conditions, propertyPath, value);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -12,15 +12,16 @@ import {
|
|||||||
} from "../interfaces";
|
} from "../interfaces";
|
||||||
import { HTTPError } from "lambert-server";
|
import { HTTPError } from "lambert-server";
|
||||||
import { Role } from "./Role";
|
import { Role } from "./Role";
|
||||||
|
import { BaseClassWithoutId } from "./BaseClass";
|
||||||
|
|
||||||
@Entity("members")
|
@Entity("members")
|
||||||
@Index(["user_id", "guild_id"], { unique: true })
|
@Index(["id", "guild_id"], { unique: true })
|
||||||
export class Member extends BaseClass {
|
export class Member extends BaseClassWithoutId {
|
||||||
@Column()
|
@Column({ primary: true, unique: false })
|
||||||
@RelationId((member: Member) => member.user)
|
@RelationId((member: Member) => member.user)
|
||||||
user_id: string;
|
id: string;
|
||||||
|
|
||||||
@JoinColumn({ name: "user_id" })
|
@JoinColumn({ name: "id" })
|
||||||
@ManyToOne(() => User)
|
@ManyToOne(() => User)
|
||||||
user: User;
|
user: User;
|
||||||
|
|
||||||
@ -35,7 +36,14 @@ export class Member extends BaseClass {
|
|||||||
@Column({ nullable: true })
|
@Column({ nullable: true })
|
||||||
nick?: string;
|
nick?: string;
|
||||||
|
|
||||||
@JoinTable({ name: "member_roles" })
|
@JoinTable({
|
||||||
|
name: "member_roles",
|
||||||
|
joinColumn: { name: "id", referencedColumnName: "id" },
|
||||||
|
inverseJoinColumn: {
|
||||||
|
name: "role_id",
|
||||||
|
referencedColumnName: "id",
|
||||||
|
},
|
||||||
|
})
|
||||||
@ManyToMany(() => Role)
|
@ManyToMany(() => Role)
|
||||||
roles: Role[];
|
roles: Role[];
|
||||||
|
|
||||||
@ -62,19 +70,19 @@ export class Member extends BaseClass {
|
|||||||
// read_state: ReadState;
|
// read_state: ReadState;
|
||||||
|
|
||||||
static async IsInGuildOrFail(user_id: string, guild_id: string) {
|
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);
|
throw new HTTPError("You are not member of this guild", 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
static async removeFromGuild(user_id: string, guild_id: string) {
|
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"], where: { id: guild_id } });
|
||||||
if (guild.owner_id === user_id) throw new Error("The owner cannot be removed of the guild");
|
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
|
// use promise all to execute all promises at the same time -> save time
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
Member.delete({
|
Member.delete({
|
||||||
user_id,
|
id: user_id,
|
||||||
guild_id,
|
guild_id,
|
||||||
}),
|
}),
|
||||||
Guild.decrement({ id: guild_id }, "member_count", -1),
|
Guild.decrement({ id: guild_id }, "member_count", -1),
|
||||||
@ -98,7 +106,7 @@ export class Member extends BaseClass {
|
|||||||
const [member] = await Promise.all([
|
const [member] = await Promise.all([
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
Member.findOneOrFail({
|
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
|
relations: ["user", "roles"], // we don't want to load the role objects just the ids
|
||||||
select: ["roles.id"],
|
select: ["roles.id"],
|
||||||
}),
|
}),
|
||||||
@ -124,7 +132,7 @@ export class Member extends BaseClass {
|
|||||||
const [member] = await Promise.all([
|
const [member] = await Promise.all([
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
Member.findOneOrFail({
|
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
|
relations: ["user", "roles"], // we don't want to load the role objects just the ids
|
||||||
select: ["roles.id"],
|
select: ["roles.id"],
|
||||||
}),
|
}),
|
||||||
@ -149,7 +157,7 @@ export class Member extends BaseClass {
|
|||||||
static async changeNickname(user_id: string, guild_id: string, nickname: string) {
|
static async changeNickname(user_id: string, guild_id: string, nickname: string) {
|
||||||
const member = await Member.findOneOrFail({
|
const member = await Member.findOneOrFail({
|
||||||
where: {
|
where: {
|
||||||
user_id: user_id,
|
id: user_id,
|
||||||
guild_id,
|
guild_id,
|
||||||
},
|
},
|
||||||
relations: ["user"],
|
relations: ["user"],
|
||||||
@ -175,7 +183,7 @@ export class Member extends BaseClass {
|
|||||||
const user = await User.getPublicUser(user_id);
|
const user = await User.getPublicUser(user_id);
|
||||||
|
|
||||||
const { maxGuilds } = Config.get().limits.user;
|
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) {
|
if (guild_count >= maxGuilds) {
|
||||||
throw new HTTPError(`You are at the ${maxGuilds} server limit.`, 403);
|
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"],
|
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);
|
throw new HTTPError("You are already a member of this guild", 400);
|
||||||
|
|
||||||
const member = {
|
const member = {
|
||||||
user_id,
|
id: user_id,
|
||||||
guild_id,
|
guild_id,
|
||||||
nick: undefined,
|
nick: undefined,
|
||||||
roles: [guild_id], // @everyone role
|
roles: [guild_id], // @everyone role
|
||||||
@ -207,7 +215,7 @@ export class Member extends BaseClass {
|
|||||||
await Promise.all([
|
await Promise.all([
|
||||||
new Member({
|
new Member({
|
||||||
...member,
|
...member,
|
||||||
roles: undefined,
|
roles: [new Role({ id: guild_id })],
|
||||||
// read_state: {},
|
// read_state: {},
|
||||||
settings: {
|
settings: {
|
||||||
channel_overrides: [],
|
channel_overrides: [],
|
||||||
|
@ -103,7 +103,6 @@ export class Message extends BaseClass {
|
|||||||
timestamp: Date;
|
timestamp: Date;
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
@UpdateDateColumn()
|
|
||||||
edited_timestamp?: Date;
|
edited_timestamp?: Date;
|
||||||
|
|
||||||
@Column({ nullable: true })
|
@Column({ nullable: true })
|
||||||
|
@ -11,7 +11,11 @@ export class Recipient extends BaseClass {
|
|||||||
@ManyToOne(() => require("./Channel").Channel)
|
@ManyToOne(() => require("./Channel").Channel)
|
||||||
channel: import("./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)
|
@ManyToOne(() => require("./User").User)
|
||||||
user: import("./User").User;
|
user: import("./User").User;
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ export const PublicUserProjection = Object.values(PublicUserEnum).filter(
|
|||||||
) as PublicUserKeys[];
|
) as PublicUserKeys[];
|
||||||
export const PrivateUserProjection = [
|
export const PrivateUserProjection = [
|
||||||
...PublicUserProjection,
|
...PublicUserProjection,
|
||||||
Object.values(PrivateUserEnum).filter((x) => typeof x === "string"),
|
...Object.values(PrivateUserEnum).filter((x) => typeof x === "string"),
|
||||||
] as PrivateUserKeys[];
|
] as PrivateUserKeys[];
|
||||||
|
|
||||||
// Private user data that should never get sent to the client
|
// Private user data that should never get sent to the client
|
||||||
|
Loading…
x
Reference in New Issue
Block a user