From 02b35cc2307e842f1b92ad36b776bd71c3fca73a Mon Sep 17 00:00:00 2001 From: topchetoeu <36534413+TopchetoEU@users.noreply.github.com> Date: Thu, 29 Jun 2023 13:00:29 +0300 Subject: [PATCH] some minor changes --- .gitignore | 2 +- package.json | 5 ----- src/routers/ImageRouter.ts | 13 +++++++------ src/routers/RootRouter.ts | 2 +- src/routers/UserRouter.ts | 8 ++++++-- src/server/serialize.ts | 2 +- 6 files changed, 16 insertions(+), 16 deletions(-) delete mode 100644 package.json diff --git a/.gitignore b/.gitignore index 3d1de9d..2c083b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ .vscode/ node_modules/ -keys/ +cwd/ yarn.lock pacakage.json package.lock.json \ No newline at end of file diff --git a/package.json b/package.json deleted file mode 100644 index c653748..0000000 --- a/package.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "dependencies": { - "typescript": "^5.1.5" - } -} diff --git a/src/routers/ImageRouter.ts b/src/routers/ImageRouter.ts index 3a132bf..ae06489 100644 --- a/src/routers/ImageRouter.ts +++ b/src/routers/ImageRouter.ts @@ -13,7 +13,7 @@ import { convert } from "../server/decorators/schema.ts"; import { now } from "../utils/utils.ts"; export default class ImageRouter extends AppRouter { - public static serialize(image: Image) { + public static deserialize(image: Image) { return { author: image.author, created: image.created, @@ -32,7 +32,7 @@ export default class ImageRouter extends AppRouter { image.visibility === Visibility.Private && image.author !== jwt?.name ) throw new HttpError("Image doesn't exist."); - return ImageRouter.serialize(image); + return ImageRouter.deserialize(image); } @rest('GET', '/mine') @@ -40,7 +40,7 @@ export default class ImageRouter extends AppRouter { const user = await this.users.findOne({ username: jwt.name }); if (!user) throw new HttpError("You don't exist."); const res = await page.apply(this.images.find({ _id: { $in: user.images } })).toArray(); - return res.map(v => ImageRouter.serialize(v)); + return res.map(v => ImageRouter.deserialize(v)); } @rest('POST', '/upload') @@ -91,8 +91,9 @@ export default class ImageRouter extends AppRouter { // Write to DB try { - this.images.insertOne(img); - return ImageRouter.serialize(img); + await this.images.insertOne(img); + await this.users.updateOne({ username: user.username }, { $push: { images: img._id } }); + return ImageRouter.deserialize(img); } catch (e) { await Deno.remove(`images/${img._id}.${ext}`); @@ -100,7 +101,7 @@ export default class ImageRouter extends AppRouter { } } - public constructor(private salt: string, private images: Collection, private users: Collection) { + public constructor(private images: Collection, private users: Collection) { super(); users.createIndexes({ indexes: [ { key: { username: 1 }, name: 'Username Index' } ] }); } diff --git a/src/routers/RootRouter.ts b/src/routers/RootRouter.ts index e7437e2..fde9c05 100644 --- a/src/routers/RootRouter.ts +++ b/src/routers/RootRouter.ts @@ -24,6 +24,6 @@ export class RootRouter extends AppRouter { const images = db.collection('images'); this.users = new UserRouter(salt, users); - this.images = new ImageRouter(salt, images, users); + this.images = new ImageRouter(images, users); } } diff --git a/src/routers/UserRouter.ts b/src/routers/UserRouter.ts index 11017b8..2f60f20 100644 --- a/src/routers/UserRouter.ts +++ b/src/routers/UserRouter.ts @@ -19,13 +19,17 @@ export interface LoginRequest { } export default class UserRouter extends AppRouter { + public static deserialize(user: User) { + return { username: user.username, images: user.images }; + } + @rest('GET', '/') async get(@schema('string') username: string) { const res = await this.users.findOne({ username }); if (res === undefined) throw new HttpError('User not found.'); - return { username: res.username }; + return UserRouter.deserialize(res); } @rest('GET', '/self') async self(@jwt(self => self.salt, true) @auth() auth: JWTPayload) { @@ -34,7 +38,7 @@ export default class UserRouter extends AppRouter { if (res === undefined) throw new HttpError('User not found.'); - return { username: res.username }; + return UserRouter.deserialize(res); } @rest('POST', '/signup') diff --git a/src/server/serialize.ts b/src/server/serialize.ts index ffada28..44699a6 100644 --- a/src/server/serialize.ts +++ b/src/server/serialize.ts @@ -24,7 +24,7 @@ export default async function serialize(val: unknown, depth = 16): Promise