summary refs log tree commit diff
path: root/util/src/models
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-21 16:47:22 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-21 16:47:22 +0200
commitf711a0411cbe48319ea3497c4f26f872c18cd2a2 (patch)
tree681aa1e830c0bd9aba70af885139ad809476a779 /util/src/models
parent:art: update bundle build (diff)
downloadserver-f711a0411cbe48319ea3497c4f26f872c18cd2a2.tar.xz
:construction: typeorm
Diffstat (limited to '')
-rw-r--r--util/oldModels/Application.ts (renamed from util/src/models/Application.ts)0
-rw-r--r--util/oldModels/AuditLog.ts (renamed from util/src/models/AuditLog.ts)0
-rw-r--r--util/oldModels/Ban.ts (renamed from util/src/models/Ban.ts)0
-rw-r--r--util/oldModels/Channel.ts (renamed from util/src/models/Channel.ts)0
-rw-r--r--util/oldModels/Emoji.ts (renamed from util/src/models/Emoji.ts)0
-rw-r--r--util/oldModels/Event.ts (renamed from util/src/models/Event.ts)2
-rw-r--r--util/oldModels/Guild.ts (renamed from util/src/models/Guild.ts)0
-rw-r--r--util/oldModels/Interaction.ts (renamed from util/src/models/Interaction.ts)0
-rw-r--r--util/oldModels/Invite.ts (renamed from util/src/models/Invite.ts)0
-rw-r--r--util/oldModels/Member.ts (renamed from util/src/models/Member.ts)0
-rw-r--r--util/oldModels/Message.ts (renamed from util/src/models/Message.ts)0
-rw-r--r--util/oldModels/RateLimit.ts (renamed from util/src/models/RateLimit.ts)0
-rw-r--r--util/oldModels/ReadState.ts (renamed from util/src/models/ReadState.ts)0
-rw-r--r--util/oldModels/Role.ts (renamed from util/src/models/Role.ts)0
-rw-r--r--util/oldModels/Team.ts (renamed from util/src/models/Team.ts)0
-rw-r--r--util/oldModels/Template.ts (renamed from util/src/models/Template.ts)0
-rw-r--r--util/oldModels/VoiceState.ts (renamed from util/src/models/VoiceState.ts)0
-rw-r--r--util/oldModels/Webhook.ts (renamed from util/src/models/Webhook.ts)0
-rw-r--r--util/src/models/Activity.ts80
-rw-r--r--util/src/models/BaseClass.ts28
-rw-r--r--util/src/models/Status.ts6
-rw-r--r--util/src/models/User.ts196
-rw-r--r--util/src/models/index.ts91
23 files changed, 103 insertions, 300 deletions
diff --git a/util/src/models/Application.ts b/util/oldModels/Application.ts

