Remove unnecessary exception handling in private notes.controller.ts

and change noteIdOrAlias to Note as parameter

Signed-off-by: Yannick Bungers <git@innay.de>
This commit is contained in:
Yannick Bungers 2021-10-18 21:12:33 +02:00 committed by David Mehren
parent 7017d85b47
commit 85dc2cd02d
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3

View file

@ -73,18 +73,11 @@ export class NotesController {
@Param('noteIdOrAlias', GetNotePipe) note: Note, @Param('noteIdOrAlias', GetNotePipe) note: Note,
@RequestUser() user: User, @RequestUser() user: User,
): Promise<MediaUploadDto[]> { ): Promise<MediaUploadDto[]> {
try { if (!this.permissionsService.mayRead(user, note)) {
if (!this.permissionsService.mayRead(user, note)) { throw new UnauthorizedException('Reading note denied!');
throw new UnauthorizedException('Reading note denied!');
}
const media = await this.mediaService.listUploadsByNote(note);
return media.map((media) => this.mediaService.toMediaUploadDto(media));
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
throw e;
} }
const media = await this.mediaService.listUploadsByNote(note);
return media.map((media) => this.mediaService.toMediaUploadDto(media));
} }
@Post() @Post()
@ -135,28 +128,21 @@ export class NotesController {
@Param('noteIdOrAlias', GetNotePipe) note: Note, @Param('noteIdOrAlias', GetNotePipe) note: Note,
@Body() noteMediaDeletionDto: NoteMediaDeletionDto, @Body() noteMediaDeletionDto: NoteMediaDeletionDto,
): Promise<void> { ): Promise<void> {
try { if (!this.permissionsService.isOwner(user, note)) {
if (!this.permissionsService.isOwner(user, note)) { throw new UnauthorizedException('Deleting note denied!');
throw new UnauthorizedException('Deleting note denied!');
}
const mediaUploads = await this.mediaService.listUploadsByNote(note);
for (const mediaUpload of mediaUploads) {
if (!noteMediaDeletionDto.keepMedia) {
await this.mediaService.deleteFile(mediaUpload);
} else {
await this.mediaService.removeNoteFromMediaUpload(mediaUpload);
}
}
this.logger.debug('Deleting note: ' + note.id, 'deleteNote');
await this.noteService.deleteNote(note);
this.logger.debug('Successfully deleted ' + note.id, 'deleteNote');
return;
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
throw e;
} }
const mediaUploads = await this.mediaService.listUploadsByNote(note);
for (const mediaUpload of mediaUploads) {
if (!noteMediaDeletionDto.keepMedia) {
await this.mediaService.deleteFile(mediaUpload);
} else {
await this.mediaService.removeNoteFromMediaUpload(mediaUpload);
}
}
this.logger.debug('Deleting note: ' + note.id, 'deleteNote');
await this.noteService.deleteNote(note);
this.logger.debug('Successfully deleted ' + note.id, 'deleteNote');
return;
} }
@Get(':noteIdOrAlias/revisions') @Get(':noteIdOrAlias/revisions')
@ -164,54 +150,36 @@ export class NotesController {
@RequestUser() user: User, @RequestUser() user: User,
@Param('noteIdOrAlias', GetNotePipe) note: Note, @Param('noteIdOrAlias', GetNotePipe) note: Note,
): Promise<RevisionMetadataDto[]> { ): Promise<RevisionMetadataDto[]> {
try { if (!this.permissionsService.mayRead(user, note)) {
if (!this.permissionsService.mayRead(user, note)) { throw new UnauthorizedException('Reading note denied!');
throw new UnauthorizedException('Reading note denied!');
}
const revisions = await this.revisionsService.getAllRevisions(note);
return await Promise.all(
revisions.map((revision) =>
this.revisionsService.toRevisionMetadataDto(revision),
),
);
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
throw e;
} }
const revisions = await this.revisionsService.getAllRevisions(note);
return await Promise.all(
revisions.map((revision) =>
this.revisionsService.toRevisionMetadataDto(revision),
),
);
} }
@Delete(':noteIdOrAlias/revisions') @Delete(':noteIdOrAlias/revisions')
@HttpCode(204) @HttpCode(204)
async purgeNoteRevisions( async purgeNoteRevisions(
@RequestUser() user: User, @RequestUser() user: User,
@Param('noteIdOrAlias') noteIdOrAlias: string, @Param('noteIdOrAlias', GetNotePipe) note: Note,
): Promise<void> { ): Promise<void> {
try { if (!this.permissionsService.mayRead(user, note)) {
const note = await this.noteService.getNoteByIdOrAlias(noteIdOrAlias); throw new UnauthorizedException('Reading note denied!');
if (!this.permissionsService.mayRead(user, note)) {
throw new UnauthorizedException('Reading note denied!');
}
this.logger.debug(
'Purging history of note: ' + noteIdOrAlias,
'purgeNoteRevisions',
);
await this.revisionsService.purgeRevisions(note);
this.logger.debug(
'Successfully purged history of note ' + noteIdOrAlias,
'purgeNoteRevisions',
);
return;
} catch (e) {
if (e instanceof NotInDBError) {
throw new NotFoundException(e.message);
}
if (e instanceof ForbiddenIdError) {
throw new BadRequestException(e.message);
}
throw e;
} }
this.logger.debug(
'Purging history of note: ' + note.id,
'purgeNoteRevisions',
);
await this.revisionsService.purgeRevisions(note);
this.logger.debug(
'Successfully purged history of note ' + note.id,
'purgeNoteRevisions',
);
return;
} }
@Get(':noteIdOrAlias/revisions/:revisionId') @Get(':noteIdOrAlias/revisions/:revisionId')