feat: change request for images
This commit is contained in:
parent
02b35cc230
commit
f1cc795eb2
@ -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<Image>, private users: Collection<User>) {
|
||||
super();
|
||||
users.createIndexes({ indexes: [ { key: { username: 1 }, name: 'Username Index' } ] });
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user