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

🐛 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 { 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,

View File

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

Binary file not shown.

14
util/package-lock.json generated
View File

@ -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",

View File

@ -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",

View File

@ -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({

View File

@ -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()

View File

@ -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,

View File

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

View File

@ -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()

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