some minor changes

This commit is contained in:
TopchetoEU 2023-06-29 13:00:29 +03:00
parent fba42d96c0
commit 02b35cc230
6 changed files with 16 additions and 16 deletions

2
.gitignore vendored
View File

@ -1,6 +1,6 @@
.vscode/ .vscode/
node_modules/ node_modules/
keys/ cwd/
yarn.lock yarn.lock
pacakage.json pacakage.json
package.lock.json package.lock.json

View File

@ -1,5 +0,0 @@
{
"dependencies": {
"typescript": "^5.1.5"
}
}

View File

@ -13,7 +13,7 @@ import { convert } from "../server/decorators/schema.ts";
import { now } from "../utils/utils.ts"; import { now } from "../utils/utils.ts";
export default class ImageRouter extends AppRouter { export default class ImageRouter extends AppRouter {
public static serialize(image: Image) { public static deserialize(image: Image) {
return { return {
author: image.author, author: image.author,
created: image.created, created: image.created,
@ -32,7 +32,7 @@ export default class ImageRouter extends AppRouter {
image.visibility === Visibility.Private && image.author !== jwt?.name image.visibility === Visibility.Private && image.author !== jwt?.name
) throw new HttpError("Image doesn't exist."); ) throw new HttpError("Image doesn't exist.");
return ImageRouter.serialize(image); return ImageRouter.deserialize(image);
} }
@rest('GET', '/mine') @rest('GET', '/mine')
@ -40,7 +40,7 @@ export default class ImageRouter extends AppRouter {
const user = await this.users.findOne({ username: jwt.name }); const user = await this.users.findOne({ username: jwt.name });
if (!user) throw new HttpError("You don't exist."); if (!user) throw new HttpError("You don't exist.");
const res = await page.apply(this.images.find({ _id: { $in: user.images } })).toArray(); 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') @rest('POST', '/upload')
@ -91,8 +91,9 @@ export default class ImageRouter extends AppRouter {
// Write to DB // Write to DB
try { try {
this.images.insertOne(img); await this.images.insertOne(img);
return ImageRouter.serialize(img); await this.users.updateOne({ username: user.username }, { $push: { images: img._id } });
return ImageRouter.deserialize(img);
} }
catch (e) { catch (e) {
await Deno.remove(`images/${img._id}.${ext}`); 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<Image>, private users: Collection<User>) { public constructor(private images: Collection<Image>, private users: Collection<User>) {
super(); super();
users.createIndexes({ indexes: [ { key: { username: 1 }, name: 'Username Index' } ] }); users.createIndexes({ indexes: [ { key: { username: 1 }, name: 'Username Index' } ] });
} }

View File

@ -24,6 +24,6 @@ export class RootRouter extends AppRouter {
const images = db.collection<Image>('images'); const images = db.collection<Image>('images');
this.users = new UserRouter(salt, users); this.users = new UserRouter(salt, users);
this.images = new ImageRouter(salt, images, users); this.images = new ImageRouter(images, users);
} }
} }

View File

@ -19,13 +19,17 @@ export interface LoginRequest {
} }
export default class UserRouter extends AppRouter { export default class UserRouter extends AppRouter {
public static deserialize(user: User) {
return { username: user.username, images: user.images };
}
@rest('GET', '/') @rest('GET', '/')
async get(@schema('string') username: string) { async get(@schema('string') username: string) {
const res = await this.users.findOne({ username }); const res = await this.users.findOne({ username });
if (res === undefined) throw new HttpError('User not found.'); if (res === undefined) throw new HttpError('User not found.');
return { username: res.username }; return UserRouter.deserialize(res);
} }
@rest('GET', '/self') @rest('GET', '/self')
async self(@jwt(self => self.salt, true) @auth() auth: JWTPayload) { 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.'); if (res === undefined) throw new HttpError('User not found.');
return { username: res.username }; return UserRouter.deserialize(res);
} }
@rest('POST', '/signup') @rest('POST', '/signup')

View File

@ -24,7 +24,7 @@ export default async function serialize(val: unknown, depth = 16): Promise<Reada
if (val instanceof ReadableStream) return val; if (val instanceof ReadableStream) return val;
if (val instanceof Uint8Array) return new Blob([val]).stream(); if (val instanceof Uint8Array) return new Blob([val]).stream();
if (val.toString !== Object.prototype.toString && val.toString instanceof Function) { if (!(val instanceof Array) && val.toString !== Object.prototype.toString && val.toString instanceof Function) {
val = val.toString(); val = val.toString();
} }