diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-09-01 23:34:55 +0200 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-09-01 23:34:55 +0200 |
commit | 295159fc4a7d32ffa7686ff7df510d098a1794f9 (patch) | |
tree | 51dd0493766f0eda1551dd44f546e8189aafa182 | |
parent | :sparkles: generate open api schema based on body and db entities (diff) | |
download | server-295159fc4a7d32ffa7686ff7df510d098a1794f9.tar.xz |
:bug: fix entity types and projection
-rw-r--r-- | api/src/routes/channels/#channel_id/messages/#message_id/index.ts | 3 | ||||
-rw-r--r-- | api/src/routes/users/@me/index.ts | 25 | ||||
-rw-r--r-- | bundle/database.db | bin | 225280 -> 0 bytes | |||
-rw-r--r-- | util/package-lock.json | 14 | ||||
-rw-r--r-- | util/package.json | 2 | ||||
-rw-r--r-- | util/src/entities/AuditLog.ts | 6 | ||||
-rw-r--r-- | util/src/entities/BaseClass.ts | 10 | ||||
-rw-r--r-- | util/src/entities/Member.ts | 7 | ||||
-rw-r--r-- | util/src/entities/Message.ts | 2 | ||||
-rw-r--r-- | util/src/entities/User.ts | 66 | ||||
-rw-r--r-- | util/src/tes.ts | 23 |
11 files changed, 68 insertions, 90 deletions
diff --git a/api/src/routes/channels/#channel_id/messages/#message_id/index.ts b/api/src/routes/channels/#channel_id/messages/#message_id/index.ts index b9d46c4f..9c631a18 100644 --- a/api/src/routes/channels/#channel_id/messages/#message_id/index.ts +++ b/api/src/routes/channels/#channel_id/messages/#message_id/index.ts @@ -1,7 +1,6 @@ import { Channel, emitEvent, getPermission, MessageDeleteEvent, Message, MessageUpdateEvent } from "@fosscord/util"; import { Router, Response, Request } from "express"; import { MessageCreateSchema } from "../../../../../schema/Message"; - import { check } from "../../../../../util/instanceOf"; import { handleMessage, postHandleMessage } from "../../../../../util/Message"; @@ -32,7 +31,7 @@ router.patch("/", check(MessageCreateSchema), async (req: Request, res: Response }); await Promise.all([ - new_message.save(), + new_message!.save(), await emitEvent({ event: "MESSAGE_UPDATE", channel_id, diff --git a/api/src/routes/users/@me/index.ts b/api/src/routes/users/@me/index.ts index d5a5723c..68649215 100644 --- a/api/src/routes/users/@me/index.ts +++ b/api/src/routes/users/@me/index.ts @@ -1,5 +1,5 @@ import { Router, Request, Response } from "express"; -import { User } from "@fosscord/util"; +import { User, PrivateUserProjection } from "@fosscord/util"; import { UserModifySchema } from "../../../schema/User"; import { check } from "../../../util/instanceOf"; import { handleFile } from "../../../util/cdn"; @@ -7,30 +7,9 @@ import { handleFile } from "../../../util/cdn"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { - res.json(await User.getPublicUser(req.user_id)); + res.json(await User.getPublicUser(req.user_id, { select: PrivateUserProjection })); }); -const UserUpdateProjection = [ - "accent_color", - "avatar", - "banner", - "bio", - "bot", - "discriminator", - "email", - "flags", - "id", - "locale", - "mfa_enabled", - "nsfw_alllowed", - "phone", - "public_flags", - "purchased_flags", - // "token", // this isn't saved in the db and needs to be set manually - "username", - "verified" -]; - router.patch("/", check(UserModifySchema), async (req: Request, res: Response) => { const body = req.body as UserModifySchema; diff --git a/bundle/database.db b/bundle/database.db deleted file mode 100644 index 2d4abd49..00000000 --- a/bundle/database.db +++ /dev/null Binary files differdiff --git a/util/package-lock.json b/util/package-lock.json index 47aca2d1..571fffbe 100644 --- a/util/package-lock.json +++ b/util/package-lock.json @@ -17,7 +17,7 @@ "env-paths": "^2.2.1", "jsonwebtoken": "^8.5.1", "lambert-server": "^1.2.10", - "missing-native-js-functions": "^1.2.11", + "missing-native-js-functions": "^1.2.13", "node-fetch": "^2.6.1", "patch-package": "^6.4.7", "pg": "^8.7.1", @@ -6338,9 +6338,9 @@ } }, "node_modules/missing-native-js-functions": { - "version": "1.2.11", - "resolved": "https://registry.npmjs.org/missing-native-js-functions/-/missing-native-js-functions-1.2.11.tgz", - "integrity": "sha512-U97IscNBL4Wg9adYjEBT46Hb0Ld5dPT8vbdwFX+TNzGrFQCc4WqoGAZouaLNFwUqxzzHZ9DVg59unwnQyeIIQg==" + "version": "1.2.13", + "resolved": "https://registry.npmjs.org/missing-native-js-functions/-/missing-native-js-functions-1.2.13.tgz", + "integrity": "sha512-1RAArfUkrGkj5N3xJVW251F2PvfP2ozAcxsLLDR6uiiAixTP5Abh8zzGMadepbqgiHC0FGlTSAUNbh9abN4Osg==" }, "node_modules/mkdirp": { "version": "1.0.4", @@ -13735,9 +13735,9 @@ } }, "missing-native-js-functions": { - "version": "1.2.11", - "resolved": "https://registry.npmjs.org/missing-native-js-functions/-/missing-native-js-functions-1.2.11.tgz", - "integrity": "sha512-U97IscNBL4Wg9adYjEBT46Hb0Ld5dPT8vbdwFX+TNzGrFQCc4WqoGAZouaLNFwUqxzzHZ9DVg59unwnQyeIIQg==" + "version": "1.2.13", + "resolved": "https://registry.npmjs.org/missing-native-js-functions/-/missing-native-js-functions-1.2.13.tgz", + "integrity": "sha512-1RAArfUkrGkj5N3xJVW251F2PvfP2ozAcxsLLDR6uiiAixTP5Abh8zzGMadepbqgiHC0FGlTSAUNbh9abN4Osg==" }, "mkdirp": { "version": "1.0.4", diff --git a/util/package.json b/util/package.json index 39af7526..38cd8d35 100644 --- a/util/package.json +++ b/util/package.json @@ -44,7 +44,7 @@ "env-paths": "^2.2.1", "jsonwebtoken": "^8.5.1", "lambert-server": "^1.2.10", - "missing-native-js-functions": "^1.2.11", + "missing-native-js-functions": "^1.2.13", "node-fetch": "^2.6.1", "patch-package": "^6.4.7", "pg": "^8.7.1", diff --git a/util/src/entities/AuditLog.ts b/util/src/entities/AuditLog.ts index ceeb21fd..ae9feb76 100644 --- a/util/src/entities/AuditLog.ts +++ b/util/src/entities/AuditLog.ts @@ -42,17 +42,17 @@ export enum AuditLogEvents { } @Entity("audit_logs") -export class AuditLogEntry extends BaseClass { +export class AuditLog extends BaseClass { @JoinColumn({ name: "target_id" }) @ManyToOne(() => User) target?: User; @Column({ nullable: true }) - @RelationId((auditlog: AuditLogEntry) => auditlog.user) + @RelationId((auditlog: AuditLog) => auditlog.user) user_id: string; @JoinColumn({ name: "user_id" }) - @ManyToOne(() => User) + @ManyToOne(() => User, (user: User) => user.id) user: User; @Column({ diff --git a/util/src/entities/BaseClass.ts b/util/src/entities/BaseClass.ts index 0856ccd1..403a6fc6 100644 --- a/util/src/entities/BaseClass.ts +++ b/util/src/entities/BaseClass.ts @@ -8,19 +8,19 @@ import "missing-native-js-functions"; export class BaseClass extends BaseEntity { @PrimaryColumn() - id: string = Snowflake.generate(); + id: string; // @ts-ignore - constructor(public props?: any) { + constructor(private props?: any) { super(); this.assign(props); } - get construct(): any { + private get construct(): any { return this.constructor; } - get metadata() { + private get metadata() { return this.construct.getRepository().metadata as EntityMetadata; } @@ -48,6 +48,8 @@ export class BaseClass extends BaseEntity { this[key] = props[key]; } } + + if (!this.id) this.id = Snowflake.generate(); } @BeforeUpdate() diff --git a/util/src/entities/Member.ts b/util/src/entities/Member.ts index d2d78bb9..e1773730 100644 --- a/util/src/entities/Member.ts +++ b/util/src/entities/Member.ts @@ -12,6 +12,7 @@ import { } from "../interfaces"; import { HTTPError } from "lambert-server"; import { Role } from "./Role"; +import { ReadState } from "./ReadState"; @Entity("members") export class Member extends BaseClass { @@ -53,8 +54,8 @@ export class Member extends BaseClass { settings: UserGuildSettings; // TODO: update - @Column({ type: "simple-json" }) - read_state: Record<string, string | null>; + // @Column({ type: "simple-json" }) + // read_state: ReadState; static async IsInGuildOrFail(user_id: string, guild_id: string) { if (await Member.count({ id: user_id, guild: { id: guild_id } })) return true; @@ -206,7 +207,7 @@ export class Member extends BaseClass { Member.insert({ ...member, roles: undefined, - read_state: {}, + // read_state: {}, settings: { channel_overrides: [], message_notifications: 0, diff --git a/util/src/entities/Message.ts b/util/src/entities/Message.ts index 542b2b55..aacca526 100644 --- a/util/src/entities/Message.ts +++ b/util/src/entities/Message.ts @@ -139,7 +139,7 @@ export class Message extends BaseClass { reactions: Reaction[]; @Column({ type: "text", nullable: true }) - nonce?: string | number; + nonce?: string; @Column({ nullable: true }) pinned?: boolean; diff --git a/util/src/entities/User.ts b/util/src/entities/User.ts index 39f654be..1b4ce351 100644 --- a/util/src/entities/User.ts +++ b/util/src/entities/User.ts @@ -4,33 +4,53 @@ import { BitField } from "../util/BitField"; import { Relationship } from "./Relationship"; import { ConnectedAccount } from "./ConnectedAccount"; import { HTTPError } from "lambert-server"; -import { Channel } from "./Channel"; - -type PublicUserKeys = - | "username" - | "discriminator" - | "id" - | "public_flags" - | "avatar" - | "accent_color" - | "banner" - | "bio" - | "bot"; -export const PublicUserProjection: PublicUserKeys[] = [ - "username", - "discriminator", - "id", - "public_flags", - "avatar", - "accent_color", - "banner", - "bio", - "bot", -]; + +export enum PublicUserEnum { + username, + discriminator, + id, + public_flags, + avatar, + accent_color, + banner, + bio, + bot, +} +export type PublicUserKeys = keyof typeof PublicUserEnum; + +export enum PrivateUserEnum { + flags, + mfa_enabled, + email, + phone, + verified, + nsfw_allowed, + premium, + premium_type, + disabled, + // locale +} +export type PrivateUserKeys = keyof typeof PrivateUserEnum | PublicUserKeys; + +export const PublicUserProjection = Object.values(PublicUserEnum).filter( + (x) => typeof x === "string" +) as PublicUserKeys[]; +export const PrivateUserProjection = [ + ...PublicUserProjection, + Object.values(PrivateUserEnum).filter((x) => typeof x === "string"), +] as PrivateUserKeys[]; // Private user data that should never get sent to the client export type PublicUser = Pick<User, PublicUserKeys>; +export interface UserPublic extends Pick<User, PublicUserKeys> {} + +export interface UserPrivate extends Pick<User, PrivateUserKeys> { + locale: string; +} + +// TODO: add purchased_flags, premium_usage_flags + @Entity("users") export class User extends BaseClass { @Column() diff --git a/util/src/tes.ts b/util/src/tes.ts deleted file mode 100644 index e326dee1..00000000 --- a/util/src/tes.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { performance } from "perf_hooks"; -import { Guild, Relationship, RelationshipType } from "./entities"; -import { User } from "./entities/User"; -import { initDatabase } from "./util"; - -initDatabase().then(async (x) => { - try { - const user = await new User({ - guilds: [], - discriminator: "1", - username: "test", - flags: "0", - public_flags: "0", - id: "0", - }).save(); - - user.relationships = [new Relationship({ type: RelationshipType.friends })]; - - user.save(); - } catch (error) { - console.error(error); - } -}); |