1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-10 20:52:42 +01:00

🚧 typeorm

This commit is contained in:
Flam3rboy 2021-08-30 12:15:06 +02:00
parent eb6efd14c1
commit 584d4a039c
10 changed files with 67 additions and 76 deletions

View File

@ -40,7 +40,7 @@ router.post("/", check(RoleModifySchema), async (req: Request, res: Response) =>
managed: false,
position: 0,
tags: null,
permissions: perms.bitfield & (body.permissions || 0n)
permissions: String(perms.bitfield & (body.permissions || 0n))
}).save();
await emitEvent({

View File

@ -17,7 +17,7 @@ const TemplateGuildProjection: (keyof Guild)[] = [
"preferred_locale",
"afk_timeout",
"roles",
"channels",
// "channels",
"afk_channel_id",
"system_channel_id",
"system_channel_flags",

View File

@ -19,63 +19,55 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) =
}
const guild_id = Snowflake.generate();
const guild = new Guild({
name: body.name,
region: Config.get().regions.default,
owner_id: req.user_id,
icon: undefined,
afk_channel_id: undefined,
afk_timeout: 300,
application_id: undefined,
banner: undefined,
default_message_notifications: 0,
description: undefined,
splash: undefined,
discovery_splash: undefined,
explicit_content_filter: 0,
features: [],
id: guild_id,
large: undefined,
max_members: 250000,
max_presences: 250000,
max_video_channel_users: 25,
presence_count: 0,
member_count: 0, // will automatically be increased by addMember()
mfa_level: 0,
preferred_locale: "en-US",
premium_subscription_count: 0,
premium_tier: 0,
public_updates_channel_id: undefined,
rules_channel_id: undefined,
system_channel_flags: "0",
system_channel_id: undefined,
unavailable: false,
vanity_url_code: undefined,
verification_level: 0,
welcome_screen: {
enabled: false,
description: "No description",
welcome_channels: []
},
widget_channel_id: undefined,
widget_enabled: false
});
const [guild_doc, role] = await Promise.all([
new Guild(guild).save(),
new Role({
const guild = new Guild(
{
name: body.name,
region: Config.get().regions.default,
owner_id: req.user_id,
afk_timeout: 300,
default_message_notifications: 0,
explicit_content_filter: 0,
features: [],
id: guild_id,
max_members: 250000,
max_presences: 250000,
max_video_channel_users: 25,
presence_count: 0,
member_count: 0, // will automatically be increased by addMember()
mfa_level: 0,
preferred_locale: "en-US",
premium_subscription_count: 0,
premium_tier: 0,
system_channel_flags: "0",
unavailable: false,
verification_level: 0,
welcome_screen: {
enabled: false,
description: "No description",
welcome_channels: []
},
widget_enabled: false
},
{ id: guild_id }
);
const role = new Role(
{
guild_id: guild_id,
color: 0,
hoist: false,
managed: false,
mentionable: false,
name: "@everyone",
permissions: 2251804225n,
permissions: String("2251804225"),
position: 0,
tags: null
}).save()
]);
},
{
id: guild_id
}
);
await Promise.all([guild.save(), role.save()]);
if (!body.channels || !body.channels.length) body.channels = [{ id: "01", type: 0, name: "general" }];

View File

@ -51,7 +51,8 @@ export class BaseClass extends BaseEntity {
if (setter) {
setter.call(this, props[key]);
} else {
Object.defineProperty(this, key, { value: props[key] });
// @ts-ignore
this[key] = props[key];
}
}
}

View File

