diff --git a/src/api/private/me/history/history.controller.spec.ts b/src/api/private/me/history/history.controller.spec.ts new file mode 100644 index 000000000..f52891cae --- /dev/null +++ b/src/api/private/me/history/history.controller.spec.ts @@ -0,0 +1,78 @@ +/* + * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { Test, TestingModule } from '@nestjs/testing'; +import { HistoryController } from './history.controller'; +import { LoggerModule } from '../../../../logger/logger.module'; +import { UsersModule } from '../../../../users/users.module'; +import { HistoryModule } from '../../../../history/history.module'; +import { NotesModule } from '../../../../notes/notes.module'; +import { getRepositoryToken } from '@nestjs/typeorm'; +import { User } from '../../../../users/user.entity'; +import { Note } from '../../../../notes/note.entity'; +import { AuthToken } from '../../../../auth/auth-token.entity'; +import { Identity } from '../../../../users/identity.entity'; +import { AuthorColor } from '../../../../notes/author-color.entity'; +import { Authorship } from '../../../../revisions/authorship.entity'; +import { Revision } from '../../../../revisions/revision.entity'; +import { Tag } from '../../../../notes/tag.entity'; +import { HistoryEntry } from '../../../../history/history-entry.entity'; +import { NoteGroupPermission } from '../../../../permissions/note-group-permission.entity'; +import { NoteUserPermission } from '../../../../permissions/note-user-permission.entity'; +import { Group } from '../../../../groups/group.entity'; +import { ConfigModule } from '@nestjs/config'; +import appConfigMock from '../../../../config/app.config.mock'; + +describe('HistoryController', () => { + let controller: HistoryController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [HistoryController], + imports: [ + UsersModule, + HistoryModule, + NotesModule, + LoggerModule, + ConfigModule.forRoot({ + isGlobal: true, + load: [appConfigMock], + }), + ], + }) + .overrideProvider(getRepositoryToken(User)) + .useValue({}) + .overrideProvider(getRepositoryToken(Note)) + .useValue({}) + .overrideProvider(getRepositoryToken(AuthToken)) + .useValue({}) + .overrideProvider(getRepositoryToken(Identity)) + .useValue({}) + .overrideProvider(getRepositoryToken(AuthorColor)) + .useValue({}) + .overrideProvider(getRepositoryToken(Authorship)) + .useValue({}) + .overrideProvider(getRepositoryToken(Revision)) + .useValue({}) + .overrideProvider(getRepositoryToken(Tag)) + .useValue({}) + .overrideProvider(getRepositoryToken(HistoryEntry)) + .useValue({}) + .overrideProvider(getRepositoryToken(NoteGroupPermission)) + .useValue({}) + .overrideProvider(getRepositoryToken(NoteUserPermission)) + .useValue({}) + .overrideProvider(getRepositoryToken(Group)) + .useValue({}) + .compile(); + + controller = module.get(HistoryController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/src/api/private/me/history/history.controller.ts b/src/api/private/me/history/history.controller.ts new file mode 100644 index 000000000..85203b78b --- /dev/null +++ b/src/api/private/me/history/history.controller.ts @@ -0,0 +1,127 @@ +/* + * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { + Body, + Controller, + Delete, + Get, + NotFoundException, + Param, + Post, + Put, +} from '@nestjs/common'; +import { ApiTags } from '@nestjs/swagger'; +import { UsersService } from '../../../../users/users.service'; +import { NotesService } from '../../../../notes/notes.service'; +import { HistoryEntryDto } from '../../../../history/history-entry.dto'; +import { NotInDBError } from '../../../../errors/errors'; +import { HistoryEntryCreationDto } from '../../../../history/history-entry-creation.dto'; +import { HistoryEntryUpdateDto } from '../../../../history/history-entry-update.dto'; +import { ConsoleLoggerService } from '../../../../logger/console-logger.service'; +import { HistoryService } from '../../../../history/history.service'; + +@ApiTags('history') +@Controller('/me/history') +export class HistoryController { + constructor( + private readonly logger: ConsoleLoggerService, + private historyService: HistoryService, + private userService: UsersService, + private noteService: NotesService, + ) { + this.logger.setContext(HistoryController.name); + } + + @Get() + async getHistory(): Promise { + // ToDo: use actual user here + try { + const user = await this.userService.getUserByUsername('hardcoded'); + const foundEntries = await this.historyService.getEntriesByUser(user); + return foundEntries.map((entry) => + this.historyService.toHistoryEntryDto(entry), + ); + } catch (e) { + if (e instanceof NotInDBError) { + throw new NotFoundException(e.message); + } + throw e; + } + } + + @Post() + async setHistory( + @Body('history') history: HistoryEntryCreationDto[], + ): Promise { + try { + // ToDo: use actual user here + const user = await this.userService.getUserByUsername('hardcoded'); + await this.historyService.deleteHistory(user); + for (const historyEntry of history) { + const note = await this.noteService.getNoteByIdOrAlias( + historyEntry.note, + ); + await this.historyService.createOrUpdateHistoryEntry(note, user); + } + } catch (e) { + if (e instanceof NotInDBError) { + throw new NotFoundException(e.message); + } + throw e; + } + } + + @Delete() + async deleteHistory(): Promise { + try { + // ToDo: use actual user here + const user = await this.userService.getUserByUsername('hardcoded'); + await this.historyService.deleteHistory(user); + } catch (e) { + if (e instanceof NotInDBError) { + throw new NotFoundException(e.message); + } + throw e; + } + } + + @Put(':note') + async updateHistoryEntry( + @Param('note') noteId: string, + @Body() entryUpdateDto: HistoryEntryUpdateDto, + ): Promise { + try { + // ToDo: use actual user here + const user = await this.userService.getUserByUsername('hardcoded'); + const newEntry = await this.historyService.updateHistoryEntry( + noteId, + user, + entryUpdateDto, + ); + return this.historyService.toHistoryEntryDto(newEntry); + } catch (e) { + if (e instanceof NotInDBError) { + throw new NotFoundException(e.message); + } + throw e; + } + } + + @Delete(':note') + async deleteHistoryEntry(@Param('note') noteId: string): Promise { + try { + // ToDo: use actual user here + const user = await this.userService.getUserByUsername('hardcoded'); + await this.historyService.deleteHistoryEntry(noteId, user); + } catch (e) { + if (e instanceof NotInDBError) { + throw new NotFoundException(e.message); + } + throw e; + } + } +} diff --git a/src/api/private/private-api.module.ts b/src/api/private/private-api.module.ts index 74dc95fe8..3d6d8b4a2 100644 --- a/src/api/private/private-api.module.ts +++ b/src/api/private/private-api.module.ts @@ -9,9 +9,12 @@ import { TokensController } from './tokens/tokens.controller'; import { LoggerModule } from '../../logger/logger.module'; import { UsersModule } from '../../users/users.module'; import { AuthModule } from '../../auth/auth.module'; +import { HistoryController } from './me/history/history.controller'; +import { HistoryModule } from '../../history/history.module'; +import { NotesModule } from '../../notes/notes.module'; @Module({ - imports: [LoggerModule, UsersModule, AuthModule], - controllers: [TokensController], + imports: [LoggerModule, UsersModule, AuthModule, HistoryModule, NotesModule], + controllers: [TokensController, HistoryController], }) export class PrivateApiModule {} diff --git a/src/history/history-entry-creation.dto.ts b/src/history/history-entry-creation.dto.ts new file mode 100644 index 000000000..aa565ca1d --- /dev/null +++ b/src/history/history-entry-creation.dto.ts @@ -0,0 +1,15 @@ +/* + * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { IsString } from 'class-validator'; + +export class HistoryEntryCreationDto { + /** + * ID or Alias of the note + */ + @IsString() + note: string; +}