diff --git a/src/routers/ImageRouter.ts b/src/routers/ImageRouter.ts index ae06489..a716850 100644 --- a/src/routers/ImageRouter.ts +++ b/src/routers/ImageRouter.ts @@ -34,12 +34,10 @@ export default class ImageRouter extends AppRouter { return ImageRouter.deserialize(image); } - - @rest('GET', '/mine') - async mine(@page() page: Page, @jwt(v => v.salt, true) @auth() jwt: JWTPayload) { - 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(); + @rest('GET', '/feed') + async self(@page() page: Page) { + const res = await page.apply(this.images.find({})).toArray(); + if (!res) throw new HttpError('User not found.'); return res.map(v => ImageRouter.deserialize(v)); } @@ -100,9 +98,21 @@ export default class ImageRouter extends AppRouter { throw e; } } + @rest('POST', '/change') + async change(@body() raw: unknown, @jwt(v => v.salt, true) @auth() jwt: JWTPayload) { + const body = await convert(raw, { id: 'uuid', name: 'string?', visibility: 'number?' }); + const user = await this.users.findOne({ username: jwt.name }); + if (!user) throw new HttpError("You don't exist."); + + const img = await this.images.findOne({ _id: body.id }); + if (!img) throw new HttpError("Image doesn't exist."); + if (user.username !== img.author) throw new HttpError("You don't own the image."); + + await this.images.updateOne({ _id: body.id }, { $set: { name: body.name, visibility: body.visibility } }); + return ImageRouter.deserialize(img); + } public constructor(private images: Collection, private users: Collection) { super(); - users.createIndexes({ indexes: [ { key: { username: 1 }, name: 'Username Index' } ] }); } } \ No newline at end of file