feat: change request for images

This commit is contained in:
TopchetoEU 2023-06-29 17:19:50 +03:00
parent 02b35cc230
commit f1cc795eb2

View File

@ -34,12 +34,10 @@ export default class ImageRouter extends AppRouter {
return ImageRouter.deserialize(image); return ImageRouter.deserialize(image);
} }
@rest('GET', '/feed')
@rest('GET', '/mine') async self(@page() page: Page) {
async mine(@page() page: Page, @jwt(v => v.salt, true) @auth() jwt: JWTPayload) { const res = await page.apply(this.images.find({})).toArray();
const user = await this.users.findOne({ username: jwt.name }); if (!res) throw new HttpError('User not found.');
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.deserialize(v)); return res.map(v => ImageRouter.deserialize(v));
} }
@ -100,9 +98,21 @@ export default class ImageRouter extends AppRouter {
throw e; 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<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' } ] });
} }
} }