2021-01-05 16:12:38 -05:00
|
|
|
/*
|
2021-01-06 15:36:07 -05:00
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
2021-01-05 16:12:38 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2020-07-26 15:00:18 -04:00
|
|
|
import {
|
|
|
|
Body,
|
|
|
|
Controller,
|
|
|
|
Delete,
|
|
|
|
Get,
|
|
|
|
Header,
|
2021-01-09 16:38:10 -05:00
|
|
|
NotFoundException,
|
2020-07-26 15:00:18 -04:00
|
|
|
Param,
|
|
|
|
Post,
|
|
|
|
Put,
|
2021-01-15 12:53:09 -05:00
|
|
|
Request,
|
|
|
|
UseGuards,
|
2020-07-26 15:00:18 -04:00
|
|
|
} from '@nestjs/common';
|
2021-01-09 16:38:10 -05:00
|
|
|
import { NotInDBError } from '../../../errors/errors';
|
2020-09-27 15:48:42 -04:00
|
|
|
import { ConsoleLoggerService } from '../../../logger/console-logger.service';
|
2020-07-26 15:00:18 -04:00
|
|
|
import { NotePermissionsUpdateDto } from '../../../notes/note-permissions.dto';
|
|
|
|
import { NotesService } from '../../../notes/notes.service';
|
|
|
|
import { RevisionsService } from '../../../revisions/revisions.service';
|
2020-09-27 10:16:07 -04:00
|
|
|
import { MarkdownBody } from '../../utils/markdownbody-decorator';
|
2021-01-15 12:53:09 -05:00
|
|
|
import { TokenAuthGuard } from '../../../auth/token-auth.guard';
|
2020-07-25 14:13:06 -04:00
|
|
|
|
|
|
|
@Controller('notes')
|
2020-07-26 15:00:18 -04:00
|
|
|
export class NotesController {
|
|
|
|
constructor(
|
2020-09-27 15:48:42 -04:00
|
|
|
private readonly logger: ConsoleLoggerService,
|
2020-07-26 15:00:18 -04:00
|
|
|
private noteService: NotesService,
|
|
|
|
private revisionsService: RevisionsService,
|
2020-09-27 15:48:42 -04:00
|
|
|
) {
|
2020-10-03 09:53:30 -04:00
|
|
|
this.logger.setContext(NotesController.name);
|
2020-09-27 15:48:42 -04:00
|
|
|
}
|
2020-07-26 15:00:18 -04:00
|
|
|
|
2021-01-15 12:53:09 -05:00
|
|
|
@UseGuards(TokenAuthGuard)
|
2020-07-26 15:00:18 -04:00
|
|
|
@Post()
|
2021-01-15 12:53:09 -05:00
|
|
|
async createNote(@Request() req, @MarkdownBody() text: string) {
|
|
|
|
// ToDo: provide user for createNoteDto
|
2020-09-27 10:16:07 -04:00
|
|
|
this.logger.debug('Got raw markdown:\n' + text);
|
|
|
|
return this.noteService.createNoteDto(text);
|
2020-07-26 15:00:18 -04:00
|
|
|
}
|
|
|
|
|
2021-01-15 12:53:09 -05:00
|
|
|
@UseGuards(TokenAuthGuard)
|
2020-07-26 15:00:18 -04:00
|
|
|
@Get(':noteIdOrAlias')
|
2021-01-15 12:53:09 -05:00
|
|
|
async getNote(@Request() req, @Param('noteIdOrAlias') noteIdOrAlias: string) {
|
|
|
|
// ToDo: check if user is allowed to view this note
|
2021-01-09 16:38:10 -05:00
|
|
|
try {
|
|
|
|
return await this.noteService.getNoteDtoByIdOrAlias(noteIdOrAlias);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof NotInDBError) {
|
|
|
|
throw new NotFoundException(e.message);
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
2020-07-26 15:00:18 -04:00
|
|
|
}
|
|
|
|
|
2021-01-15 12:53:09 -05:00
|
|
|
@UseGuards(TokenAuthGuard)
|
2020-07-26 15:00:18 -04:00
|
|
|
@Post(':noteAlias')
|
2020-09-22 12:31:20 -04:00
|
|
|
async createNamedNote(
|
2021-01-15 12:53:09 -05:00
|
|
|
@Request() req,
|
2020-07-26 15:00:18 -04:00
|
|
|
@Param('noteAlias') noteAlias: string,
|
2020-09-27 10:16:07 -04:00
|
|
|
@MarkdownBody() text: string,
|
2020-07-26 15:00:18 -04:00
|
|
|
) {
|
2021-01-15 12:53:09 -05:00
|
|
|
// ToDo: check if user is allowed to view this note
|
2020-09-27 10:16:07 -04:00
|
|
|
this.logger.debug('Got raw markdown:\n' + text);
|
|
|
|
return this.noteService.createNoteDto(text, noteAlias);
|
2020-07-26 15:00:18 -04:00
|
|
|
}
|
|
|
|
|
2021-01-15 12:53:09 -05:00
|
|
|
@UseGuards(TokenAuthGuard)
|
2020-07-26 15:00:18 -04:00
|
|
|
@Delete(':noteIdOrAlias')
|
2021-01-15 12:53:09 -05:00
|
|
|
async deleteNote(
|
|
|
|
@Request() req,
|
|
|
|
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
|
|
|
) {
|
|
|
|
// ToDo: check if user is allowed to delete this note
|
2020-09-22 14:06:56 -04:00
|
|
|
this.logger.debug('Deleting note: ' + noteIdOrAlias);
|
2021-01-09 16:38:10 -05:00
|
|
|
try {
|
|
|
|
await this.noteService.deleteNoteByIdOrAlias(noteIdOrAlias);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof NotInDBError) {
|
|
|
|
throw new NotFoundException(e.message);
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
2020-09-22 14:06:56 -04:00
|
|
|
this.logger.debug('Successfully deleted ' + noteIdOrAlias);
|
|
|
|
return;
|
2020-07-26 15:00:18 -04:00
|
|
|
}
|
|
|
|
|
2021-01-15 12:53:09 -05:00
|
|
|
@UseGuards(TokenAuthGuard)
|
2020-07-26 15:00:18 -04:00
|
|
|
@Put(':noteIdOrAlias')
|
2020-09-22 14:06:56 -04:00
|
|
|
async updateNote(
|
2021-01-15 12:53:09 -05:00
|
|
|
@Request() req,
|
2020-07-26 15:00:18 -04:00
|
|
|
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
2020-09-27 10:16:07 -04:00
|
|
|
@MarkdownBody() text: string,
|
2020-07-26 15:00:18 -04:00
|
|
|
) {
|
2021-01-15 12:53:09 -05:00
|
|
|
// ToDo: check if user is allowed to change this note
|
2020-09-27 10:16:07 -04:00
|
|
|
this.logger.debug('Got raw markdown:\n' + text);
|
2021-01-09 16:38:10 -05:00
|
|
|
try {
|
|
|
|
return await this.noteService.updateNoteByIdOrAlias(noteIdOrAlias, text);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof NotInDBError) {
|
|
|
|
throw new NotFoundException(e.message);
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
2020-07-26 15:00:18 -04:00
|
|
|
}
|
|
|
|
|
2021-01-15 12:53:09 -05:00
|
|
|
@UseGuards(TokenAuthGuard)
|
2020-07-26 15:00:18 -04:00
|
|
|
@Get(':noteIdOrAlias/content')
|
|
|
|
@Header('content-type', 'text/markdown')
|
2021-01-15 12:53:09 -05:00
|
|
|
async getNoteContent(
|
|
|
|
@Request() req,
|
|
|
|
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
|
|
|
) {
|
|
|
|
// ToDo: check if user is allowed to view this notes content
|
2021-01-09 16:38:10 -05:00
|
|
|
try {
|
|
|
|
return await this.noteService.getNoteContent(noteIdOrAlias);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof NotInDBError) {
|
|
|
|
throw new NotFoundException(e.message);
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
2020-07-26 15:00:18 -04:00
|
|
|
}
|
|
|
|
|
2021-01-15 12:53:09 -05:00
|
|
|
@UseGuards(TokenAuthGuard)
|
2020-07-26 15:00:18 -04:00
|
|
|
@Get(':noteIdOrAlias/metadata')
|
2021-01-15 12:53:09 -05:00
|
|
|
async getNoteMetadata(
|
|
|
|
@Request() req,
|
|
|
|
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
|
|
|
) {
|
|
|
|
// ToDo: check if user is allowed to view this notes metadata
|
2021-01-09 16:38:10 -05:00
|
|
|
try {
|
|
|
|
return await this.noteService.getNoteMetadata(noteIdOrAlias);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof NotInDBError) {
|
|
|
|
throw new NotFoundException(e.message);
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
2020-07-26 15:00:18 -04:00
|
|
|
}
|
|
|
|
|
2021-01-15 12:53:09 -05:00
|
|
|
@UseGuards(TokenAuthGuard)
|
2021-01-10 14:25:28 -05:00
|
|
|
@Put(':noteIdOrAlias/metadata/permissions')
|
2021-01-09 16:38:10 -05:00
|
|
|
async updateNotePermissions(
|
2021-01-15 12:53:09 -05:00
|
|
|
@Request() req,
|
2020-07-26 15:00:18 -04:00
|
|
|
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
|
|
|
@Body() updateDto: NotePermissionsUpdateDto,
|
|
|
|
) {
|
2021-01-15 12:53:09 -05:00
|
|
|
// ToDo: check if user is allowed to view this notes permissions
|
2021-01-09 16:38:10 -05:00
|
|
|
try {
|
|
|
|
return await this.noteService.updateNotePermissions(
|
|
|
|
noteIdOrAlias,
|
|
|
|
updateDto,
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof NotInDBError) {
|
|
|
|
throw new NotFoundException(e.message);
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
2020-07-26 15:00:18 -04:00
|
|
|
}
|
|
|
|
|
2021-01-15 12:53:09 -05:00
|
|
|
@UseGuards(TokenAuthGuard)
|
2020-07-26 15:00:18 -04:00
|
|
|
@Get(':noteIdOrAlias/revisions')
|
2021-01-15 12:53:09 -05:00
|
|
|
async getNoteRevisions(
|
|
|
|
@Request() req,
|
|
|
|
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
|
|
|
) {
|
|
|
|
// ToDo: check if user is allowed to view this notes revisions
|
2021-01-09 16:38:10 -05:00
|
|
|
try {
|
|
|
|
return await this.revisionsService.getNoteRevisionMetadatas(
|
|
|
|
noteIdOrAlias,
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof NotInDBError) {
|
|
|
|
throw new NotFoundException(e.message);
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
2020-07-26 15:00:18 -04:00
|
|
|
}
|
|
|
|
|
2021-01-15 12:53:09 -05:00
|
|
|
@UseGuards(TokenAuthGuard)
|
2020-07-26 15:00:18 -04:00
|
|
|
@Get(':noteIdOrAlias/revisions/:revisionId')
|
2021-01-09 16:38:10 -05:00
|
|
|
async getNoteRevision(
|
2021-01-15 12:53:09 -05:00
|
|
|
@Request() req,
|
2020-07-26 15:00:18 -04:00
|
|
|
@Param('noteIdOrAlias') noteIdOrAlias: string,
|
2020-09-22 15:08:14 -04:00
|
|
|
@Param('revisionId') revisionId: number,
|
2020-07-26 15:00:18 -04:00
|
|
|
) {
|
2021-01-15 12:53:09 -05:00
|
|
|
// ToDo: check if user is allowed to view this notes revision
|
2021-01-09 16:38:10 -05:00
|
|
|
try {
|
|
|
|
return await this.revisionsService.getNoteRevision(
|
|
|
|
noteIdOrAlias,
|
|
|
|
revisionId,
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof NotInDBError) {
|
|
|
|
throw new NotFoundException(e.message);
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
2020-07-26 15:00:18 -04:00
|
|
|
}
|
|
|
|
}
|