index fae6e8db..fae6e8db 100644 --- a/util/src/models/Application.ts +++ b/util/oldModels/Application.ts
diff --git a/util/src/models/AuditLog.ts b/util/oldModels/AuditLog.ts
index 02b2c444..02b2c444 100644 --- a/util/src/models/AuditLog.ts +++ b/util/oldModels/AuditLog.ts
diff --git a/util/src/models/Ban.ts b/util/oldModels/Ban.ts
index f09950ee..f09950ee 100644 --- a/util/src/models/Ban.ts +++ b/util/oldModels/Ban.ts
diff --git a/util/src/models/Channel.ts b/util/oldModels/Channel.ts
index 2959decd..2959decd 100644 --- a/util/src/models/Channel.ts +++ b/util/oldModels/Channel.ts
diff --git a/util/src/models/Emoji.ts b/util/oldModels/Emoji.ts
index 3e5cad53..3e5cad53 100644 --- a/util/src/models/Emoji.ts +++ b/util/oldModels/Emoji.ts
diff --git a/util/src/models/Event.ts b/util/oldModels/Event.ts
index 86d0fd00..904522a8 100644 --- a/util/src/models/Event.ts +++ b/util/oldModels/Event.ts
@@ -3,7 +3,7 @@ import { DMChannel, Channel } from "./Channel"; import { Guild } from "./Guild"; import { Member, PublicMember, UserGuildSettings } from "./Member"; import { Emoji } from "./Emoji"; -import { Presence } from "./Activity"; +import { Presence } from "../models/Activity"; import { Role } from "./Role"; import { Invite } from "./Invite"; import { Message, PartialEmoji } from "./Message"; diff --git a/util/src/models/Guild.ts b/util/oldModels/Guild.ts
index a5dcd8e3..a5dcd8e3 100644 --- a/util/src/models/Guild.ts +++ b/util/oldModels/Guild.ts
diff --git a/util/src/models/Interaction.ts b/util/oldModels/Interaction.ts
index 764247a5..764247a5 100644 --- a/util/src/models/Interaction.ts +++ b/util/oldModels/Interaction.ts
diff --git a/util/src/models/Invite.ts b/util/oldModels/Invite.ts
index 01f12003..01f12003 100644 --- a/util/src/models/Invite.ts +++ b/util/oldModels/Invite.ts
diff --git a/util/src/models/Member.ts b/util/oldModels/Member.ts
index d1c9ad9b..d1c9ad9b 100644 --- a/util/src/models/Member.ts +++ b/util/oldModels/Member.ts
diff --git a/util/src/models/Message.ts b/util/oldModels/Message.ts
index 15a6f40d..15a6f40d 100644 --- a/util/src/models/Message.ts +++ b/util/oldModels/Message.ts
diff --git a/util/src/models/RateLimit.ts b/util/oldModels/RateLimit.ts
index 6a0e1ffd..6a0e1ffd 100644 --- a/util/src/models/RateLimit.ts +++ b/util/oldModels/RateLimit.ts
diff --git a/util/src/models/ReadState.ts b/util/oldModels/ReadState.ts
index 9c4fb323..9c4fb323 100644 --- a/util/src/models/ReadState.ts +++ b/util/oldModels/ReadState.ts
diff --git a/util/src/models/Role.ts b/util/oldModels/Role.ts
index c1111c84..c1111c84 100644 --- a/util/src/models/Role.ts +++ b/util/oldModels/Role.ts
diff --git a/util/src/models/Team.ts b/util/oldModels/Team.ts
index 795c82d2..795c82d2 100644 --- a/util/src/models/Team.ts +++ b/util/oldModels/Team.ts
diff --git a/util/src/models/Template.ts b/util/oldModels/Template.ts
index ad0f9104..ad0f9104 100644 --- a/util/src/models/Template.ts +++ b/util/oldModels/Template.ts
diff --git a/util/src/models/VoiceState.ts b/util/oldModels/VoiceState.ts
index c1f90edd..c1f90edd 100644 --- a/util/src/models/VoiceState.ts +++ b/util/oldModels/VoiceState.ts
diff --git a/util/src/models/Webhook.ts b/util/oldModels/Webhook.ts
index 7379e98f..7379e98f 100644 --- a/util/src/models/Webhook.ts +++ b/util/oldModels/Webhook.ts
diff --git a/util/src/models/Activity.ts b/util/src/models/Activity.ts
index 17abd1ca..6b13477f 100644 --- a/util/src/models/Activity.ts +++ b/util/src/models/Activity.ts
@@ -1,7 +1,5 @@ -import { User } from ".."; +import { User } from "./User"; import { ClientStatus, Status } from "./Status"; -import { Schema, model, Types, Document } from "mongoose"; -import toBigInt from "../util/toBigInt"; export interface Presence { user: User; @@ -47,82 +45,6 @@ export interface Activity { flags?: bigint; } -export const ActivitySchema = { - name: { type: String, required: true }, - type: { type: Number, required: true }, - url: String, - created_at: Date, - timestamps: [ - { - start: Number, - end: Number, - }, - ], - application_id: String, - details: String, - state: String, - emoji: { - name: String, - id: String, - amimated: Boolean, - }, - party: { - id: String, - size: [Number, Number], - }, - assets: { - large_image: String, - large_text: String, - small_image: String, - small_text: String, - }, - secrets: { - join: String, - spectate: String, - match: String, - }, - instance: Boolean, - flags: { type: String, get: toBigInt }, -}; - -export const ActivityBodySchema = { - name: String, - type: Number, - $url: String, - $created_at: Date, - $timestamps: [ - { - $start: Number, - $end: Number, - }, - ], - $application_id: String, - $details: String, - $state: String, - $emoji: { - $name: String, - $id: String, - $amimated: Boolean, - }, - $party: { - $id: String, - $size: [Number, Number], - }, - $assets: { - $large_image: String, - $large_text: String, - $small_image: String, - $small_text: String, - }, - $secrets: { - $join: String, - $spectate: String, - $match: String, - }, - $instance: Boolean, - $flags: BigInt, -}; - export enum ActivityType { GAME = 0, STREAMING = 1, diff --git a/util/src/models/BaseClass.ts b/util/src/models/BaseClass.ts new file mode 100644
index 00000000..78cd329c --- /dev/null +++ b/util/src/models/BaseClass.ts
@@ -0,0 +1,28 @@ +import "reflect-metadata"; +import { BaseEntity, Column } from "typeorm"; + +export class BaseClass extends BaseEntity { + @Column() + id?: string; + + constructor(props?: any) { + super(); + BaseClass.assign(props, this, "body."); + } + + private static assign(props: any, object: any, path?: string): any { + const expectedType = Reflect.getMetadata("design:type", object, props); + console.log(expectedType, object, props, path, typeof object); + + if (typeof object !== typeof props) throw new Error(`Property at ${path} must be`); + if (typeof object === "object") + return Object.keys(object).map((key) => BaseClass.assign(props[key], object[key], `${path}.${key}`)); + } +} + +// @ts-ignore +global.BaseClass = BaseClass; + +var test = new BaseClass({}); + +setTimeout(() => {}, 10000 * 1000); diff --git a/util/src/models/Status.ts b/util/src/models/Status.ts
index 5a9bf2ca..c4dab586 100644 --- a/util/src/models/Status.ts +++ b/util/src/models/Status.ts
@@ -5,9 +5,3 @@ export interface ClientStatus { mobile?: string; // e.g. iOS/Android web?: string; // e.g. browser, bot account } - -export const ClientStatus = { - desktop: String, - mobile: String, - web: String, -}; diff --git a/util/src/models/User.ts b/util/src/models/User.ts
index c667e954..38045738 100644 --- a/util/src/models/User.ts +++ b/util/src/models/User.ts
@@ -1,8 +1,7 @@ -import { Activity, ActivitySchema } from "./Activity"; +import { Column, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm"; +import { Activity } from "./Activity"; +import { BaseClass } from "./BaseClass"; import { ClientStatus, Status } from "./Status"; -import { Schema, Types, Document } from "mongoose"; -import db from "../util/Database"; -import toBigInt from "../util/toBigInt"; export const PublicUserProjection = { username: true, @@ -16,53 +15,109 @@ export const PublicUserProjection = { bot: true, }; -export interface User { +export class User extends BaseClass { + @PrimaryGeneratedColumn() id: string; - username: string; // username max length 32, min 2 + + @Column() + username: string; // username max length 32, min 2 (should be configurable) + + @Column() discriminator: string; // #0001 4 digit long string from #0001 - #9999 + + @Column() avatar: string | null; // hash of the user avatar + + @Column() accent_color: number | null; // banner color of user - banner: string | null; + + @Column() + banner: string | null; // hash of the user banner + + @Column() phone: string | null; // phone number of the user + + @Column() desktop: boolean; // if the user has desktop app installed + + @Column() mobile: boolean; // if the user has mobile app installed + + @Column() premium: boolean; // if user bought nitro + + @Column() premium_type: number; // nitro level + + @Column() bot: boolean; // if user is bot - bio: string; // short description of the user (max 190 chars) - system: boolean; // shouldn't be used, the api sents this field type true, if the genetaed message comes from a system generated author + + @Column() + bio: string; // short description of the user (max 190 chars -> should be configurable) + + @Column() + system: boolean; // shouldn't be used, the api sents this field type true, if the generated message comes from a system generated author + + @Column() nsfw_allowed: boolean; // if the user is older than 18 (resp. Config) + + @Column() mfa_enabled: boolean; // if multi factor authentication is enabled + + @Column() created_at: Date; // registration date + + @Column() verified: boolean; // if the user is offically verified + + @Column() disabled: boolean; // if the account is disabled + + @Column() deleted: boolean; // if the user was deleted + + @Column() email: string | null; // email of the user + + @Column() flags: bigint; // UserFlags + + @Column() public_flags: bigint; - user_settings: UserSettings; + + @Column("simple-array") // string in simple-array must not contain commas guilds: string[]; // array of guild ids the user is part of + + @Column("simple-json") + user_settings: UserSettings; + + @Column("simple-json") user_data: UserData; + + @Column("simple-json") presence: { status: Status; activities: Activity[]; client_status: ClientStatus; }; + + @Column("simple-json") + relationships: Relationship[]; + + @Column("simple-json") + connected_accounts: ConnectedAccount[]; } -// Private user data: +// @ts-ignore +global.User = User; + +// Private user data that should never get sent to the client export interface UserData { valid_tokens_since: Date; // all tokens with a previous issue date are invalid - relationships: Relationship[]; - connected_accounts: ConnectedAccount[]; hash: string; // hash of the password, salt is saved in password (bcrypt) fingerprints: string[]; // array of fingerprints -> used to prevent multiple accounts } -export interface UserDocument extends User, Document { - id: string; -} - export interface PublicUser { id: string; discriminator: string; @@ -143,110 +198,3 @@ export interface UserSettings { theme: "dark" | "white"; // dark timezone_offset: number; // e.g -60 } - -export const UserSchema = new Schema({ - id: String, - username: String, - discriminator: String, - avatar: String, - accent_color: Number, - banner: String, - phone: String, - desktop: Boolean, - mobile: Boolean, - premium: Boolean, - premium_type: Number, - bot: Boolean, - bio: String, - system: Boolean, - nsfw_allowed: Boolean, - mfa_enabled: Boolean, - created_at: Date, - verified: Boolean, - disabled: Boolean, - deleted: Boolean, - email: String, - flags: { type: String, get: toBigInt }, // TODO: automatically convert Types.Long to BitField of UserFlags - public_flags: { type: String, get: toBigInt }, - guilds: [String], // array of guild ids the user is part of - user_data: { - fingerprints: [String], - hash: String, // hash of the password, salt is saved in password (bcrypt) - valid_tokens_since: Date, // all tokens with a previous issue date are invalid - relationships: [ - { - id: { type: String, required: true }, - nickname: String, - type: { type: Number }, - }, - ], - connected_accounts: [ - { - access_token: String, - friend_sync: Boolean, - id: String, - name: String, - revoked: Boolean, - show_activity: Boolean, - type: { type: String }, - verifie: Boolean, - visibility: Number, - }, - ], - }, - user_settings: { - afk_timeout: Number, - allow_accessibility_detection: Boolean, - animate_emoji: Boolean, - animate_stickers: Number, - contact_sync_enabled: Boolean, - convert_emoticons: Boolean, - custom_status: { - emoji_id: String, - emoji_name: String, - expires_at: Number, - text: String, - }, - default_guilds_restricted: Boolean, - detect_platform_accounts: Boolean, - developer_mode: Boolean, - disable_games_tab: Boolean, - enable_tts_command: Boolean, - explicit_content_filter: Number, - friend_source_flags: { all: Boolean }, - gateway_connected: Boolean, - gif_auto_play: Boolean, - // every top guild is displayed as a "folder" - guild_folders: [ - { - color: Number, - guild_ids: [String], - id: Number, - name: String, - }, - ], - guild_positions: [String], // guild ids ordered by position - inline_attachment_media: Boolean, - inline_embed_media: Boolean, - locale: String, // en_US - message_display_compact: Boolean, - native_phone_integration_enabled: Boolean, - render_embeds: Boolean, - render_reactions: Boolean, - restricted_guilds: [String], - show_current_game: Boolean, - status: String, - stream_notifications_enabled: Boolean, - theme: String, // dark - timezone_offset: Number, // e.g -60, - }, - - presence: { - status: String, - activities: [ActivitySchema], - client_status: ClientStatus, - }, -}); - -// @ts-ignore -export const UserModel = db.model<UserDocument>("User", UserSchema, "users"); diff --git a/util/src/models/index.ts b/util/src/models/index.ts
index b6100f86..94882a0a 100644 --- a/util/src/models/index.ts +++ b/util/src/models/index.ts
@@ -1,93 +1,4 @@ -// @ts-nocheck -import mongoose, { Schema, Document } from "mongoose"; -import mongooseAutoPopulate from "mongoose-autopopulate"; - -type UpdateWithAggregationPipeline = UpdateAggregationStage[]; -type UpdateAggregationStage = - | { $addFields: any } - | { $set: any } - | { $project: any } - | { $unset: any } - | { $replaceRoot: any } - | { $replaceWith: any }; -type EnforceDocument<T, TMethods> = T extends Document ? T : T & Document & TMethods; - -declare module "mongoose" { - interface SchemaOptions { - removeResponse?: string[]; - } - interface Model<T, TQueryHelpers = {}, TMethods = {}> { - // removed null -> always return document -> throw error if it doesn't exist - findOne( - filter?: FilterQuery<T>, - projection?: any | null, - options?: QueryOptions | null, - callback?: (err: CallbackError, doc: EnforceDocument<T, TMethods>) => void - ): QueryWithHelpers<EnforceDocument<T, TMethods>, EnforceDocument<T, TMethods>, TQueryHelpers>; - findOneAndUpdate( - filter?: FilterQuery<T>, - update?: UpdateQuery<T> | UpdateWithAggregationPipeline, - options?: QueryOptions | null, - callback?: (err: any, doc: EnforceDocument<T, TMethods> | null, res: any) => void - ): QueryWithHelpers<EnforceDocument<T, TMethods>, EnforceDocument<T, TMethods>, TQueryHelpers>; - } -} - -var HTTPError: any; - -try { - HTTPError = require("lambert-server").HTTPError; -} catch (e) { - HTTPError = Error; -} - -mongoose.plugin(mongooseAutoPopulate); - -mongoose.plugin((schema: Schema, opts: any) => { - schema.set("toObject", { - virtuals: true, - versionKey: false, - transform(doc: any, ret: any) { - delete ret._id; - delete ret.__v; - const props = schema.get("removeResponse") || []; - props.forEach((prop: string) => { - delete ret[prop]; - }); - }, - }); - schema.post("findOne", function (doc, next) { - try { - // @ts-ignore - const isExistsQuery = JSON.stringify(this._userProvidedFields) === JSON.stringify({ _id: 1 }); - if (!doc && !isExistsQuery) { - // @ts-ignore - return next(new HTTPError(`${this?.mongooseCollection?.name}.${this?._conditions?.id} not found`, 400)); - } - // @ts-ignore - return next(); - } catch (error) { - // @ts-ignore - next(); - } - }); -}); - export * from "./Activity"; -export * from "./Application"; -export * from "./Ban"; -export * from "./Channel"; -export * from "./Emoji"; -export * from "./Event"; -export * from "./Template"; -export * from "./Guild"; -export * from "./Invite"; -export * from "./Interaction"; -export * from "./Member"; -export * from "./Message"; +export * from "./BaseClass"; export * from "./Status"; -export * from "./Role"; export * from "./User"; -export * from "./VoiceState"; -export * from "./ReadState"; -export * from "./RateLimit";