From 7050c9f13b2b34ed96353f56bf70e4bbd9bd0182 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sun, 13 Feb 2022 19:07:52 +0100 Subject: [PATCH] feat(revision): include edits in dto Signed-off-by: David Mehren --- src/api/private/notes/notes.controller.ts | 2 +- src/api/public/notes/notes.controller.ts | 2 +- src/revisions/edit.service.ts | 24 +++++++++++++++++++++++ src/revisions/revision.dto.ts | 10 +++++++++- src/revisions/revisions.module.ts | 5 +++-- src/revisions/revisions.service.spec.ts | 2 ++ src/revisions/revisions.service.ts | 9 ++++++++- 7 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 src/revisions/edit.service.ts diff --git a/src/api/private/notes/notes.controller.ts b/src/api/private/notes/notes.controller.ts index 83edcb494..59b95dbc0 100644 --- a/src/api/private/notes/notes.controller.ts +++ b/src/api/private/notes/notes.controller.ts @@ -172,7 +172,7 @@ export class NotesController { @RequestNote() note: Note, @Param('revisionId') revisionId: number, ): Promise { - return this.revisionsService.toRevisionDto( + return await this.revisionsService.toRevisionDto( await this.revisionsService.getRevision(note, revisionId), ); } diff --git a/src/api/public/notes/notes.controller.ts b/src/api/public/notes/notes.controller.ts index 68508ca4a..8addb3de7 100644 --- a/src/api/public/notes/notes.controller.ts +++ b/src/api/public/notes/notes.controller.ts @@ -264,7 +264,7 @@ export class NotesController { @RequestNote() note: Note, @Param('revisionId') revisionId: number, ): Promise { - return this.revisionsService.toRevisionDto( + return await this.revisionsService.toRevisionDto( await this.revisionsService.getRevision(note, revisionId), ); } diff --git a/src/revisions/edit.service.ts b/src/revisions/edit.service.ts new file mode 100644 index 000000000..4cdf731fa --- /dev/null +++ b/src/revisions/edit.service.ts @@ -0,0 +1,24 @@ +/* + * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ +import { Injectable } from '@nestjs/common'; + +import { EditDto } from './edit.dto'; +import { Edit } from './edit.entity'; + +@Injectable() +export class EditService { + async toEditDto(edit: Edit): Promise { + const authorUser = await (await edit.author).user; + + return { + username: authorUser ? authorUser.username : null, + startPos: edit.startPos, + endPos: edit.endPos, + createdAt: edit.createdAt, + updatedAt: edit.updatedAt, + }; + } +} diff --git a/src/revisions/revision.dto.ts b/src/revisions/revision.dto.ts index 1b07117d2..4704f9e9a 100644 --- a/src/revisions/revision.dto.ts +++ b/src/revisions/revision.dto.ts @@ -5,9 +5,10 @@ */ import { ApiProperty } from '@nestjs/swagger'; import { Type } from 'class-transformer'; -import { IsDate, IsNumber, IsString } from 'class-validator'; +import { IsDate, IsNumber, IsString, ValidateNested } from 'class-validator'; import { BaseDto } from '../utils/base.dto.'; +import { EditDto } from './edit.dto'; import { Revision } from './revision.entity'; export class RevisionDto extends BaseDto { @@ -42,4 +43,11 @@ export class RevisionDto extends BaseDto { @Type(() => Date) @ApiProperty() createdAt: Date; + + /** + * All edit objects which are used in the revision. + */ + @ValidateNested() + @ApiProperty() + edits: EditDto[]; } diff --git a/src/revisions/revisions.module.ts b/src/revisions/revisions.module.ts index 8e14ac48f..06ebd7c41 100644 --- a/src/revisions/revisions.module.ts +++ b/src/revisions/revisions.module.ts @@ -11,6 +11,7 @@ import { AuthorsModule } from '../authors/authors.module'; import { LoggerModule } from '../logger/logger.module'; import { NotesModule } from '../notes/notes.module'; import { Edit } from './edit.entity'; +import { EditService } from './edit.service'; import { Revision } from './revision.entity'; import { RevisionsService } from './revisions.service'; @@ -22,7 +23,7 @@ import { RevisionsService } from './revisions.service'; ConfigModule, AuthorsModule, ], - providers: [RevisionsService], - exports: [RevisionsService], + providers: [RevisionsService, EditService], + exports: [RevisionsService, EditService], }) export class RevisionsModule {} diff --git a/src/revisions/revisions.service.spec.ts b/src/revisions/revisions.service.spec.ts index 20cbc0fbc..3ee94ed1a 100644 --- a/src/revisions/revisions.service.spec.ts +++ b/src/revisions/revisions.service.spec.ts @@ -25,6 +25,7 @@ import { NoteUserPermission } from '../permissions/note-user-permission.entity'; import { Session } from '../users/session.entity'; import { User } from '../users/user.entity'; import { Edit } from './edit.entity'; +import { EditService } from './edit.service'; import { Revision } from './revision.entity'; import { RevisionsService } from './revisions.service'; @@ -36,6 +37,7 @@ describe('RevisionsService', () => { const module: TestingModule = await Test.createTestingModule({ providers: [ RevisionsService, + EditService, { provide: getRepositoryToken(Revision), useClass: Repository, diff --git a/src/revisions/revisions.service.ts b/src/revisions/revisions.service.ts index 130edadb2..2363c8167 100644 --- a/src/revisions/revisions.service.ts +++ b/src/revisions/revisions.service.ts @@ -11,6 +11,7 @@ import { NotInDBError } from '../errors/errors'; import { ConsoleLoggerService } from '../logger/console-logger.service'; import { Note } from '../notes/note.entity'; import { NotesService } from '../notes/notes.service'; +import { EditService } from './edit.service'; import { RevisionMetadataDto } from './revision-metadata.dto'; import { RevisionDto } from './revision.dto'; import { Revision } from './revision.entity'; @@ -22,6 +23,7 @@ export class RevisionsService { @InjectRepository(Revision) private revisionRepository: Repository, @Inject(forwardRef(() => NotesService)) private notesService: NotesService, + private editService: EditService, ) { this.logger.setContext(RevisionsService.name); } @@ -109,12 +111,17 @@ export class RevisionsService { }; } - toRevisionDto(revision: Revision): RevisionDto { + async toRevisionDto(revision: Revision): Promise { return { id: revision.id, content: revision.content, createdAt: revision.createdAt, patch: revision.patch, + edits: await Promise.all( + ( + await revision.edits + ).map(async (edit) => await this.editService.toEditDto(edit)), + ), }; }