@ -15,7 +15,7 @@ export class Emoji extends BaseClass {
guild_id: string;
@JoinColumn({ name: "guild_id" })
@ManyToOne(() => Guild, (guild: Guild) => guild.id)
@ManyToOne(() => Guild, (guild: Guild) => guild.emojis)
guild: Guild;
@Column()

View File

@ -1,4 +1,4 @@
import { Column, Entity, JoinColumn, ManyToMany, ManyToOne, OneToOne, RelationId } from "typeorm";
import { Column, Entity, JoinColumn, ManyToMany, ManyToOne, OneToMany, OneToOne, RelationId } from "typeorm";
import { BaseClass } from "./BaseClass";
import { Channel } from "./Channel";
import { Emoji } from "./Emoji";
@ -68,35 +68,35 @@ export class Guild extends BaseClass {
member_ids: string[];
@JoinColumn({ name: "member_ids" })
@ManyToMany(() => Member, (member: Member) => member.id)
@OneToMany(() => Member, (member: Member) => member.guild)
members: Member[];
@RelationId((guild: Guild) => guild.roles)
role_ids: string[];
@JoinColumn({ name: "role_ids" })
@ManyToMany(() => Role, (role: Role) => role.id)
@OneToMany(() => Role, (role: Role) => role.guild)
roles: Role[];
@RelationId((guild: Guild) => guild.channels)
channel_ids: string[];
@JoinColumn({ name: "channel_ids" })
@ManyToMany(() => Channel, (channel: Channel) => channel.id)
@OneToMany(() => Channel, (channel: Channel) => channel.guild)
channels: Channel[];
@RelationId((guild: Guild) => guild.emojis)
emoji_ids: string[];
@JoinColumn({ name: "emoji_ids" })
@ManyToMany(() => Emoji, (emoji: Emoji) => emoji.id)
@OneToMany(() => Emoji, (emoji: Emoji) => emoji.guild)
emojis: Emoji[];
@RelationId((guild: Guild) => guild.voice_states)
voice_state_ids: string[];
@JoinColumn({ name: "voice_state_ids" })
@ManyToMany(() => VoiceState, (voicestate: VoiceState) => voicestate.id)
@OneToMany(() => VoiceState, (voicestate: VoiceState) => voicestate.guild)
voice_states: VoiceState[];
@Column({ nullable: true })
@ -109,7 +109,7 @@ export class Guild extends BaseClass {
owner_id: string;
@JoinColumn({ name: "owner_id" })
@ManyToOne(() => User, (user: User) => user.id)
@OneToOne(() => User)
owner: User;
@Column({ nullable: true })
@ -125,14 +125,14 @@ export class Guild extends BaseClass {
public_updates_channel_id: string;
@JoinColumn({ name: "public_updates_channel_id" })
@ManyToOne(() => Channel, (channel: Channel) => channel.id)
@OneToOne(() => Channel, (channel: Channel) => channel.id)
public_updates_channel?: Channel;
@RelationId((guild: Guild) => guild.rules_channel)
rules_channel_id?: string;
@JoinColumn({ name: "rules_channel_id" })
@ManyToOne(() => Channel, (channel: Channel) => channel.id)
@OneToOne(() => Channel, (channel: Channel) => channel.id)
rules_channel?: string;
@Column({ nullable: true })
@ -145,7 +145,7 @@ export class Guild extends BaseClass {
system_channel_id?: string;
@JoinColumn({ name: "system_channel_id" })
@ManyToMany(() => Channel, (channel: Channel) => channel.id)
@OneToOne(() => Channel, (channel: Channel) => channel.id)
system_channel?: Channel;
@Column({ nullable: true })
@ -158,7 +158,7 @@ export class Guild extends BaseClass {
vanity_url_code?: string;
@JoinColumn({ name: "vanity_url_code" })
@ManyToOne(() => Invite)
@OneToOne(() => Invite)
vanity_url?: Invite;
@Column({ nullable: true })
@ -180,7 +180,7 @@ export class Guild extends BaseClass {
widget_channel_id?: string;
@JoinColumn({ name: "widget_channel_id" })
@ManyToOne(() => Channel, (channel: Channel) => channel.id)
@OneToOne(() => Channel, (channel: Channel) => channel.id)
widget_channel?: Channel;
@Column({ nullable: true })

View File

@ -1,6 +1,6 @@
import { PublicUser, User } from "./User";
import { BaseClass } from "./BaseClass";
import { Column, Entity, JoinColumn, ManyToMany, ManyToOne, RelationId } from "typeorm";
import { Column, Entity, JoinColumn, JoinTable, ManyToMany, ManyToOne, OneToMany, RelationId } from "typeorm";
import { Guild } from "./Guild";
import { Config, emitEvent } from "../util";
import {
@ -26,7 +26,7 @@ export class Member extends BaseClass {
guild_id: string;
@JoinColumn({ name: "guild_id" })
@ManyToOne(() => Guild, (guild: Guild) => guild.id)
@ManyToOne(() => Guild, (guild: Guild) => guild.members)
guild: Guild;
@Column({ nullable: true })
@ -35,7 +35,7 @@ export class Member extends BaseClass {
@RelationId((member: Member) => member.roles)
role_ids: string[];
@JoinColumn({ name: "role_ids" })
@JoinTable()
@ManyToMany(() => Role)
roles: Role[];

View File

@ -27,8 +27,8 @@ export class Role extends BaseClass {
@Column()
name: string;
@Column({ type: "bigint" })
permissions: bigint;
@Column()
permissions: string;
@Column()
position: number;

View File

@ -7,7 +7,8 @@ var config: ConfigEntity;
export const Config = {
init: async function init() {
if (config) return config;
config = new ConfigEntity({}, { id: "0" });
config = (await ConfigEntity.findOne({ id: "0" })) || new ConfigEntity({}, { id: "0" });
return this.set((config.value || {}).merge(DefaultConfigOptions));
},
get: function get() {

View File

@ -212,18 +212,15 @@ export async function getPermission(user_id?: string, guild_id?: string, channel
var guild: Guild | undefined;
if (channel_id) {
channel = await Channel.findOneOrFail(
{ id: channel_id },
{ select: ["permission_overwrites", "recipients", "owner", "guild"] }
);
channel = await Channel.findOneOrFail({ id: channel_id });
if (channel.guild_id) guild_id = channel.guild_id; // derive guild_id from the channel
}
if (guild_id) {
guild = await Guild.findOneOrFail({ id: guild_id }, { select: ["owner"] });
guild = await Guild.findOneOrFail({ id: guild_id });
if (guild.owner_id === user_id) return new Permissions(Permissions.FLAGS.ADMINISTRATOR);
member = await Member.findOneOrFail({ guild_id, id: user_id }, { select: ["roles"] });
member = await Member.findOneOrFail({ where: { guild_id, id: user_id }, relations: ["roles"] });
}
var permission = Permissions.finalPermission({