🐛 fix entity types and projection

This commit is contained in:
Flam3rboy 2021-09-01 23:34:55 +02:00
parent 1a76c663d2
commit cba341a6d6
11 changed files with 67 additions and 89 deletions

View File

@ -1,7 +1,6 @@
import { Channel, emitEvent, getPermission, MessageDeleteEvent, Message, MessageUpdateEvent } from "@fosscord/util"; import { Channel, emitEvent, getPermission, MessageDeleteEvent, Message, MessageUpdateEvent } from "@fosscord/util";
import { Router, Response, Request } from "express"; import { Router, Response, Request } from "express";
import { MessageCreateSchema } from "../../../../../schema/Message"; import { MessageCreateSchema } from "../../../../../schema/Message";
import { check } from "../../../../../util/instanceOf"; import { check } from "../../../../../util/instanceOf";
import { handleMessage, postHandleMessage } from "../../../../../util/Message"; import { handleMessage, postHandleMessage } from "../../../../../util/Message";
@ -32,7 +31,7 @@ router.patch("/", check(MessageCreateSchema), async (req: Request, res: Response
}); });
await Promise.all([ await Promise.all([
new_message.save(), new_message!.save(),
await emitEvent({ await emitEvent({
event: "MESSAGE_UPDATE", event: "MESSAGE_UPDATE",
channel_id, channel_id,

View File

@ -1,5 +1,5 @@
import { Router, Request, Response } from "express"; import { Router, Request, Response } from "express";
import { User } from "@fosscord/util"; import { User, PrivateUserProjection } from "@fosscord/util";
import { UserModifySchema } from "../../../schema/User"; import { UserModifySchema } from "../../../schema/User";
import { check } from "../../../util/instanceOf"; import { check } from "../../../util/instanceOf";
import { handleFile } from "../../../util/cdn"; import { handleFile } from "../../../util/cdn";
@ -7,30 +7,9 @@ import { handleFile } from "../../../util/cdn";
const router: Router = Router(); const router: Router = Router();
router.get("/", async (req: Request, res: Response) => { 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) => { router.patch("/", check(UserModifySchema), async (req: Request, res: Response) => {
const body = req.body as UserModifySchema; const body = req.body as UserModifySchema;

Binary file not shown.

14
util/package-lock.json generated
View File

@ -17,7 +17,7 @@
"env-paths": "^2.2.1", "env-paths": "^2.2.1",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"lambert-server": "^1.2.10", "lambert-server": "^1.2.10",
"missing-native-js-functions": "^1.2.11", "missing-native-js-functions": "^1.2.13",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"patch-package": "^6.4.7", "patch-package": "^6.4.7",
"pg": "^8.7.1", "pg": "^8.7.1",
@ -6338,9 +6338,9 @@
} }
}, },
"node_modules/missing-native-js-functions": { "node_modules/missing-native-js-functions": {
"version": "1.2.11", "version": "1.2.13",
"resolved": "https://registry.npmjs.org/missing-native-js-functions/-/missing-native-js-functions-1.2.11.tgz", "resolved": "https://registry.npmjs.org/missing-native-js-functions/-/missing-native-js-functions-1.2.13.tgz",
"integrity": "sha512-U97IscNBL4Wg9adYjEBT46Hb0Ld5dPT8vbdwFX+TNzGrFQCc4WqoGAZouaLNFwUqxzzHZ9DVg59unwnQyeIIQg==" "integrity": "sha512-1RAArfUkrGkj5N3xJVW251F2PvfP2ozAcxsLLDR6uiiAixTP5Abh8zzGMadepbqgiHC0FGlTSAUNbh9abN4Osg=="
}, },
"node_modules/mkdirp": { "node_modules/mkdirp": {
"version": "1.0.4", "version": "1.0.4",
@ -13735,9 +13735,9 @@
} }
}, },
"missing-native-js-functions": { "missing-native-js-functions": {
"version": "1.2.11", "version": "1.2.13",
"resolved": "https://registry.npmjs.org/missing-native-js-functions/-/missing-native-js-functions-1.2.11.tgz", "resolved": "https://registry.npmjs.org/missing-native-js-functions/-/missing-native-js-functions-1.2.13.tgz",
"integrity": "sha512-U97IscNBL4Wg9adYjEBT46Hb0Ld5dPT8vbdwFX+TNzGrFQCc4WqoGAZouaLNFwUqxzzHZ9DVg59unwnQyeIIQg==" "integrity": "sha512-1RAArfUkrGkj5N3xJVW251F2PvfP2ozAcxsLLDR6uiiAixTP5Abh8zzGMadepbqgiHC0FGlTSAUNbh9abN4Osg=="
}, },
"mkdirp": { "mkdirp": {
"version": "1.0.4", "version": "1.0.4",

View File

@ -44,7 +44,7 @@
"env-paths": "^2.2.1", "env-paths": "^2.2.1",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"lambert-server": "^1.2.10", "lambert-server": "^1.2.10",
"missing-native-js-functions": "^1.2.11", "missing-native-js-functions": "^1.2.13",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"patch-package": "^6.4.7", "patch-package": "^6.4.7",
"pg": "^8.7.1", "pg": "^8.7.1",

View File

@ -42,17 +42,17 @@ export enum AuditLogEvents {
} }
@Entity("audit_logs") @Entity("audit_logs")
export class AuditLogEntry extends BaseClass { export class AuditLog extends BaseClass {
@JoinColumn({ name: "target_id" }) @JoinColumn({ name: "target_id" })
@ManyToOne(() => User) @ManyToOne(() => User)
target?: User; target?: User;
@Column({ nullable: true }) @Column({ nullable: true })
@RelationId((auditlog: AuditLogEntry) => auditlog.user) @RelationId((auditlog: AuditLog) => auditlog.user)
user_id: string; user_id: string;
@JoinColumn({ name: "user_id" }) @JoinColumn({ name: "user_id" })
@ManyToOne(() => User) @ManyToOne(() => User, (user: User) => user.id)
user: User; user: User;
@Column({ @Column({

View File

@ -8,19 +8,19 @@ import "missing-native-js-functions";
export class BaseClass extends BaseEntity { export class BaseClass extends BaseEntity {
@PrimaryColumn() @PrimaryColumn()
id: string = Snowflake.generate(); id: string;
// @ts-ignore // @ts-ignore
constructor(public props?: any) { constructor(private props?: any) {
super(); super();
this.assign(props); this.assign(props);
} }
get construct(): any { private get construct(): any {
return this.constructor; return this.constructor;
} }
get metadata() { private get metadata() {
return this.construct.getRepository().metadata as EntityMetadata; return this.construct.getRepository().metadata as EntityMetadata;
} }
@ -48,6 +48,8 @@ export class BaseClass extends BaseEntity {
this[key] = props[key]; this[key] = props[key];
} }
} }
if (!this.id) this.id = Snowflake.generate();
} }
@BeforeUpdate() @BeforeUpdate()

View File

@ -12,6 +12,7 @@ import {
} from "../interfaces"; } from "../interfaces";
import { HTTPError } from "lambert-server"; import { HTTPError } from "lambert-server";
import { Role } from "./Role"; import { Role } from "./Role";
import { ReadState } from "./ReadState";
@Entity("members") @Entity("members")
export class Member extends BaseClass { export class Member extends BaseClass {
@ -53,8 +54,8 @@ export class Member extends BaseClass {
settings: UserGuildSettings; settings: UserGuildSettings;
// TODO: update // TODO: update
@Column({ type: "simple-json" }) // @Column({ type: "simple-json" })
read_state: Record<string, string | null>; // 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({ id: user_id, guild: { id: guild_id } })) return true; 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.insert({
...member, ...member,
roles: undefined, roles: undefined,
read_state: {}, // read_state: {},
settings: { settings: {
channel_overrides: [], channel_overrides: [],
message_notifications: 0, message_notifications: 0,

View File

@ -139,7 +139,7 @@ export class Message extends BaseClass {
reactions: Reaction[]; reactions: Reaction[];
@Column({ type: "text", nullable: true }) @Column({ type: "text", nullable: true })
nonce?: string | number; nonce?: string;
@Column({ nullable: true }) @Column({ nullable: true })
pinned?: boolean; pinned?: boolean;

View File

@ -4,33 +4,53 @@ import { BitField } from "../util/BitField";
import { Relationship } from "./Relationship"; import { Relationship } from "./Relationship";
import { ConnectedAccount } from "./ConnectedAccount"; import { ConnectedAccount } from "./ConnectedAccount";
import { HTTPError } from "lambert-server"; import { HTTPError } from "lambert-server";
import { Channel } from "./Channel";
type PublicUserKeys = export enum PublicUserEnum {
| "username" username,
| "discriminator" discriminator,
| "id" id,
| "public_flags" public_flags,
| "avatar" avatar,
| "accent_color" accent_color,
| "banner" banner,
| "bio" bio,
| "bot"; bot,
export const PublicUserProjection: PublicUserKeys[] = [ }
"username", export type PublicUserKeys = keyof typeof PublicUserEnum;
"discriminator",
"id", export enum PrivateUserEnum {
"public_flags", flags,
"avatar", mfa_enabled,
"accent_color", email,
"banner", phone,
"bio", verified,
"bot", 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 // Private user data that should never get sent to the client
export type PublicUser = Pick<User, PublicUserKeys>; 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") @Entity("users")
export class User extends BaseClass { export class User extends BaseClass {
@Column() @Column()

View File

@ -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);
}
});